forked from vercel/pkg
-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathtest.js
More file actions
264 lines (226 loc) · 6.54 KB
/
test.js
File metadata and controls
264 lines (226 loc) · 6.54 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
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
#!/usr/bin/env node
'use strict';
const path = require('path');
const pc = require('picocolors');
const { globSync } = require('tinyglobby');
const utils = require('./utils.js');
const { spawn } = require('child_process');
const host = 'node' + utils.getNodeMajorVersion();
let target = process.argv[2] || 'host';
if (target === 'host') target = host;
// note to developer , you can use
// FLAVOR=test-1191 npm test
// if you only want to run all combination of this specific test case
// ( the env variable FLAVOR takes precedence over the second argument passed to this main.js file)
const flavor = process.env.FLAVOR || process.argv[3] || 'all';
// If a 4th argument is provided and flavor is not a test name, use the 4th argument as test filter
const testFilter = process.argv[4] || (flavor.match(/^test/) ? flavor : null);
const isCI = process.env.CI === 'true';
console.log('');
console.log('*************************************');
console.log(target + ' ' + flavor);
console.log(
`Host Info: ${process.version} ${process.platform} ${process.arch}`,
);
console.log('*************************************');
console.log('');
if (process.env.CI) {
if (
target === 'node0' ||
target === 'node4' ||
target === 'node6' ||
target === 'node7' ||
target === 'node9' ||
target === 'node11' ||
target === 'node13' ||
target === 'node15'
) {
console.log(target + ' is skipped in CI!');
console.log('');
process.exit();
}
}
function joinAndForward(d) {
let r = path.join(__dirname, d);
if (process.platform === 'win32') r = r.replace(/\\/g, '/');
return r;
}
const list = [];
const ignore = [];
// test that should be run on `host` target only
const npmTests = [
'test-01-hybrid-esm',
'test-42-fetch-all',
'test-46-multi-arch',
'test-46-multi-arch-2',
// 'test-79-npm', // TODO: fix this test
'test-10-pnpm',
'test-11-pnpm',
'test-50-aedes-esm',
'test-50-esm-pure',
'test-50-uuid-v10',
'test-80-compression-node-opcua',
'test-99-#1135',
'test-99-#1191',
'test-99-#1192',
// SEA tests — they ignore the target argument (always build for the host
// Node version), so running them in both test:22 and test:24 is redundant.
'test-00-sea',
'test-00-sea-picker',
'test-85-sea-enhanced',
'test-86-sea-assets',
'test-87-sea-esm',
'test-89-sea-fs-ops',
'test-90-sea-worker-threads',
'test-91-sea-esm-entry',
'test-92-sea-tla',
'test-94-sea-esm-import-meta',
];
if (testFilter) {
list.push(joinAndForward(`${testFilter}/main.js`));
} else if (flavor === 'only-npm') {
npmTests.forEach((t) => {
list.push(joinAndForward(`${t}/main.js`));
});
} else {
list.push(joinAndForward('**/main.js'));
if (flavor === 'no-npm') {
// TODO: fix this test
ignore.push(joinAndForward('test-79-npm'));
npmTests.forEach((t) => {
ignore.push(joinAndForward(t));
});
}
}
const files = globSync(list, { ignore });
function msToHumanDuration(ms) {
if (ms < 1000) return `${ms}ms`;
const seconds = Math.floor(ms / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const human = [];
if (hours > 0) human.push(`${hours}h`);
if (minutes > 0) human.push(`${minutes % 60}m`);
if (seconds > 0) human.push(`${seconds % 60}s`);
return human.join(' ');
}
/** @type {Array<import('child_process').ChildProcessWithoutNullStreams>} */
const activeProcesses = [];
function runTest(file) {
return new Promise((resolve, reject) => {
const process = spawn('node', [path.basename(file), target], {
cwd: path.dirname(file),
stdio: 'pipe',
});
activeProcesses.push(process);
const removeProcess = () => {
const index = activeProcesses.indexOf(process);
if (index !== -1) {
activeProcesses.splice(index, 1);
}
};
const output = [];
const rejectWithError = (error) => {
error.logOutput = `${error.message}\n${output.join('')}`;
reject(error);
};
process.on('close', (code) => {
removeProcess();
if (code !== 0) {
rejectWithError(new Error(`Process exited with code ${code}`));
} else {
resolve();
}
});
process.stdout.on('data', (data) => {
output.push(data.toString());
});
process.stderr.on('data', (data) => {
output.push(data.toString());
});
process.on('error', (error) => {
removeProcess();
rejectWithError(error);
});
});
}
const clearLastLine = () => {
if (
isCI ||
!process.stdout.isTTY ||
typeof process.stdout.moveCursor !== 'function' ||
typeof process.stdout.clearLine !== 'function'
)
return;
process.stdout.moveCursor(0, -1); // up one line
process.stdout.clearLine(1); // from cursor to end
};
async function run() {
let done = 0;
let ok = 0;
let failed = [];
const start = Date.now();
for (const file of files.sort().map((f) => path.resolve(f))) {
const startTest = Date.now();
try {
if (!isCI && process.stdout.isTTY) {
console.log(pc.gray(`⏳ ${file} - ${done}/${files.length}`));
}
await runTest(file);
ok++;
clearLastLine();
console.log(
pc.green(`✔ ${file} ok - ${msToHumanDuration(Date.now() - startTest)}`),
);
} catch (error) {
failed.push({
file,
output: error.logOutput,
});
clearLastLine();
console.error(
pc.red(
`✖ ${file} FAILED (in ${target}) - ${msToHumanDuration(Date.now() - startTest)}\n${error.message}`,
),
);
}
done++;
}
const end = Date.now();
console.log('');
console.log('*************************************');
console.log('Summary');
console.log('*************************************');
console.log('');
console.log(`Total: ${done}`);
console.log(`Ok: ${ok}`);
console.log(`Failed: ${failed.length}`);
// print failed tests
for (const { file, output } of failed) {
console.log('');
console.log(`--- ${file} ---`);
console.log(pc.red(output));
}
console.log(`Time: ${msToHumanDuration(end - start)}`);
if (failed.length > 0) {
process.exit(2);
}
}
let isExiting = false;
function cleanup(signal) {
if (isExiting) return;
isExiting = true;
console.log(`\n\nReceived ${signal}, cleaning up...`);
for (const process of activeProcesses) {
try {
process.kill('SIGTERM');
} catch (_error) {
// Ignore errors when killing processes
}
}
// Exit immediately
process.exit(130); // 128 + SIGINT(2) = 130
}
process.on('SIGINT', () => cleanup('SIGINT'));
process.on('SIGTERM', () => cleanup('SIGTERM'));
run();