-
Notifications
You must be signed in to change notification settings - Fork 57
/
runTest.ts
195 lines (168 loc) · 5.9 KB
/
runTest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as cp from 'child_process';
import { DownloadOptions, downloadAndUnzipVSCode } from './download';
import { getProfileArguments, killTree } from './util';
export interface TestOptions extends Partial<DownloadOptions> {
/**
* The VS Code executable path used for testing.
*
* If not passed, will use `options.version` to download a copy of VS Code for testing.
* If `version` is not specified either, will download and use latest stable release.
*/
vscodeExecutablePath?: string;
/**
* Whether VS Code should be launched using default settings and extensions
* installed on this machine. If `false`, then separate directories will be
* used inside the `.vscode-test` folder within the project.
*
* Defaults to `false`.
*/
reuseMachineInstall?: boolean;
/**
* Absolute path to the extension root. Passed to `--extensionDevelopmentPath`.
* Must include a `package.json` Extension Manifest.
*/
extensionDevelopmentPath: string | string[];
/**
* Absolute path to the extension tests runner. Passed to `--extensionTestsPath`.
* Can be either a file path or a directory path that contains an `index.js`.
* Must export a `run` function of the following signature:
*
* ```ts
* function run(): Promise<void>;
* ```
*
* When running the extension test, the Extension Development Host will call this function
* that runs the test suite. This function should throws an error if any test fails.
*
* The first argument is the path to the file specified in `extensionTestsPath`.
*
*/
extensionTestsPath: string;
/**
* Environment variables being passed to the extension test script.
*/
extensionTestsEnv?: {
[key: string]: string | undefined;
};
/**
* A list of launch arguments passed to VS Code executable, in addition to `--extensionDevelopmentPath`
* and `--extensionTestsPath` which are provided by `extensionDevelopmentPath` and `extensionTestsPath`
* options.
*
* If the first argument is a path to a file/folder/workspace, the launched VS Code instance
* will open it.
*
* See `code --help` for possible arguments.
*/
launchArgs?: string[];
}
/**
* Run VS Code extension test
*
* @returns The exit code of the command to launch VS Code extension test
*/
export async function runTests(options: TestOptions): Promise<number> {
if (!options.vscodeExecutablePath) {
options.vscodeExecutablePath = await downloadAndUnzipVSCode(options);
}
let args = [
// https://github.com/microsoft/vscode/issues/84238
'--no-sandbox',
// https://github.com/microsoft/vscode-test/issues/221
'--disable-gpu-sandbox',
// https://github.com/microsoft/vscode-test/issues/120
'--disable-updates',
'--skip-welcome',
'--skip-release-notes',
'--disable-workspace-trust',
'--extensionTestsPath=' + options.extensionTestsPath,
];
if (Array.isArray(options.extensionDevelopmentPath)) {
args.push(...options.extensionDevelopmentPath.map((devPath) => `--extensionDevelopmentPath=${devPath}`));
} else {
args.push(`--extensionDevelopmentPath=${options.extensionDevelopmentPath}`);
}
if (options.launchArgs) {
args = options.launchArgs.concat(args);
}
if (!options.reuseMachineInstall) {
args.push(...getProfileArguments(args));
}
return innerRunTests(options.vscodeExecutablePath, args, options.extensionTestsEnv);
}
const SIGINT = 'SIGINT';
async function innerRunTests(
executable: string,
args: string[],
testRunnerEnv?: {
[key: string]: string | undefined;
}
): Promise<number> {
const fullEnv = Object.assign({}, process.env, testRunnerEnv);
const shell = process.platform === 'win32';
const cmd = cp.spawn(shell ? `"${executable}"` : executable, args, { env: fullEnv, shell });
let exitRequested = false;
const ctrlc1 = () => {
process.removeListener(SIGINT, ctrlc1);
process.on(SIGINT, ctrlc2);
console.log('Closing VS Code gracefully. Press Ctrl+C to force close.');
exitRequested = true;
cmd.kill(SIGINT); // this should cause the returned promise to resolve
};
const ctrlc2 = () => {
console.log('Closing VS Code forcefully.');
process.removeListener(SIGINT, ctrlc2);
exitRequested = true;
killTree(cmd.pid!, true);
};
const prom = new Promise<number>((resolve, reject) => {
if (cmd.pid) {
process.on(SIGINT, ctrlc1);
}
cmd.stdout.on('data', (d) => process.stdout.write(d));
cmd.stderr.on('data', (d) => process.stderr.write(d));
cmd.on('error', function (data) {
console.log('Test error: ' + data.toString());
});
let finished = false;
function onProcessClosed(code: number | null, signal: NodeJS.Signals | null): void {
if (finished) {
return;
}
finished = true;
console.log(`Exit code: ${code ?? signal}`);
// fix: on windows, it seems like these descriptors can linger for an
// indeterminate amount of time, causing the process to hang.
cmd.stdout.destroy();
cmd.stderr.destroy();
if (code !== 0) {
reject(new TestRunFailedError(code ?? undefined, signal ?? undefined));
} else {
resolve(0);
}
}
cmd.on('close', onProcessClosed);
cmd.on('exit', onProcessClosed);
});
let code: number;
try {
code = await prom;
} finally {
process.removeListener(SIGINT, ctrlc1);
process.removeListener(SIGINT, ctrlc2);
}
// exit immediately if we handled a SIGINT and no one else did
if (exitRequested && process.listenerCount(SIGINT) === 0) {
process.exit(1);
}
return code;
}
export class TestRunFailedError extends Error {
constructor(public readonly code: number | undefined, public readonly signal: string | undefined) {
super(signal ? `Test run terminated with signal ${signal}` : `Test run failed with code ${code}`);
}
}