forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.ts
272 lines (244 loc) · 9.21 KB
/
runner.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import * as FourSlash from "./_namespaces/FourSlash.js";
import {
CompilerBaselineRunner,
CompilerTestType,
FourSlashRunner,
GeneratedFourslashRunner,
IO,
Parallel,
RunnerBase,
setLightMode,
setShardId,
setShards,
TestRunnerKind,
TranspileRunner,
} from "./_namespaces/Harness.js";
import * as project from "./_namespaces/project.js";
import * as ts from "./_namespaces/ts.js";
import * as vpath from "./_namespaces/vpath.js";
/* eslint-disable prefer-const */
export let runners: RunnerBase[] = [];
export let iterations = 1;
/* eslint-enable prefer-const */
function runTests(runners: RunnerBase[]) {
for (let i = iterations; i > 0; i--) {
const seen = new Map<string, string>();
const dupes: [string, string][] = [];
for (const runner of runners) {
if (runner instanceof CompilerBaselineRunner || runner instanceof FourSlashRunner) {
for (const full of runner.enumerateTestFiles()) {
const base = vpath.basename(full).toLowerCase();
// allow existing dupes in fourslash/shims and fourslash/server
if (seen.has(base) && !/fourslash\/(shim|server)/.test(full)) {
dupes.push([seen.get(base)!, full]);
}
else {
seen.set(base, full);
}
}
}
runner.initializeTests();
}
if (dupes.length) {
throw new Error(`${dupes.length} Tests with duplicate baseline names:
${JSON.stringify(dupes, undefined, 2)}`);
}
}
}
function tryGetConfig(args: string[]) {
const prefix = "--config=";
const configPath = ts.forEach(args, arg => arg.lastIndexOf(prefix, 0) === 0 && arg.substr(prefix.length));
// strip leading and trailing quotes from the path (necessary on Windows since shell does not do it automatically)
return configPath && configPath.replace(/(^["'])|(["']$)/g, "");
}
export function createRunner(kind: TestRunnerKind): RunnerBase {
switch (kind) {
case "conformance":
return new CompilerBaselineRunner(CompilerTestType.Conformance);
case "compiler":
return new CompilerBaselineRunner(CompilerTestType.Regressions);
case "fourslash":
return new FourSlashRunner(FourSlash.FourSlashTestType.Native);
case "fourslash-server":
return new FourSlashRunner(FourSlash.FourSlashTestType.Server);
case "project":
return new project.ProjectRunner();
case "transpile":
return new TranspileRunner();
}
return ts.Debug.fail(`Unknown runner kind ${kind}`);
}
// users can define tests to run in mytest.config that will override cmd line args, otherwise use cmd line args (test.config), otherwise no options
const mytestconfigFileName = "mytest.config";
const testconfigFileName = "test.config";
const customConfig = tryGetConfig(IO.args());
const testConfigContent = customConfig && IO.fileExists(customConfig)
? IO.readFile(customConfig)!
: IO.fileExists(mytestconfigFileName)
? IO.readFile(mytestconfigFileName)!
: IO.fileExists(testconfigFileName) ? IO.readFile(testconfigFileName)! : "";
export let taskConfigsFolder: string;
export let workerCount: number;
export let runUnitTests: boolean | undefined;
export let stackTraceLimit: number | "full" | undefined;
export let noColors = false;
export let keepFailed = false;
export interface TestConfig {
light?: boolean;
taskConfigsFolder?: string;
listenForWork?: boolean;
workerCount?: number;
stackTraceLimit?: number | "full";
test?: string[];
runners?: string[];
runUnitTests?: boolean;
noColors?: boolean;
timeout?: number;
keepFailed?: boolean;
shardId?: number;
shards?: number;
}
export interface TaskSet {
runner: TestRunnerKind;
files: string[];
}
export let configOption: string;
export let globalTimeout: number;
function handleTestConfig() {
if (testConfigContent !== "") {
const testConfig = JSON.parse(testConfigContent) as TestConfig;
if (testConfig.light) {
setLightMode(true);
}
if (testConfig.timeout) {
globalTimeout = testConfig.timeout;
}
runUnitTests = testConfig.runUnitTests;
if (testConfig.workerCount) {
workerCount = +testConfig.workerCount;
}
if (testConfig.taskConfigsFolder) {
taskConfigsFolder = testConfig.taskConfigsFolder;
}
if (testConfig.noColors !== undefined) {
noColors = testConfig.noColors;
}
if (testConfig.keepFailed) {
keepFailed = true;
}
if (testConfig.shardId) {
setShardId(testConfig.shardId);
}
if (testConfig.shards) {
setShards(testConfig.shards);
}
if (testConfig.stackTraceLimit === "full") {
(Error as any).stackTraceLimit = Infinity;
stackTraceLimit = testConfig.stackTraceLimit;
}
else if ((+testConfig.stackTraceLimit! | 0) > 0) {
(Error as any).stackTraceLimit = +testConfig.stackTraceLimit! | 0;
stackTraceLimit = +testConfig.stackTraceLimit! | 0;
}
if (testConfig.listenForWork) {
return true;
}
const runnerConfig = testConfig.runners || testConfig.test;
if (runnerConfig && runnerConfig.length > 0) {
if (testConfig.runners) {
runUnitTests = runnerConfig.includes("unittest");
}
for (const option of runnerConfig) {
if (!option) {
continue;
}
if (!configOption) {
configOption = option;
}
else {
configOption += "+" + option;
}
switch (option) {
case "compiler":
runners.push(new CompilerBaselineRunner(CompilerTestType.Conformance));
runners.push(new CompilerBaselineRunner(CompilerTestType.Regressions));
break;
case "conformance":
runners.push(new CompilerBaselineRunner(CompilerTestType.Conformance));
break;
case "project":
runners.push(new project.ProjectRunner());
break;
case "fourslash":
runners.push(new FourSlashRunner(FourSlash.FourSlashTestType.Native));
break;
case "fourslash-server":
runners.push(new FourSlashRunner(FourSlash.FourSlashTestType.Server));
break;
case "fourslash-generated":
runners.push(new GeneratedFourslashRunner(FourSlash.FourSlashTestType.Native));
break;
case "transpile":
runners.push(new TranspileRunner());
break;
}
}
}
}
if (runners.length === 0) {
// compiler
runners.push(new CompilerBaselineRunner(CompilerTestType.Conformance));
runners.push(new CompilerBaselineRunner(CompilerTestType.Regressions));
runners.push(new project.ProjectRunner());
// language services
runners.push(new FourSlashRunner(FourSlash.FourSlashTestType.Native));
runners.push(new FourSlashRunner(FourSlash.FourSlashTestType.Server));
// runners.push(new GeneratedFourslashRunner());
// transpile
runners.push(new TranspileRunner());
}
if (runUnitTests === undefined) {
runUnitTests = runners.length !== 1; // Don't run unit tests when running only one runner if unit tests were not explicitly asked for
}
return false;
}
function beginTests() {
ts.Debug.loggingHost = {
log(_level, s) {
console.log(s || "");
},
};
if (ts.Debug.isDebugging) {
ts.Debug.enableDebugInfo();
}
// run tests in en-US by default.
let savedUILocale: string | undefined;
beforeEach(() => {
savedUILocale = ts.getUILocale();
ts.setUILocale("en-US");
});
afterEach(() => ts.setUILocale(savedUILocale));
runTests(runners);
if (!runUnitTests) {
// patch `describe` to skip unit tests
(global as any).describe = ts.noop;
}
}
function importTests() {
return import("./tests.js");
}
export let isWorker: boolean;
function startTestEnvironment() {
// For debugging convenience.
(globalThis as any).ts = ts;
isWorker = handleTestConfig();
if (isWorker) {
return Parallel.Worker.start(importTests);
}
else if (taskConfigsFolder && workerCount && workerCount > 1) {
return Parallel.Host.start(importTests);
}
beginTests();
importTests();
}
startTestEnvironment();