bivium 0.8.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -6
- package/cli.js +51 -2191
- package/compute.js +4 -3
- package/config.js +3 -2
- package/docs/api.md +1 -0
- package/docs/ci.md +21 -0
- package/docs/cli.md +28 -2
- package/docs/config.md +50 -0
- package/docs/how-it-works.md +99 -0
- package/docs/tuning.md +24 -1
- package/errors.js +2 -1
- package/index.d.ts +6 -0
- package/package.json +1 -1
- package/shared-118ck8qt.js +40 -0
- package/{shared-9qcbzxw3.js → shared-70w84x4c.js} +2008 -1293
- package/shared-84c9vs8a.js +475 -0
- package/shared-a5kfg3qy.js +68 -0
- package/shared-a8ebfhzf.js +319 -0
- package/shared-byhfswm9.js +1047 -0
- package/shared-graedbt1.js +12 -0
- package/shared-ke52sg37.js +149 -0
- package/shared-m8z858pr.js +26 -0
- package/shared-my87dhfd.js +164 -0
- package/{shared-550cvwt2.js → shared-px5a17nj.js} +1 -1
- package/shared-qe34axez.js +19 -0
- package/shared-rzbfbyqq.js +549 -0
- package/shared-v5gfq9zy.js +13 -0
- package/shared-wahjx628.js +66 -0
- package/shared-x17wzptz.js +4 -0
- package/shared-y7y281x3.js +103 -0
- package/shared-yfvpkeff.js +30 -0
- package/shared-211m8fjt.js +0 -69
package/cli.js
CHANGED
|
@@ -1,2234 +1,94 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
|
-
BiviumError2,
|
|
4
|
-
didYouMean2
|
|
5
|
-
} from "./shared-211m8fjt.js";
|
|
6
|
-
import {
|
|
7
|
-
globToRegExp,
|
|
8
|
-
inRoot2
|
|
9
|
-
} from "./shared-550cvwt2.js";
|
|
10
|
-
import {
|
|
11
|
-
githubOutputPath,
|
|
12
3
|
nowMs,
|
|
13
|
-
environment
|
|
14
|
-
|
|
15
|
-
version,
|
|
16
|
-
QUERY,
|
|
17
|
-
UNDECLARED_READS,
|
|
18
|
-
RELATIVE,
|
|
19
|
-
aliasPath,
|
|
20
|
-
createResolver,
|
|
21
|
-
SCANNED,
|
|
22
|
-
formatOf,
|
|
23
|
-
extract,
|
|
24
|
-
stringsOf,
|
|
25
|
-
finish,
|
|
26
|
-
gotoPage,
|
|
27
|
-
walksByTypes,
|
|
28
|
-
importersOf,
|
|
29
|
-
affectedBy,
|
|
30
|
-
sorted,
|
|
31
|
-
PLAIN,
|
|
32
|
-
createPaint,
|
|
33
|
-
wantsColor,
|
|
34
|
-
isDumb,
|
|
35
|
-
SEPARATOR,
|
|
36
|
-
branch,
|
|
37
|
-
NOTE_INDENT,
|
|
38
|
-
closingLine,
|
|
39
|
-
failLine,
|
|
40
|
-
truncateWords,
|
|
41
|
-
truncatePath,
|
|
42
|
-
textWidth,
|
|
43
|
-
MEASURE,
|
|
44
|
-
RULE,
|
|
45
|
-
aligned,
|
|
46
|
-
headerRow,
|
|
47
|
-
plural,
|
|
48
|
-
SILENT,
|
|
49
|
-
createProgress,
|
|
50
|
-
formatSelection,
|
|
51
|
-
orphanOf,
|
|
52
|
-
computePlan2,
|
|
53
|
-
prepare2
|
|
54
|
-
} from "./shared-9qcbzxw3.js";
|
|
55
|
-
|
|
56
|
-
// src/main.ts
|
|
57
|
-
import { spawnSync } from "node:child_process";
|
|
58
|
-
import { appendFileSync, realpathSync } from "node:fs";
|
|
4
|
+
environment
|
|
5
|
+
} from "./shared-graedbt1.js";
|
|
59
6
|
import {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
isAbsolute as isAbsolute2,
|
|
63
|
-
join as join3,
|
|
64
|
-
posix as posix3,
|
|
65
|
-
relative,
|
|
66
|
-
resolve
|
|
67
|
-
} from "node:path";
|
|
68
|
-
|
|
69
|
-
// src/check.ts
|
|
70
|
-
import { statSync } from "node:fs";
|
|
71
|
-
import { join as join2, posix as posix2 } from "node:path";
|
|
72
|
-
|
|
73
|
-
// src/fanin.ts
|
|
74
|
-
function fanIn(graph, suite) {
|
|
75
|
-
const ids = new Map;
|
|
76
|
-
const nodes = [...graph.keys()];
|
|
77
|
-
for (const [i, node] of nodes.entries())
|
|
78
|
-
ids.set(node, i);
|
|
79
|
-
const matches = new Map;
|
|
80
|
-
const matching = (re) => {
|
|
81
|
-
let found = matches.get(re.source);
|
|
82
|
-
if (!found) {
|
|
83
|
-
found = [];
|
|
84
|
-
for (const [i, node] of nodes.entries())
|
|
85
|
-
if (re.test(node))
|
|
86
|
-
found.push(i);
|
|
87
|
-
matches.set(re.source, found);
|
|
88
|
-
}
|
|
89
|
-
return found;
|
|
90
|
-
};
|
|
91
|
-
const forward = [];
|
|
92
|
-
const direct = [];
|
|
93
|
-
for (const node of nodes) {
|
|
94
|
-
const deps = graph.get(node);
|
|
95
|
-
const out = [];
|
|
96
|
-
const read = [];
|
|
97
|
-
if (deps) {
|
|
98
|
-
for (const file of deps.files) {
|
|
99
|
-
if (!suite.types && deps.types?.has(file))
|
|
100
|
-
continue;
|
|
101
|
-
const id = ids.get(file);
|
|
102
|
-
if (id !== undefined)
|
|
103
|
-
out.push(id);
|
|
104
|
-
}
|
|
105
|
-
for (const re of deps.patterns)
|
|
106
|
-
out.push(...matching(re));
|
|
107
|
-
for (const re of [...deps.reads, ...deps.lists ?? []]) {
|
|
108
|
-
read.push(...matching(re));
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
forward.push(out);
|
|
112
|
-
direct.push(read);
|
|
113
|
-
}
|
|
114
|
-
const counts = new Uint32Array(nodes.length);
|
|
115
|
-
const stamp = new Int32Array(nodes.length).fill(-1);
|
|
116
|
-
const stack = [];
|
|
117
|
-
for (const [t, test] of nodes.entries()) {
|
|
118
|
-
if (!suite.isTest(test))
|
|
119
|
-
continue;
|
|
120
|
-
const reached = [];
|
|
121
|
-
const visit = (id) => {
|
|
122
|
-
if (stamp[id] === t)
|
|
123
|
-
return;
|
|
124
|
-
stamp[id] = t;
|
|
125
|
-
reached.push(id);
|
|
126
|
-
};
|
|
127
|
-
visit(t);
|
|
128
|
-
stack.push(t);
|
|
129
|
-
for (let at = stack.pop();at !== undefined; at = stack.pop()) {
|
|
130
|
-
for (const next of byId(forward, at)) {
|
|
131
|
-
if (stamp[next] === t)
|
|
132
|
-
continue;
|
|
133
|
-
visit(next);
|
|
134
|
-
stack.push(next);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
for (const id of [...reached]) {
|
|
138
|
-
for (const read of byId(direct, id))
|
|
139
|
-
visit(read);
|
|
140
|
-
}
|
|
141
|
-
for (const id of reached)
|
|
142
|
-
counts[id] = byId(counts, id) + 1;
|
|
143
|
-
}
|
|
144
|
-
const result = new Map;
|
|
145
|
-
for (const [i, node] of nodes.entries())
|
|
146
|
-
result.set(node, byId(counts, i));
|
|
147
|
-
return result;
|
|
148
|
-
}
|
|
149
|
-
function byId(list, id) {
|
|
150
|
-
const value = list[id];
|
|
151
|
-
if (value === undefined) {
|
|
152
|
-
throw new Error(`bivium: fanin: no node with id ${id}.`);
|
|
153
|
-
}
|
|
154
|
-
return value;
|
|
155
|
-
}
|
|
156
|
-
function graphReport(graph, suites) {
|
|
157
|
-
const counts = suites.map((suite) => [suite.name, fanIn(graph, suite)]);
|
|
158
|
-
const unknown = [...graph].filter(([, deps]) => deps.unknown.length > 0).map(([node]) => node);
|
|
159
|
-
const everyChange = walksByTypes(graph, unknown, {
|
|
160
|
-
includeUnknown: false,
|
|
161
|
-
readers: false
|
|
162
|
-
});
|
|
163
|
-
return {
|
|
164
|
-
suites: Object.fromEntries(suites.map((suite) => [
|
|
165
|
-
suite.name,
|
|
166
|
-
{
|
|
167
|
-
total: [...graph.keys()].filter(suite.isTest).length,
|
|
168
|
-
everyChange: sorted(everyChange(suite.types)).filter(suite.isTest)
|
|
169
|
-
}
|
|
170
|
-
])),
|
|
171
|
-
nodes: [...graph].map(([node, deps]) => ({
|
|
172
|
-
node,
|
|
173
|
-
imports: sorted(deps.files),
|
|
174
|
-
importers: sorted(importersOf(graph, node)),
|
|
175
|
-
fanIn: Object.fromEntries(counts.map(([name, count]) => [name, count.get(node) ?? 0])),
|
|
176
|
-
unknown: deps.unknown.length > 0
|
|
177
|
-
}))
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
function buckets(total) {
|
|
181
|
-
const list = [
|
|
182
|
-
[0, 0],
|
|
183
|
-
[1, 1]
|
|
184
|
-
];
|
|
185
|
-
for (let low = 2, high = 10;low < total; low = high + 1, high *= 10) {
|
|
186
|
-
list.push([low, Math.min(high, total - 1)]);
|
|
187
|
-
}
|
|
188
|
-
if (total > 1)
|
|
189
|
-
list.push([total, total]);
|
|
190
|
-
return list;
|
|
191
|
-
}
|
|
192
|
-
function formatGraph(report, top) {
|
|
193
|
-
const lines = [];
|
|
194
|
-
const files = report.nodes.filter(({ node }) => !node.startsWith("pkg:") && !node.includes("#"));
|
|
195
|
-
for (const [name, { total: tests, everyChange }] of Object.entries(report.suites)) {
|
|
196
|
-
const any = everyChange.length;
|
|
197
|
-
lines.push(`${name}: ${plural(tests, "test")}${any > 0 ? `; ${any} run on any change (\`bivium check\` says why)` : ""}`);
|
|
198
|
-
const ranked = files.map(({ node, fanIn: reach }) => [node, reach[name] ?? 0]).filter(([, count]) => count > 0).sort(([a, x], [b, y]) => y - x || (a < b ? -1 : 1));
|
|
199
|
-
const width = Math.max(String(tests).length, 5);
|
|
200
|
-
if (ranked.length > 0) {
|
|
201
|
-
lines.push(` ${"tests".padStart(width)} file`);
|
|
202
|
-
for (const [id, count] of ranked.slice(0, top)) {
|
|
203
|
-
lines.push(` ${String(count).padStart(width)} ${id}`);
|
|
204
|
-
}
|
|
205
|
-
if (ranked.length > top) {
|
|
206
|
-
lines.push(` … ${ranked.length - top} more (--top <n> shows more)`);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
lines.push(` files by how many ${name} tests a change to one selects:`);
|
|
210
|
-
const rows = buckets(tests).map(([low, high]) => {
|
|
211
|
-
const label = low === high ? String(low) : `${low}–${high}`;
|
|
212
|
-
const count = files.filter(({ fanIn: reach }) => {
|
|
213
|
-
const n = reach[name] ?? 0;
|
|
214
|
-
return n >= low && n <= high;
|
|
215
|
-
}).length;
|
|
216
|
-
return [label, count];
|
|
217
|
-
});
|
|
218
|
-
const labelWidth = Math.max(...rows.map(([label]) => label.length));
|
|
219
|
-
for (const [label, count] of rows) {
|
|
220
|
-
lines.push(` ${label.padStart(labelWidth)} ${count}`);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
return lines.join(`
|
|
224
|
-
`);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// src/human.ts
|
|
228
|
-
function duration(ms) {
|
|
229
|
-
if (ms < 999.5)
|
|
230
|
-
return `${Math.round(ms)} ms`;
|
|
231
|
-
const s = ms / 1000;
|
|
232
|
-
return `${s < 999.5 ? s.toPrecision(3) : Math.round(s)} s`;
|
|
233
|
-
}
|
|
234
|
-
function wrapped(text, first, hang) {
|
|
235
|
-
const lines = [];
|
|
236
|
-
let line = first;
|
|
237
|
-
let empty = true;
|
|
238
|
-
for (const word of text.split(" ")) {
|
|
239
|
-
if (!empty && textWidth(line) + 1 + textWidth(word) > MEASURE) {
|
|
240
|
-
lines.push(line);
|
|
241
|
-
line = hang + word;
|
|
242
|
-
continue;
|
|
243
|
-
}
|
|
244
|
-
line += empty ? word : ` ${word}`;
|
|
245
|
-
empty = false;
|
|
246
|
-
}
|
|
247
|
-
lines.push(line);
|
|
248
|
-
return lines;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// src/locate.ts
|
|
252
|
-
import { readFile } from "node:fs/promises";
|
|
253
|
-
import { isAbsolute, join } from "node:path";
|
|
254
|
-
var DISK_READ = /\bfrom\s*["'](?:node:)?(?:fs|fs\/promises|child_process)["']|\brequire\s*\(\s*["'](?:node:)?(?:fs|fs\/promises|child_process)["']|\bBun\.(?:file|Glob|spawn)/;
|
|
255
|
-
var NON_LITERAL_IMPORT = /\b(?:import|require|vi\.(?:mock|doMock|unmock|importActual|importMock))\s*(?:<[^>]*>)?\(\s*(?!["'][^"'`$]*["']\s*[,)])/;
|
|
256
|
-
var GLOB_CALL = /\bimport\.meta\.glob/;
|
|
257
|
-
var QUOTED_CAUSE = /"([^"]+)"/;
|
|
258
|
-
var LOADS = /\b(?:import|export|from|require|mock|doMock|unmock|importActual|importMock)\b|\b(?:src|href)\s*=/;
|
|
259
|
-
var GOTO_CALL = /\bgoto\s*\(/;
|
|
260
|
-
function lineAt(source, index) {
|
|
261
|
-
let line = 1;
|
|
262
|
-
for (let i = source.indexOf(`
|
|
263
|
-
`);i !== -1 && i < index; i = source.indexOf(`
|
|
264
|
-
`, i + 1)) {
|
|
265
|
-
line++;
|
|
266
|
-
}
|
|
267
|
-
return line;
|
|
268
|
-
}
|
|
269
|
-
function lineOf(source, needle) {
|
|
270
|
-
const index = typeof needle === "string" ? source.indexOf(needle) : source.search(needle);
|
|
271
|
-
return index === -1 ? undefined : lineAt(source, index);
|
|
272
|
-
}
|
|
273
|
-
function commentRanges(source) {
|
|
274
|
-
const ranges = [];
|
|
275
|
-
for (let i = 0;i < source.length; i++) {
|
|
276
|
-
const c = source[i];
|
|
277
|
-
if (c === '"' || c === "'") {
|
|
278
|
-
while (++i < source.length && source[i] !== c && source[i] !== `
|
|
279
|
-
`) {
|
|
280
|
-
if (source[i] === "\\")
|
|
281
|
-
i++;
|
|
282
|
-
}
|
|
283
|
-
} else if (c === "`") {
|
|
284
|
-
while (++i < source.length && source[i] !== "`") {
|
|
285
|
-
if (source[i] === "\\")
|
|
286
|
-
i++;
|
|
287
|
-
}
|
|
288
|
-
} else if (source.startsWith("//", i)) {
|
|
289
|
-
const end = source.indexOf(`
|
|
290
|
-
`, i);
|
|
291
|
-
ranges.push([i, end === -1 ? source.length : end]);
|
|
292
|
-
i = end === -1 ? source.length : end;
|
|
293
|
-
} else if (source.startsWith("/*", i) || source.startsWith("<!--", i)) {
|
|
294
|
-
const close = c === "/" ? "*/" : "-->";
|
|
295
|
-
const end = source.indexOf(close, i + 2);
|
|
296
|
-
const stop = end === -1 ? source.length : end + close.length;
|
|
297
|
-
ranges.push([i, stop]);
|
|
298
|
-
i = stop - 1;
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
return ranges;
|
|
302
|
-
}
|
|
303
|
-
function lineTextAt(source, index) {
|
|
304
|
-
const start = source.lastIndexOf(`
|
|
305
|
-
`, index - 1) + 1;
|
|
306
|
-
const end = source.indexOf(`
|
|
307
|
-
`, index);
|
|
308
|
-
return source.slice(start, end === -1 ? source.length : end);
|
|
309
|
-
}
|
|
310
|
-
function bestLine(source, needles, near) {
|
|
311
|
-
let comments;
|
|
312
|
-
const inComment = (index) => {
|
|
313
|
-
comments ??= commentRanges(source);
|
|
314
|
-
return comments.some(([start, end]) => index >= start && index < end);
|
|
315
|
-
};
|
|
316
|
-
let code;
|
|
317
|
-
let any;
|
|
318
|
-
for (const needle of needles) {
|
|
319
|
-
for (let index = source.indexOf(needle);index !== -1; index = source.indexOf(needle, index + 1)) {
|
|
320
|
-
any ??= index;
|
|
321
|
-
if (inComment(index))
|
|
322
|
-
continue;
|
|
323
|
-
if (near === undefined || near.test(lineTextAt(source, index))) {
|
|
324
|
-
return lineAt(source, index);
|
|
325
|
-
}
|
|
326
|
-
code ??= index;
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
const found = code ?? any;
|
|
330
|
-
return found === undefined ? undefined : lineAt(source, found);
|
|
331
|
-
}
|
|
332
|
-
function specifierLine(source, specifier) {
|
|
333
|
-
const quoted = ['"', "'", "`"].map((quote) => `${quote}${specifier}${quote}`);
|
|
334
|
-
return bestLine(source, quoted, LOADS) ?? bestLine(source, [specifier]);
|
|
335
|
-
}
|
|
336
|
-
function gotoLine(source, argument, page) {
|
|
337
|
-
return (argument === undefined ? undefined : bestLine(source, [argument], GOTO_CALL)) ?? bestLine(source, [page.slice(page.lastIndexOf("/") + 1)], GOTO_CALL);
|
|
338
|
-
}
|
|
339
|
-
function callLine(source, call) {
|
|
340
|
-
return bestLine(source, [call]);
|
|
341
|
-
}
|
|
342
|
-
function causeLine(source, cause) {
|
|
343
|
-
if (cause.startsWith("unresolved ")) {
|
|
344
|
-
const specifier = QUOTED_CAUSE.exec(cause)?.[1];
|
|
345
|
-
return specifier === undefined ? undefined : specifierLine(source, specifier);
|
|
346
|
-
}
|
|
347
|
-
if (cause === "non-literal dynamic import")
|
|
348
|
-
return lineOf(source, NON_LITERAL_IMPORT);
|
|
349
|
-
if (cause === "non-literal glob import")
|
|
350
|
-
return lineOf(source, GLOB_CALL);
|
|
351
|
-
if (cause === UNDECLARED_READS) {
|
|
352
|
-
return lineOf(source, DISK_READ);
|
|
353
|
-
}
|
|
354
|
-
if (cause.startsWith("non-literal goto(")) {
|
|
355
|
-
return lineOf(source, cause.slice("non-literal ".length, -1));
|
|
356
|
-
}
|
|
357
|
-
const url = QUOTED_CAUSE.exec(cause)?.[1];
|
|
358
|
-
return url === undefined ? undefined : lineOf(source, url);
|
|
359
|
-
}
|
|
360
|
-
function textLine(source, text) {
|
|
361
|
-
return lineOf(source, text);
|
|
362
|
-
}
|
|
363
|
-
function annotationLine(source, tag, glob) {
|
|
364
|
-
const index = source.split(`
|
|
365
|
-
`).findIndex((line) => line.includes(tag) && line.includes(glob));
|
|
366
|
-
return index === -1 ? undefined : index + 1;
|
|
367
|
-
}
|
|
368
|
-
function lineText(source, line) {
|
|
369
|
-
let start = 0;
|
|
370
|
-
for (let n = 1;n < line; n++) {
|
|
371
|
-
const next = source.indexOf(`
|
|
372
|
-
`, start);
|
|
373
|
-
if (next === -1)
|
|
374
|
-
return "";
|
|
375
|
-
start = next + 1;
|
|
376
|
-
}
|
|
377
|
-
const end = source.indexOf(`
|
|
378
|
-
`, start);
|
|
379
|
-
return source.slice(start, end === -1 ? source.length : end).trim();
|
|
380
|
-
}
|
|
381
|
-
async function sourceOf(root, file) {
|
|
382
|
-
try {
|
|
383
|
-
return await readFile(isAbsolute(file) ? file : join(root, file), "utf-8");
|
|
384
|
-
} catch {
|
|
385
|
-
return;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
function fileOf(node) {
|
|
389
|
-
const hash = node.indexOf("#");
|
|
390
|
-
return hash === -1 ? node : node.slice(0, hash);
|
|
391
|
-
}
|
|
392
|
-
async function causesOf(root, computed, node) {
|
|
393
|
-
const file = fileOf(node);
|
|
394
|
-
const source = await sourceOf(root, file);
|
|
395
|
-
return (computed.graph.get(node)?.unknown ?? []).map((cause) => {
|
|
396
|
-
if (source === undefined)
|
|
397
|
-
return { cause };
|
|
398
|
-
const line = causeLine(source, cause);
|
|
399
|
-
return line === undefined ? { cause } : { cause, at: `${file}:${line}`, text: lineText(source, line) };
|
|
400
|
-
});
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
// src/spelled.ts
|
|
404
|
-
import { posix } from "node:path";
|
|
405
|
-
var PATH_LIKE = /^[\w@.+~()[\]-]+(?:\/[\w@.+~()[\]-]+)*$/;
|
|
406
|
-
var LETTER = /[a-z]/i;
|
|
407
|
-
var LEADING_SLASHES = /^\/+/;
|
|
408
|
-
var TRAILING_SLASHES = /\/+$/;
|
|
409
|
-
var DOT_RELATIVE = /^\.\.?\//;
|
|
410
|
-
function candidates(file, text) {
|
|
411
|
-
let path = text.replace(TRAILING_SLASHES, "");
|
|
412
|
-
const rooted = path.startsWith("/");
|
|
413
|
-
if (rooted)
|
|
414
|
-
path = path.replace(LEADING_SLASHES, "");
|
|
415
|
-
if (!PATH_LIKE.test(path) || !LETTER.test(path))
|
|
416
|
-
return [];
|
|
417
|
-
const dir = posix.dirname(file);
|
|
418
|
-
let found;
|
|
419
|
-
if (rooted)
|
|
420
|
-
found = path.includes("/") ? [posix.normalize(path)] : [];
|
|
421
|
-
else if (DOT_RELATIVE.test(path))
|
|
422
|
-
found = [posix.join(dir, path)];
|
|
423
|
-
else if (path.includes("/")) {
|
|
424
|
-
found = [posix.normalize(path), posix.join(dir, path)];
|
|
425
|
-
} else
|
|
426
|
-
found = [posix.join(dir, path)];
|
|
427
|
-
return found.filter((p) => p !== "." && !p.startsWith("../"));
|
|
428
|
-
}
|
|
429
|
-
function fixFor(file, path, dir) {
|
|
430
|
-
if (dir) {
|
|
431
|
-
return `if it reads them, add \`reads: { "${file}": "${path}/**" }\` (or \`// @reads ${path}/**\`), \`@depends-on\` if it runs them, \`@lists\` if it only names them`;
|
|
432
|
-
}
|
|
433
|
-
if (SCANNED.test(path)) {
|
|
434
|
-
return `if it runs it, add \`edges: { "${file}": "${path}" }\` (or \`// @depends-on ${path}\`), \`@reads\` if it reads it as text, \`@lists\` if it only names it`;
|
|
435
|
-
}
|
|
436
|
-
return `if it reads it, add \`reads: { "${file}": "${path}" }\` (or \`// @reads ${path}\`), \`@lists\` if it only names it`;
|
|
437
|
-
}
|
|
438
|
-
function spellers(config, { graph }) {
|
|
439
|
-
const found = new Map;
|
|
440
|
-
for (const suite of config.suites) {
|
|
441
|
-
const reached = fanIn(graph, suite);
|
|
442
|
-
for (const [node, deps] of graph) {
|
|
443
|
-
if (deps.unknown.length > 0 || !SCANNED.test(node))
|
|
444
|
-
continue;
|
|
445
|
-
if (node.startsWith("pkg:") || node.includes("#"))
|
|
446
|
-
continue;
|
|
447
|
-
if (suite.isTest(node) || (reached.get(node) ?? 0) > 0 && suite.roots.some((root) => inRoot2(node, root))) {
|
|
448
|
-
const suites = found.get(node);
|
|
449
|
-
suites ? suites.push(suite) : found.set(node, [suite]);
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
return new Map(sorted(found.keys()).map((node) => [node, found.get(node) ?? []]));
|
|
454
|
-
}
|
|
455
|
-
function runsAnyway(config, { graph }, suites, targets) {
|
|
456
|
-
if (targets.some((file) => config.isSelf(file) || suites.some((suite) => suite.isWiring(file)))) {
|
|
457
|
-
return "wiring";
|
|
458
|
-
}
|
|
459
|
-
const orphan = targets.some((file) => !config.isIgnored(file) && suites.some((suite) => orphanOf(graph, config, suite, file)));
|
|
460
|
-
return orphan ? "orphan" : undefined;
|
|
461
|
-
}
|
|
462
|
-
function withNames(graph, targets) {
|
|
463
|
-
return targets.flatMap((file) => [
|
|
464
|
-
file,
|
|
465
|
-
...[...graph.get(file)?.barrel?.keys() ?? []].map((name) => `${file}#${name}`)
|
|
466
|
-
]);
|
|
467
|
-
}
|
|
468
|
-
async function spelledPaths(root, config, computed, done = () => {}) {
|
|
469
|
-
const { graph } = computed;
|
|
470
|
-
const files = new Set;
|
|
471
|
-
const dirs = new Map;
|
|
472
|
-
for (const node of graph.keys()) {
|
|
473
|
-
if (node.startsWith("pkg:") || node.includes("#"))
|
|
474
|
-
continue;
|
|
475
|
-
files.add(node);
|
|
476
|
-
for (let dir = posix.dirname(node);dir !== "."; ) {
|
|
477
|
-
const list = dirs.get(dir);
|
|
478
|
-
list ? list.push(node) : dirs.set(dir, [node]);
|
|
479
|
-
dir = posix.dirname(dir);
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
const found = { undeclared: [], orphaned: [] };
|
|
483
|
-
const spelling = spellers(config, computed);
|
|
484
|
-
const nodes = [...spelling.keys()];
|
|
485
|
-
const sources = await Promise.all(nodes.map((node) => sourceOf(root, node)));
|
|
486
|
-
const every = Math.max(1, Math.ceil(nodes.length / 20));
|
|
487
|
-
for (const [i, node] of nodes.entries()) {
|
|
488
|
-
if (i % every === 0)
|
|
489
|
-
done(i / nodes.length);
|
|
490
|
-
const source = sources[i];
|
|
491
|
-
const suites = spelling.get(node) ?? [];
|
|
492
|
-
const through = [
|
|
493
|
-
...affectedBy(graph, [node], { includeUnknown: false })
|
|
494
|
-
].filter((path) => suites.some((suite) => suite.isTest(path)));
|
|
495
|
-
if (source === undefined)
|
|
496
|
-
continue;
|
|
497
|
-
const seen = new Set;
|
|
498
|
-
for (const text of stringsOf(formatOf(node), source) ?? []) {
|
|
499
|
-
for (const path of candidates(node, text)) {
|
|
500
|
-
if (seen.has(path) || path === node)
|
|
501
|
-
continue;
|
|
502
|
-
seen.add(path);
|
|
503
|
-
const dir = !files.has(path);
|
|
504
|
-
if (dir && node.startsWith(`${path}/`))
|
|
505
|
-
continue;
|
|
506
|
-
const targets = dir ? dirs.get(path) : [path];
|
|
507
|
-
if (!targets)
|
|
508
|
-
continue;
|
|
509
|
-
const selected = affectedBy(graph, withNames(graph, targets));
|
|
510
|
-
if (through.every((test) => selected.has(test)))
|
|
511
|
-
continue;
|
|
512
|
-
const anyway = runsAnyway(config, computed, suites, targets);
|
|
513
|
-
if (anyway === "wiring")
|
|
514
|
-
continue;
|
|
515
|
-
const line = specifierLine(source, text);
|
|
516
|
-
const entry = {
|
|
517
|
-
file: node,
|
|
518
|
-
path,
|
|
519
|
-
dir,
|
|
520
|
-
fix: fixFor(node, path, dir)
|
|
521
|
-
};
|
|
522
|
-
if (line !== undefined)
|
|
523
|
-
entry.at = `${node}:${line}`;
|
|
524
|
-
found[anyway === "orphan" ? "orphaned" : "undeclared"].push(entry);
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
return found;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
// src/check.ts
|
|
532
|
-
var EXAMPLES = 3;
|
|
533
|
-
var TAGS = {
|
|
534
|
-
"depends-on": "@depends-on",
|
|
535
|
-
reads: "@reads",
|
|
536
|
-
lists: "@lists",
|
|
537
|
-
glob: "import.meta.glob",
|
|
538
|
-
edges: "edges",
|
|
539
|
-
setup: "setup"
|
|
540
|
-
};
|
|
541
|
-
var QUOTED = /"([^"]+)"/;
|
|
542
|
-
function fixFor2(cause) {
|
|
543
|
-
if (cause.startsWith("unresolved ")) {
|
|
544
|
-
const specifier = QUOTED.exec(cause)?.[1] ?? "";
|
|
545
|
-
if (specifier.startsWith("./%sveltekit.assets%/")) {
|
|
546
|
-
return "no file in the SvelteKit project's `static/` (a custom `kit.files.assets` isn't read): fix the path, or add an `edges` entry for it";
|
|
547
|
-
}
|
|
548
|
-
if (RELATIVE.test(specifier)) {
|
|
549
|
-
return "no file at that path: fix the import, or add an `edges` entry if a build generates it";
|
|
550
|
-
}
|
|
551
|
-
if (specifier.startsWith("#")) {
|
|
552
|
-
return "map it in the `imports` of the file's package.json, or with `alias`";
|
|
553
|
-
}
|
|
554
|
-
return "declare the package in a package.json, list it in `external` if a dependency installs it, or map it with `alias` or tsconfig `paths`";
|
|
555
|
-
}
|
|
556
|
-
if (cause.startsWith("only declarations for ")) {
|
|
557
|
-
return "only a declaration file is there, so what runs isn't in the graph: if a build writes it, name it in `edges` with its inputs, or import it with `import type` if only its types are used";
|
|
558
|
-
}
|
|
559
|
-
if (cause === "non-literal dynamic import") {
|
|
560
|
-
return "spell the path as a string literal, load the files with `import.meta.glob` and literal patterns, or list the file in `hermetic` if it only imports paths its caller passes";
|
|
561
|
-
}
|
|
562
|
-
if (cause === "non-literal glob import") {
|
|
563
|
-
return "pass `import.meta.glob` literal patterns";
|
|
564
|
-
}
|
|
565
|
-
if (cause === UNDECLARED_READS) {
|
|
566
|
-
return "name what it reads: `// @depends-on <glob>`, or `// @reads <glob>` for files read as text, or an `edges` or `reads` entry for the file in the config; list the file in `hermetic` if it reads no file of the repository";
|
|
567
|
-
}
|
|
568
|
-
if (cause.startsWith("non-literal goto(")) {
|
|
569
|
-
return "pass `goto()` a literal URL, or a template whose path before `?` or `#` is literal; or name the pages it can open with `// @depends-on <glob>`";
|
|
570
|
-
}
|
|
571
|
-
if (cause.startsWith("missing fixture ")) {
|
|
572
|
-
return "no page at that URL: fix the URL, or the suite's `urls`";
|
|
573
|
-
}
|
|
574
|
-
if (cause.startsWith("goto(")) {
|
|
575
|
-
return "map the URL to a file with the suite's `urls`";
|
|
576
|
-
}
|
|
577
|
-
return "see the cause";
|
|
578
|
-
}
|
|
579
|
-
function isFile(path) {
|
|
580
|
-
return statSync(path, { throwIfNoEntry: false })?.isFile() ?? false;
|
|
581
|
-
}
|
|
582
|
-
function stemOf(path) {
|
|
583
|
-
const slash = path.lastIndexOf("/");
|
|
584
|
-
const dot = path.indexOf(".", slash + 2);
|
|
585
|
-
return dot === -1 ? path : path.slice(0, dot);
|
|
586
|
-
}
|
|
587
|
-
function nearMisses(root, config, { graph, listed }) {
|
|
588
|
-
let stems;
|
|
589
|
-
const listedSet = new Set(listed);
|
|
590
|
-
const siblings = (path) => {
|
|
591
|
-
if (!stems) {
|
|
592
|
-
stems = new Map;
|
|
593
|
-
for (const file of listed) {
|
|
594
|
-
const stem = stemOf(file);
|
|
595
|
-
const list = stems.get(stem);
|
|
596
|
-
list ? list.push(file) : stems.set(stem, [file]);
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
return stems.get(stemOf(path)) ?? [];
|
|
600
|
-
};
|
|
601
|
-
const candidates = (base) => [
|
|
602
|
-
base,
|
|
603
|
-
...config.extensions.map((ext) => base + ext),
|
|
604
|
-
...config.extensions.map((ext) => `${base}/index${ext}`)
|
|
605
|
-
];
|
|
606
|
-
return (node, cause) => {
|
|
607
|
-
if (!cause.startsWith("unresolved "))
|
|
608
|
-
return;
|
|
609
|
-
const specifier = (QUOTED.exec(cause)?.[1] ?? "").replace(QUERY, "");
|
|
610
|
-
const base = RELATIVE.test(specifier) ? posix2.join(posix2.dirname(fileOf(node)), specifier) : config.alias.map((alias) => aliasPath(alias, specifier)).find((path) => path !== undefined);
|
|
611
|
-
if (base === undefined || base.startsWith("../"))
|
|
612
|
-
return;
|
|
613
|
-
const tried = candidates(base);
|
|
614
|
-
const outside = tried.find((path) => listedSet.has(path) && !graph.has(path));
|
|
615
|
-
if (outside !== undefined) {
|
|
616
|
-
return `${outside} exists, but outside \`roots\`: add it to \`roots\``;
|
|
617
|
-
}
|
|
618
|
-
const unlisted = tried.find((path) => isFile(join2(root, path)));
|
|
619
|
-
if (unlisted !== undefined && !listedSet.has(unlisted)) {
|
|
620
|
-
return `${unlisted} exists, but git doesn't list it (ignored?): if a build writes it, name it in \`edges\` with its inputs`;
|
|
621
|
-
}
|
|
622
|
-
const sibling = siblings(base).find((file) => graph.has(file));
|
|
623
|
-
if (sibling !== undefined) {
|
|
624
|
-
return `${sibling} exists: import it by that name, or add its extension to \`extensions\``;
|
|
625
|
-
}
|
|
626
|
-
return;
|
|
627
|
-
};
|
|
628
|
-
}
|
|
629
|
-
function declaredGlobs(node, extraction) {
|
|
630
|
-
const relative = finish(node, extraction).globs;
|
|
631
|
-
return [
|
|
632
|
-
...extraction.dependsOn.map((glob) => ({
|
|
633
|
-
kind: "depends-on",
|
|
634
|
-
glob,
|
|
635
|
-
pattern: globToRegExp(glob)
|
|
636
|
-
})),
|
|
637
|
-
...extraction.reads.map((glob) => ({
|
|
638
|
-
kind: "reads",
|
|
639
|
-
glob,
|
|
640
|
-
pattern: globToRegExp(glob)
|
|
641
|
-
})),
|
|
642
|
-
...extraction.lists.map((glob) => ({
|
|
643
|
-
kind: "lists",
|
|
644
|
-
glob,
|
|
645
|
-
pattern: globToRegExp(glob)
|
|
646
|
-
})),
|
|
647
|
-
...extraction.globs.map((glob, i) => {
|
|
648
|
-
const path = relative[i];
|
|
649
|
-
if (path === undefined) {
|
|
650
|
-
throw new Error(`bivium: check: no resolved path for glob ${glob}.`);
|
|
651
|
-
}
|
|
652
|
-
return { kind: "glob", glob, pattern: globToRegExp(path) };
|
|
653
|
-
})
|
|
654
|
-
];
|
|
655
|
-
}
|
|
656
|
-
function problemOf(matches, ignored) {
|
|
657
|
-
if (matches === 0)
|
|
658
|
-
return "matches nothing";
|
|
659
|
-
if (ignored === 0)
|
|
660
|
-
return;
|
|
661
|
-
return ignored === matches ? "matches only ignored files" : "partly ignored";
|
|
662
|
-
}
|
|
663
|
-
function deadOf(node, kind, glob, patterns, listed, skipped) {
|
|
664
|
-
const matches = listed.filter((file) => patterns.some((re) => re.test(file)));
|
|
665
|
-
const ignored = sorted(matches.filter(skipped));
|
|
666
|
-
const problem = problemOf(matches.length, ignored.length);
|
|
667
|
-
if (problem === undefined)
|
|
668
|
-
return;
|
|
669
|
-
return {
|
|
670
|
-
node,
|
|
671
|
-
kind,
|
|
672
|
-
glob,
|
|
673
|
-
problem,
|
|
674
|
-
matches: matches.length,
|
|
675
|
-
ignored: ignored.length,
|
|
676
|
-
examples: ignored.slice(0, EXAMPLES),
|
|
677
|
-
fix: problem === "matches nothing" ? "fix the path" : "point it at files the selector reads, or allowlist it in `deadGlobs` with a reason"
|
|
678
|
-
};
|
|
679
|
-
}
|
|
680
|
-
function configDeadGlobs(config, { graph, listed }, skipped) {
|
|
681
|
-
const file = config.file ?? "the config";
|
|
682
|
-
const found = [];
|
|
683
|
-
const entries = [
|
|
684
|
-
...config.generated.map((map) => ["edges", map]),
|
|
685
|
-
...config.reads.map((map) => ["reads", map])
|
|
686
|
-
];
|
|
687
|
-
for (const [kind, map] of entries) {
|
|
688
|
-
const patterns = map.values.map(() => new Map);
|
|
689
|
-
let matched = false;
|
|
690
|
-
for (const node of graph.keys()) {
|
|
691
|
-
const targets = map.targets(node);
|
|
692
|
-
if (!targets)
|
|
693
|
-
continue;
|
|
694
|
-
matched = true;
|
|
695
|
-
for (const [i, re] of targets.entries())
|
|
696
|
-
patterns[i]?.set(re.source, re);
|
|
697
|
-
}
|
|
698
|
-
if (!matched) {
|
|
699
|
-
found.push({
|
|
700
|
-
node: file,
|
|
701
|
-
kind,
|
|
702
|
-
glob: map.key,
|
|
703
|
-
problem: "matches nothing",
|
|
704
|
-
matches: 0,
|
|
705
|
-
ignored: 0,
|
|
706
|
-
examples: [],
|
|
707
|
-
fix: "fix the path: no file the graph reads matches it"
|
|
708
|
-
});
|
|
709
|
-
continue;
|
|
710
|
-
}
|
|
711
|
-
for (const [i, glob] of map.values.entries()) {
|
|
712
|
-
const expanded = [...patterns[i]?.values() ?? []];
|
|
713
|
-
const dead = deadOf(file, kind, glob, expanded, listed, skipped);
|
|
714
|
-
if (dead)
|
|
715
|
-
found.push(dead);
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
for (const suite of config.suites) {
|
|
719
|
-
for (const glob of suite.setup) {
|
|
720
|
-
const pattern = globToRegExp(glob);
|
|
721
|
-
const dead = deadOf(file, "setup", glob, [pattern], listed, skipped);
|
|
722
|
-
if (dead)
|
|
723
|
-
found.push(dead);
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
return found;
|
|
727
|
-
}
|
|
728
|
-
async function findDeadGlobs(root, config, computed) {
|
|
729
|
-
const { graph, extractions, listed } = computed;
|
|
730
|
-
const skipped = (file) => config.isIgnored(file) && !graph.has(file);
|
|
731
|
-
const found = [];
|
|
732
|
-
for (const node of graph.keys()) {
|
|
733
|
-
const extraction = extractions.get(node);
|
|
734
|
-
if (!extraction)
|
|
735
|
-
continue;
|
|
736
|
-
for (const { kind, glob, pattern } of declaredGlobs(node, extraction)) {
|
|
737
|
-
const dead = deadOf(node, kind, glob, [pattern], listed, skipped);
|
|
738
|
-
if (dead)
|
|
739
|
-
found.push(dead);
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
const configured = configDeadGlobs(config, computed, skipped);
|
|
743
|
-
const nodes = [
|
|
744
|
-
...new Set([
|
|
745
|
-
...found.map(({ node }) => node),
|
|
746
|
-
...configured.map(({ node }) => node)
|
|
747
|
-
])
|
|
748
|
-
];
|
|
749
|
-
const texts = await Promise.all(nodes.map((node) => sourceOf(root, node)));
|
|
750
|
-
const sources = new Map(nodes.map((node, i) => [node, texts[i]]));
|
|
751
|
-
const locate = (dead, line) => {
|
|
752
|
-
const source = sources.get(dead.node);
|
|
753
|
-
const at = source === undefined ? undefined : line(source);
|
|
754
|
-
return at === undefined ? dead : { ...dead, at: `${dead.node}:${at}` };
|
|
755
|
-
};
|
|
756
|
-
return [
|
|
757
|
-
...found.map((dead) => locate(dead, (source) => dead.kind === "glob" ? specifierLine(source, dead.glob) : annotationLine(source, TAGS[dead.kind], dead.glob))),
|
|
758
|
-
...configured.map((dead) => locate(dead, (source) => specifierLine(source, dead.glob)))
|
|
759
|
-
];
|
|
760
|
-
}
|
|
761
|
-
var CONFIG_FILE = /(?:^|[./_-])(?:config|setup|teardown|harness)(?:[./_-]|$)|^tsconfig[^/]*\.json$|^\.[^/]*rc(?:\.[^/]+)?$/;
|
|
762
|
-
function unreachedFix(file, tests, spelled) {
|
|
763
|
-
const named = spelled.find(({ path }) => file === path || file.startsWith(`${path}/`));
|
|
764
|
-
if (named !== undefined) {
|
|
765
|
-
return `${named.at ?? named.file} names ${named.path}: ${named.fix}`;
|
|
766
|
-
}
|
|
767
|
-
const name = posix2.basename(file);
|
|
768
|
-
const owner = tests.find((test) => name.startsWith(`${posix2.basename(test)}.`));
|
|
769
|
-
if (owner !== undefined) {
|
|
770
|
-
return `if ${owner} reads it, add \`reads: { "${owner}": "${file}" }\` (or \`// @reads ${file}\` in the test)`;
|
|
771
|
-
}
|
|
772
|
-
if (CONFIG_FILE.test(name)) {
|
|
773
|
-
return "if the runner loads it, name it in the suite's `wiring` (or `setup`, if it runs before every test), which makes the full run deliberate";
|
|
774
|
-
}
|
|
775
|
-
return "if a test reads or runs it, say so with `reads` or `@depends-on` in that test or what loads it; if none can observe it, list it in the suite's `nonInputs`, or delete it if it's dead";
|
|
776
|
-
}
|
|
777
|
-
function unreachedFiles(config, { graph }, spelled) {
|
|
778
|
-
const found = [];
|
|
779
|
-
const files = [...graph.keys()].filter((node) => !node.startsWith("pkg:") && !node.includes("#"));
|
|
780
|
-
for (const suite of config.suites) {
|
|
781
|
-
const reached = fanIn(graph, suite);
|
|
782
|
-
const tests = files.filter(suite.isTest);
|
|
783
|
-
for (const file of files) {
|
|
784
|
-
if ((reached.get(file) ?? 0) > 0)
|
|
785
|
-
continue;
|
|
786
|
-
if (config.isIgnored(file) || config.isSelf(file))
|
|
787
|
-
continue;
|
|
788
|
-
if (suite.isWiring(file))
|
|
789
|
-
continue;
|
|
790
|
-
if (!orphanOf(graph, config, suite, file))
|
|
791
|
-
continue;
|
|
792
|
-
found.push({
|
|
793
|
-
suite: suite.name,
|
|
794
|
-
file,
|
|
795
|
-
fix: unreachedFix(file, tests, spelled)
|
|
796
|
-
});
|
|
797
|
-
}
|
|
798
|
-
}
|
|
799
|
-
return found;
|
|
800
|
-
}
|
|
801
|
-
async function check(root, config, computed, step = () => {}) {
|
|
802
|
-
step("finding what the graph can't read", 0.05);
|
|
803
|
-
const nodes = [...computed.graph].filter(([, deps]) => deps.unknown.length > 0).map(([node]) => node);
|
|
804
|
-
const testsOf = (node) => {
|
|
805
|
-
const reach = walksByTypes(computed.graph, [node], {
|
|
806
|
-
includeUnknown: false,
|
|
807
|
-
readers: false
|
|
808
|
-
});
|
|
809
|
-
const found = new Set;
|
|
810
|
-
for (const suite of config.suites) {
|
|
811
|
-
for (const test of reach(suite.types)) {
|
|
812
|
-
if (suite.isTest(test))
|
|
813
|
-
found.add(test);
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
|
-
return sorted(found);
|
|
817
|
-
};
|
|
818
|
-
const nearMiss = nearMisses(root, config, computed);
|
|
819
|
-
const known = new Map((config.knownUnknowns ?? []).map((entry) => [entry.file, entry.reason]));
|
|
820
|
-
const unknown = await Promise.all(nodes.map(async (node) => {
|
|
821
|
-
const causes = (await causesOf(root, computed, node)).map(({ cause, at }) => {
|
|
822
|
-
const fix = nearMiss(node, cause) ?? fixFor2(cause);
|
|
823
|
-
return at === undefined ? { cause, fix } : { cause, at, fix };
|
|
824
|
-
});
|
|
825
|
-
const reason = known.get(node);
|
|
826
|
-
const entry = { node, causes, tests: testsOf(node) };
|
|
827
|
-
if (reason !== undefined)
|
|
828
|
-
entry.known = reason;
|
|
829
|
-
return entry;
|
|
830
|
-
}));
|
|
831
|
-
const fallbacks = [...computed.fallbacks].map(([file, reason]) => ({
|
|
832
|
-
file,
|
|
833
|
-
reason
|
|
834
|
-
}));
|
|
835
|
-
step("finding dead globs", 0.1);
|
|
836
|
-
const dead = await findDeadGlobs(root, config, computed);
|
|
837
|
-
const spelling = "finding paths nothing declares";
|
|
838
|
-
const spelled = await spelledPaths(root, config, computed, (ratio) => step(spelling, 0.15 + 0.8 * ratio));
|
|
839
|
-
step("finding files no test reaches", 0.95);
|
|
840
|
-
const unreached = unreachedFiles(config, computed, [
|
|
841
|
-
...spelled.undeclared,
|
|
842
|
-
...spelled.orphaned
|
|
843
|
-
]);
|
|
844
|
-
const allows = (entry, found) => entry.file === found.node && entry.glob === found.glob;
|
|
845
|
-
return {
|
|
846
|
-
unknown,
|
|
847
|
-
fallbacks,
|
|
848
|
-
unparsedTsconfigs: computed.unparsedTsconfigs,
|
|
849
|
-
deadGlobs: dead.filter((found) => !config.deadGlobs.some((entry) => allows(entry, found))),
|
|
850
|
-
unlistedUnknowns: config.knownUnknowns ? unknown.filter((entry) => entry.tests.length > 0 && entry.known === undefined).map((entry) => entry.node) : [],
|
|
851
|
-
staleKnownUnknowns: (config.knownUnknowns ?? []).filter((entry) => !nodes.includes(entry.file)).map((entry) => ({
|
|
852
|
-
...entry,
|
|
853
|
-
fix: "remove the entry: its dependencies are known now"
|
|
854
|
-
})),
|
|
855
|
-
unreached,
|
|
856
|
-
undeclaredPaths: spelled.undeclared,
|
|
857
|
-
staleDeadGlobs: config.deadGlobs.filter((entry) => !dead.some((found) => allows(entry, found))).map((entry) => ({
|
|
858
|
-
...entry,
|
|
859
|
-
fix: "remove the entry: its glob is no longer dead"
|
|
860
|
-
}))
|
|
861
|
-
};
|
|
862
|
-
}
|
|
863
|
-
function summary({ problem, matches, ignored, examples }) {
|
|
864
|
-
if (problem === "matches nothing")
|
|
865
|
-
return problem;
|
|
866
|
-
const shown = [...examples, ...ignored > examples.length ? ["…"] : []];
|
|
867
|
-
const count = problem === "partly ignored" ? `${ignored} of ${matches}` : `${ignored}`;
|
|
868
|
-
return `${problem} (${count}: ${shown.join(", ")})`;
|
|
869
|
-
}
|
|
870
|
-
function widening(report) {
|
|
871
|
-
return report.unknown.filter((node) => node.tests.length > 0);
|
|
872
|
-
}
|
|
873
|
-
function failed(report, max) {
|
|
874
|
-
return max !== undefined && widening(report).length > max || report.unlistedUnknowns.length > 0 || report.staleKnownUnknowns.length > 0 || report.deadGlobs.length > 0 || report.staleDeadGlobs.length > 0 || report.undeclaredPaths.length > 0;
|
|
875
|
-
}
|
|
876
|
-
function categoryOf(cause) {
|
|
877
|
-
if (cause.startsWith("unresolved ")) {
|
|
878
|
-
const specifier = QUOTED.exec(cause)?.[1] ?? "";
|
|
879
|
-
if (RELATIVE.test(specifier))
|
|
880
|
-
return "unresolved relative import";
|
|
881
|
-
if (specifier.startsWith("#"))
|
|
882
|
-
return "unresolved # import";
|
|
883
|
-
return "unresolved bare import (a package, alias or path)";
|
|
884
|
-
}
|
|
885
|
-
if (cause.startsWith("only declarations for ")) {
|
|
886
|
-
return "a .js import with only its declarations there";
|
|
887
|
-
}
|
|
888
|
-
if (cause.startsWith("non-literal goto("))
|
|
889
|
-
return "non-literal goto()";
|
|
890
|
-
if (cause.startsWith("goto("))
|
|
891
|
-
return "goto() URL no `urls` pattern maps";
|
|
892
|
-
if (cause.startsWith("missing fixture "))
|
|
893
|
-
return "goto() of a missing page";
|
|
894
|
-
return cause;
|
|
895
|
-
}
|
|
896
|
-
var POSITION = /(?: in <>)? at \d+.*$/;
|
|
897
|
-
function byCount(counts) {
|
|
898
|
-
return Object.fromEntries([...counts].sort(([a, x], [b, y]) => y - x || (a < b ? -1 : 1)));
|
|
899
|
-
}
|
|
900
|
-
function summarize(report) {
|
|
901
|
-
const widen = widening(report);
|
|
902
|
-
const causes = new Map;
|
|
903
|
-
for (const node of widen) {
|
|
904
|
-
for (const category of new Set(node.causes.map(({ cause }) => categoryOf(cause)))) {
|
|
905
|
-
causes.set(category, (causes.get(category) ?? 0) + 1);
|
|
906
|
-
}
|
|
907
|
-
}
|
|
908
|
-
const reasons = new Map;
|
|
909
|
-
for (const { reason } of report.fallbacks) {
|
|
910
|
-
const what = reason.replace(POSITION, "");
|
|
911
|
-
reasons.set(what, (reasons.get(what) ?? 0) + 1);
|
|
912
|
-
}
|
|
913
|
-
return {
|
|
914
|
-
unknown: widen.length,
|
|
915
|
-
testsOnAnyChange: new Set(widen.flatMap((node) => node.tests)).size,
|
|
916
|
-
unknownByCause: byCount(causes),
|
|
917
|
-
unknownNoTestReaches: report.unknown.length - widen.length,
|
|
918
|
-
fallbacks: report.fallbacks.length,
|
|
919
|
-
fallbackReasons: byCount(reasons),
|
|
920
|
-
unparsedTsconfigs: report.unparsedTsconfigs.length,
|
|
921
|
-
deadGlobs: report.deadGlobs.length,
|
|
922
|
-
staleDeadGlobs: report.staleDeadGlobs.length,
|
|
923
|
-
unlistedUnknowns: report.unlistedUnknowns.length,
|
|
924
|
-
staleKnownUnknowns: report.staleKnownUnknowns.length,
|
|
925
|
-
undeclaredPaths: report.undeclaredPaths.length,
|
|
926
|
-
unreached: report.unreached.length
|
|
927
|
-
};
|
|
928
|
-
}
|
|
929
|
-
var SHOWN = 3;
|
|
930
|
-
var BRANCH_WIDTH = " ├ ".length;
|
|
931
|
-
function sharedFix(examples) {
|
|
932
|
-
const counts = new Map;
|
|
933
|
-
for (const { fix } of examples) {
|
|
934
|
-
if (fix !== undefined)
|
|
935
|
-
counts.set(fix, (counts.get(fix) ?? 0) + 1);
|
|
936
|
-
}
|
|
937
|
-
const [top] = [...counts].sort(([, a], [, b]) => b - a);
|
|
938
|
-
if (top === undefined)
|
|
939
|
-
return;
|
|
940
|
-
return top[1] > 1 || examples.length === 1 ? top[0] : undefined;
|
|
941
|
-
}
|
|
942
|
-
function fixLines({ paint, all }, first, fix) {
|
|
943
|
-
const label = `${first}${paint.good("fix")} `;
|
|
944
|
-
if (all) {
|
|
945
|
-
return {
|
|
946
|
-
lines: wrapped(fix, label, " ".repeat(textWidth(label))),
|
|
947
|
-
cut: false
|
|
948
|
-
};
|
|
949
|
-
}
|
|
950
|
-
const shown = truncateWords(fix, MEASURE - textWidth(label));
|
|
951
|
-
return { lines: [`${label}${shown}`], cut: shown !== fix };
|
|
952
|
-
}
|
|
953
|
-
function renderSection(view, { title, count, examples }) {
|
|
954
|
-
const { paint, all } = view;
|
|
955
|
-
const lines = [`${paint.bold(title)}${paint.dim(`${SEPARATOR}${count}`)}`];
|
|
956
|
-
const shared = sharedFix(examples);
|
|
957
|
-
const shown = all ? examples : examples.slice(0, SHOWN);
|
|
958
|
-
const hidden = examples.length - shown.length;
|
|
959
|
-
let cut = hidden > 0;
|
|
960
|
-
const detailWidth = Math.min(40, Math.max(0, ...shown.map(({ detail }) => textWidth(detail ?? ""))));
|
|
961
|
-
const placeMax = Math.max(30, MEASURE - BRANCH_WIDTH - (detailWidth > 0 ? detailWidth + 2 : 0));
|
|
962
|
-
const places = shown.map(({ place }) => {
|
|
963
|
-
const fitted = all ? place : truncatePath(place, placeMax);
|
|
964
|
-
cut ||= fitted !== place;
|
|
965
|
-
return fitted;
|
|
966
|
-
});
|
|
967
|
-
const placeWidth = Math.max(0, ...places.map(textWidth));
|
|
968
|
-
for (const [i, example] of shown.entries()) {
|
|
969
|
-
const glyph = paint.dim(branch(i, hidden > 0 ? shown.length + 1 : shown.length));
|
|
970
|
-
const place = places[i] ?? "";
|
|
971
|
-
let detail = example.detail ?? "";
|
|
972
|
-
if (detail !== "" && !all) {
|
|
973
|
-
const room = MEASURE - BRANCH_WIDTH - placeWidth - 2;
|
|
974
|
-
const fitted = truncateWords(detail, room);
|
|
975
|
-
cut ||= fitted !== detail;
|
|
976
|
-
detail = fitted;
|
|
977
|
-
}
|
|
978
|
-
lines.push(detail === "" ? ` ${glyph}${place}` : ` ${glyph}${place.padEnd(placeWidth)} ${paint.dim(detail)}`);
|
|
979
|
-
if (example.fix !== undefined && example.fix !== shared) {
|
|
980
|
-
const note = fixLines(view, NOTE_INDENT, example.fix);
|
|
981
|
-
lines.push(...note.lines);
|
|
982
|
-
cut ||= note.cut;
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
if (hidden > 0) {
|
|
986
|
-
lines.push(` ${paint.dim(`└ ${hidden} more`)}`);
|
|
987
|
-
}
|
|
988
|
-
if (shared !== undefined) {
|
|
989
|
-
const line = fixLines(view, " ", shared);
|
|
990
|
-
lines.push(...line.lines);
|
|
991
|
-
cut ||= line.cut;
|
|
992
|
-
}
|
|
993
|
-
return { lines, cut };
|
|
994
|
-
}
|
|
995
|
-
var UNRESOLVED = "unresolved ";
|
|
996
|
-
function countOf(hits, files, noun) {
|
|
997
|
-
return hits === files && noun !== "import" ? plural(files, "file") : `${plural(hits, noun)} in ${plural(files, "file")}`;
|
|
998
|
-
}
|
|
999
|
-
function causeSections(widen) {
|
|
1000
|
-
const byCategory = new Map;
|
|
1001
|
-
for (const { node, causes, known } of widen) {
|
|
1002
|
-
for (const { cause, at, fix } of causes) {
|
|
1003
|
-
const category = categoryOf(cause);
|
|
1004
|
-
const named = cause.startsWith(UNRESOLVED) ? cause.slice(UNRESOLVED.length) : cause === category ? "" : cause;
|
|
1005
|
-
const detail = [named, known === undefined ? "" : `known: ${known}`].filter((part) => part !== "").join(SEPARATOR);
|
|
1006
|
-
const example = { place: at ?? node, fix };
|
|
1007
|
-
if (detail !== "")
|
|
1008
|
-
example.detail = detail;
|
|
1009
|
-
const hits = byCategory.get(category) ?? [];
|
|
1010
|
-
hits.push({ node, example });
|
|
1011
|
-
byCategory.set(category, hits);
|
|
1012
|
-
}
|
|
1013
|
-
}
|
|
1014
|
-
return [...byCategory].map(([category, hits]) => {
|
|
1015
|
-
const files = new Set(hits.map(({ node }) => node)).size;
|
|
1016
|
-
const noun = category.includes("import") ? "import" : "place";
|
|
1017
|
-
return {
|
|
1018
|
-
files,
|
|
1019
|
-
section: {
|
|
1020
|
-
title: category,
|
|
1021
|
-
count: countOf(hits.length, files, noun),
|
|
1022
|
-
examples: hits.map(({ example }) => example)
|
|
1023
|
-
}
|
|
1024
|
-
};
|
|
1025
|
-
}).sort((a, b) => b.files - a.files || (a.section.title < b.section.title ? -1 : 1)).map(({ section }) => section);
|
|
1026
|
-
}
|
|
1027
|
-
var filesIn = (entries) => new Set(entries.map(({ file }) => file)).size;
|
|
1028
|
-
function otherSections(report) {
|
|
1029
|
-
const sections = [];
|
|
1030
|
-
const add = (section) => {
|
|
1031
|
-
if (section.examples.length > 0)
|
|
1032
|
-
sections.push(section);
|
|
1033
|
-
};
|
|
1034
|
-
const spelled = report.undeclaredPaths;
|
|
1035
|
-
add({
|
|
1036
|
-
title: "path spelled in code that nothing declares",
|
|
1037
|
-
count: `${plural(spelled.length, "path")} in ${plural(filesIn(spelled), "file")}`,
|
|
1038
|
-
examples: spelled.map(({ file, path, dir, at, fix }) => ({
|
|
1039
|
-
place: at ?? file,
|
|
1040
|
-
detail: dir ? `${path}/` : path,
|
|
1041
|
-
fix
|
|
1042
|
-
}))
|
|
1043
|
-
});
|
|
1044
|
-
const dead = report.deadGlobs;
|
|
1045
|
-
add({
|
|
1046
|
-
title: "dead glob: a change to what it matches selects nothing",
|
|
1047
|
-
count: `${plural(dead.length, "glob")} in ${plural(new Set(dead.map(({ node }) => node)).size, "file")}`,
|
|
1048
|
-
examples: dead.map((found) => ({
|
|
1049
|
-
place: found.at ?? found.node,
|
|
1050
|
-
detail: `${found.kind} ${found.glob}: ${summary(found)}`,
|
|
1051
|
-
fix: found.fix
|
|
1052
|
-
}))
|
|
1053
|
-
});
|
|
1054
|
-
add({
|
|
1055
|
-
title: "`deadGlobs` entry that matches no dead glob",
|
|
1056
|
-
count: plural(report.staleDeadGlobs.length, "entry", "entries"),
|
|
1057
|
-
examples: report.staleDeadGlobs.map(({ file, glob, fix }) => ({
|
|
1058
|
-
place: file,
|
|
1059
|
-
detail: glob,
|
|
1060
|
-
fix
|
|
1061
|
-
}))
|
|
1062
|
-
});
|
|
1063
|
-
add({
|
|
1064
|
-
title: "unknown not in `knownUnknowns`",
|
|
1065
|
-
count: plural(report.unlistedUnknowns.length, "file"),
|
|
1066
|
-
examples: report.unlistedUnknowns.map((node) => ({
|
|
1067
|
-
place: node,
|
|
1068
|
-
fix: "make its dependencies known, or list it in `knownUnknowns` with a reason"
|
|
1069
|
-
}))
|
|
1070
|
-
});
|
|
1071
|
-
add({
|
|
1072
|
-
title: "`knownUnknowns` entry whose dependencies are known",
|
|
1073
|
-
count: plural(report.staleKnownUnknowns.length, "entry", "entries"),
|
|
1074
|
-
examples: report.staleKnownUnknowns.map(({ file, reason, fix }) => ({
|
|
1075
|
-
place: file,
|
|
1076
|
-
detail: reason,
|
|
1077
|
-
fix
|
|
1078
|
-
}))
|
|
1079
|
-
});
|
|
1080
|
-
for (const suite of new Set(report.unreached.map(({ suite }) => suite))) {
|
|
1081
|
-
const entries = report.unreached.filter((entry) => entry.suite === suite);
|
|
1082
|
-
add({
|
|
1083
|
-
title: "no test reaches: a change runs the whole suite",
|
|
1084
|
-
count: `${plural(entries.length, "file")} in suite ${suite}`,
|
|
1085
|
-
examples: entries.map(({ file, fix }) => ({ place: file, fix }))
|
|
1086
|
-
});
|
|
1087
|
-
}
|
|
1088
|
-
add({
|
|
1089
|
-
title: "tsconfig that doesn't parse, whose `paths` are ignored",
|
|
1090
|
-
count: plural(report.unparsedTsconfigs.length, "file"),
|
|
1091
|
-
examples: report.unparsedTsconfigs.map(({ file, reason }) => ({
|
|
1092
|
-
place: file,
|
|
1093
|
-
detail: reason
|
|
1094
|
-
}))
|
|
1095
|
-
});
|
|
1096
|
-
add({
|
|
1097
|
-
title: "read by the regex extractor",
|
|
1098
|
-
count: `${plural(report.fallbacks.length, "file")} the lexer couldn't read`,
|
|
1099
|
-
examples: report.fallbacks.map(({ file, reason }) => ({
|
|
1100
|
-
place: file,
|
|
1101
|
-
detail: reason
|
|
1102
|
-
}))
|
|
1103
|
-
});
|
|
1104
|
-
const idle = report.unknown.filter((node) => node.tests.length === 0);
|
|
1105
|
-
add({
|
|
1106
|
-
title: "unknown, but no test reaches it, so it widens nothing",
|
|
1107
|
-
count: plural(idle.length, "file"),
|
|
1108
|
-
examples: idle.flatMap(({ node, causes, known }) => causes.map(({ cause, at }) => ({
|
|
1109
|
-
place: at ?? node,
|
|
1110
|
-
detail: known === undefined ? cause : `${cause}${SEPARATOR}known: ${known}`
|
|
1111
|
-
})))
|
|
1112
|
-
});
|
|
1113
|
-
return sections;
|
|
1114
|
-
}
|
|
1115
|
-
function verdict(paint, counts) {
|
|
1116
|
-
const { unknown, testsOnAnyChange: tests } = counts;
|
|
1117
|
-
if (unknown === 0) {
|
|
1118
|
-
return paint.good("bivium can follow every file a test reaches");
|
|
1119
|
-
}
|
|
1120
|
-
return `${paint.bad(`${plural(unknown, "file")} bivium can't follow`)} ${unknown === 1 ? "sends" : "send"} ${paint.bold(String(tests))} ${tests === 1 ? "test" : "tests"} to every change`;
|
|
1121
|
-
}
|
|
1122
|
-
function context(counts, max) {
|
|
1123
|
-
const parts = [
|
|
1124
|
-
max === undefined ? "" : `at most ${max} allowed`,
|
|
1125
|
-
counts.undeclaredPaths > 0 ? plural(counts.undeclaredPaths, "undeclared path") : "",
|
|
1126
|
-
counts.deadGlobs > 0 ? plural(counts.deadGlobs, "dead glob") : "",
|
|
1127
|
-
counts.staleDeadGlobs > 0 ? plural(counts.staleDeadGlobs, "stale `deadGlobs` entry", "stale `deadGlobs` entries") : "",
|
|
1128
|
-
counts.unlistedUnknowns > 0 ? `${counts.unlistedUnknowns} not in \`knownUnknowns\`` : "",
|
|
1129
|
-
counts.staleKnownUnknowns > 0 ? plural(counts.staleKnownUnknowns, "stale `knownUnknowns` entry", "stale `knownUnknowns` entries") : "",
|
|
1130
|
-
counts.unreached > 0 ? `${plural(counts.unreached, "file")} no test reaches` : "",
|
|
1131
|
-
counts.unparsedTsconfigs > 0 ? plural(counts.unparsedTsconfigs, "tsconfig that doesn't parse", "tsconfigs that don't parse") : "",
|
|
1132
|
-
counts.fallbacks > 0 ? `${plural(counts.fallbacks, "file")} the lexer couldn't read` : "",
|
|
1133
|
-
counts.unknownNoTestReaches > 0 ? `${counts.unknownNoTestReaches} more unknown no test reaches` : ""
|
|
1134
|
-
].filter((part) => part !== "");
|
|
1135
|
-
const lines = [];
|
|
1136
|
-
for (const part of parts) {
|
|
1137
|
-
const last = lines.at(-1);
|
|
1138
|
-
if (last !== undefined && textWidth(last) + SEPARATOR.length + textWidth(part) <= MEASURE) {
|
|
1139
|
-
lines[lines.length - 1] = `${last}${SEPARATOR}${part}`;
|
|
1140
|
-
} else
|
|
1141
|
-
lines.push(part);
|
|
1142
|
-
}
|
|
1143
|
-
return lines;
|
|
1144
|
-
}
|
|
1145
|
-
function failures(counts, max) {
|
|
1146
|
-
const parts = [
|
|
1147
|
-
[
|
|
1148
|
-
max !== undefined && counts.unknown > max ? counts.unknown : 0,
|
|
1149
|
-
`unknown, over --max-unknown ${max}`,
|
|
1150
|
-
`unknown, over --max-unknown ${max}`
|
|
1151
|
-
],
|
|
1152
|
-
[counts.unlistedUnknowns, "unlisted unknown", "unlisted unknowns"],
|
|
1153
|
-
[counts.undeclaredPaths, "undeclared path", "undeclared paths"],
|
|
1154
|
-
[counts.deadGlobs, "dead glob", "dead globs"],
|
|
1155
|
-
[
|
|
1156
|
-
counts.staleKnownUnknowns + counts.staleDeadGlobs,
|
|
1157
|
-
"stale entry",
|
|
1158
|
-
"stale entries"
|
|
1159
|
-
]
|
|
1160
|
-
];
|
|
1161
|
-
return parts.flatMap(([count, one, many]) => count > 0 ? [plural(count, one, many)] : []);
|
|
1162
|
-
}
|
|
1163
|
-
function formatReport(report, max, view) {
|
|
1164
|
-
const { paint } = view;
|
|
1165
|
-
const counts = summarize(report);
|
|
1166
|
-
const lines = [
|
|
1167
|
-
verdict(paint, counts),
|
|
1168
|
-
...context(counts, max).map((line) => paint.dim(line)),
|
|
1169
|
-
""
|
|
1170
|
-
];
|
|
1171
|
-
let cut = false;
|
|
1172
|
-
for (const section of [
|
|
1173
|
-
...causeSections(widening(report)),
|
|
1174
|
-
...otherSections(report)
|
|
1175
|
-
]) {
|
|
1176
|
-
const rendered = renderSection(view, section);
|
|
1177
|
-
lines.push(...rendered.lines, "");
|
|
1178
|
-
cut ||= rendered.cut;
|
|
1179
|
-
}
|
|
1180
|
-
const detail = `checked in ${duration(view.elapsedMs)}${cut ? `${SEPARATOR}--all for every file` : ""}`;
|
|
1181
|
-
const failed = failures(counts, max);
|
|
1182
|
-
lines.push(failed.length > 0 ? failLine(paint, failed.join(SEPARATOR), detail) : closingLine(paint, "Check passes", detail));
|
|
1183
|
-
return lines.join(`
|
|
1184
|
-
`);
|
|
1185
|
-
}
|
|
1186
|
-
|
|
1187
|
-
// src/help.ts
|
|
1188
|
-
var row = (p, name, does, preset) => ` ${name.padEnd(21)}${does}${preset === undefined ? "" : p.dim(`${SEPARATOR}${preset}`)}`;
|
|
1189
|
-
var example = (p, intent, command) => ` ${p.dim(`# ${intent}`)}
|
|
1190
|
-
${command}`;
|
|
1191
|
-
function usage(p) {
|
|
1192
|
-
return `${p.bold("bivium")} ${version}${p.dim(`${SEPARATOR}run only the tests your change can reach`)}
|
|
1193
|
-
|
|
1194
|
-
${p.bold("Usage")}
|
|
1195
|
-
bivium <command> [arguments] [options]
|
|
1196
|
-
|
|
1197
|
-
${p.bold("Commands")}
|
|
1198
|
-
${row(p, "plan [base]", "the tests a change can affect, per suite")}
|
|
1199
|
-
${row(p, "run [base]", "run a suite's selected tests")}
|
|
1200
|
-
${row(p, "why <test> [file]", "the chain from a change, or a file, to a test")}
|
|
1201
|
-
${row(p, "check", "what the graph can't read, and dead globs")}
|
|
1202
|
-
${row(p, "graph", "each file's test fan-in, and the graph")}
|
|
1203
|
-
|
|
1204
|
-
base defaults to the config's base, else the upstream default branch.
|
|
1205
|
-
Options go before or after the command, as --flag value or --flag=value.
|
|
1206
|
-
A path is repo-relative, relative to the current directory, or absolute.
|
|
1207
|
-
|
|
1208
|
-
${p.bold("Options")}
|
|
1209
|
-
${row(p, "--config <file>", "the config file", "bivium.config.{ts,mts,mjs,js,json}")}
|
|
1210
|
-
${row(p, "--cwd <dir>", "the repository", "the current directory")}
|
|
1211
|
-
${row(p, "--no-cache", "read every file; skip the extraction cache")}
|
|
1212
|
-
${row(p, "--json", "plan, why, check, graph: print JSON")}
|
|
1213
|
-
${row(p, "--color, --no-color", "force color on or off", "on in a terminal")}
|
|
1214
|
-
${row(p, "-h, --help", "this help")}
|
|
1215
|
-
${row(p, "-v, --version", "the version")}
|
|
1216
|
-
|
|
1217
|
-
${p.bold(" plan")}
|
|
1218
|
-
${row(p, "--list", "the files, as when piped, on a terminal too")}
|
|
1219
|
-
${row(p, "--github", "also write each suite's selection to $GITHUB_OUTPUT")}
|
|
1220
|
-
${p.bold(" run")}
|
|
1221
|
-
${row(p, "--suite <name>", "the suite to run", "the first")}
|
|
1222
|
-
${p.bold(" why")}
|
|
1223
|
-
${row(p, "--base <ref>", "the base ref", "as for plan")}
|
|
1224
|
-
${row(p, "--format <f>", "tree, chain, source or json", "tree")}
|
|
1225
|
-
${row(p, "--all", "every change's chain, not only the shortest")}
|
|
1226
|
-
${p.bold(" check")}
|
|
1227
|
-
${row(p, "--base <ref>", "the base ref", "HEAD")}
|
|
1228
|
-
${row(p, "--all", "every file, and each fix whole")}
|
|
1229
|
-
${row(p, "--max-unknown <n>", "fail past n unknown files that reach a test")}
|
|
1230
|
-
${p.bold(" graph")}
|
|
1231
|
-
${row(p, "--suite <name>", "only this suite", "every suite")}
|
|
1232
|
-
${row(p, "--top <n>", "files to list per suite", "10")}
|
|
1233
|
-
|
|
1234
|
-
${p.bold("Examples")}
|
|
1235
|
-
${example(p, "the tests this branch needs, as data", "bivium plan origin/main --json")}
|
|
1236
|
-
${example(p, "how a change to a file reaches a test", "bivium why tests/Button.test.ts src/Button.svelte --format chain")}
|
|
1237
|
-
${example(p, "fail CI when a file bivium can't follow reaches a test", "bivium check --max-unknown 0")}
|
|
1238
|
-
|
|
1239
|
-
${p.bold("For agents")}
|
|
1240
|
-
Run with --json; read ok and summary. Exit 0 done, 1 check found
|
|
1241
|
-
problems, 2 a usage, config or git error; run exits with its command's.
|
|
1242
|
-
With --json, an error is JSON too: error.kind (usage, config, git or
|
|
1243
|
-
other), error.code and error.message.`;
|
|
1244
|
-
}
|
|
1245
|
-
|
|
1246
|
-
// src/why.ts
|
|
1247
|
-
var CONFIG_EDGES = "edges in the config";
|
|
1248
|
-
var CONFIG_SETUP = "setup in the config";
|
|
1249
|
-
var CONFIGURED = {
|
|
1250
|
-
edges: CONFIG_EDGES,
|
|
1251
|
-
setup: CONFIG_SETUP
|
|
1252
|
-
};
|
|
1253
|
-
var CONFIG_READS = "reads in the config";
|
|
1254
|
-
async function edgeAt(context, node, via) {
|
|
1255
|
-
const spelled = CONFIGURED[via.kind];
|
|
1256
|
-
if (spelled !== undefined)
|
|
1257
|
-
return { at: spelled };
|
|
1258
|
-
if (via.kind === "unknown")
|
|
1259
|
-
return {};
|
|
1260
|
-
const file = fileOf(node);
|
|
1261
|
-
const source = SCANNED.test(file) ? await sourceOf(context.root, file) : undefined;
|
|
1262
|
-
const configured = (maps) => maps.some((map) => map.targets(node)?.some((re) => re.test(via.from)));
|
|
1263
|
-
if (via.kind === "reads" && configured(context.config.reads)) {
|
|
1264
|
-
const declared = source !== undefined && extract(formatOf(file), source).reads.some((glob) => globToRegExp(glob).test(via.from));
|
|
1265
|
-
if (!declared)
|
|
1266
|
-
return { at: CONFIG_READS };
|
|
1267
|
-
}
|
|
1268
|
-
if (source === undefined)
|
|
1269
|
-
return {};
|
|
1270
|
-
const line = edgeLine(context, node, via, source);
|
|
1271
|
-
return line === undefined ? {} : { at: `${file}:${line}`, text: lineText(source, line) };
|
|
1272
|
-
}
|
|
1273
|
-
function edgeLine({ config, resolve }, node, via, source) {
|
|
1274
|
-
const file = fileOf(node);
|
|
1275
|
-
const extraction = extract(formatOf(file), source);
|
|
1276
|
-
const matches = (glob) => globToRegExp(glob).test(via.from);
|
|
1277
|
-
if (via.kind === "reads" || via.kind === "depends-on" || via.kind === "lists") {
|
|
1278
|
-
const tag = `@${via.kind}`;
|
|
1279
|
-
const globs = {
|
|
1280
|
-
reads: extraction.reads,
|
|
1281
|
-
"depends-on": extraction.dependsOn,
|
|
1282
|
-
lists: extraction.lists
|
|
1283
|
-
}[via.kind];
|
|
1284
|
-
const glob = globs.find(matches);
|
|
1285
|
-
return (glob === undefined ? undefined : annotationLine(source, tag, glob)) ?? textLine(source, tag);
|
|
1286
|
-
}
|
|
1287
|
-
if (via.kind === "glob") {
|
|
1288
|
-
const relative = finish(file, extraction).globs;
|
|
1289
|
-
const glob = extraction.globs[relative.findIndex(matches)];
|
|
1290
|
-
return (glob === undefined ? undefined : specifierLine(source, glob)) ?? callLine(source, "import.meta.glob");
|
|
1291
|
-
}
|
|
1292
|
-
const target = fileOf(via.from);
|
|
1293
|
-
if (via.kind === "goto") {
|
|
1294
|
-
const argument = extraction.gotos.find((candidate) => gotoPage(config, file, candidate)?.includes(target));
|
|
1295
|
-
return gotoLine(source, argument, target);
|
|
1296
|
-
}
|
|
1297
|
-
const loads = (specifier) => {
|
|
1298
|
-
const resolved = resolve(file, specifier);
|
|
1299
|
-
return resolved !== undefined && "files" in resolved && resolved.files.includes(target);
|
|
1300
|
-
};
|
|
1301
|
-
if (via.kind === "export") {
|
|
1302
|
-
const barrel = extraction.barrel;
|
|
1303
|
-
if (!barrel)
|
|
1304
|
-
return;
|
|
1305
|
-
const own = barrel.names.get(node.slice(node.indexOf("#") + 1));
|
|
1306
|
-
const specifier = [
|
|
1307
|
-
...own === undefined ? [] : [own],
|
|
1308
|
-
...barrel.names.values(),
|
|
1309
|
-
...barrel.stars
|
|
1310
|
-
].find(loads);
|
|
1311
|
-
return specifier === undefined ? undefined : specifierLine(source, specifier);
|
|
1312
|
-
}
|
|
1313
|
-
const imported = extraction.imports.find(({ specifier }) => loads(specifier));
|
|
1314
|
-
return imported === undefined ? undefined : specifierLine(source, imported.specifier);
|
|
1315
|
-
}
|
|
1316
|
-
function rootCause(computed, rootNode, from) {
|
|
1317
|
-
if (from !== undefined)
|
|
1318
|
-
return "given";
|
|
1319
|
-
if (computed.changed.includes(rootNode))
|
|
1320
|
-
return "changed";
|
|
1321
|
-
const bumped = computed.plan.packageCauses.get(rootNode);
|
|
1322
|
-
if (bumped !== undefined)
|
|
1323
|
-
return bumped;
|
|
1324
|
-
const setting = computed.plan.settingCauses.get(rootNode);
|
|
1325
|
-
if (setting !== undefined)
|
|
1326
|
-
return setting;
|
|
1327
|
-
if (computed.barrelNames.includes(rootNode))
|
|
1328
|
-
return "barrel name moved";
|
|
1329
|
-
for (const { narrower, result } of computed.narrowed) {
|
|
1330
|
-
if ("seeds" in result && result.seeds.includes(rootNode)) {
|
|
1331
|
-
return `seeded by the ${narrower.name} narrower`;
|
|
1332
|
-
}
|
|
1333
|
-
}
|
|
1334
|
-
return "changed";
|
|
1335
|
-
}
|
|
1336
|
-
function walks(computed, { name, types }, from) {
|
|
1337
|
-
const { plan, graph } = computed;
|
|
1338
|
-
if (from !== undefined)
|
|
1339
|
-
return [{ seeds: [from], types }];
|
|
1340
|
-
const { quiet, layout } = plan;
|
|
1341
|
-
const narrowings = plan.narrowings.get(name);
|
|
1342
|
-
if (!narrowings) {
|
|
1343
|
-
return [{ seeds: plan.seeds, quiet, layout, types }];
|
|
1344
|
-
}
|
|
1345
|
-
const matched = new Set(narrowings.flatMap((narrowing) => narrowing.matched));
|
|
1346
|
-
return [
|
|
1347
|
-
{
|
|
1348
|
-
seeds: plan.seeds.filter((seed) => !matched.has(seed)),
|
|
1349
|
-
quiet,
|
|
1350
|
-
layout,
|
|
1351
|
-
types
|
|
1352
|
-
},
|
|
1353
|
-
...narrowings.map((narrowing) => ({
|
|
1354
|
-
seeds: [...narrowing.matched, ...narrowing.seeds].filter((seed) => graph.has(seed) || computed.changed.includes(seed)),
|
|
1355
|
-
opaque: narrowing.narrower.opaque,
|
|
1356
|
-
layout,
|
|
1357
|
-
types
|
|
1358
|
-
}))
|
|
1359
|
-
];
|
|
1360
|
-
}
|
|
1361
|
-
function shortestChain(computed, candidates, test) {
|
|
1362
|
-
for (const { seeds, ...options } of candidates) {
|
|
1363
|
-
const parents = new Map;
|
|
1364
|
-
const reached = affectedBy(computed.graph, seeds, { ...options, parents });
|
|
1365
|
-
if (!reached.has(test))
|
|
1366
|
-
continue;
|
|
1367
|
-
const edges = [];
|
|
1368
|
-
let node = test;
|
|
1369
|
-
for (let via = parents.get(node);via; via = parents.get(node)) {
|
|
1370
|
-
edges.push([node, via]);
|
|
1371
|
-
node = via.from;
|
|
1372
|
-
if (via.kind === "unknown")
|
|
1373
|
-
break;
|
|
1374
|
-
}
|
|
1375
|
-
return { edges, root: node };
|
|
1376
|
-
}
|
|
1377
|
-
return;
|
|
1378
|
-
}
|
|
1379
|
-
function reachers(computed, candidates, test, except) {
|
|
1380
|
-
const found = new Set;
|
|
1381
|
-
const unseen = (seed) => seed !== except && !found.has(seed);
|
|
1382
|
-
for (const { seeds, quiet = [], ...options } of candidates) {
|
|
1383
|
-
for (const seed of seeds.filter(unseen)) {
|
|
1384
|
-
if (affectedBy(computed.graph, [seed], options).has(test)) {
|
|
1385
|
-
found.add(seed);
|
|
1386
|
-
}
|
|
1387
|
-
}
|
|
1388
|
-
for (const seed of quiet.filter(unseen)) {
|
|
1389
|
-
const reached = affectedBy(computed.graph, [], {
|
|
1390
|
-
...options,
|
|
1391
|
-
quiet: [seed]
|
|
1392
|
-
});
|
|
1393
|
-
if (reached.has(test))
|
|
1394
|
-
found.add(seed);
|
|
1395
|
-
}
|
|
1396
|
-
}
|
|
1397
|
-
return sorted(found);
|
|
1398
|
-
}
|
|
1399
|
-
async function chainOf(context, { edges, root }, from) {
|
|
1400
|
-
const last = edges.at(-1);
|
|
1401
|
-
const [links, causes] = await Promise.all([
|
|
1402
|
-
Promise.all(edges.map(async ([node, via]) => ({
|
|
1403
|
-
node,
|
|
1404
|
-
kind: via.kind,
|
|
1405
|
-
...await edgeAt(context, node, via)
|
|
1406
|
-
}))),
|
|
1407
|
-
last?.[1].kind === "unknown" ? causesOf(context.root, context.computed, last[0]) : []
|
|
1408
|
-
]);
|
|
1409
|
-
return {
|
|
1410
|
-
links,
|
|
1411
|
-
root,
|
|
1412
|
-
cause: rootCause(context.computed, root, from),
|
|
1413
|
-
unknown: causes.map((cause) => ({ ...cause, fix: fixFor2(cause.cause) }))
|
|
1414
|
-
};
|
|
1415
|
-
}
|
|
1416
|
-
function unselected(config, computed, suite, from) {
|
|
1417
|
-
const changed = computed.changed.length;
|
|
1418
|
-
if (from === undefined)
|
|
1419
|
-
return { kind: "unselected", suite, changed };
|
|
1420
|
-
const reachedBy = walksByTypes(computed.graph, [from], {
|
|
1421
|
-
includeUnknown: false
|
|
1422
|
-
});
|
|
1423
|
-
const reaches = {};
|
|
1424
|
-
let example;
|
|
1425
|
-
for (const { name, isTest, types } of config.suites) {
|
|
1426
|
-
const tests = sorted(reachedBy(types)).filter((node) => isTest(node));
|
|
1427
|
-
if (tests.length === 0)
|
|
1428
|
-
continue;
|
|
1429
|
-
reaches[name] = tests.length;
|
|
1430
|
-
if (name === suite || example === undefined)
|
|
1431
|
-
example = tests[0];
|
|
1432
|
-
}
|
|
1433
|
-
return {
|
|
1434
|
-
kind: "unselected",
|
|
1435
|
-
suite,
|
|
1436
|
-
from,
|
|
1437
|
-
changed,
|
|
1438
|
-
reaches,
|
|
1439
|
-
...example !== undefined && { example }
|
|
1440
|
-
};
|
|
1441
|
-
}
|
|
1442
|
-
async function explain(root, config, computed, test, from, { all = false } = {}) {
|
|
1443
|
-
const suite = config.suites.find((candidate) => candidate.isTest(test));
|
|
1444
|
-
if (!suite) {
|
|
1445
|
-
throw new BiviumError2("BV_UNKNOWN_TEST", `bivium why: ${test} is no suite's test`);
|
|
1446
|
-
}
|
|
1447
|
-
if (!computed.graph.has(test)) {
|
|
1448
|
-
throw new BiviumError2("BV_UNKNOWN_FILE", `bivium why: no file ${test}`);
|
|
1449
|
-
}
|
|
1450
|
-
if (from === undefined && computed.plan.full.includes(suite.name)) {
|
|
1451
|
-
return { kind: "full", suite: suite.name, reasons: computed.plan.reasons };
|
|
1452
|
-
}
|
|
1453
|
-
const candidates = walks(computed, suite, from);
|
|
1454
|
-
const found = shortestChain(computed, candidates, test);
|
|
1455
|
-
if (!found)
|
|
1456
|
-
return unselected(config, computed, suite.name, from);
|
|
1457
|
-
const context = {
|
|
1458
|
-
root,
|
|
1459
|
-
config,
|
|
1460
|
-
computed,
|
|
1461
|
-
resolve: createResolver({
|
|
1462
|
-
files: new Set(computed.graph.keys()),
|
|
1463
|
-
config,
|
|
1464
|
-
packages: computed.packages,
|
|
1465
|
-
tsconfigs: computed.tsconfigs
|
|
1466
|
-
})
|
|
1467
|
-
};
|
|
1468
|
-
const everything = found.edges.at(-1)?.[1].kind === "unknown";
|
|
1469
|
-
const more = from !== undefined || everything ? [] : reachers(computed, candidates, test, found.root);
|
|
1470
|
-
const [chain, others] = await Promise.all([
|
|
1471
|
-
chainOf(context, found, from),
|
|
1472
|
-
Promise.all((all ? more : []).map((seed) => {
|
|
1473
|
-
const alone = candidates.map((walk) => ({
|
|
1474
|
-
...walk,
|
|
1475
|
-
seeds: walk.seeds.filter((candidate) => candidate === seed),
|
|
1476
|
-
...walk.quiet && {
|
|
1477
|
-
quiet: walk.quiet.filter((candidate) => candidate === seed)
|
|
1478
|
-
}
|
|
1479
|
-
}));
|
|
1480
|
-
const other = shortestChain(computed, alone, test);
|
|
1481
|
-
return other === undefined ? Promise.resolve(undefined) : chainOf(context, other, undefined);
|
|
1482
|
-
}))
|
|
1483
|
-
]);
|
|
1484
|
-
return {
|
|
1485
|
-
kind: "chain",
|
|
1486
|
-
suite: suite.name,
|
|
1487
|
-
test,
|
|
1488
|
-
...chain,
|
|
1489
|
-
more,
|
|
1490
|
-
others: others.filter((other) => other !== undefined)
|
|
1491
|
-
};
|
|
1492
|
-
}
|
|
1493
|
-
var FORMATS = ["tree", "chain", "source"];
|
|
1494
|
-
function isFormat(value) {
|
|
1495
|
-
return FORMATS.some((format) => format === value);
|
|
1496
|
-
}
|
|
1497
|
-
var VERBS = {
|
|
1498
|
-
import: "imported by",
|
|
1499
|
-
type: "type-imported by",
|
|
1500
|
-
setup: "a setup file of",
|
|
1501
|
-
goto: "opened by",
|
|
1502
|
-
html: "loaded by page",
|
|
1503
|
-
name: "imported by",
|
|
1504
|
-
glob: "globbed by",
|
|
1505
|
-
"depends-on": "depended on by",
|
|
1506
|
-
edges: "an input of",
|
|
1507
|
-
reads: "read as text by",
|
|
1508
|
-
lists: "listed by",
|
|
1509
|
-
unknown: "depends on every change"
|
|
1510
|
-
};
|
|
1511
|
-
var LIST = 3;
|
|
1512
|
-
function nameOf(node) {
|
|
1513
|
-
return node.slice(node.indexOf("#") + 1);
|
|
1514
|
-
}
|
|
1515
|
-
function aligned2(rows, paint) {
|
|
1516
|
-
const width = Math.max(0, ...rows.filter(([, , at]) => at !== undefined).map(([, t]) => t.length));
|
|
1517
|
-
return rows.map(([shown, plain, at]) => at === undefined ? shown : `${shown}${" ".repeat(width - plain.length)} ${paint.dim(at)}`);
|
|
1518
|
-
}
|
|
1519
|
-
function tree(test, chain, paint) {
|
|
1520
|
-
const rows = [[paint.bold(test), test, undefined]];
|
|
1521
|
-
const row = (indent, glyph, shown, plain) => [
|
|
1522
|
-
`${indent}${paint.dim(glyph)}${shown}`,
|
|
1523
|
-
`${indent}${glyph}${plain}`
|
|
1524
|
-
];
|
|
1525
|
-
const width = Math.max(0, ...chain.links.filter((link) => link.kind !== "unknown").map((link) => link.kind.length));
|
|
1526
|
-
let indent = "";
|
|
1527
|
-
for (const [i, link] of chain.links.entries()) {
|
|
1528
|
-
const next = chain.links[i + 1]?.node ?? chain.root;
|
|
1529
|
-
if (link.kind === "unknown") {
|
|
1530
|
-
const every = "depends on every change:";
|
|
1531
|
-
rows.push([...row(indent, "└─ ", every, every), undefined]);
|
|
1532
|
-
for (const { cause, at } of chain.unknown) {
|
|
1533
|
-
const plain = `${indent} ✗ ${cause}`;
|
|
1534
|
-
rows.push([`${indent} ${paint.bad("✗")} ${cause}`, plain, at]);
|
|
1535
|
-
}
|
|
1536
|
-
indent += " ";
|
|
1537
|
-
rows.push([...row(indent, "└─ ", next, next), undefined]);
|
|
1538
|
-
indent += " ";
|
|
1539
|
-
break;
|
|
1540
|
-
}
|
|
1541
|
-
const kind = link.kind.padEnd(width);
|
|
1542
|
-
rows.push([
|
|
1543
|
-
...row(indent, "└─ ", `${paint.faint(kind)} ${next}`, `${kind} ${next}`),
|
|
1544
|
-
link.at
|
|
1545
|
-
]);
|
|
1546
|
-
indent += " ";
|
|
1547
|
-
}
|
|
1548
|
-
const end = `${indent}● ${chain.cause}`;
|
|
1549
|
-
rows.push([end, end, undefined]);
|
|
1550
|
-
return aligned2(rows, paint);
|
|
1551
|
-
}
|
|
1552
|
-
function verbOf(link) {
|
|
1553
|
-
return link.kind === "export" ? `exported as ${nameOf(link.node)}` : VERBS[link.kind];
|
|
1554
|
-
}
|
|
1555
|
-
function whereOf(link) {
|
|
1556
|
-
const configured = CONFIGURED[link.kind];
|
|
1557
|
-
if (configured !== undefined)
|
|
1558
|
-
return `${link.node} (${configured})`;
|
|
1559
|
-
return link.at ?? link.node;
|
|
1560
|
-
}
|
|
1561
|
-
function steps(links, paint) {
|
|
1562
|
-
const rows = [...links].reverse().map((link) => [verbOf(link), whereOf(link)]);
|
|
1563
|
-
const width = Math.max(0, ...rows.map(([verb]) => verb.length));
|
|
1564
|
-
return rows.map(([verb, where], i) => i === rows.length - 1 ? `${paint.faint(verb.padEnd(width))} ${paint.bold(where)} ← selected` : `${paint.faint(verb.padEnd(width))} ${paint.dim(where)}`);
|
|
1565
|
-
}
|
|
1566
|
-
function header({ root, cause }) {
|
|
1567
|
-
if (cause === "changed")
|
|
1568
|
-
return `${root} changed`;
|
|
1569
|
-
if (cause === "given")
|
|
1570
|
-
return `if ${root} changes`;
|
|
1571
|
-
return `${root}: ${cause}`;
|
|
1572
|
-
}
|
|
1573
|
-
function chained(test, chain, paint) {
|
|
1574
|
-
const unknownAt = chain.links.findIndex((link) => link.kind === "unknown");
|
|
1575
|
-
const unknownLink = chain.links[unknownAt];
|
|
1576
|
-
if (unknownLink === undefined) {
|
|
1577
|
-
return [
|
|
1578
|
-
header(chain),
|
|
1579
|
-
...steps(chain.links, paint).map((step) => ` ${step}`)
|
|
1580
|
-
];
|
|
1581
|
-
}
|
|
1582
|
-
const node = unknownLink.node;
|
|
1583
|
-
const where = chain.unknown.map(({ at }) => at ?? node);
|
|
1584
|
-
const width = Math.max(0, ...where.map((at) => at.length));
|
|
1585
|
-
const lines = [`${paint.bold(test)} runs on every change`];
|
|
1586
|
-
for (const { at, cause, fix } of chain.unknown) {
|
|
1587
|
-
lines.push(` because ${paint.dim((at ?? node).padEnd(width))} ${cause}`, ` ${paint.good("fix")}: ${fix}`);
|
|
1588
|
-
}
|
|
1589
|
-
if (unknownAt > 0) {
|
|
1590
|
-
lines.push(` reached via ${node}`, ...steps(chain.links.slice(0, unknownAt), paint).map((step) => ` ${step}`));
|
|
1591
|
-
}
|
|
1592
|
-
return lines;
|
|
1593
|
-
}
|
|
1594
|
-
var LINE_NUMBER = /:(\d+)$/;
|
|
1595
|
-
function lineNumber(at) {
|
|
1596
|
-
return at === undefined ? undefined : LINE_NUMBER.exec(at)?.[1];
|
|
1597
|
-
}
|
|
1598
|
-
function sourced(test, chain, paint) {
|
|
1599
|
-
const unknownAt = chain.links.findIndex((link) => link.kind === "unknown");
|
|
1600
|
-
const unknownLink = chain.links[unknownAt];
|
|
1601
|
-
const hops = unknownLink === undefined ? chain.links : chain.links.slice(0, unknownAt);
|
|
1602
|
-
const numbers = [...chain.links, ...chain.unknown].map(({ at }) => lineNumber(at));
|
|
1603
|
-
const width = Math.max(0, ...numbers.map((n) => n?.length ?? 0));
|
|
1604
|
-
const gutter = " ".repeat(width);
|
|
1605
|
-
const cited = (at, text) => {
|
|
1606
|
-
const n = lineNumber(at);
|
|
1607
|
-
return n === undefined || text === undefined ? [] : [`${paint.dim(`${n.padStart(width)} |`)} ${text}`];
|
|
1608
|
-
};
|
|
1609
|
-
const count = plural(hops.length, "hop", "hops");
|
|
1610
|
-
const lines = unknownLink === undefined ? [
|
|
1611
|
-
`selected: ${paint.bold(test)} ← ${chain.root} (${count}${chain.cause === "changed" ? "" : `, ${chain.cause}`})`
|
|
1612
|
-
] : [
|
|
1613
|
-
`selected: ${paint.bold(test)} ← every change (${unknownAt === 0 ? "its dependencies can't be read" : `${count} to ${unknownLink.node}, whose dependencies can't be read`})`
|
|
1614
|
-
];
|
|
1615
|
-
lines.push("");
|
|
1616
|
-
for (const link of hops) {
|
|
1617
|
-
const configured = CONFIGURED[link.kind];
|
|
1618
|
-
const where = configured === undefined ? link.at ?? link.node : link.node;
|
|
1619
|
-
const kind = configured ?? link.kind;
|
|
1620
|
-
lines.push(`${gutter}${paint.dim("-->")} ${paint.dim(where)} ${paint.faint(kind)}`, ...cited(link.at, link.text));
|
|
1621
|
-
}
|
|
1622
|
-
if (unknownLink === undefined) {
|
|
1623
|
-
lines.push(`${gutter}● ${chain.root}: ${chain.cause}`);
|
|
1624
|
-
return lines;
|
|
1625
|
-
}
|
|
1626
|
-
const node = unknownLink.node;
|
|
1627
|
-
for (const { cause, at, text, fix } of chain.unknown) {
|
|
1628
|
-
lines.push(`${gutter}${paint.bad("✗")} ${paint.dim(at ?? node)} ${cause}`, ...cited(at, text), `${gutter} = ${paint.good("fix")}: ${fix}`);
|
|
1629
|
-
}
|
|
1630
|
-
return lines;
|
|
1631
|
-
}
|
|
1632
|
-
var RENDER = {
|
|
1633
|
-
tree,
|
|
1634
|
-
chain: chained,
|
|
1635
|
-
source: sourced
|
|
1636
|
-
};
|
|
1637
|
-
function moreLine(more, all) {
|
|
1638
|
-
const shown = more.slice(0, LIST).join(", ");
|
|
1639
|
-
const rest = more.length > LIST ? ", …" : "";
|
|
1640
|
-
const changes = plural(more.length, "more change reaches", "more changes reach");
|
|
1641
|
-
const hint = all ? "" : " (--all shows each chain)";
|
|
1642
|
-
return `+ ${changes} this test: ${shown}${rest}${hint}`;
|
|
1643
|
-
}
|
|
1644
|
-
function unselectedMessage(explanation) {
|
|
1645
|
-
const { from, changed, reaches, example } = explanation;
|
|
1646
|
-
if (from === undefined) {
|
|
1647
|
-
return changed === 0 ? "Not selected: nothing changed." : `Not selected: ${changed === 1 ? "the changed file doesn't reach" : `none of the ${changed} changed files reaches`} it.`;
|
|
1648
|
-
}
|
|
1649
|
-
const counts = Object.entries(reaches ?? {}).map(([suite, count]) => `${count} ${suite}`);
|
|
1650
|
-
const total = Object.values(reaches ?? {}).reduce((a, b) => a + b, 0);
|
|
1651
|
-
return [
|
|
1652
|
-
`Not selected by a change to ${from}: no chain reaches it.`,
|
|
1653
|
-
total === 0 ? "Its chains reach no test." : `Its chains reach ${plural(total, "test", "tests")} (${counts.join(", ")}), such as ${example}.`
|
|
1654
|
-
].join(`
|
|
1655
|
-
`);
|
|
1656
|
-
}
|
|
1657
|
-
function formatExplanation(explanation, format = "tree", paint = PLAIN) {
|
|
1658
|
-
if (explanation.kind === "full") {
|
|
1659
|
-
return [
|
|
1660
|
-
`The ${explanation.suite} suite runs in full:`,
|
|
1661
|
-
...explanation.reasons.map((reason) => ` ${reason}`)
|
|
1662
|
-
].join(`
|
|
1663
|
-
`);
|
|
1664
|
-
}
|
|
1665
|
-
if (explanation.kind === "unselected")
|
|
1666
|
-
return unselectedMessage(explanation);
|
|
1667
|
-
const { test, more, others } = explanation;
|
|
1668
|
-
const render = RENDER[format];
|
|
1669
|
-
const lines = render(test, explanation, paint);
|
|
1670
|
-
if (more.length > 0) {
|
|
1671
|
-
lines.push(paint.dim(moreLine(more, others.length > 0)));
|
|
1672
|
-
}
|
|
1673
|
-
for (const other of others)
|
|
1674
|
-
lines.push("", ...render(test, other, paint));
|
|
1675
|
-
return lines.join(`
|
|
1676
|
-
`);
|
|
1677
|
-
}
|
|
7
|
+
version
|
|
8
|
+
} from "./shared-x17wzptz.js";
|
|
1678
9
|
|
|
1679
10
|
// src/main.ts
|
|
1680
|
-
var WHITESPACE = /\s+/;
|
|
1681
|
-
var VALUED = new Set([
|
|
1682
|
-
"--config",
|
|
1683
|
-
"--cwd",
|
|
1684
|
-
"--suite",
|
|
1685
|
-
"--base",
|
|
1686
|
-
"--format",
|
|
1687
|
-
"--max-unknown",
|
|
1688
|
-
"--top"
|
|
1689
|
-
]);
|
|
1690
|
-
var COMMON = ["--config", "--cwd", "--no-cache", "--color", "--no-color"];
|
|
1691
|
-
var FLAGS = {
|
|
1692
|
-
plan: [...COMMON, "--json", "--github", "--list"],
|
|
1693
|
-
run: [...COMMON, "--suite"],
|
|
1694
|
-
why: [...COMMON, "--base", "--json", "--format", "--all"],
|
|
1695
|
-
check: [...COMMON, "--base", "--max-unknown", "--json", "--all"],
|
|
1696
|
-
graph: [...COMMON, "--suite", "--top", "--json"]
|
|
1697
|
-
};
|
|
1698
|
-
var POSITIONAL = {
|
|
1699
|
-
plan: 1,
|
|
1700
|
-
run: 1,
|
|
1701
|
-
why: 2,
|
|
1702
|
-
check: 0,
|
|
1703
|
-
graph: 0
|
|
1704
|
-
};
|
|
1705
11
|
var PIPED = {
|
|
1706
12
|
stdout: false,
|
|
1707
13
|
stderr: false,
|
|
1708
14
|
env: {},
|
|
1709
15
|
writeError: () => {}
|
|
1710
16
|
};
|
|
1711
|
-
function colorAsked(argv) {
|
|
1712
|
-
let forced;
|
|
1713
|
-
for (const arg of argv) {
|
|
1714
|
-
if (arg === "--color" || arg === "--color=true")
|
|
1715
|
-
forced = true;
|
|
1716
|
-
if (arg === "--no-color" || arg === "--no-color=true")
|
|
1717
|
-
forced = false;
|
|
1718
|
-
}
|
|
1719
|
-
return forced;
|
|
1720
|
-
}
|
|
1721
|
-
function outFor(io, argv, startMs) {
|
|
1722
|
-
const agent = agentVariable(io.env) !== undefined;
|
|
1723
|
-
const forced = colorAsked(argv);
|
|
1724
|
-
return {
|
|
1725
|
-
io,
|
|
1726
|
-
paint: wantsColor(forced, io.stdout, io.env, agent) ? createPaint() : PLAIN,
|
|
1727
|
-
errorPaint: wantsColor(forced, io.stderr, io.env, agent) ? createPaint() : PLAIN,
|
|
1728
|
-
agent,
|
|
1729
|
-
startMs
|
|
1730
|
-
};
|
|
1731
|
-
}
|
|
1732
|
-
var SCHEMA_VERSION = 1;
|
|
1733
|
-
function usageError(code, message) {
|
|
1734
|
-
return new BiviumError2(code, message);
|
|
1735
|
-
}
|
|
1736
|
-
function unknownOption(command, flag) {
|
|
1737
|
-
const known = FLAGS[command] ?? [];
|
|
1738
|
-
const others = Object.keys(FLAGS).filter((other) => other !== command && FLAGS[other]?.includes(flag));
|
|
1739
|
-
const hint = others.length > 0 ? ` (${others.join(" and ")} ${others.length === 1 ? "takes" : "take"} it)` : didYouMean2(flag, known);
|
|
1740
|
-
return usageError("BV_UNKNOWN_OPTION", `bivium ${command}: unknown option ${flag}${hint}`);
|
|
1741
|
-
}
|
|
1742
|
-
function parseArgs(command, argv) {
|
|
1743
|
-
const known = FLAGS[command] ?? [];
|
|
1744
|
-
const flags = new Map;
|
|
1745
|
-
const positional = [];
|
|
1746
|
-
for (let i = 0;i < argv.length; i++) {
|
|
1747
|
-
const arg = argv[i];
|
|
1748
|
-
if (arg === undefined)
|
|
1749
|
-
continue;
|
|
1750
|
-
if (!arg.startsWith("-") || arg === "-") {
|
|
1751
|
-
positional.push(arg);
|
|
1752
|
-
continue;
|
|
1753
|
-
}
|
|
1754
|
-
const equals = arg.startsWith("--") ? arg.indexOf("=") : -1;
|
|
1755
|
-
const flag = equals === -1 ? arg : arg.slice(0, equals);
|
|
1756
|
-
if (!known.includes(flag))
|
|
1757
|
-
throw unknownOption(command, flag);
|
|
1758
|
-
if (!VALUED.has(flag)) {
|
|
1759
|
-
const on = equals === -1 ? "true" : arg.slice(equals + 1);
|
|
1760
|
-
if (on !== "true" && on !== "false") {
|
|
1761
|
-
throw usageError("BV_BAD_OPTION_VALUE", `bivium ${command}: ${flag} is a switch: give it alone, or as ${flag}=true or ${flag}=false`);
|
|
1762
|
-
}
|
|
1763
|
-
if (on === "true")
|
|
1764
|
-
flags.set(flag, "");
|
|
1765
|
-
continue;
|
|
1766
|
-
}
|
|
1767
|
-
const value = equals === -1 ? argv[++i] : arg.slice(equals + 1);
|
|
1768
|
-
if (value === undefined || value === "" || value.startsWith("--")) {
|
|
1769
|
-
throw usageError("BV_BAD_OPTION_VALUE", `bivium: ${flag} needs a value`);
|
|
1770
|
-
}
|
|
1771
|
-
flags.set(flag, value);
|
|
1772
|
-
}
|
|
1773
|
-
if (positional.length > (POSITIONAL[command] ?? 0)) {
|
|
1774
|
-
throw usageError("BV_UNEXPECTED_ARGUMENT", `bivium ${command}: unexpected ${positional.at(-1)}`);
|
|
1775
|
-
}
|
|
1776
|
-
return { flags, positional };
|
|
1777
|
-
}
|
|
1778
|
-
function commandFor(run, files) {
|
|
1779
|
-
const words = run.split(WHITESPACE).filter(Boolean);
|
|
1780
|
-
const at = words.indexOf("{files}");
|
|
1781
|
-
if (at === -1)
|
|
1782
|
-
return [...words, ...files];
|
|
1783
|
-
return [...words.slice(0, at), ...files, ...words.slice(at + 1)];
|
|
1784
|
-
}
|
|
1785
|
-
function logSelection(label, selection, total, forced) {
|
|
1786
|
-
const files = total === 1 ? "file" : "files";
|
|
1787
|
-
let count;
|
|
1788
|
-
if (selection.mode === "some") {
|
|
1789
|
-
count = `${selection.files.length} of ${total} ${files}`;
|
|
1790
|
-
} else if (selection.mode === "none")
|
|
1791
|
-
count = "none";
|
|
1792
|
-
else if (forced)
|
|
1793
|
-
count = "all (full run)";
|
|
1794
|
-
else
|
|
1795
|
-
count = `all ${total} ${files} (each reaches a change)`;
|
|
1796
|
-
console.log(`${label}: ${count}`);
|
|
1797
|
-
if (selection.mode === "some") {
|
|
1798
|
-
for (const file of selection.files)
|
|
1799
|
-
console.log(` ${file}`);
|
|
1800
|
-
}
|
|
1801
|
-
}
|
|
1802
|
-
async function logContext(base, config, computed) {
|
|
1803
|
-
const mergeBase = (await computed.mergeBase()).slice(0, 7);
|
|
1804
|
-
const count = computed.changed.length;
|
|
1805
|
-
const changed = `${count} changed ${count === 1 ? "file" : "files"}`;
|
|
1806
|
-
const file = config.file ? `, config ${config.file}` : "";
|
|
1807
|
-
console.log(`Against ${base} (merge base ${mergeBase}): ${changed}${file}`);
|
|
1808
|
-
}
|
|
1809
|
-
function logPlan({ plan, narrowed }) {
|
|
1810
|
-
for (const reason of plan.reasons)
|
|
1811
|
-
console.log(`Full run: ${reason}`);
|
|
1812
|
-
for (const line of plan.packages)
|
|
1813
|
-
console.log(line);
|
|
1814
|
-
for (const line of plan.settings)
|
|
1815
|
-
console.log(line);
|
|
1816
|
-
for (const { narrower, result } of narrowed) {
|
|
1817
|
-
if ("note" in result && result.note) {
|
|
1818
|
-
console.log(`${narrower.name}: ${result.note}`);
|
|
1819
|
-
}
|
|
1820
|
-
}
|
|
1821
|
-
for (const [name, selection] of Object.entries(plan.suites)) {
|
|
1822
|
-
const total = plan.totals[name] ?? 0;
|
|
1823
|
-
logSelection(`${name} tests`, selection, total, plan.full.includes(name));
|
|
1824
|
-
}
|
|
1825
|
-
}
|
|
1826
|
-
function verdictOf(name, selection, total, { plan, changed }, used, paint) {
|
|
1827
|
-
const suite = Object.keys(plan.suites).length > 1 ? ` (${name})` : "";
|
|
1828
|
-
if (selection.mode === "some") {
|
|
1829
|
-
return `${paint.bold(String(selection.files.length))} of ${plural(total, "test")} run for ${plural(changed.length, "changed file")}${suite}`;
|
|
1830
|
-
}
|
|
1831
|
-
if (selection.mode === "none") {
|
|
1832
|
-
const why = changed.length === 0 ? "nothing changed" : "no change reaches a test";
|
|
1833
|
-
return `no tests run${suite}: ${why}`;
|
|
1834
|
-
}
|
|
1835
|
-
const cause = plan.full.includes(name) ? plan.causes.find((candidate) => candidate.suites.includes(name)) : undefined;
|
|
1836
|
-
if (cause !== undefined)
|
|
1837
|
-
used.add(cause);
|
|
1838
|
-
const why = plan.full.includes(name) ? cause?.message ?? "a full run" : "each reaches a change";
|
|
1839
|
-
const every = total === 1 ? "the 1 test runs" : `all ${total} tests run`;
|
|
1840
|
-
return `${every}${suite}: ${why}`;
|
|
1841
|
-
}
|
|
1842
|
-
function commonDir(files) {
|
|
1843
|
-
const [first = ""] = files;
|
|
1844
|
-
let prefix = first.slice(0, first.lastIndexOf("/") + 1);
|
|
1845
|
-
for (const file of files) {
|
|
1846
|
-
while (!file.startsWith(prefix)) {
|
|
1847
|
-
prefix = prefix.slice(0, prefix.lastIndexOf("/", prefix.length - 2) + 1);
|
|
1848
|
-
}
|
|
1849
|
-
}
|
|
1850
|
-
return prefix;
|
|
1851
|
-
}
|
|
1852
|
-
function dirOf(file, prefix) {
|
|
1853
|
-
const slash = file.indexOf("/", prefix.length);
|
|
1854
|
-
return slash === -1 ? `${prefix}*` : file.slice(0, slash + 1);
|
|
1855
|
-
}
|
|
1856
|
-
function countTable(paint, { plan }) {
|
|
1857
|
-
const suites = Object.entries(plan.suites).flatMap(([name, selection]) => selection.mode === "some" ? [{ name, files: selection.files }] : []);
|
|
1858
|
-
const prefix = commonDir(suites.flatMap(({ files }) => files));
|
|
1859
|
-
const counts = new Map;
|
|
1860
|
-
for (const [i, { files }] of suites.entries()) {
|
|
1861
|
-
for (const file of files) {
|
|
1862
|
-
const dir = dirOf(file, prefix);
|
|
1863
|
-
const row = counts.get(dir) ?? suites.map(() => 0);
|
|
1864
|
-
row[i] = (row[i] ?? 0) + 1;
|
|
1865
|
-
counts.set(dir, row);
|
|
1866
|
-
}
|
|
1867
|
-
}
|
|
1868
|
-
if (counts.size < 2)
|
|
1869
|
-
return [];
|
|
1870
|
-
const sum = (row) => row.reduce((a, b) => a + b, 0);
|
|
1871
|
-
const rows = [...counts].sort(([a, x], [b, y]) => sum(y) - sum(x) || (a < b ? -1 : 1));
|
|
1872
|
-
const several = Object.keys(plan.suites).length > 1;
|
|
1873
|
-
const heads = several ? suites.map(({ name }) => name) : ["tests"];
|
|
1874
|
-
const right = heads.map((_, i) => i + 1);
|
|
1875
|
-
return aligned([
|
|
1876
|
-
headerRow(paint, ["directory", ...heads]),
|
|
1877
|
-
...rows.map(([dir, row]) => [truncatePath(dir, 48), ...row.map(String)]),
|
|
1878
|
-
["", ...heads.map(() => RULE)],
|
|
1879
|
-
[
|
|
1880
|
-
paint.dim(plural(rows.length, "directory", "directories")),
|
|
1881
|
-
...suites.map(({ files }) => String(files.length))
|
|
1882
|
-
]
|
|
1883
|
-
], right, paint.dim);
|
|
1884
|
-
}
|
|
1885
|
-
async function showPlan(out, base, config, computed) {
|
|
1886
|
-
const { paint } = out;
|
|
1887
|
-
const { plan, narrowed } = computed;
|
|
1888
|
-
const used = new Set;
|
|
1889
|
-
const lines = [];
|
|
1890
|
-
let planned = 0;
|
|
1891
|
-
for (const [name, selection] of Object.entries(plan.suites)) {
|
|
1892
|
-
const total = plan.totals[name] ?? 0;
|
|
1893
|
-
lines.push(verdictOf(name, selection, total, computed, used, paint));
|
|
1894
|
-
if (selection.mode === "some")
|
|
1895
|
-
planned += selection.files.length;
|
|
1896
|
-
if (selection.mode === "all")
|
|
1897
|
-
planned += total;
|
|
1898
|
-
}
|
|
1899
|
-
const mergeBase = (await computed.mergeBase()).slice(0, 7);
|
|
1900
|
-
const notes = [
|
|
1901
|
-
[
|
|
1902
|
-
`since ${base} (merge base ${mergeBase})`,
|
|
1903
|
-
...config.file ? [`config ${config.file}`] : []
|
|
1904
|
-
].join(SEPARATOR),
|
|
1905
|
-
...plan.causes.filter((cause) => !used.has(cause)).map((cause) => `full run: ${cause.message}`),
|
|
1906
|
-
...plan.packages,
|
|
1907
|
-
...plan.settings,
|
|
1908
|
-
...narrowed.flatMap(({ narrower, result }) => ("note" in result) && result.note ? [`${narrower.name}: ${result.note}`] : [])
|
|
1909
|
-
];
|
|
1910
|
-
lines.push(...notes.map((note) => paint.dim(truncateWords(note, MEASURE))));
|
|
1911
|
-
const table = countTable(paint, computed);
|
|
1912
|
-
if (table.length > 0)
|
|
1913
|
-
lines.push("", ...table);
|
|
1914
|
-
const some = Object.values(plan.suites).some(({ mode }) => mode === "some");
|
|
1915
|
-
const cost = duration(nowMs() - out.startMs);
|
|
1916
|
-
lines.push("", closingLine(paint, `Planned ${plural(planned, "test")}`, some ? `${cost}${SEPARATOR}--list for the files` : cost));
|
|
1917
|
-
console.log(lines.join(`
|
|
1918
|
-
`));
|
|
1919
|
-
}
|
|
1920
|
-
function githubOutput() {
|
|
1921
|
-
const output = githubOutputPath();
|
|
1922
|
-
if (!output) {
|
|
1923
|
-
throw usageError("BV_NO_GITHUB_OUTPUT", "bivium plan: --github writes to $GITHUB_OUTPUT, which is not set (it is in a GitHub Actions step)");
|
|
1924
|
-
}
|
|
1925
|
-
return output;
|
|
1926
|
-
}
|
|
1927
|
-
function writeOutputs(output, config, { plan }) {
|
|
1928
|
-
const lines = config.suites.map((suite) => {
|
|
1929
|
-
const selection = plan.suites[suite.name];
|
|
1930
|
-
if (selection === undefined) {
|
|
1931
|
-
throw new BiviumError2("BV_INTERNAL", `bivium: plan: no selection for suite "${suite.name}".`);
|
|
1932
|
-
}
|
|
1933
|
-
return `${suite.output}=${formatSelection(selection)}
|
|
1934
|
-
`;
|
|
1935
|
-
});
|
|
1936
|
-
appendFileSync(output, lines.join(""));
|
|
1937
|
-
}
|
|
1938
|
-
function json(value) {
|
|
1939
|
-
return JSON.stringify({ schemaVersion: SCHEMA_VERSION, ...value }, null, 2);
|
|
1940
|
-
}
|
|
1941
|
-
function findSuite(config, name) {
|
|
1942
|
-
if (name === undefined)
|
|
1943
|
-
return;
|
|
1944
|
-
const suite = config.suites.find((candidate) => candidate.name === name);
|
|
1945
|
-
if (suite)
|
|
1946
|
-
return suite;
|
|
1947
|
-
const names = config.suites.map((candidate) => candidate.name);
|
|
1948
|
-
throw usageError("BV_UNKNOWN_SUITE", `bivium: no suite "${name}"${didYouMean2(name, names)}; the config has ${names.join(", ")}`);
|
|
1949
|
-
}
|
|
1950
|
-
function real(path) {
|
|
1951
|
-
try {
|
|
1952
|
-
return realpathSync(path);
|
|
1953
|
-
} catch {
|
|
1954
|
-
const parent = dirname(path);
|
|
1955
|
-
return parent === path ? path : join3(real(parent), basename(path));
|
|
1956
|
-
}
|
|
1957
|
-
}
|
|
1958
|
-
function repoPath(root, arg, known) {
|
|
1959
|
-
const absolute = resolve(arg);
|
|
1960
|
-
const realRoot = real(root);
|
|
1961
|
-
const mappings = [
|
|
1962
|
-
relative(root, absolute),
|
|
1963
|
-
relative(realRoot, join3(real(dirname(absolute)), basename(absolute))),
|
|
1964
|
-
relative(realRoot, real(absolute))
|
|
1965
|
-
].map((path) => path.split("\\").join("/")).filter((path) => !(path.startsWith("..") || isAbsolute2(path)));
|
|
1966
|
-
const fromCwd = mappings.find(known) ?? mappings[0];
|
|
1967
|
-
if (isAbsolute2(arg))
|
|
1968
|
-
return fromCwd ?? arg;
|
|
1969
|
-
const asWritten = posix3.normalize(arg.split("\\").join("/"));
|
|
1970
|
-
if (known(asWritten))
|
|
1971
|
-
return asWritten;
|
|
1972
|
-
return fromCwd !== undefined && known(fromCwd) ? fromCwd : asWritten;
|
|
1973
|
-
}
|
|
1974
|
-
async function inspect(command, { flags, positional }, out) {
|
|
1975
|
-
const format = flags.get("--format") ?? "tree";
|
|
1976
|
-
if (format !== "json" && !isFormat(format)) {
|
|
1977
|
-
throw usageError("BV_BAD_OPTION_VALUE", `bivium why: --format takes tree, chain, source or json${didYouMean2(format, ["tree", "chain", "source", "json"])}`);
|
|
1978
|
-
}
|
|
1979
|
-
const maxFlag = flags.get("--max-unknown");
|
|
1980
|
-
const max = maxFlag === undefined ? undefined : Number(maxFlag);
|
|
1981
|
-
if (max !== undefined && !Number.isInteger(max)) {
|
|
1982
|
-
throw usageError("BV_BAD_OPTION_VALUE", "bivium check: --max-unknown takes a whole number");
|
|
1983
|
-
}
|
|
1984
|
-
const [testArg] = positional;
|
|
1985
|
-
if (command === "why" && testArg === undefined) {
|
|
1986
|
-
throw usageError("BV_MISSING_ARGUMENT", "bivium why: which test? bivium why <test> [file]");
|
|
1987
|
-
}
|
|
1988
|
-
const progress = command === "check" && out.io.stderr && !isDumb(out.io.env) && !out.agent && !flags.has("--json") ? createProgress(out.io.writeError, out.errorPaint, nowMs) : SILENT;
|
|
1989
|
-
progress.step("building the graph", 0);
|
|
1990
|
-
try {
|
|
1991
|
-
const { root, base, config } = await prepare2({
|
|
1992
|
-
cwd: flags.get("--cwd"),
|
|
1993
|
-
base: flags.get("--base") ?? (command === "check" ? "HEAD" : undefined),
|
|
1994
|
-
configFile: flags.get("--config")
|
|
1995
|
-
});
|
|
1996
|
-
const computed = await computePlan2(root, base, config, {
|
|
1997
|
-
...flags.has("--no-cache") && { cache: false }
|
|
1998
|
-
});
|
|
1999
|
-
if (command === "check") {
|
|
2000
|
-
const report = await check(root, config, computed, (what, done) => progress.step(what, done));
|
|
2001
|
-
progress.clear();
|
|
2002
|
-
const ok = !failed(report, max);
|
|
2003
|
-
console.log(flags.has("--json") ? json({ ok, summary: summarize(report), ...report }) : formatReport(report, max, {
|
|
2004
|
-
paint: out.paint,
|
|
2005
|
-
all: flags.has("--all"),
|
|
2006
|
-
elapsedMs: nowMs() - out.startMs
|
|
2007
|
-
}));
|
|
2008
|
-
return ok ? 0 : 1;
|
|
2009
|
-
}
|
|
2010
|
-
return await whyCommand(root, config, computed, flags, positional, out);
|
|
2011
|
-
} catch (error) {
|
|
2012
|
-
progress.clear();
|
|
2013
|
-
throw error;
|
|
2014
|
-
}
|
|
2015
|
-
}
|
|
2016
|
-
async function whyCommand(root, config, computed, flags, [testArg, fileArg], out) {
|
|
2017
|
-
const format = flags.get("--format") ?? "tree";
|
|
2018
|
-
const inGraph = (path) => computed.graph.has(path);
|
|
2019
|
-
const isTest = (path) => config.suites.some((suite) => suite.isTest(path));
|
|
2020
|
-
const test = repoPath(root, testArg ?? "", (path) => inGraph(path) && isTest(path));
|
|
2021
|
-
const file = fileArg === undefined ? undefined : repoPath(root, fileArg, inGraph);
|
|
2022
|
-
if (!isTest(test) || !inGraph(test)) {
|
|
2023
|
-
const tests = [...computed.graph.keys()].filter(isTest);
|
|
2024
|
-
throw usageError(inGraph(test) ? "BV_UNKNOWN_TEST" : "BV_UNKNOWN_FILE", inGraph(test) ? `bivium why: ${test} is no suite's test: name a test first, as in bivium why <test> ${test}` : `bivium why: no test ${test}${didYouMean2(test, tests)}`);
|
|
2025
|
-
}
|
|
2026
|
-
if (file !== undefined && !inGraph(file)) {
|
|
2027
|
-
throw usageError("BV_UNKNOWN_FILE", `bivium why: no file ${file}${didYouMean2(file, computed.graph.keys())}`);
|
|
2028
|
-
}
|
|
2029
|
-
const explanation = await explain(root, config, computed, test, file, {
|
|
2030
|
-
all: flags.has("--all")
|
|
2031
|
-
});
|
|
2032
|
-
console.log(flags.has("--json") || format === "json" ? json(explanation) : formatExplanation(explanation, isFormat(format) ? format : "tree", out.paint));
|
|
2033
|
-
return 0;
|
|
2034
|
-
}
|
|
2035
|
-
async function graphCommand({ flags }) {
|
|
2036
|
-
const top = Number(flags.get("--top") ?? "10");
|
|
2037
|
-
if (!Number.isInteger(top) || top < 0) {
|
|
2038
|
-
throw usageError("BV_BAD_OPTION_VALUE", "bivium graph: --top takes a whole number");
|
|
2039
|
-
}
|
|
2040
|
-
const { root, base, config } = await prepare2({
|
|
2041
|
-
cwd: flags.get("--cwd"),
|
|
2042
|
-
base: "HEAD",
|
|
2043
|
-
configFile: flags.get("--config")
|
|
2044
|
-
});
|
|
2045
|
-
const one = findSuite(config, flags.get("--suite"));
|
|
2046
|
-
const suites = one ? [one] : config.suites;
|
|
2047
|
-
const { graph } = await computePlan2(root, base, config, {
|
|
2048
|
-
...flags.has("--no-cache") && { cache: false }
|
|
2049
|
-
});
|
|
2050
|
-
const report = graphReport(graph, suites);
|
|
2051
|
-
console.log(flags.has("--json") ? json(report) : formatGraph(report, top));
|
|
2052
|
-
return 0;
|
|
2053
|
-
}
|
|
2054
|
-
async function runCommand(command, { flags, positional }, out) {
|
|
2055
|
-
const output = flags.has("--github") ? githubOutput() : undefined;
|
|
2056
|
-
const { root, base, config } = await prepare2({
|
|
2057
|
-
cwd: flags.get("--cwd"),
|
|
2058
|
-
base: positional[0],
|
|
2059
|
-
configFile: flags.get("--config")
|
|
2060
|
-
});
|
|
2061
|
-
const suite = findSuite(config, flags.get("--suite")) ?? config.suites[0];
|
|
2062
|
-
if (suite === undefined) {
|
|
2063
|
-
throw new BiviumError2("BV_CONFIG_INVALID", "bivium config: no suites");
|
|
2064
|
-
}
|
|
2065
|
-
if (command === "run" && !suite.run) {
|
|
2066
|
-
throw usageError("BV_NO_RUN_COMMAND", `bivium run: suite "${suite.name}" has no run command: add suites.${suite.name}.run to the config`);
|
|
2067
|
-
}
|
|
2068
|
-
const computed = await computePlan2(root, base, config, {
|
|
2069
|
-
...flags.has("--no-cache") && { cache: false },
|
|
2070
|
-
...command === "run" && { suites: [suite.name] },
|
|
2071
|
-
mergeBase: true
|
|
2072
|
-
});
|
|
2073
|
-
if (command === "plan") {
|
|
2074
|
-
if (flags.has("--json")) {
|
|
2075
|
-
const { suites, reasons, causes, totals, packages } = computed.plan;
|
|
2076
|
-
const narrowed = computed.narrowed.map((ran) => ({
|
|
2077
|
-
name: ran.narrower.name,
|
|
2078
|
-
result: ran.result
|
|
2079
|
-
}));
|
|
2080
|
-
console.log(json({
|
|
2081
|
-
base,
|
|
2082
|
-
mergeBase: await computed.mergeBase(),
|
|
2083
|
-
config: config.file,
|
|
2084
|
-
changed: computed.changed,
|
|
2085
|
-
suites,
|
|
2086
|
-
reasons,
|
|
2087
|
-
causes,
|
|
2088
|
-
totals,
|
|
2089
|
-
packages,
|
|
2090
|
-
narrowed
|
|
2091
|
-
}));
|
|
2092
|
-
} else if (out.io.stdout && !out.agent && !flags.has("--list")) {
|
|
2093
|
-
await showPlan(out, base, config, computed);
|
|
2094
|
-
} else {
|
|
2095
|
-
await logContext(base, config, computed);
|
|
2096
|
-
logPlan(computed);
|
|
2097
|
-
}
|
|
2098
|
-
if (output !== undefined)
|
|
2099
|
-
writeOutputs(output, config, computed);
|
|
2100
|
-
return 0;
|
|
2101
|
-
}
|
|
2102
|
-
const human = out.io.stdout && !out.agent;
|
|
2103
|
-
const others = flags.has("--suite") ? [] : config.suites.slice(1).map((s) => `--suite=${s.name}`);
|
|
2104
|
-
if (human)
|
|
2105
|
-
await showRun(out, base, config, computed, suite.name, others);
|
|
2106
|
-
else {
|
|
2107
|
-
await logContext(base, config, computed);
|
|
2108
|
-
logPlan(computed);
|
|
2109
|
-
}
|
|
2110
|
-
const selection = computed.plan.suites[suite.name];
|
|
2111
|
-
if (selection === undefined || selection.mode === "none") {
|
|
2112
|
-
if (human) {
|
|
2113
|
-
console.log(closingLine(out.paint, `Nothing to run for ${suite.name}`));
|
|
2114
|
-
} else {
|
|
2115
|
-
console.log(`No ${suite.name} tests affected against ${base}; skipping.`);
|
|
2116
|
-
}
|
|
2117
|
-
return 0;
|
|
2118
|
-
}
|
|
2119
|
-
const [program, ...args] = selection.mode === "some" ? commandFor(suite.run ?? "", selection.files) : suite.runAll !== undefined ? commandFor(suite.runAll, []) : commandFor(suite.run ?? "", suite.roots.filter((root) => root !== "."));
|
|
2120
|
-
if (program === undefined) {
|
|
2121
|
-
throw usageError("BV_NO_RUN_COMMAND", `bivium run: suite "${suite.name}" has no run command: add suites.${suite.name}.run to the config`);
|
|
2122
|
-
}
|
|
2123
|
-
if (human) {
|
|
2124
|
-
const files = selection.mode === "some" ? selection.files.length : 0;
|
|
2125
|
-
const bare = commandFor(suite.run ?? "", []);
|
|
2126
|
-
console.log(`${out.paint.dim(shownCommand([program, ...args], bare, files))}
|
|
2127
|
-
`);
|
|
2128
|
-
}
|
|
2129
|
-
const started = nowMs();
|
|
2130
|
-
const result = spawnSync(program, args, { cwd: root, stdio: "inherit" });
|
|
2131
|
-
const interrupted = result.signal === "SIGINT";
|
|
2132
|
-
const status = interrupted ? 130 : result.status ?? 1;
|
|
2133
|
-
if (human) {
|
|
2134
|
-
const took = duration(nowMs() - started);
|
|
2135
|
-
const name = suite.name;
|
|
2136
|
-
console.log(`
|
|
2137
|
-
${interrupted ? failLine(out.paint, `${name} interrupted`, took) : status === 0 ? closingLine(out.paint, `${name} passed`, took) : failLine(out.paint, `${name} failed`, `${result.signal ?? `exit ${status}`}${SEPARATOR}${took}`)}`);
|
|
2138
|
-
}
|
|
2139
|
-
return status;
|
|
2140
|
-
}
|
|
2141
|
-
function shownCommand(whole, bare, files) {
|
|
2142
|
-
const line = `$ ${whole.join(" ")}`;
|
|
2143
|
-
if (textWidth(line) <= MEASURE)
|
|
2144
|
-
return line;
|
|
2145
|
-
return `$ ${bare.join(" ")} ${SEPARATOR.trim()} ${plural(files, "file")}, as bivium plan --list names them`;
|
|
2146
|
-
}
|
|
2147
|
-
async function showRun(out, base, config, computed, name, others) {
|
|
2148
|
-
const { paint } = out;
|
|
2149
|
-
const selection = computed.plan.suites[name];
|
|
2150
|
-
const total = computed.plan.totals[name] ?? 0;
|
|
2151
|
-
const used = new Set;
|
|
2152
|
-
const verdict = selection === undefined ? `no ${name} tests` : verdictOf(name, selection, total, computed, used, paint);
|
|
2153
|
-
const mergeBase = (await computed.mergeBase()).slice(0, 7);
|
|
2154
|
-
const context = [
|
|
2155
|
-
`since ${base} (merge base ${mergeBase})`,
|
|
2156
|
-
...config.file ? [`config ${config.file}`] : []
|
|
2157
|
-
].join(SEPARATOR);
|
|
2158
|
-
const lines = [verdict, paint.dim(context)];
|
|
2159
|
-
if (others.length > 0) {
|
|
2160
|
-
lines.push(paint.dim(`running ${name}${SEPARATOR}${plural(others.length, "more suite")}: ${others.join(", ")}`));
|
|
2161
|
-
}
|
|
2162
|
-
console.log(lines.join(`
|
|
2163
|
-
`));
|
|
2164
|
-
}
|
|
2165
|
-
function commandAt(argv) {
|
|
2166
|
-
for (let i = 0;i < argv.length; i++) {
|
|
2167
|
-
const arg = argv[i];
|
|
2168
|
-
if (arg === undefined)
|
|
2169
|
-
continue;
|
|
2170
|
-
if (!arg.startsWith("-"))
|
|
2171
|
-
return i;
|
|
2172
|
-
if (VALUED.has(arg))
|
|
2173
|
-
i++;
|
|
2174
|
-
}
|
|
2175
|
-
return -1;
|
|
2176
|
-
}
|
|
2177
|
-
var COMMANDS = Object.keys(FLAGS);
|
|
2178
|
-
function wantsJson(argv) {
|
|
2179
|
-
return argv.some((arg, i) => arg === "--json" || arg === "--json=true" || arg === "--format=json" || arg === "--format" && argv[i + 1] === "json");
|
|
2180
|
-
}
|
|
2181
|
-
function fail(argv, error) {
|
|
2182
|
-
console.error(error.message);
|
|
2183
|
-
if (wantsJson(argv)) {
|
|
2184
|
-
const { kind, code, message } = error;
|
|
2185
|
-
console.log(json({ error: { kind, code, message } }));
|
|
2186
|
-
}
|
|
2187
|
-
return 2;
|
|
2188
|
-
}
|
|
2189
17
|
async function main(argv, io = PIPED) {
|
|
2190
|
-
const
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
return 0;
|
|
2194
|
-
}
|
|
2195
|
-
if (argv.includes("--version") || argv.includes("-v")) {
|
|
18
|
+
const startMs = nowMs();
|
|
19
|
+
const help = argv.includes("--help") || argv.includes("-h");
|
|
20
|
+
if (!help && (argv.includes("--version") || argv.includes("-v"))) {
|
|
2196
21
|
console.log(version);
|
|
2197
22
|
return 0;
|
|
2198
23
|
}
|
|
24
|
+
const [
|
|
25
|
+
{
|
|
26
|
+
COMMANDS,
|
|
27
|
+
commandAt,
|
|
28
|
+
EVERY_FLAG,
|
|
29
|
+
fail,
|
|
30
|
+
FLAGS,
|
|
31
|
+
outFor,
|
|
32
|
+
parseArgs,
|
|
33
|
+
unknownFlag,
|
|
34
|
+
usageError
|
|
35
|
+
},
|
|
36
|
+
{ BiviumError, didYouMean },
|
|
37
|
+
{ fitTerminal }
|
|
38
|
+
] = await Promise.all([
|
|
39
|
+
import("./shared-m8z858pr.js"),
|
|
40
|
+
import("./errors.js"),
|
|
41
|
+
import("./shared-wahjx628.js")
|
|
42
|
+
]);
|
|
43
|
+
const out = outFor(io, argv, startMs);
|
|
44
|
+
const showHelp = async () => {
|
|
45
|
+
const { usage } = await import("./shared-a5kfg3qy.js");
|
|
46
|
+
console.log(fitTerminal(usage(out.paint), out.io.stdout, process.stdout.columns));
|
|
47
|
+
return 0;
|
|
48
|
+
};
|
|
49
|
+
if (help)
|
|
50
|
+
return await showHelp();
|
|
2199
51
|
const at = commandAt(argv);
|
|
2200
52
|
const command = at === -1 ? undefined : argv[at];
|
|
2201
|
-
if (command === "help")
|
|
2202
|
-
|
|
2203
|
-
|
|
53
|
+
if (command === "help")
|
|
54
|
+
return await showHelp();
|
|
55
|
+
const typo = command === undefined ? unknownFlag(argv) : undefined;
|
|
56
|
+
if (typo !== undefined) {
|
|
57
|
+
return fail(argv, usageError("BV_UNKNOWN_OPTION", `bivium: unknown option ${typo}${didYouMean(typo, EVERY_FLAG)}`));
|
|
2204
58
|
}
|
|
2205
59
|
if (command === undefined) {
|
|
2206
60
|
return fail(argv, usageError("BV_MISSING_ARGUMENT", `bivium: which command? ${COMMANDS.join(", ")} (bivium --help for usage)`));
|
|
2207
61
|
}
|
|
2208
62
|
if (!(command in FLAGS)) {
|
|
2209
|
-
return fail(argv, usageError("BV_UNKNOWN_COMMAND", `bivium: unknown command "${command}"${
|
|
63
|
+
return fail(argv, usageError("BV_UNKNOWN_COMMAND", `bivium: unknown command "${command}"${didYouMean(command, COMMANDS)}; the commands are ${COMMANDS.join(", ")} (bivium --help for usage)`));
|
|
2210
64
|
}
|
|
2211
65
|
const rest = [...argv.slice(0, at), ...argv.slice(at + 1)];
|
|
2212
66
|
try {
|
|
2213
67
|
const args = parseArgs(command, rest);
|
|
68
|
+
const { graphCommand, inspect, runCommand } = await import("./shared-rzbfbyqq.js");
|
|
2214
69
|
if (command === "why" || command === "check")
|
|
2215
70
|
return await inspect(command, args, out);
|
|
2216
71
|
if (command === "graph")
|
|
2217
72
|
return await graphCommand(args);
|
|
2218
73
|
return await runCommand(command, args, out);
|
|
2219
74
|
} catch (error) {
|
|
2220
|
-
if (error instanceof
|
|
75
|
+
if (error instanceof BiviumError)
|
|
2221
76
|
return fail(argv, error);
|
|
2222
77
|
const message = error instanceof Error ? error.message : String(error);
|
|
2223
|
-
return fail(argv, new
|
|
78
|
+
return fail(argv, new BiviumError("BV_INTERNAL", message, { cause: error }));
|
|
2224
79
|
}
|
|
2225
80
|
}
|
|
2226
81
|
|
|
2227
82
|
// src/cli.ts
|
|
2228
83
|
var io = {
|
|
2229
|
-
|
|
2230
|
-
|
|
84
|
+
get stdout() {
|
|
85
|
+
return process.stdout.isTTY === true;
|
|
86
|
+
},
|
|
87
|
+
get stderr() {
|
|
88
|
+
return process.stderr.isTTY === true;
|
|
89
|
+
},
|
|
2231
90
|
env: environment(),
|
|
2232
|
-
writeError: (text) => process.stderr.write(text)
|
|
91
|
+
writeError: (text) => process.stderr.write(text),
|
|
92
|
+
signals: process
|
|
2233
93
|
};
|
|
2234
94
|
process.exitCode = await main(process.argv.slice(2), io);
|