forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestRunner.ts
More file actions
94 lines (84 loc) · 3.53 KB
/
testRunner.ts
File metadata and controls
94 lines (84 loc) · 3.53 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// tslint:disable:no-require-imports no-var-requires import-name no-function-expression no-any prefer-template no-console no-var-self
// Most of the source is in node_modules/vscode/lib/testrunner.js
'use strict';
import * as glob from 'glob';
import * as Mocha from 'mocha';
import * as path from 'path';
import { IS_SMOKE_TEST } from './constants';
import { initialize } from './initialize';
// Linux: prevent a weird NPE when mocha on Linux requires the window size from the TTY.
// Since we are not running in a tty environment, we just implement the method statically.
const tty = require('tty');
if (!tty.getWindowSize) {
tty.getWindowSize = function (): number[] {
return [80, 75];
};
}
let mocha = new Mocha(<any>{
ui: 'tdd',
colors: true
});
export type SetupOptions = Mocha.MochaOptions & {
testFilesSuffix?: string;
reporterOptions?: {
mochaFile?: string;
properties?: string;
};
};
let testFilesGlob = 'test';
export function configure(setupOptions: SetupOptions): void {
if (setupOptions.testFilesSuffix) {
testFilesGlob = setupOptions.testFilesSuffix;
}
// Force Mocha to exit.
(setupOptions as any).exit = true;
mocha = new Mocha(setupOptions);
}
export async function run(): Promise<void> {
const testsRoot = path.join(__dirname);
// Enable source map support.
require('source-map-support').install();
// nteract/transforms-full expects to run in the browser so we have to fake
// parts of the browser here.
if (!IS_SMOKE_TEST) {
const reactHelpers = require('./datascience/reactHelpers') as typeof import('./datascience/reactHelpers');
reactHelpers.setUpDomEnvironment();
}
/**
* Waits until the Python Extension completes loading or a timeout.
* When running tests within VSC, we need to wait for the Python Extension to complete loading,
* this is where `initialize` comes in, we load the PVSC extension using VSC API, wait for it
* to complete.
* That's when we know out PVSC extension specific code is ready for testing.
* So, this code needs to run always for every test running in VS Code (what we call these `system test`) .
* @returns
*/
function initializationScript() {
const ex = new Error('Failed to initialize Python extension for tests after 2 minutes');
let timer: NodeJS.Timer | undefined;
const failed = new Promise((_, reject) => {
timer = setTimeout(() => reject(ex), 120_000);
});
const promise = Promise.race([initialize(), failed]);
promise.then(() => clearTimeout(timer!)).catch(() => clearTimeout(timer!));
return promise;
}
// Run the tests.
await new Promise<void>((resolve, reject) => {
glob(`**/**.${testFilesGlob}.js`, { ignore: ['**/**.unit.test.js', '**/**.functional.test.js'], cwd: testsRoot }, (error, files) => {
if (error) {
return reject(error);
}
try {
files.forEach(file => mocha.addFile(path.join(testsRoot, file)));
initializationScript()
.then(() => mocha.run(failures => (failures > 0 ? reject(new Error(`${failures} total failures`)) : resolve())))
.catch(reject);
} catch (error) {
return reject(error);
}
});
});
}