/
run-tests.js
165 lines (147 loc) · 5.11 KB
/
run-tests.js
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
const fs = require('fs-extra');
const path = require('path');
const rimraf = require('rimraf');
const typescript = require('typescript');
const semver = require('semver');
const execSync = require('child_process').execSync;
// We only want to run comparison tests for the latest released version
const typescriptVersion = parseFloat(
semver.major(typescript.version) + '.' + semver.minor(typescript.version)
);
// @ts-ignore
if (typescriptVersion < 3.6 || typescriptVersion > 3.6) return;
// Parse command line arguments
const saveOutputMode = process.argv.indexOf('--save-output') !== -1;
const indexOfSingleTest = process.argv.indexOf('--single-test');
const singleTestToRun =
indexOfSingleTest !== -1 && process.argv[indexOfSingleTest + 1];
const indexOfTestCriteria = process.argv.indexOf('--match-test');
const testCriteria =
indexOfTestCriteria !== -1 && new RegExp(process.argv[indexOfTestCriteria + 1]);
/** @type {string[]} */
let passingTests = [];
/** @type {string[]} */
let failingTests = [];
// set up new empty staging area
const stagingPath = path.resolve(__dirname, '../../.test');
rimraf.sync(stagingPath);
runTests();
// --------------------------------------------------------------
function runTests() {
const start = new Date().getTime();
console.log(
'\n-------------------------------------------------------------------------\n'
);
console.log('Starting to run test suites...\n');
const testDir = __dirname;
if (singleTestToRun) {
if (runTestAsChildProcess(getTestNameFromPath(singleTestToRun))) {
passingTests.push(singleTestToRun);
} else {
failingTests.push(singleTestToRun);
}
} else {
// loop through each test directory triggering a test run as child process
/** @type {string[]} */
const availableTests = fs.readdirSync(testDir)
.filter(
/**
* @param {string} testName
*/ testName => {
const testPath = path.join(testDir, testName);
return fs.statSync(testPath).isDirectory();
}
)
.filter(
/**
* @param {string} testName
*/ testName =>
testCriteria ? !!testName.match(testCriteria) : true
);
// Allow multiple attempts to pass tests as they're flaky
let attempt = 0;
while (++attempt <= 40 && passingTests.length < availableTests.length) {
if (attempt > 1) {
console.log(`Some tests failed; re-running (attempt ${attempt})`)
}
availableTests
.filter(testName => !passingTests.includes(testName))
.forEach(testName => {
if (runTestAsChildProcess(testName)) {
passingTests.push(testName);
}
});
}
failingTests = availableTests.filter(testName => !passingTests.includes(testName));
}
const end = new Date().getTime();
console.log(
'\n-------------------------------------------------------------------------\n'
);
const totalTests = passingTests.length + failingTests.length;
console.log(
`${totalTests} test suites took ${(end - start) / 1000} seconds to run.\n`
);
if (passingTests.length > 0) {
console.log(
`${passingTests.length} test suite(s) passed.\n\n - ${passingTests.join(
'\n - '
)}\n`
);
}
if (failingTests.length > 0) {
console.log(
`${failingTests.length} test suite(s) failed.\n\n - ${failingTests.join(
'\n - '
)}\n`
);
process.exit(1);
} else {
console.log('No tests failed; congratulations!');
}
}
/** @param {string} testNameOrPath */
function getTestNameFromPath (testNameOrPath) {
var tsLoaderPath = path.resolve(__dirname, '../..');
var tsLoaderBasename = path.basename(tsLoaderPath);
var comparisonTestsRelativeRoot = path.relative(tsLoaderPath, __dirname);
var comparisonTestsAbsoluteRoot = path.join(tsLoaderPath, comparisonTestsRelativeRoot);
// It wasn’t a path in comparison-tests; assume it was a test name
if (testNameOrPath.indexOf(path.join(tsLoaderBasename, comparisonTestsRelativeRoot)) === -1) {
return testNameOrPath;
}
// E.g. projectReferences/lib/index.ts
var testPathRelativeToComparisonTests = path.relative(comparisonTestsAbsoluteRoot, testNameOrPath);
return testPathRelativeToComparisonTests.split(path.sep)[0];
}
/**
* Run test isolated in a child process
*
* @param {string} testName
*/
function runTestAsChildProcess(testName) {
const testToRun = ' --test-to-run ' + testName;
const debug = process.argv.indexOf('--debug') > -1;
const testCommand =
'mocha --reporter spec ' +
(debug ? '--inspect-brk=5858 ' : '') +
'test/comparison-tests/create-and-execute-test.js ' +
testToRun;
try {
const _testOutput = execSync(
testCommand + (saveOutputMode ? ' --save-output' : ''),
{ stdio: 'inherit' }
);
/* No longer necessary and experimentalFileCaching is enabled by default - approach may prove useful in future though
if (!saveOutputMode) {
const _testOutput2 = execSync(
testCommand + ' --extra-option experimentalFileCaching',
{ stdio: 'inherit' }
);
}
*/
return true;
} catch (err) {
return false;
}
}