src/vs/base/test/common/utils.ts
107 LOC · 84 covered · 23 uncovered · 23 ranges · 20961 concepts · 7 introducers · 12741 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
map.ts ×97
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { DisposableStore, DisposableTracker, IDisposable, setDisposableTracker } from '../../common/lifecycle.js';
import { join } from '../../common/path.js';
import { isWindows } from '../../common/platform.js';
import { URI } from '../../common/uri.js';
export type ValueCallback<T = any> = (value: T | Promise<T>) => void;
export function toResource(this: any, path: string): URI {
return URI.file(join('C:\\', btoa(this.test.fullTitle()), path));
}
return URI.file(join('/', btoa(this.test.fullTitle()), path));
}
export function suiteRepeat(n: number, description: string, callback: (this: any) => void): void {
for (let i = 0; i < n; i++) {
suite(`${description} (iteration ${i})`, callback);
}
}
export function testRepeat(n: number, description: string, callback: (this: any) => any): void {
for (let i = 0; i < n; i++) {
test(`${description} (iteration ${i})`, callback);
}
}
export async function assertThrowsAsync(block: () => any, message: string | Error = 'Missing expected exception'): Promise<void> {
utils.ts ×2
try {
await block();
} catch {
return;
}
throw err;
}
/**
* Use this function to ensure that all disposables are cleaned up at the end of each test in the current suite.
*
* Use `markAsSingleton` if disposable singletons are created lazily that are allowed to outlive the test.
* Make sure that the singleton properly registers all child disposables so that they are excluded too.
*
* @returns A {@link DisposableStore} that can optionally be used to track disposables in the test.
* This will be automatically disposed on test teardown.
*/
export function ensureNoDisposablesAreLeakedInTestSuite(): Pick<DisposableStore, 'add'> {
let tracker: DisposableTracker | undefined;
let store: DisposableStore;
setup(() => {
tracker = new DisposableTracker();
setDisposableTracker(tracker);
teardown(function (this: import('mocha').Context) {
setDisposableTracker(null);
if (this.currentTest?.state !== 'failed') {
const result = tracker!.computeLeakingDisposables();
if (result) {
console.error(result.details);
throw new Error(`There are ${result.leaks.length} undisposed disposables!${result.details}`);
}
// Wrap store as the suite function is called before it's initialized
const testContext = {
add<T extends IDisposable>(o: T): T {
}
return testContext;
}
export function throwIfDisposablesAreLeaked(body: () => void, logToConsole = true): void {
setDisposableTracker(tracker);
body();
setDisposableTracker(null);
computeLeakingDisposables(tracker, logToConsole);
}
export async function throwIfDisposablesAreLeakedAsync(body: () => Promise<void>): Promise<void> {
const tracker = new DisposableTracker();
setDisposableTracker(tracker);
await body();
setDisposableTracker(null);
computeLeakingDisposables(tracker);
}
function computeLeakingDisposables(tracker: DisposableTracker, logToConsole = true) {
utils.ts ×3
const result = tracker.computeLeakingDisposables();
if (result) {
console.error(result.details);
}
throw new Error(`There are ${result.leaks.length} undisposed disposables!${result.details}`);
lifecycle.ts ×5
}