-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmtest
More file actions
executable file
·168 lines (142 loc) · 3.98 KB
/
mtest
File metadata and controls
executable file
·168 lines (142 loc) · 3.98 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
#!/usr/bin/env node
import { promises as fsp, watch as fsWatch } from "fs";
import path from "path";
import { spawn } from "child_process";
const GREEN = "\x1b[32m";
const RED = "\x1b[31m";
const RESET = "\x1b[0m";
const TEST_DIR = path.join(process.cwd(), "tests");
function printHelp() {
console.log(`Usage: mtest [options]
Options:
-h, --help Show this help message
-w, --watch Watch current directory and re-run tests on changes
Behaviour:
No options Run all executable files under ./tests recursively.
Test passes if it exits with code 0.`);
}
function parseArgs() {
const args = process.argv.slice(2);
const watch = args.includes("-w") || args.includes("--watch");
const help = args.includes("-h") || args.includes("--help");
const known = new Set(["-w", "--watch", "-h", "--help"]);
const unknown = args.filter((a) => !known.has(a));
if (unknown.length) {
console.error("Unknown option(s):", unknown.join(" "));
printHelp();
process.exit(1);
}
return { watch, help };
}
async function isExecutable(file) {
try {
const st = await fsp.stat(file);
return st.isFile() && (st.mode & 0o111);
} catch {
return false;
}
}
async function findTests(dir = TEST_DIR) {
let tests = [];
try {
const entries = await fsp.readdir(dir, { withFileTypes: true });
for (const e of entries) {
const full = path.join(dir, e.name);
if (e.isDirectory()) {
tests = tests.concat(await findTests(full));
} else if (await isExecutable(full)) {
tests.push(full);
}
}
} catch {
// ignore missing ./tests
}
return tests;
}
function runTest(file) {
return new Promise((resolve) => {
const child = spawn(file, { stdio: ["ignore", "pipe", "pipe"] });
let out = "";
let err = "";
child.stdout.on("data", (d) => (out += d));
child.stderr.on("data", (d) => (err += d));
child.on("close", (code) => {
if (code === 0) {
console.log(`${GREEN}PASS${RESET} ${file}`);
return resolve(true);
}
console.log(`${RED}FAIL${RESET} ${file}`);
// If running inside GitHub Actions, emit an annotation
if (process.env.GITHUB_ACTIONS === "true") {
console.log(`::error file=${file}::Test failed`);
}
if (out.trim()) process.stdout.write(out);
if (err.trim()) process.stderr.write(err);
resolve(false);
});
child.on("error", (e) => {
console.log(`${RED}FAIL${RESET} ${file}`);
if (process.env.GITHUB_ACTIONS === "true") {
console.log(`::error file=${file}::${e.message}`);
} else {
console.error(e.message);
}
resolve(false);
});
});
}
async function runAllTests() {
const tests = await findTests();
if (!tests.length) {
console.log("No tests found in", TEST_DIR);
return { passed: 0, total: 0 };
}
let passed = 0;
for (const t of tests) {
if (await runTest(t)) passed++;
}
console.log(`\n${passed}/${tests.length} tests passed`);
return { passed, total: tests.length };
}
let rerunTimer = null;
function scheduleRerun() {
if (rerunTimer) return;
rerunTimer = setTimeout(async () => {
rerunTimer = null;
console.clear();
await runAllTests();
}, 100);
}
async function watchDir(dir) {
try {
fsWatch(dir, () => scheduleRerun());
} catch {
return;
}
const entries = await fsp.readdir(dir, { withFileTypes: true });
for (const e of entries) {
if (e.isDirectory()) {
if (e.name === "node_modules" || e.name === ".git") continue;
await watchDir(path.join(dir, e.name));
}
}
}
async function startWatch() {
await watchDir(process.cwd());
console.log("Watching for changes... (Ctrl+C to exit)");
}
(async () => {
const { watch, help } = parseArgs();
if (help) {
printHelp();
return;
}
const { passed, total } = await runAllTests();
// CI-friendly exit code: fail if any tests failed
if (!watch && total > 0 && passed < total) {
process.exitCode = 1;
}
if (watch) {
await startWatch();
}
})();