-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
80 lines (70 loc) · 2.42 KB
/
test.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
import assert from 'node:assert';
import cd from 'node:child_process';
import test, { after, describe } from 'node:test';
import { parseArgs, promisify } from 'node:util';
import tests from './tests.json' with { type: 'json' };
import { writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const exec = promisify(cd.exec);
const args = parseArgs({ options: {
update: {
type: 'boolean',
short: 'u',
default: false,
}
}}).values;
// dummy run to make sure the packages are installed
await exec('node ./index.js @tstpkgs/basic-esm');
for (const [suiteName, suiteTestCases] of Object.entries(tests)) {
describe(suiteName, () => {
for (const testCase of suiteTestCases) {
test(testCase.name, async () => {
/** @type {{stdout: string, stderr: string} | cd.ExecException | undefined} */
let result;
try {
result = await exec(`node ./index.js ${testCase.args}`);
} catch (e) {
result = /** @type {cd.ExecException} */(e);
}
if (args.update) {
// @ts-ignore
testCase.exitCode = result?.code;
// @ts-ignore
testCase.stdout = clean(result?.stdout);
// @ts-ignore
testCase.stderr = clean(result?.stderr);
assert.ok(true);
} else {
// @ts-ignore
assert.strictEqual(result?.code, testCase.exitCode);
// @ts-ignore
assert.strictEqual(clean(result?.stdout), testCase.stdout);
// @ts-ignore
assert.strictEqual(clean(result?.stderr), testCase.stderr);
}
});
}
});
}
after(async () => {
// await cleanDir();
if (args.update) {
await writeTestCases();
}
});
// function cleanDir() {
// return rm(dir, { recursive: true, force: true });
// }
function writeTestCases() {
return writeFile('./tests.json', JSON.stringify(tests, null, 4));
}
/**
* @param {string} str
*/
function clean(str) {
let result = str.replace(/\s+/g, ' ').trim();
result = result.replaceAll(__dirname, '<dirname>');
return result;
}