This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
runner.ts
77 lines (68 loc) · 2.24 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
#!/usr/bin/env -S node --no-warnings
import glob from 'glob';
import { spawn } from 'child_process';
import * as path from 'path';
const args = process.argv.slice(2);
const globalize = args[0] === '--global';
if (globalize) args.shift();
run(args[0] || 'test/**/*.js').then(
(success) => process.exit(success ? 0 : 1),
(error) => {
console.error(error);
process.exit(2);
}
);
async function run(pattern: string) {
const files = await findFiles(pattern);
if (!files.length) throw new Error(`no files found for ${pattern}`);
console.log('TAP version 13');
if (files.length === 1) return 0 === (await runFile(files[0], ''));
console.log(`1..${files.length}`);
let failed = 0;
for (let idx = 0; idx < files.length; idx++) {
const file = files[idx];
console.log(`# ${file}`);
const result = await runFile(files[idx], ' ');
failed += Math.min(result, 1);
console.log(`${!result ? 'ok' : 'not ok'} ${idx + 1} - ${file}`);
}
console.log(`# Passed ${files.length - failed} of ${files.length}`);
return !failed;
}
function runFile(file: string, indent: string): Promise<number> {
return new Promise((resolve, reject) => {
const args = ['--no-warnings'];
if (global) {
args.push(path.join(__dirname, `global.js`));
}
args.push(path.resolve(file));
const child = spawn(process.argv0, args, { stdio: ['ignore', 'pipe', 'inherit'], windowsHide: true });
let buffer: string = '';
child.stdout.on('data', (chunk) => {
buffer += chunk.toString('utf-8');
const lines = buffer.split(/\r?\n/);
buffer = lines.pop() as string;
lines.forEach((line) => writeLine(line, indent));
});
child.on('error', reject);
child.on('close', (code) => {
writeLine(buffer, indent);
buffer = '';
resolve(code);
});
});
}
function findFiles(pattern: string): Promise<string[]> {
return new Promise((resolve, reject) => {
glob(pattern, (err, files) => {
if (err) return reject(err);
resolve(files);
});
});
}
function writeLine(line: string, indent: string = '') {
if (/^\s*$/.test(line)) return;
if (/^TAP\s+version\s+\d+/.test(line)) return;
if (/^#\s+(?:Tests|Skipped|Passed|Failed):/.test(line)) return;
console.log(`${indent}${line}`);
}