-
-
Notifications
You must be signed in to change notification settings - Fork 144
/
cli.ts
253 lines (233 loc) · 5.52 KB
/
cli.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
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
// thing:no-export
import { watch } from "chokidar";
import { readdirSync, statSync, writeFileSync } from "fs";
import { resolve } from "path";
import { GLOBAL_OPTS, TestResult } from "./api.js";
import { execute } from "./exec.js";
import { isString } from "./utils.js";
interface TestamentArgs {
csv: boolean;
json: boolean;
watch: boolean;
exclude: string[];
out?: string;
rest: string[];
}
const parseOpts = (args: string[], i = 2): TestamentArgs | number => {
const res = <TestamentArgs>{
csv: false,
json: false,
watch: false,
exclude: ["test/fixtures"],
rest: [],
};
for (; i < args.length; i++) {
switch (args[i]) {
case "-a":
case "--all":
GLOBAL_OPTS.stop = false;
break;
case "--csv":
res.csv = true;
break;
case "--json":
res.json = true;
break;
case "-o":
res.out = args[i + 1];
i++;
break;
case "-t":
case "--timeout":
const val = parseInt(args[i + 1]);
if (!isNaN(val)) {
GLOBAL_OPTS.timeOut = val;
} else {
console.warn("invalid timeout value", args[i + 1]);
return 1;
}
i++;
break;
case "-w":
case "--watch":
res.watch = true;
break;
case "-e":
case "--exclude":
res.exclude.push(args[i + 1]);
i++;
break;
case "-h":
case "--help":
console.log(`
Usage: testament [opts] path1 [path2...] [opts]
Options:
--all, -a Run all tests (don't stop at 1st failure)
--exclude, -e Exclude path prefix (multiple, always 'test/fixtures')
--csv Export results as CSV
--json Export results as JSON
-o Output file path for exported results
--timeout, -t Set default timeout value (milliseconds)
--watch, -w Watch given files/dirs for changes
--help, -h Print this help and quit
`);
return 0;
default:
if (/^--?/.test(args[i])) {
console.warn(`unknown option: ${args[i]}`);
return 1;
}
res.rest.push(args[i]);
break;
}
}
if (res.out && res.csv && res.json) {
console.warn(
"only CSV *or* JSON file output is supported, not both at the same time, exiting..."
);
return 1;
}
return res;
};
/**
* Recursively reads given directory and yields sequence of file names matching
* given extension (or regexp).
*
* @param dir -
* @param match -
* @param maxDepth -
* @param depth -
*/
export function* files(
dir: string,
match: string | RegExp,
maxDepth = Infinity,
depth = 0
): IterableIterator<string> {
if (depth >= maxDepth) return;
const re = isString(match)
? new RegExp(`${match.replace(/\./g, "\\.")}$`)
: match;
for (let f of readdirSync(dir)) {
const curr = dir + "/" + f;
if (re.test(f)) {
yield curr;
} else if (statSync(curr).isDirectory()) {
yield* files(curr, match, maxDepth, depth + 1);
}
}
}
const output = (body: string, path?: string) => {
if (path) {
GLOBAL_OPTS.logger.info("writing results to:", path);
try {
writeFileSync(path, body, "utf-8");
} catch (e) {
GLOBAL_OPTS.logger.warn((<Error>e).message);
}
} else {
console.log(body);
}
};
const formatCSV = (results: TestResult[]) =>
[
"Status,Group,Test,Time,Trials,Error",
...results.map((r) =>
[
r.error ? "fail" : "ok",
r.group || "",
r.title,
r.time,
r.trials,
r.error ? r.error.message.split("\n")[0] : "",
].join(",")
),
].join("\n");
const formatJSON = (results: TestResult[]) =>
JSON.stringify(
results.map((r) => ({
status: r.error ? "fail" : "ok",
group: r.group,
title: r.title,
time: r.time,
trials: r.trials,
error: r.error ? r.error.message.split("\n")[0] : undefined,
})),
null,
4
);
const randomID = () => `#${(Math.random() * 1e9) | 0}`;
const runTests = async (opts: TestamentArgs) => {
const imports: Promise<any>[] = [];
const sources: string[] = [];
const excludes = opts.exclude.map((x) => resolve(x));
const enque = (src: string) => {
src += randomID();
sources.push(src);
imports.push(import(src));
};
for (let src of opts.rest) {
src = resolve(src);
if (statSync(src).isDirectory()) {
for (let f of files(src, /\.[jt]s$/)) {
if (excludes.some((prefix) => f.startsWith(prefix))) continue;
enque(f);
}
} else {
enque(src);
}
}
GLOBAL_OPTS.logger.info(`importing ${imports.length} sources...`);
try {
await Promise.all(imports);
const results = await execute();
opts.csv && output(formatCSV(results), opts.out);
opts.json && output(formatJSON(results), opts.out);
} catch (e) {
if (GLOBAL_OPTS.exit) throw e;
}
};
const watchTests = async (opts: TestamentArgs) => {
const watcher = watch(opts.rest, { persistent: true });
const files = new Set<string>();
let tid: NodeJS.Timeout;
const rerunWith = (files: string[]) => {
clearTimeout(tid);
tid = setTimeout(() => runTests({ ...opts, rest: files }), 10);
};
GLOBAL_OPTS.logger.info("watching files... Press <Ctrl+C> to abort");
watcher.on("all", (id, path) => {
switch (id) {
case "add":
files.add(path);
rerunWith([...files]);
break;
case "change":
files.add(path);
rerunWith([path]);
break;
case "unlink":
files.delete(path);
break;
default:
}
});
};
(async () => {
const opts = parseOpts(process.argv);
if (typeof opts === "number") process.exit(opts);
// enable ANSI coloring for status messages
if (!process.env.NO_COLOR) {
GLOBAL_OPTS.fmt = {
success: (x) => `\x1b[32m✔︎ ${x}\x1b[0m`,
fail: (x) => `\x1b[31m✘ ${x}\x1b[0m`,
retry: (x) => `\x1b[93m${x}\x1b[0m`,
};
}
if (opts.watch) {
watchTests(opts);
} else {
GLOBAL_OPTS.exit = true;
await runTests(opts);
}
})();