-
Notifications
You must be signed in to change notification settings - Fork 59
/
runTest.ts
145 lines (125 loc) · 4.22 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
/*---------------------------------------------------------------------------------------------
* 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 { downloadAndUnzipVSCode, DownloadVersion, DownloadPlatform } from './download';
export interface TestOptions {
/**
* 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;
/**
* The VS Code version to download. Valid versions are:
* - `'stable'`
* - `'insiders'`
* - `'1.32.0'`, `'1.31.1'`, etc
*
* Defaults to `stable`, which is latest stable version.
*
* *If a local copy exists at `.vscode-test/vscode-<VERSION>`, skip download.*
*/
version?: DownloadVersion;
/**
* The VS Code platform to download. If not specified, defaults to:
* - Windows: `win32-archive`
* - macOS: `darwin`
* - Linux: `linux-x64`
*
* Possible values are: `win32-archive`, `win32-x64-archive`, `darwin` and `linux-x64`.
*/
platform?: DownloadPlatform;
/**
* Absolute path to the extension root. Passed to `--extensionDevelopmentPath`.
* Must include a `package.json` Extension Manifest.
*/
extensionDevelopmentPath: 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.
*
*/
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.version, options.platform);
}
let args = [
// https://github.com/microsoft/vscode/issues/84238
'--no-sandbox',
'--extensionDevelopmentPath=' + options.extensionDevelopmentPath,
'--extensionTestsPath=' + options.extensionTestsPath
];
if (options.launchArgs) {
args = options.launchArgs.concat(args);
}
return innerRunTests(options.vscodeExecutablePath, args, options.extensionTestsEnv);
}
async function innerRunTests(
executable: string,
args: string[],
testRunnerEnv?: {
[key: string]: string | undefined;
}
): Promise<number> {
return new Promise<number>((resolve, reject) => {
const fullEnv = Object.assign({}, process.env, testRunnerEnv);
const cmd = cp.spawn(executable, args, { env: fullEnv });
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}`);
if (code === null) {
reject(signal);
} else if (code !== 0) {
reject('Failed');
}
console.log('Done\n');
resolve(code ?? -1);
}
cmd.on('close', onProcessClosed);
cmd.on('exit', onProcessClosed);
});
}