Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/test/smoke/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 { Suite, Context } from 'mocha';
7
import { dirname, join } from 'path';
8
import { Application, ApplicationOptions, Logger } from '../../automation';
9
10
export function describeRepeat(n: number, description: string, callback: (this: Suite) => void): void {
11
for (let i = 0; i < n; i++) {
12
describe(`${description} (iteration ${i})`, callback);
13
}
14
}
15
16
export function itRepeat(n: number, description: string, callback: (this: Context) => any): void {
17
for (let i = 0; i < n; i++) {
18
it(`${description} (iteration ${i})`, callback);
19
}
20
}
21
22
export function installAllHandlers(logger: Logger, optionsTransform?: (opts: ApplicationOptions) => ApplicationOptions) {
23
installDiagnosticsHandler(logger);
24
installAppBeforeHandler(optionsTransform);
25
installAppAfterHandler();
26
}
27
28
export function installDiagnosticsHandler(logger: Logger, appFn?: () => Application | undefined) {
29
30
// Before each suite
31
before(async function () {
32
const suiteTitle = this.currentTest?.parent?.title;
33
logger.log('');
34
logger.log(`>>> Suite start: '${suiteTitle ?? 'unknown'}' <<<`);
35
logger.log('');
36
});
37
38
// Before each test
39
beforeEach(async function () {
40
const testTitle = this.currentTest?.title;
41
logger.log('');
42
logger.log(`>>> Test start: '${testTitle ?? 'unknown'}' <<<`);
43
logger.log('');
44
45
const app: Application = appFn?.() ?? this.app;
46
await app?.startTracing(testTitle ?? 'unknown');
47
});
48
49
// After each test
50
afterEach(async function () {
51
const currentTest = this.currentTest;
52
if (!currentTest) {
53
return;
54
}
55
56
const failed = currentTest.state === 'failed';
57
const testTitle = currentTest.title;
58
logger.log('');
59
if (failed) {
60
logger.log(`>>> !!! FAILURE !!! Test end: '${testTitle}' !!! FAILURE !!! <<<`);
61
} else {
62
logger.log(`>>> Test end: '${testTitle}' <<<`);
63
}
64
logger.log('');
65
66
const app: Application = appFn?.() ?? this.app;
67
await app?.stopTracing(testTitle.replace(/[^a-z0-9\-]/ig, '_'), failed);
68
});
69
}
70
71
let logsCounter = 1;
72
let crashCounter = 1;
73
74
export function suiteLogsPath(options: ApplicationOptions, suiteName: string): string {
75
return join(dirname(options.logsPath), `${logsCounter++}_suite_${suiteName.replace(/[^a-z0-9\-]/ig, '_')}`);
76
}
77
78
export function suiteCrashPath(options: ApplicationOptions, suiteName: string): string {
79
return join(dirname(options.crashesPath), `${crashCounter++}_suite_${suiteName.replace(/[^a-z0-9\-]/ig, '_')}`);
80
}
81
82
function installAppBeforeHandler(optionsTransform?: (opts: ApplicationOptions) => ApplicationOptions) {
83
before(async function () {
84
const suiteName = this.test?.parent?.title ?? 'unknown';
85
86
this.app = createApp({
87
...this.defaultOptions,
88
logsPath: suiteLogsPath(this.defaultOptions, suiteName),
89
crashesPath: suiteCrashPath(this.defaultOptions, suiteName)
90
}, optionsTransform);
91
await this.app.start();
92
});
93
}
94
95
export function installAppAfterHandler(appFn?: () => Application | undefined, joinFn?: () => Promise<unknown>) {
96
after(async function () {
97
const app: Application = appFn?.() ?? this.app;
98
if (app) {
99
await app.stop();
100
}
101
102
if (joinFn) {
103
await joinFn();
104
}
105
});
106
}
107
108
export function createApp(options: ApplicationOptions, optionsTransform?: (opts: ApplicationOptions) => ApplicationOptions): Application {
109
if (optionsTransform) {
110
options = optionsTransform({ ...options });
111
}
112
113
const config = options.userDataDir
114
? { ...options, userDataDir: getRandomUserDataDir(options.userDataDir) }
115
: options;
116
const app = new Application(config);
117
118
return app;
119
}
120
121
export function getRandomUserDataDir(baseUserDataDir: string): string {
122
123
// Pick a random user data dir suffix that is not
124
// too long to not run into max path length issues
125
// https://github.com/microsoft/vscode/issues/34988
126
const userDataPathSuffix = [...Array(8)].map(() => Math.random().toString(36)[3]).join('');
127
128
return baseUserDataDir.concat(`-${userDataPathSuffix}`);
129
}
130
131
export function timeout(i: number) {
132
return new Promise<void>(resolve => {
133
setTimeout(() => {
134
resolve();
135
}, i);
136
});
137
}
138
139
export async function retryWithRestart(app: Application, testFn: () => Promise<unknown>, retries = 3, timeoutMs = 20000): Promise<unknown> {
140
let lastError: Error | undefined = undefined;
141
for (let i = 0; i < retries; i++) {
142
const result = await Promise.race([
143
testFn().then(() => true, error => {
144
lastError = error;
145
return false;
146
}),
147
timeout(timeoutMs).then(() => false)
148
]);
149
150
if (result) {
151
return;
152
}
153
154
await app.restart();
155
}
156
157
throw lastError ?? new Error('retryWithRestart failed with an unknown error');
158
}
159
160
export interface ITask<T> {
161
(): T;
162
}
163
164
export async function retry<T>(task: ITask<Promise<T>>, delay: number, retries: number, onBeforeRetry?: () => Promise<unknown>): Promise<T> {
165
let lastError: Error | undefined;
166
167
for (let i = 0; i < retries; i++) {
168
try {
169
if (i > 0 && typeof onBeforeRetry === 'function') {
170
try {
171
await onBeforeRetry();
172
} catch (error) {
173
console.warn(`onBeforeRetry failed with: ${error}`);
174
}
175
}
176
177
return await task();
178
} catch (error) {
179
lastError = error as Error;
180
181
await timeout(delay);
182
}
183
}
184
185
throw lastError;
186
}
187
188