bivium 0.9.0 → 0.11.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 -2192
- package/compute.js +4 -3
- package/config.js +3 -2
- package/docs/api.md +3 -0
- package/docs/ci.md +138 -4
- package/docs/cli.md +101 -3
- package/docs/config.md +115 -0
- package/docs/how-it-works.md +129 -0
- package/docs/tuning.md +24 -1
- package/errors.js +2 -1
- package/index.d.ts +62 -0
- package/package.json +1 -1
- package/shared-1erx1bv8.js +26 -0
- package/shared-6n8bazs5.js +103 -0
- package/shared-8kbmg99n.js +164 -0
- package/shared-9e8vfbjs.js +475 -0
- package/shared-9xwazb0f.js +1047 -0
- package/shared-apgq7xbk.js +13 -0
- package/shared-b9dpffxv.js +30 -0
- package/shared-dyz928f5.js +68 -0
- package/shared-graedbt1.js +12 -0
- package/shared-jee85qxj.js +149 -0
- package/shared-jp8yw63q.js +4 -0
- package/shared-k2ahcc61.js +68 -0
- package/shared-mpjk8met.js +320 -0
- package/shared-nyb7n090.js +655 -0
- package/shared-p3qfqmwd.js +40 -0
- package/{shared-550cvwt2.js → shared-qaqjsxsk.js} +42 -5
- package/shared-vaqyv5nn.js +19 -0
- package/{shared-07rt4zr7.js → shared-wxem8d5w.js} +2162 -1363
- package/shared-211m8fjt.js +0 -69
|
@@ -0,0 +1,655 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isDumb2,
|
|
3
|
+
MISSING2,
|
|
4
|
+
SEPARATOR2,
|
|
5
|
+
closingLine2,
|
|
6
|
+
failLine2,
|
|
7
|
+
truncateWords2,
|
|
8
|
+
truncatePath2,
|
|
9
|
+
textWidth2,
|
|
10
|
+
MEASURE2,
|
|
11
|
+
RULE2,
|
|
12
|
+
aligned2,
|
|
13
|
+
headerRow2,
|
|
14
|
+
plural2,
|
|
15
|
+
SILENT2,
|
|
16
|
+
createProgress2
|
|
17
|
+
} from "./shared-mpjk8met.js";
|
|
18
|
+
import {
|
|
19
|
+
BiviumError2,
|
|
20
|
+
didYouMean2
|
|
21
|
+
} from "./shared-p3qfqmwd.js";
|
|
22
|
+
import {
|
|
23
|
+
usageError2,
|
|
24
|
+
json2
|
|
25
|
+
} from "./shared-jee85qxj.js";
|
|
26
|
+
import"./shared-qaqjsxsk.js";
|
|
27
|
+
import {
|
|
28
|
+
formatSelection,
|
|
29
|
+
loadTimings,
|
|
30
|
+
timingsWarning,
|
|
31
|
+
durationsOf,
|
|
32
|
+
longestFirst,
|
|
33
|
+
shardPlan,
|
|
34
|
+
computePlan2,
|
|
35
|
+
prepare2
|
|
36
|
+
} from "./shared-wxem8d5w.js";
|
|
37
|
+
import {
|
|
38
|
+
duration
|
|
39
|
+
} from "./shared-b9dpffxv.js";
|
|
40
|
+
import {
|
|
41
|
+
githubOutputPath,
|
|
42
|
+
nowMs,
|
|
43
|
+
environment
|
|
44
|
+
} from "./shared-graedbt1.js";
|
|
45
|
+
|
|
46
|
+
// src/commands.ts
|
|
47
|
+
import { spawn } from "node:child_process";
|
|
48
|
+
import {
|
|
49
|
+
appendFileSync,
|
|
50
|
+
mkdtempSync,
|
|
51
|
+
realpathSync,
|
|
52
|
+
rmSync,
|
|
53
|
+
writeFileSync
|
|
54
|
+
} from "node:fs";
|
|
55
|
+
import { tmpdir } from "node:os";
|
|
56
|
+
import {
|
|
57
|
+
basename,
|
|
58
|
+
dirname,
|
|
59
|
+
isAbsolute,
|
|
60
|
+
join,
|
|
61
|
+
posix,
|
|
62
|
+
relative,
|
|
63
|
+
resolve
|
|
64
|
+
} from "node:path";
|
|
65
|
+
var WHITESPACE = /\s+/;
|
|
66
|
+
var CASES_VARIABLE = "BIVIUM_CASES";
|
|
67
|
+
function commandFor(run, files) {
|
|
68
|
+
const words = run.split(WHITESPACE).filter(Boolean);
|
|
69
|
+
const at = words.indexOf("{files}");
|
|
70
|
+
if (at === -1)
|
|
71
|
+
return [...words, ...files];
|
|
72
|
+
return [...words.slice(0, at), ...files, ...words.slice(at + 1)];
|
|
73
|
+
}
|
|
74
|
+
function logSelection(label, selection, total, forced) {
|
|
75
|
+
const files = total === 1 ? "file" : "files";
|
|
76
|
+
let count;
|
|
77
|
+
if (selection.mode === "some") {
|
|
78
|
+
count = `${selection.files.length} of ${total} ${files}`;
|
|
79
|
+
} else if (selection.mode === "none")
|
|
80
|
+
count = "none";
|
|
81
|
+
else if (forced)
|
|
82
|
+
count = "all (full run)";
|
|
83
|
+
else
|
|
84
|
+
count = `all ${total} ${files} (each reaches a change)`;
|
|
85
|
+
console.log(`${label}: ${count}`);
|
|
86
|
+
if (selection.mode === "some") {
|
|
87
|
+
for (const file of selection.files)
|
|
88
|
+
console.log(` ${file}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async function logContext(base, config, computed) {
|
|
92
|
+
const mergeBase = (await computed.mergeBase()).slice(0, 7);
|
|
93
|
+
const count = computed.changed.length;
|
|
94
|
+
const changed = `${count} changed ${count === 1 ? "file" : "files"}`;
|
|
95
|
+
const file = config.file ? `, config ${config.file}` : "";
|
|
96
|
+
console.log(`Against ${base} (merge base ${mergeBase}): ${changed}${file}`);
|
|
97
|
+
}
|
|
98
|
+
function caseNotes({ plan }, only) {
|
|
99
|
+
return Object.entries(plan.cases).flatMap(([name, tests]) => only === undefined || name === only ? Object.entries(tests).map(([test, cases]) => `${test} runs ${cases.length} of ${plural2(plan.caseTotals.get(test) ?? cases.length, "case")}: ${cases.join(" ")}`) : []);
|
|
100
|
+
}
|
|
101
|
+
function shardSummaries(config, sharding) {
|
|
102
|
+
return config.suites.flatMap(({ name, shards: spec }) => {
|
|
103
|
+
const shards = sharding.shards[name];
|
|
104
|
+
const timings = sharding.timings[name];
|
|
105
|
+
if (shards === undefined || timings === undefined)
|
|
106
|
+
return [];
|
|
107
|
+
const files = shards.reduce((sum, shard) => sum + shard.files.length, 0);
|
|
108
|
+
const count = plural2(shards.length, "shard");
|
|
109
|
+
const cold = sharding.cold.has(name);
|
|
110
|
+
if (files === 0)
|
|
111
|
+
return [{ name, summary: count, where: "", shards, cold }];
|
|
112
|
+
if (timings.timed === 0) {
|
|
113
|
+
const summary = `${count} by file count${SEPARATOR2}none timed in ${timings.file}`;
|
|
114
|
+
return [{ name, summary, where: "", shards, cold }];
|
|
115
|
+
}
|
|
116
|
+
const target = typeof spec === "object" ? `${SEPARATOR2}target ${duration(spec.targetMs)}, max ${spec.max}` : "";
|
|
117
|
+
const timed = `${timings.timed} of ${plural2(files, "file")} timed`;
|
|
118
|
+
return [
|
|
119
|
+
{
|
|
120
|
+
name,
|
|
121
|
+
summary: `${count}${target}${SEPARATOR2}${timed}`,
|
|
122
|
+
where: ` in ${timings.file}`,
|
|
123
|
+
shards,
|
|
124
|
+
cold
|
|
125
|
+
}
|
|
126
|
+
];
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
function logPlan(computed, sharded) {
|
|
130
|
+
const { plan, narrowed } = computed;
|
|
131
|
+
for (const reason of plan.reasons)
|
|
132
|
+
console.log(`Full run: ${reason}`);
|
|
133
|
+
for (const line of plan.packages)
|
|
134
|
+
console.log(line);
|
|
135
|
+
for (const line of plan.settings)
|
|
136
|
+
console.log(line);
|
|
137
|
+
for (const line of caseNotes(computed))
|
|
138
|
+
console.log(line);
|
|
139
|
+
for (const line of plan.edits)
|
|
140
|
+
console.log(line);
|
|
141
|
+
for (const { narrower, result } of narrowed) {
|
|
142
|
+
if ("note" in result && result.note) {
|
|
143
|
+
console.log(`${narrower.name}: ${result.note}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
for (const [name, selection] of Object.entries(plan.suites)) {
|
|
147
|
+
const total = plan.totals[name] ?? 0;
|
|
148
|
+
logSelection(`${name} tests`, selection, total, plan.full.includes(name));
|
|
149
|
+
}
|
|
150
|
+
if (sharded === undefined)
|
|
151
|
+
return;
|
|
152
|
+
const { config, sharding } = sharded;
|
|
153
|
+
for (const { name, summary, where, shards, cold } of shardSummaries(config, sharding)) {
|
|
154
|
+
console.log(`${name} shards: ${summary}${where}`);
|
|
155
|
+
for (const { shard, files, estimatedMs } of shards) {
|
|
156
|
+
const estimate = cold ? "" : `, ${duration(estimatedMs)}`;
|
|
157
|
+
console.log(` shard ${shard}: ${plural2(files.length, "file")}${estimate}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
var SHOWN_SHARDS = 8;
|
|
162
|
+
function shardTables(paint, config, sharding, several) {
|
|
163
|
+
return shardSummaries(config, sharding).flatMap(({ name, summary, shards, cold }) => {
|
|
164
|
+
const note = paint.dim(truncateWords2(`${several ? `${name}: ` : ""}${summary}`, MEASURE2));
|
|
165
|
+
if (shards.length === 0)
|
|
166
|
+
return [];
|
|
167
|
+
const rows = shards.slice(0, SHOWN_SHARDS).map(({ shard, files, estimatedMs }) => [
|
|
168
|
+
String(shard),
|
|
169
|
+
String(files.length),
|
|
170
|
+
cold ? MISSING2 : duration(estimatedMs)
|
|
171
|
+
]);
|
|
172
|
+
const hidden = shards.length - rows.length;
|
|
173
|
+
return [
|
|
174
|
+
"",
|
|
175
|
+
note,
|
|
176
|
+
...aligned2([headerRow2(paint, ["shard", "files", "estimate"]), ...rows], [0, 1, 2], paint.dim),
|
|
177
|
+
...hidden > 0 ? [paint.dim(` + ${plural2(hidden, "more shard")}: --json for each`)] : []
|
|
178
|
+
];
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
function verdictOf(name, selection, total, { plan, changed }, used, paint) {
|
|
182
|
+
const suite = Object.keys(plan.suites).length > 1 ? ` (${name})` : "";
|
|
183
|
+
if (selection.mode === "some") {
|
|
184
|
+
return `${paint.bold(String(selection.files.length))} of ${plural2(total, "test")} run for ${plural2(changed.length, "changed file")}${suite}`;
|
|
185
|
+
}
|
|
186
|
+
if (selection.mode === "none") {
|
|
187
|
+
const why = changed.length === 0 ? "nothing changed" : "no change reaches a test";
|
|
188
|
+
return `no tests run${suite}: ${why}`;
|
|
189
|
+
}
|
|
190
|
+
const cause = plan.full.includes(name) ? plan.causes.find((candidate) => candidate.suites.includes(name)) : undefined;
|
|
191
|
+
if (cause !== undefined)
|
|
192
|
+
used.add(cause);
|
|
193
|
+
const why = plan.full.includes(name) ? cause?.message ?? "a full run" : "each reaches a change";
|
|
194
|
+
const every = total === 1 ? "the 1 test runs" : `all ${total} tests run`;
|
|
195
|
+
return `${every}${suite}: ${why}`;
|
|
196
|
+
}
|
|
197
|
+
function commonDir(files) {
|
|
198
|
+
const [first = ""] = files;
|
|
199
|
+
let prefix = first.slice(0, first.lastIndexOf("/") + 1);
|
|
200
|
+
for (const file of files) {
|
|
201
|
+
while (!file.startsWith(prefix)) {
|
|
202
|
+
prefix = prefix.slice(0, prefix.lastIndexOf("/", prefix.length - 2) + 1);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return prefix;
|
|
206
|
+
}
|
|
207
|
+
function dirOf(file, prefix) {
|
|
208
|
+
const slash = file.indexOf("/", prefix.length);
|
|
209
|
+
return slash === -1 ? `${prefix}*` : file.slice(0, slash + 1);
|
|
210
|
+
}
|
|
211
|
+
function countTable(paint, { plan }) {
|
|
212
|
+
const suites = Object.entries(plan.suites).flatMap(([name, selection]) => selection.mode === "some" ? [{ name, files: selection.files }] : []);
|
|
213
|
+
const prefix = commonDir(suites.flatMap(({ files }) => files));
|
|
214
|
+
const counts = new Map;
|
|
215
|
+
for (const [i, { files }] of suites.entries()) {
|
|
216
|
+
for (const file of files) {
|
|
217
|
+
const dir = dirOf(file, prefix);
|
|
218
|
+
const row = counts.get(dir) ?? suites.map(() => 0);
|
|
219
|
+
row[i] = (row[i] ?? 0) + 1;
|
|
220
|
+
counts.set(dir, row);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (counts.size < 2)
|
|
224
|
+
return [];
|
|
225
|
+
const sum = (row) => row.reduce((a, b) => a + b, 0);
|
|
226
|
+
const rows = [...counts].sort(([a, x], [b, y]) => sum(y) - sum(x) || (a < b ? -1 : 1));
|
|
227
|
+
const several = Object.keys(plan.suites).length > 1;
|
|
228
|
+
const heads = several ? suites.map(({ name }) => name) : ["tests"];
|
|
229
|
+
const right = heads.map((_, i) => i + 1);
|
|
230
|
+
return aligned2([
|
|
231
|
+
headerRow2(paint, ["directory", ...heads]),
|
|
232
|
+
...rows.map(([dir, row]) => [truncatePath2(dir, 48), ...row.map(String)]),
|
|
233
|
+
["", ...heads.map(() => RULE2)],
|
|
234
|
+
[
|
|
235
|
+
paint.dim(plural2(rows.length, "directory", "directories")),
|
|
236
|
+
...suites.map(({ files }) => String(files.length))
|
|
237
|
+
]
|
|
238
|
+
], right, paint.dim);
|
|
239
|
+
}
|
|
240
|
+
async function showPlan(out, base, config, computed, sharding) {
|
|
241
|
+
const { paint } = out;
|
|
242
|
+
const { plan, narrowed } = computed;
|
|
243
|
+
const used = new Set;
|
|
244
|
+
const lines = [];
|
|
245
|
+
let planned = 0;
|
|
246
|
+
for (const [name, selection] of Object.entries(plan.suites)) {
|
|
247
|
+
const total = plan.totals[name] ?? 0;
|
|
248
|
+
lines.push(verdictOf(name, selection, total, computed, used, paint));
|
|
249
|
+
if (selection.mode === "some")
|
|
250
|
+
planned += selection.files.length;
|
|
251
|
+
if (selection.mode === "all")
|
|
252
|
+
planned += total;
|
|
253
|
+
}
|
|
254
|
+
const mergeBase = (await computed.mergeBase()).slice(0, 7);
|
|
255
|
+
const notes = [
|
|
256
|
+
[
|
|
257
|
+
`since ${base} (merge base ${mergeBase})`,
|
|
258
|
+
...config.file ? [`config ${config.file}`] : []
|
|
259
|
+
].join(SEPARATOR2),
|
|
260
|
+
...plan.causes.filter((cause) => !used.has(cause)).map((cause) => `full run: ${cause.message}`),
|
|
261
|
+
...plan.packages,
|
|
262
|
+
...plan.settings,
|
|
263
|
+
...caseNotes(computed),
|
|
264
|
+
...plan.edits,
|
|
265
|
+
...narrowed.flatMap(({ narrower, result }) => ("note" in result) && result.note ? [`${narrower.name}: ${result.note}`] : [])
|
|
266
|
+
];
|
|
267
|
+
lines.push(...notes.map((note) => paint.dim(truncateWords2(note, MEASURE2))));
|
|
268
|
+
const table = countTable(paint, computed);
|
|
269
|
+
if (table.length > 0)
|
|
270
|
+
lines.push("", ...table);
|
|
271
|
+
const several = Object.keys(plan.suites).length > 1;
|
|
272
|
+
lines.push(...shardTables(paint, config, sharding, several));
|
|
273
|
+
const some = Object.values(plan.suites).some(({ mode }) => mode === "some");
|
|
274
|
+
const cost = duration(nowMs() - out.startMs);
|
|
275
|
+
lines.push("", closingLine2(paint, `Planned ${plural2(planned, "test")}`, some ? `${cost}${SEPARATOR2}--list for the files` : cost));
|
|
276
|
+
console.log(lines.join(`
|
|
277
|
+
`));
|
|
278
|
+
}
|
|
279
|
+
function githubOutput() {
|
|
280
|
+
const output = githubOutputPath();
|
|
281
|
+
if (!output) {
|
|
282
|
+
throw usageError2("BV_NO_GITHUB_OUTPUT", "bivium plan: --github writes to $GITHUB_OUTPUT, which is not set (it is in a GitHub Actions step)");
|
|
283
|
+
}
|
|
284
|
+
return output;
|
|
285
|
+
}
|
|
286
|
+
var LINE_BREAK = /[\r\n]/;
|
|
287
|
+
var LINE_ENDING = /\r?\n/;
|
|
288
|
+
function outputEntry(name, value) {
|
|
289
|
+
if (!LINE_BREAK.test(value))
|
|
290
|
+
return `${name}=${value}
|
|
291
|
+
`;
|
|
292
|
+
const lines = new Set(value.split(LINE_ENDING));
|
|
293
|
+
let delimiter = "BIVIUM_EOF";
|
|
294
|
+
for (let i = 1;lines.has(delimiter); i++)
|
|
295
|
+
delimiter = `BIVIUM_EOF_${i}`;
|
|
296
|
+
return `${name}<<${delimiter}
|
|
297
|
+
${value}
|
|
298
|
+
${delimiter}
|
|
299
|
+
`;
|
|
300
|
+
}
|
|
301
|
+
function matrixOf(shards) {
|
|
302
|
+
return JSON.stringify(shards.map(({ shard, files }) => ({ shard, files: files.join(" ") })));
|
|
303
|
+
}
|
|
304
|
+
function writeOutputs(output, config, { plan }, sharding) {
|
|
305
|
+
const entries = config.suites.map((suite) => {
|
|
306
|
+
const selection = plan.suites[suite.name];
|
|
307
|
+
if (selection === undefined) {
|
|
308
|
+
throw new BiviumError2("BV_INTERNAL", `bivium: plan: no selection for suite "${suite.name}".`);
|
|
309
|
+
}
|
|
310
|
+
const cases = plan.caseLists.get(suite.name);
|
|
311
|
+
const shards = sharding.shards[suite.name];
|
|
312
|
+
return [
|
|
313
|
+
outputEntry(suite.output, formatSelection(selection)),
|
|
314
|
+
cases ? outputEntry(`${suite.name}-cases`, cases.join(" ")) : "",
|
|
315
|
+
shards ? outputEntry(`${suite.name}-matrix`, matrixOf(shards)) : "",
|
|
316
|
+
shards ? outputEntry(`${suite.name}-shards`, String(shards.length)) : ""
|
|
317
|
+
].join("");
|
|
318
|
+
});
|
|
319
|
+
appendFileSync(output, entries.join(""));
|
|
320
|
+
}
|
|
321
|
+
function findSuite(config, name) {
|
|
322
|
+
if (name === undefined)
|
|
323
|
+
return;
|
|
324
|
+
const suite = config.suites.find((candidate) => candidate.name === name);
|
|
325
|
+
if (suite)
|
|
326
|
+
return suite;
|
|
327
|
+
const names = config.suites.map((candidate) => candidate.name);
|
|
328
|
+
throw usageError2("BV_UNKNOWN_SUITE", `bivium: no suite "${name}"${didYouMean2(name, names)}; the config has ${names.join(", ")}`);
|
|
329
|
+
}
|
|
330
|
+
function real(path) {
|
|
331
|
+
try {
|
|
332
|
+
return realpathSync(path);
|
|
333
|
+
} catch {
|
|
334
|
+
const parent = dirname(path);
|
|
335
|
+
return parent === path ? path : join(real(parent), basename(path));
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
function repoPath(root, arg, known) {
|
|
339
|
+
const absolute = resolve(arg);
|
|
340
|
+
const realRoot = real(root);
|
|
341
|
+
const mappings = [
|
|
342
|
+
relative(root, absolute),
|
|
343
|
+
relative(realRoot, join(real(dirname(absolute)), basename(absolute))),
|
|
344
|
+
relative(realRoot, real(absolute))
|
|
345
|
+
].map((path) => path.split("\\").join("/")).filter((path) => !(path.startsWith("..") || isAbsolute(path)));
|
|
346
|
+
const fromCwd = mappings.find(known) ?? mappings[0];
|
|
347
|
+
if (isAbsolute(arg))
|
|
348
|
+
return fromCwd ?? arg;
|
|
349
|
+
const asWritten = posix.normalize(arg.split("\\").join("/"));
|
|
350
|
+
if (known(asWritten))
|
|
351
|
+
return asWritten;
|
|
352
|
+
return fromCwd !== undefined && known(fromCwd) ? fromCwd : asWritten;
|
|
353
|
+
}
|
|
354
|
+
async function inspect(command, { flags, positional }, out) {
|
|
355
|
+
const [{ check, failed, formatReport, summarize }, why] = await Promise.all([
|
|
356
|
+
import("./shared-vaqyv5nn.js"),
|
|
357
|
+
import("./shared-9e8vfbjs.js")
|
|
358
|
+
]);
|
|
359
|
+
const format = flags.get("--format") ?? "tree";
|
|
360
|
+
if (format !== "json" && !why.isFormat(format)) {
|
|
361
|
+
throw usageError2("BV_BAD_OPTION_VALUE", `bivium why: --format takes tree, chain, source or json${didYouMean2(format, ["tree", "chain", "source", "json"])}`);
|
|
362
|
+
}
|
|
363
|
+
const maxFlag = flags.get("--max-unknown");
|
|
364
|
+
const max = maxFlag === undefined ? undefined : Number(maxFlag);
|
|
365
|
+
if (max !== undefined && !Number.isInteger(max)) {
|
|
366
|
+
throw usageError2("BV_BAD_OPTION_VALUE", "bivium check: --max-unknown takes a whole number");
|
|
367
|
+
}
|
|
368
|
+
const [testArg] = positional;
|
|
369
|
+
if (command === "why" && testArg === undefined) {
|
|
370
|
+
throw usageError2("BV_MISSING_ARGUMENT", "bivium why: which test? bivium why <test> [file]");
|
|
371
|
+
}
|
|
372
|
+
const progress = command === "check" && out.io.stderr && !isDumb2(out.io.env) && !out.agent && !flags.has("--json") ? createProgress2(out.io.writeError, out.errorPaint, nowMs) : SILENT2;
|
|
373
|
+
progress.step("building the graph", 0);
|
|
374
|
+
try {
|
|
375
|
+
const { root, base, config } = await prepare2({
|
|
376
|
+
cwd: flags.get("--cwd"),
|
|
377
|
+
base: flags.get("--base") ?? (command === "check" ? "HEAD" : undefined),
|
|
378
|
+
configFile: flags.get("--config")
|
|
379
|
+
});
|
|
380
|
+
const computed = await computePlan2(root, base, config, {
|
|
381
|
+
...flags.has("--no-cache") && { cache: false }
|
|
382
|
+
});
|
|
383
|
+
if (command === "check") {
|
|
384
|
+
const report = await check(root, config, computed, (what, done) => progress.step(what, done));
|
|
385
|
+
progress.clear();
|
|
386
|
+
const ok = !failed(report, max);
|
|
387
|
+
console.log(flags.has("--json") ? json2({ ok, summary: summarize(report), ...report }) : formatReport(report, max, {
|
|
388
|
+
paint: out.paint,
|
|
389
|
+
all: flags.has("--all"),
|
|
390
|
+
elapsedMs: nowMs() - out.startMs
|
|
391
|
+
}));
|
|
392
|
+
return ok ? 0 : 1;
|
|
393
|
+
}
|
|
394
|
+
return await whyCommand(root, config, computed, flags, positional, out);
|
|
395
|
+
} catch (error) {
|
|
396
|
+
progress.clear();
|
|
397
|
+
throw error;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
async function whyCommand(root, config, computed, flags, [testArg, fileArg], out) {
|
|
401
|
+
const { explain, formatExplanation, isFormat } = await import("./shared-9e8vfbjs.js");
|
|
402
|
+
const format = flags.get("--format") ?? "tree";
|
|
403
|
+
const inGraph = (path) => computed.graph.has(path);
|
|
404
|
+
const isTest = (path) => config.suites.some((suite) => suite.isTest(path));
|
|
405
|
+
const test = repoPath(root, testArg ?? "", (path) => inGraph(path) && isTest(path));
|
|
406
|
+
const file = fileArg === undefined ? undefined : repoPath(root, fileArg, inGraph);
|
|
407
|
+
if (!isTest(test) || !inGraph(test)) {
|
|
408
|
+
const tests = [...computed.graph.keys()].filter(isTest);
|
|
409
|
+
throw usageError2(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)}`);
|
|
410
|
+
}
|
|
411
|
+
if (file !== undefined && !inGraph(file)) {
|
|
412
|
+
throw usageError2("BV_UNKNOWN_FILE", `bivium why: no file ${file}${didYouMean2(file, computed.graph.keys())}`);
|
|
413
|
+
}
|
|
414
|
+
const explanation = await explain(root, config, computed, test, file, {
|
|
415
|
+
all: flags.has("--all")
|
|
416
|
+
});
|
|
417
|
+
console.log(flags.has("--json") || format === "json" ? json2(explanation) : formatExplanation(explanation, isFormat(format) ? format : "tree", out.paint));
|
|
418
|
+
return 0;
|
|
419
|
+
}
|
|
420
|
+
async function graphCommand({ flags }) {
|
|
421
|
+
const top = Number(flags.get("--top") ?? "10");
|
|
422
|
+
if (!Number.isInteger(top) || top < 0) {
|
|
423
|
+
throw usageError2("BV_BAD_OPTION_VALUE", "bivium graph: --top takes a whole number");
|
|
424
|
+
}
|
|
425
|
+
const { root, base, config } = await prepare2({
|
|
426
|
+
cwd: flags.get("--cwd"),
|
|
427
|
+
base: "HEAD",
|
|
428
|
+
configFile: flags.get("--config")
|
|
429
|
+
});
|
|
430
|
+
const one = findSuite(config, flags.get("--suite"));
|
|
431
|
+
const suites = one ? [one] : config.suites;
|
|
432
|
+
const { graph } = await computePlan2(root, base, config, {
|
|
433
|
+
...flags.has("--no-cache") && { cache: false }
|
|
434
|
+
});
|
|
435
|
+
const { formatGraph, graphReport } = await import("./shared-apgq7xbk.js");
|
|
436
|
+
const report = graphReport(graph, suites);
|
|
437
|
+
console.log(flags.has("--json") ? json2(report) : formatGraph(report, top));
|
|
438
|
+
return 0;
|
|
439
|
+
}
|
|
440
|
+
async function runCommand(command, { flags, positional }, out) {
|
|
441
|
+
const output = flags.has("--github") ? githubOutput() : undefined;
|
|
442
|
+
const { root, base, config } = await prepare2({
|
|
443
|
+
cwd: flags.get("--cwd"),
|
|
444
|
+
base: positional[0],
|
|
445
|
+
configFile: flags.get("--config")
|
|
446
|
+
});
|
|
447
|
+
const suite = findSuite(config, flags.get("--suite")) ?? config.suites[0];
|
|
448
|
+
if (suite === undefined) {
|
|
449
|
+
throw new BiviumError2("BV_CONFIG_INVALID", "bivium config: no suites");
|
|
450
|
+
}
|
|
451
|
+
if (command === "run" && !suite.run) {
|
|
452
|
+
throw usageError2("BV_NO_RUN_COMMAND", `bivium run: suite "${suite.name}" has no run command: add suites.${suite.name}.run to the config`);
|
|
453
|
+
}
|
|
454
|
+
const computed = await computePlan2(root, base, config, {
|
|
455
|
+
...flags.has("--no-cache") && { cache: false },
|
|
456
|
+
...command === "run" && { suites: [suite.name] },
|
|
457
|
+
mergeBase: true
|
|
458
|
+
});
|
|
459
|
+
if (command === "plan") {
|
|
460
|
+
const sharding = await shardPlan(root, config, computed.plan);
|
|
461
|
+
for (const suite of config.suites) {
|
|
462
|
+
const problem = sharding.timings[suite.name]?.problem;
|
|
463
|
+
if (problem !== undefined) {
|
|
464
|
+
out.io.writeError(timingsWarning("plan", suite, problem));
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
if (flags.has("--json")) {
|
|
468
|
+
const { suites, cases, reasons, causes, totals, packages } = computed.plan;
|
|
469
|
+
const narrowed = computed.narrowed.map((ran) => ({
|
|
470
|
+
name: ran.narrower.name,
|
|
471
|
+
result: ran.result
|
|
472
|
+
}));
|
|
473
|
+
console.log(json2({
|
|
474
|
+
base,
|
|
475
|
+
mergeBase: await computed.mergeBase(),
|
|
476
|
+
config: config.file,
|
|
477
|
+
changed: computed.changed,
|
|
478
|
+
suites,
|
|
479
|
+
cases,
|
|
480
|
+
reasons,
|
|
481
|
+
causes,
|
|
482
|
+
totals,
|
|
483
|
+
packages,
|
|
484
|
+
narrowed,
|
|
485
|
+
shards: sharding.shards,
|
|
486
|
+
timings: sharding.timings
|
|
487
|
+
}));
|
|
488
|
+
} else if (out.io.stdout && !out.agent && !flags.has("--list")) {
|
|
489
|
+
await showPlan(out, base, config, computed, sharding);
|
|
490
|
+
} else {
|
|
491
|
+
await logContext(base, config, computed);
|
|
492
|
+
logPlan(computed, { config, sharding });
|
|
493
|
+
}
|
|
494
|
+
if (output !== undefined)
|
|
495
|
+
writeOutputs(output, config, computed, sharding);
|
|
496
|
+
return 0;
|
|
497
|
+
}
|
|
498
|
+
const human = out.io.stdout && !out.agent;
|
|
499
|
+
const others = flags.has("--suite") ? [] : config.suites.slice(1).map((s) => `--suite=${s.name}`);
|
|
500
|
+
if (human)
|
|
501
|
+
await showRun(out, base, config, computed, suite.name, others);
|
|
502
|
+
else {
|
|
503
|
+
await logContext(base, config, computed);
|
|
504
|
+
logPlan(computed);
|
|
505
|
+
}
|
|
506
|
+
const selection = computed.plan.suites[suite.name];
|
|
507
|
+
if (selection === undefined || selection.mode === "none") {
|
|
508
|
+
if (human) {
|
|
509
|
+
console.log(closingLine2(out.paint, `Nothing to run for ${suite.name}`));
|
|
510
|
+
} else {
|
|
511
|
+
console.log(`No ${suite.name} tests affected against ${base}; skipping.`);
|
|
512
|
+
}
|
|
513
|
+
return 0;
|
|
514
|
+
}
|
|
515
|
+
let files = [];
|
|
516
|
+
if (selection.mode === "some") {
|
|
517
|
+
const { recorded, problem } = await loadTimings(root, suite);
|
|
518
|
+
if (problem !== undefined) {
|
|
519
|
+
out.io.writeError(timingsWarning("run", suite, problem));
|
|
520
|
+
}
|
|
521
|
+
const tests = computed.plan.tests[suite.name] ?? [];
|
|
522
|
+
files = longestFirst(selection.files, durationsOf(tests, recorded));
|
|
523
|
+
}
|
|
524
|
+
const [program, ...args] = selection.mode === "some" ? commandFor(suite.run ?? "", files) : suite.runAll !== undefined ? commandFor(suite.runAll, []) : commandFor(suite.run ?? "", suite.roots.filter((root) => root !== "."));
|
|
525
|
+
if (program === undefined) {
|
|
526
|
+
throw usageError2("BV_NO_RUN_COMMAND", `bivium run: suite "${suite.name}" has no run command: add suites.${suite.name}.run to the config`);
|
|
527
|
+
}
|
|
528
|
+
if (human) {
|
|
529
|
+
const files = selection.mode === "some" ? selection.files.length : 0;
|
|
530
|
+
const bare = commandFor(suite.run ?? "", []);
|
|
531
|
+
console.log(`${out.paint.dim(shownCommand([program, ...args], bare, files))}
|
|
532
|
+
`);
|
|
533
|
+
}
|
|
534
|
+
const cases = computed.plan.caseLists.get(suite.name);
|
|
535
|
+
if (cases === undefined) {
|
|
536
|
+
return await runSuite(program, args, root, suite.name, out, human);
|
|
537
|
+
}
|
|
538
|
+
const dir = mkdtempSync(join(tmpdir(), "bivium-cases-"));
|
|
539
|
+
try {
|
|
540
|
+
const file = join(dir, "cases.txt");
|
|
541
|
+
writeFileSync(file, `${cases.join(`
|
|
542
|
+
`)}
|
|
543
|
+
`);
|
|
544
|
+
const env = { ...environment(), [CASES_VARIABLE]: file };
|
|
545
|
+
return await runSuite(program, args, root, suite.name, out, human, env);
|
|
546
|
+
} finally {
|
|
547
|
+
rmSync(dir, { recursive: true, force: true });
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
var SIGNAL_NAMES = { SIGINT: "Ctrl-C", SIGTERM: "SIGTERM" };
|
|
551
|
+
var PASS_ON_MS = 1000;
|
|
552
|
+
async function runSuite(program, args, root, name, out, human, env) {
|
|
553
|
+
const { EXIT_INTERRUPTED, exitCodeOf, handleInterrupt } = await import("./shared-6n8bazs5.js");
|
|
554
|
+
const started = nowMs();
|
|
555
|
+
const child = spawn(program, args, {
|
|
556
|
+
cwd: root,
|
|
557
|
+
stdio: "inherit",
|
|
558
|
+
...env && { env }
|
|
559
|
+
});
|
|
560
|
+
const ended = new Promise((resolve) => {
|
|
561
|
+
child.once("error", (error) => resolve({ code: null, signal: null, error }));
|
|
562
|
+
child.once("exit", (code, signal) => resolve({ code, signal }));
|
|
563
|
+
});
|
|
564
|
+
let stoppedBy;
|
|
565
|
+
let closed;
|
|
566
|
+
const close = (result) => {
|
|
567
|
+
if (closed !== undefined)
|
|
568
|
+
return closed;
|
|
569
|
+
if (result.error !== undefined) {
|
|
570
|
+
console.error(result.error.code === "ENOENT" ? `bivium run: suite "${name}" runs ${program}, which isn't on PATH; run bivium through bunx, npx or a package script, which put node_modules/.bin on it` : `bivium run: suite "${name}" couldn't start ${program}: ${result.error.message}`);
|
|
571
|
+
closed = 2;
|
|
572
|
+
return closed;
|
|
573
|
+
}
|
|
574
|
+
const by = stoppedBy ?? (result.signal === "SIGINT" || result.code === EXIT_INTERRUPTED ? "SIGINT" : undefined);
|
|
575
|
+
closed = by === "SIGTERM" ? 143 : by === "SIGINT" ? EXIT_INTERRUPTED : result.code ?? 1;
|
|
576
|
+
const took = duration(nowMs() - started);
|
|
577
|
+
if (by !== undefined && !human) {
|
|
578
|
+
out.io.writeError(`bivium run: ${name} stopped by ${SIGNAL_NAMES[by]} after ${took}
|
|
579
|
+
`);
|
|
580
|
+
}
|
|
581
|
+
if (!human)
|
|
582
|
+
return closed;
|
|
583
|
+
console.log(`
|
|
584
|
+
${by !== undefined ? failLine2(out.paint, `${name} stopped by ${SIGNAL_NAMES[by]}`, took) : closed === 0 ? closingLine2(out.paint, `${name} passed`, took) : failLine2(out.paint, `${name} failed`, `${result.signal ?? `exit ${closed}`}${SEPARATOR2}${took}`)}`);
|
|
585
|
+
return closed;
|
|
586
|
+
};
|
|
587
|
+
const { signals } = out.io;
|
|
588
|
+
if (signals === undefined)
|
|
589
|
+
return close(await ended);
|
|
590
|
+
const interrupt = handleInterrupt({
|
|
591
|
+
process: signals,
|
|
592
|
+
exit: false,
|
|
593
|
+
write: (text) => process.stdout.write(text),
|
|
594
|
+
terminal: out.io.stdout,
|
|
595
|
+
async onStop(signal) {
|
|
596
|
+
stoppedBy = signal;
|
|
597
|
+
if (signal === "SIGTERM")
|
|
598
|
+
child.kill("SIGTERM");
|
|
599
|
+
else {
|
|
600
|
+
const pass = setTimeout(() => child.kill("SIGINT"), PASS_ON_MS);
|
|
601
|
+
pass.unref();
|
|
602
|
+
}
|
|
603
|
+
close(await ended);
|
|
604
|
+
},
|
|
605
|
+
onQuit() {
|
|
606
|
+
child.kill("SIGKILL");
|
|
607
|
+
close({ code: null, signal: "SIGKILL" });
|
|
608
|
+
}
|
|
609
|
+
});
|
|
610
|
+
try {
|
|
611
|
+
const result = await ended;
|
|
612
|
+
const signal = await interrupt.settle();
|
|
613
|
+
if (signal === undefined)
|
|
614
|
+
return close(result);
|
|
615
|
+
await interrupt.stopped;
|
|
616
|
+
return exitCodeOf(signal);
|
|
617
|
+
} finally {
|
|
618
|
+
interrupt.dispose();
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
function shownCommand(whole, bare, files) {
|
|
622
|
+
const line = `$ ${whole.join(" ")}`;
|
|
623
|
+
if (textWidth2(line) <= MEASURE2)
|
|
624
|
+
return line;
|
|
625
|
+
const counted = `$ ${bare.join(" ")}${SEPARATOR2}${plural2(files, "file")}`;
|
|
626
|
+
const pointed = `${counted}, as bivium plan --list names them`;
|
|
627
|
+
return textWidth2(pointed) <= MEASURE2 ? pointed : counted;
|
|
628
|
+
}
|
|
629
|
+
async function showRun(out, base, config, computed, name, others) {
|
|
630
|
+
const { paint } = out;
|
|
631
|
+
const selection = computed.plan.suites[name];
|
|
632
|
+
const total = computed.plan.totals[name] ?? 0;
|
|
633
|
+
const used = new Set;
|
|
634
|
+
const verdict = selection === undefined ? `no ${name} tests` : verdictOf(name, selection, total, computed, used, paint);
|
|
635
|
+
const mergeBase = (await computed.mergeBase()).slice(0, 7);
|
|
636
|
+
const context = [
|
|
637
|
+
`since ${base} (merge base ${mergeBase})`,
|
|
638
|
+
...config.file ? [`config ${config.file}`] : []
|
|
639
|
+
].join(SEPARATOR2);
|
|
640
|
+
const lines = [
|
|
641
|
+
verdict,
|
|
642
|
+
paint.dim(context),
|
|
643
|
+
...caseNotes(computed, name).map((note) => paint.dim(truncateWords2(note, MEASURE2)))
|
|
644
|
+
];
|
|
645
|
+
if (others.length > 0) {
|
|
646
|
+
lines.push(paint.dim(`running ${name}${SEPARATOR2}${plural2(others.length, "more suite")}: ${others.join(", ")}`));
|
|
647
|
+
}
|
|
648
|
+
console.log(lines.join(`
|
|
649
|
+
`));
|
|
650
|
+
}
|
|
651
|
+
export {
|
|
652
|
+
graphCommand,
|
|
653
|
+
inspect,
|
|
654
|
+
runCommand
|
|
655
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {
|
|
2
|
+
nearest2
|
|
3
|
+
} from "./shared-mpjk8met.js";
|
|
4
|
+
|
|
5
|
+
// src/errors.ts
|
|
6
|
+
var KINDS = {
|
|
7
|
+
BV_MISSING_ARGUMENT: "usage",
|
|
8
|
+
BV_UNKNOWN_COMMAND: "usage",
|
|
9
|
+
BV_UNKNOWN_OPTION: "usage",
|
|
10
|
+
BV_BAD_OPTION_VALUE: "usage",
|
|
11
|
+
BV_UNEXPECTED_ARGUMENT: "usage",
|
|
12
|
+
BV_UNKNOWN_SUITE: "usage",
|
|
13
|
+
BV_UNKNOWN_TEST: "usage",
|
|
14
|
+
BV_UNKNOWN_FILE: "usage",
|
|
15
|
+
BV_NO_RUN_COMMAND: "usage",
|
|
16
|
+
BV_NO_GITHUB_OUTPUT: "usage",
|
|
17
|
+
BV_CONFIG_NOT_FOUND: "config",
|
|
18
|
+
BV_CONFIG_UNREADABLE: "config",
|
|
19
|
+
BV_CONFIG_INVALID: "config",
|
|
20
|
+
BV_GIT_NO_BASE: "git",
|
|
21
|
+
BV_GIT_NO_MERGE_BASE: "git",
|
|
22
|
+
BV_GIT_FAILED: "git",
|
|
23
|
+
BV_INTERNAL: "other"
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
class BiviumError2 extends Error {
|
|
27
|
+
code;
|
|
28
|
+
kind;
|
|
29
|
+
constructor(code, message, options) {
|
|
30
|
+
super(message, options);
|
|
31
|
+
this.code = code;
|
|
32
|
+
this.kind = KINDS[code];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function didYouMean2(input, candidates) {
|
|
36
|
+
const match = nearest2(input, candidates);
|
|
37
|
+
return match === undefined ? "" : ` (did you mean ${match}?)`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { BiviumError2, didYouMean2 };
|