-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbin.mts
More file actions
180 lines (155 loc) · 4.44 KB
/
bin.mts
File metadata and controls
180 lines (155 loc) · 4.44 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
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
#!/usr/bin/env node
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import * as chokidar from 'chokidar';
import { resolve } from 'path';
import { cliArgs, configFileDefault } from './cli/args.mjs';
import {
ResolvedTestConfiguration,
loadDefaultConfigFile,
tryLoadConfigFile,
} from './cli/config.mjs';
import { Coverage } from './cli/coverage.mjs';
import { IPreparedRun, IRunContext, platforms } from './cli/platform/index.mjs';
import { TestConfiguration } from './config.cjs';
export const args = cliArgs.parseSync();
class CliExpectedError extends Error {}
main();
async function main() {
let code = 0;
try {
const config =
args.config !== configFileDefault
? await tryLoadConfigFile(resolve(process.cwd(), args.config))
: await loadDefaultConfigFile();
const enabledTests = new Set(
args.label?.length
? args.label.map((label) => {
const found = config.tests.find((c, i) =>
typeof label === 'string' ? c.label === label : i === label,
);
if (!found) {
throw new CliExpectedError(`Could not find a configuration with label "${label}"`);
}
return found;
})
: new Set(config.tests),
);
if (args.watch) {
await watchConfigs(config, enabledTests);
} else {
code = await runConfigs(config, enabledTests);
}
} catch (e) {
code = 1;
if (e instanceof CliExpectedError) {
console.error(e.message);
} else {
console.error((e as Error).stack || e);
}
} finally {
process.exit(code);
}
}
async function prepareConfigs(
config: ResolvedTestConfiguration,
enabledTests: Set<TestConfiguration>,
): Promise<IPreparedRun[]> {
return await Promise.all(
[...enabledTests].map(async (test, i) => {
for (const platform of platforms) {
const p = await platform.prepare({ args, config, test });
if (p) {
return p;
}
}
throw new CliExpectedError(
`Could not find a runner for test configuration ${test.label || i}`,
);
}),
);
}
const WATCH_RUN_DEBOUNCE = 500;
async function watchConfigs(
config: ResolvedTestConfiguration,
enabledTests: Set<TestConfiguration>,
) {
let debounceRun: NodeJS.Timeout;
let rerun = false;
let running = true;
let prepared: IPreparedRun[] | undefined;
const runOrDebounce = () => {
if (debounceRun) {
clearTimeout(debounceRun);
}
debounceRun = setTimeout(async () => {
running = true;
rerun = false;
try {
prepared ??= await prepareConfigs(config, enabledTests);
await runPreparedConfigs(config, prepared);
} finally {
running = false;
if (rerun) {
runOrDebounce();
}
}
}, WATCH_RUN_DEBOUNCE);
};
const watcher = chokidar.watch(
args.watchFiles?.length ? args.watchFiles.map(String) : process.cwd(),
{
ignored: [
'**/.vscode-test/**',
'**/node_modules/**',
...(args.watchIgnore || []).map(String),
],
ignoreInitial: true,
},
);
watcher.on('all', (evts) => {
if (evts !== 'change') {
prepared = undefined; // invalidate since files will need to be re-scanned
}
if (running) {
rerun = true;
} else {
runOrDebounce();
}
});
watcher.on('ready', () => {
runOrDebounce();
});
// wait until interrupted
await new Promise(() => {
/* no-op */
});
}
async function runPreparedConfigs(
config: ResolvedTestConfiguration,
prepared: IPreparedRun[],
): Promise<number> {
const coverage = args.coverage ? new Coverage(config, args) : undefined;
const context: IRunContext = { coverage: coverage?.targetDir };
let code = 0;
for (const p of prepared) {
code = Math.max(code, await p.run(context));
if (args.bail && code !== 0) {
return code;
}
}
await coverage?.write();
return code;
}
/** Runs the given test configurations. */
async function runConfigs(config: ResolvedTestConfiguration, enabledTests: Set<TestConfiguration>) {
const prepared = await prepareConfigs(config, enabledTests);
if (args.listConfiguration) {
await new Promise((r) =>
process.stdout.write(JSON.stringify(prepared.map((p) => p.dumpJson())), r),
);
return 0;
}
return runPreparedConfigs(config, prepared);
}