Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/test/mcp/src/utils.ts
3520 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { dirname, join } from 'path';
7
import { Application, ApplicationOptions } from '../../automation';
8
9
let logsCounter = 1;
10
let crashCounter = 1;
11
12
export function parseVersion(version: string): { major: number; minor: number; patch: number } {
13
const [, major, minor, patch] = /^(\d+)\.(\d+)\.(\d+)/.exec(version)!;
14
return { major: parseInt(major), minor: parseInt(minor), patch: parseInt(patch) };
15
}
16
17
export function suiteLogsPath(options: ApplicationOptions, suiteName: string): string {
18
return join(dirname(options.logsPath), `${logsCounter++}_suite_${suiteName.replace(/[^a-z0-9\-]/ig, '_')}`);
19
}
20
21
export function suiteCrashPath(options: ApplicationOptions, suiteName: string): string {
22
return join(dirname(options.crashesPath), `${crashCounter++}_suite_${suiteName.replace(/[^a-z0-9\-]/ig, '_')}`);
23
}
24
25
export function getRandomUserDataDir(baseUserDataDir: string): string {
26
27
// Pick a random user data dir suffix that is not
28
// too long to not run into max path length issues
29
// https://github.com/microsoft/vscode/issues/34988
30
const userDataPathSuffix = [...Array(8)].map(() => Math.random().toString(36)[3]).join('');
31
32
return baseUserDataDir.concat(`-${userDataPathSuffix}`);
33
}
34
35
export function createApp(options: ApplicationOptions, optionsTransform?: (opts: ApplicationOptions) => ApplicationOptions): Application {
36
if (optionsTransform) {
37
options = optionsTransform({ ...options });
38
}
39
40
const config = options.userDataDir
41
? { ...options, userDataDir: getRandomUserDataDir(options.userDataDir) }
42
: options;
43
const app = new Application(config);
44
return app;
45
}
46
47
export function timeout(i: number) {
48
return new Promise<void>(resolve => {
49
setTimeout(() => {
50
resolve();
51
}, i);
52
});
53
}
54
55
export interface ITask<T> {
56
(): T;
57
}
58
59
export async function retry<T>(task: ITask<Promise<T>>, delay: number, retries: number, onBeforeRetry?: () => Promise<unknown>): Promise<T> {
60
let lastError: Error | undefined;
61
62
for (let i = 0; i < retries; i++) {
63
try {
64
if (i > 0 && typeof onBeforeRetry === 'function') {
65
try {
66
await onBeforeRetry();
67
} catch (error) {
68
console.warn(`onBeforeRetry failed with: ${error}`);
69
}
70
}
71
72
return await task();
73
} catch (error) {
74
lastError = error as Error;
75
76
await timeout(delay);
77
}
78
}
79
80
throw lastError;
81
}
82
83