bivium 0.6.2 → 0.7.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 +26 -22
- package/cli.js +573 -273
- package/docs/api.md +5 -1
- package/docs/ci.md +4 -2
- package/docs/cli.md +69 -8
- package/docs/config.md +13 -10
- package/docs/how-it-works.md +18 -3
- package/docs/recipes.md +6 -4
- package/docs/tuning.md +35 -29
- package/index.d.ts +14 -26
- package/package.json +1 -1
- package/shared.js +189 -40
package/cli.js
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
import {
|
|
3
3
|
version,
|
|
4
4
|
githubOutputPath,
|
|
5
|
+
nowMs,
|
|
6
|
+
environment,
|
|
7
|
+
agentVariable,
|
|
5
8
|
BiviumError2,
|
|
6
9
|
didYouMean,
|
|
7
10
|
globToRegExp,
|
|
@@ -23,7 +26,24 @@ import {
|
|
|
23
26
|
affectedBy,
|
|
24
27
|
formatSelection,
|
|
25
28
|
orphanOf,
|
|
29
|
+
PLAIN,
|
|
30
|
+
createPaint,
|
|
31
|
+
wantsColor,
|
|
32
|
+
SEPARATOR,
|
|
33
|
+
branch,
|
|
34
|
+
NOTE_INDENT,
|
|
35
|
+
closingLine,
|
|
36
|
+
failLine,
|
|
37
|
+
truncateWords,
|
|
38
|
+
truncatePath,
|
|
39
|
+
textWidth,
|
|
40
|
+
MEASURE,
|
|
41
|
+
RULE,
|
|
42
|
+
aligned,
|
|
43
|
+
headerRow,
|
|
26
44
|
plural,
|
|
45
|
+
SILENT,
|
|
46
|
+
createProgress,
|
|
27
47
|
computePlan,
|
|
28
48
|
prepare
|
|
29
49
|
} from "./shared.js";
|
|
@@ -199,6 +219,30 @@ function formatGraph(report, top) {
|
|
|
199
219
|
`);
|
|
200
220
|
}
|
|
201
221
|
|
|
222
|
+
// src/human.ts
|
|
223
|
+
function duration(ms) {
|
|
224
|
+
if (ms < 999.5)
|
|
225
|
+
return `${Math.round(ms)} ms`;
|
|
226
|
+
const s = ms / 1000;
|
|
227
|
+
return `${s < 999.5 ? s.toPrecision(3) : Math.round(s)} s`;
|
|
228
|
+
}
|
|
229
|
+
function wrapped(text, first, hang) {
|
|
230
|
+
const lines = [];
|
|
231
|
+
let line = first;
|
|
232
|
+
let empty = true;
|
|
233
|
+
for (const word of text.split(" ")) {
|
|
234
|
+
if (!empty && textWidth(line) + 1 + textWidth(word) > MEASURE) {
|
|
235
|
+
lines.push(line);
|
|
236
|
+
line = hang + word;
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
line += empty ? word : ` ${word}`;
|
|
240
|
+
empty = false;
|
|
241
|
+
}
|
|
242
|
+
lines.push(line);
|
|
243
|
+
return lines;
|
|
244
|
+
}
|
|
245
|
+
|
|
202
246
|
// src/locate.ts
|
|
203
247
|
import { readFile } from "node:fs/promises";
|
|
204
248
|
import { isAbsolute, join } from "node:path";
|
|
@@ -416,7 +460,7 @@ function withNames(graph, targets) {
|
|
|
416
460
|
...[...graph.get(file)?.barrel?.keys() ?? []].map((name) => `${file}#${name}`)
|
|
417
461
|
]);
|
|
418
462
|
}
|
|
419
|
-
async function spelledPaths(root, config, computed) {
|
|
463
|
+
async function spelledPaths(root, config, computed, done = () => {}) {
|
|
420
464
|
const { graph } = computed;
|
|
421
465
|
const files = new Set;
|
|
422
466
|
const dirs = new Map;
|
|
@@ -434,7 +478,10 @@ async function spelledPaths(root, config, computed) {
|
|
|
434
478
|
const spelling = spellers(config, computed);
|
|
435
479
|
const nodes = [...spelling.keys()];
|
|
436
480
|
const sources = await Promise.all(nodes.map((node) => sourceOf(root, node)));
|
|
481
|
+
const every = Math.max(1, Math.ceil(nodes.length / 20));
|
|
437
482
|
for (const [i, node] of nodes.entries()) {
|
|
483
|
+
if (i % every === 0)
|
|
484
|
+
done(i / nodes.length);
|
|
438
485
|
const source = sources[i];
|
|
439
486
|
const suites = spelling.get(node) ?? [];
|
|
440
487
|
const through = [
|
|
@@ -746,7 +793,8 @@ function unreachedFiles(config, { graph }, spelled) {
|
|
|
746
793
|
}
|
|
747
794
|
return found;
|
|
748
795
|
}
|
|
749
|
-
async function check(root, config, computed) {
|
|
796
|
+
async function check(root, config, computed, step = () => {}) {
|
|
797
|
+
step("finding what the graph can't read", 0.05);
|
|
750
798
|
const nodes = [...computed.graph].filter(([, deps]) => deps.unknown.length > 0).map(([node]) => node);
|
|
751
799
|
const testsOf = (node) => {
|
|
752
800
|
const reach = walksByTypes(computed.graph, [node], {
|
|
@@ -779,8 +827,11 @@ async function check(root, config, computed) {
|
|
|
779
827
|
file,
|
|
780
828
|
reason
|
|
781
829
|
}));
|
|
830
|
+
step("finding dead globs", 0.1);
|
|
782
831
|
const dead = await findDeadGlobs(root, config, computed);
|
|
783
|
-
const
|
|
832
|
+
const spelling = "finding paths nothing declares";
|
|
833
|
+
const spelled = await spelledPaths(root, config, computed, (ratio) => step(spelling, 0.15 + 0.8 * ratio));
|
|
834
|
+
step("finding files no test reaches", 0.95);
|
|
784
835
|
const unreached = unreachedFiles(config, computed, [
|
|
785
836
|
...spelled.undeclared,
|
|
786
837
|
...spelled.orphaned
|
|
@@ -817,19 +868,6 @@ function widening(report) {
|
|
|
817
868
|
function failed(report, max) {
|
|
818
869
|
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;
|
|
819
870
|
}
|
|
820
|
-
function pushUnknown(lines, { node, causes, tests, known }, shown) {
|
|
821
|
-
lines.push(known === undefined ? node : `${node} (known: ${known})`);
|
|
822
|
-
for (const { cause, at, fix } of causes) {
|
|
823
|
-
const first = shown.get(fix);
|
|
824
|
-
if (first === undefined)
|
|
825
|
-
shown.set(fix, node);
|
|
826
|
-
lines.push(` ✗ ${cause}${at ? ` ${at}` : ""}`, ` fix: ${first === undefined ? fix : `as for ${first}`}`);
|
|
827
|
-
}
|
|
828
|
-
if (tests.length > 0) {
|
|
829
|
-
const more = tests.length > 3 ? `, and ${tests.length - 3} more` : "";
|
|
830
|
-
lines.push(` runs on any change: ${tests.slice(0, 3).join(", ")}${more}`);
|
|
831
|
-
}
|
|
832
|
-
}
|
|
833
871
|
function categoryOf(cause) {
|
|
834
872
|
if (cause.startsWith("unresolved ")) {
|
|
835
873
|
const specifier = QUOTED.exec(cause)?.[1] ?? "";
|
|
@@ -851,13 +889,9 @@ function categoryOf(cause) {
|
|
|
851
889
|
return cause;
|
|
852
890
|
}
|
|
853
891
|
var POSITION = /(?: in <>)? at \d+.*$/;
|
|
854
|
-
var BLANK_LINES = /\n{3,}/g;
|
|
855
892
|
function byCount(counts) {
|
|
856
893
|
return Object.fromEntries([...counts].sort(([a, x], [b, y]) => y - x || (a < b ? -1 : 1)));
|
|
857
894
|
}
|
|
858
|
-
function counted(counts) {
|
|
859
|
-
return Object.entries(counts).map(([what, count]) => `${count} ${what}`);
|
|
860
|
-
}
|
|
861
895
|
function summarize(report) {
|
|
862
896
|
const widen = widening(report);
|
|
863
897
|
const causes = new Map;
|
|
@@ -887,172 +921,321 @@ function summarize(report) {
|
|
|
887
921
|
unreached: report.unreached.length
|
|
888
922
|
};
|
|
889
923
|
}
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
}
|
|
898
|
-
if (counts.unknownNoTestReaches > 0) {
|
|
899
|
-
lines.push(`${plural(counts.unknownNoTestReaches, "more node")} with unknown dependencies no test reaches`);
|
|
900
|
-
}
|
|
901
|
-
if (counts.fallbacks > 0) {
|
|
902
|
-
lines.push(`${plural(counts.fallbacks, "file")} read by the regex extractor: ${counted(counts.fallbackReasons).join(", ")}`);
|
|
903
|
-
}
|
|
904
|
-
const problems = [
|
|
905
|
-
[
|
|
906
|
-
counts.unparsedTsconfigs,
|
|
907
|
-
"tsconfig file that doesn't parse",
|
|
908
|
-
"tsconfig files that don't parse"
|
|
909
|
-
],
|
|
910
|
-
[counts.deadGlobs, "dead glob", "dead globs"],
|
|
911
|
-
[
|
|
912
|
-
counts.staleDeadGlobs,
|
|
913
|
-
"stale `deadGlobs` entry",
|
|
914
|
-
"stale `deadGlobs` entries"
|
|
915
|
-
],
|
|
916
|
-
[
|
|
917
|
-
counts.unlistedUnknowns,
|
|
918
|
-
"unknown not in `knownUnknowns`",
|
|
919
|
-
"unknowns not in `knownUnknowns`"
|
|
920
|
-
],
|
|
921
|
-
[
|
|
922
|
-
counts.staleKnownUnknowns,
|
|
923
|
-
"stale `knownUnknowns` entry",
|
|
924
|
-
"stale `knownUnknowns` entries"
|
|
925
|
-
],
|
|
926
|
-
[
|
|
927
|
-
counts.undeclaredPaths,
|
|
928
|
-
"path spelled in code that nothing declares",
|
|
929
|
-
"paths spelled in code that nothing declares"
|
|
930
|
-
],
|
|
931
|
-
[
|
|
932
|
-
counts.unreached,
|
|
933
|
-
"file under a suite's root that no test reaches",
|
|
934
|
-
"files under a suite's root that no test reaches"
|
|
935
|
-
]
|
|
936
|
-
];
|
|
937
|
-
for (const [count, one, many] of problems) {
|
|
938
|
-
if (count > 0)
|
|
939
|
-
lines.push(plural(count, one, many));
|
|
940
|
-
}
|
|
941
|
-
return lines.length === 0 ? [] : ["Summary:", ...lines.map((line) => ` ${line}`), ""];
|
|
942
|
-
}
|
|
943
|
-
var UNREACHED_SHOWN = 10;
|
|
944
|
-
function pushUnreached(lines, unreached) {
|
|
945
|
-
if (unreached.length === 0)
|
|
946
|
-
return;
|
|
947
|
-
lines.push("", "Files under a suite's root that no test reaches: a change to one runs the whole suite:");
|
|
948
|
-
for (const suite of new Set(unreached.map((entry) => entry.suite))) {
|
|
949
|
-
const entries = unreached.filter((entry) => entry.suite === suite);
|
|
950
|
-
lines.push(` ${suite} (${entries.length})`);
|
|
951
|
-
for (const { file, fix } of entries.slice(0, UNREACHED_SHOWN)) {
|
|
952
|
-
lines.push(` ${file}`, ` fix: ${fix}`);
|
|
953
|
-
}
|
|
954
|
-
const rest = entries.slice(UNREACHED_SHOWN);
|
|
955
|
-
if (rest.length === 0)
|
|
956
|
-
continue;
|
|
957
|
-
const dirs = new Map;
|
|
958
|
-
for (const { file } of rest) {
|
|
959
|
-
const dir = posix2.dirname(file);
|
|
960
|
-
dirs.set(dir, (dirs.get(dir) ?? 0) + 1);
|
|
961
|
-
}
|
|
962
|
-
const counts = [...dirs].sort(([a, x], [b, y]) => y - x || (a < b ? -1 : 1)).map(([dir, count]) => `${dir}/ (${count})`);
|
|
963
|
-
lines.push(` … ${rest.length} more in ${counts.slice(0, 5).join(", ")}${counts.length > 5 ? ", …" : ""} (--json lists each)`);
|
|
924
|
+
var SHOWN = 3;
|
|
925
|
+
var BRANCH_WIDTH = " ├ ".length;
|
|
926
|
+
function sharedFix(examples) {
|
|
927
|
+
const counts = new Map;
|
|
928
|
+
for (const { fix } of examples) {
|
|
929
|
+
if (fix !== undefined)
|
|
930
|
+
counts.set(fix, (counts.get(fix) ?? 0) + 1);
|
|
964
931
|
}
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
if (spelled.length === 0)
|
|
932
|
+
const [top] = [...counts].sort(([, a], [, b]) => b - a);
|
|
933
|
+
if (top === undefined)
|
|
968
934
|
return;
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
}
|
|
978
|
-
function formatReport(report, max) {
|
|
979
|
-
const lines = summaryLines(report);
|
|
980
|
-
const widen = widening(report);
|
|
981
|
-
const shown = new Map;
|
|
982
|
-
for (const unknown of widen)
|
|
983
|
-
pushUnknown(lines, unknown, shown);
|
|
984
|
-
const idle = report.unknown.filter((node) => node.tests.length === 0);
|
|
985
|
-
if (idle.length > 0) {
|
|
986
|
-
lines.push("", "Unknown, but no test depends on them, so they widen nothing:");
|
|
987
|
-
for (const unknown of idle)
|
|
988
|
-
pushUnknown(lines, unknown, shown);
|
|
989
|
-
}
|
|
990
|
-
if (report.fallbacks.length > 0) {
|
|
991
|
-
lines.push("", "Read by the regex extractor, which the lexer couldn't read:");
|
|
992
|
-
for (const { file, reason } of report.fallbacks) {
|
|
993
|
-
lines.push(` ${file}: ${reason}`);
|
|
994
|
-
}
|
|
995
|
-
}
|
|
996
|
-
pushUndeclared(lines, report.undeclaredPaths);
|
|
997
|
-
pushUnreached(lines, report.unreached);
|
|
998
|
-
if (report.unparsedTsconfigs.length > 0) {
|
|
999
|
-
lines.push("", "tsconfig files that don't parse, whose `paths` are ignored:");
|
|
1000
|
-
for (const { file, reason } of report.unparsedTsconfigs) {
|
|
1001
|
-
lines.push(` ${file}: ${reason}`);
|
|
1002
|
-
}
|
|
935
|
+
return top[1] > 1 || examples.length === 1 ? top[0] : undefined;
|
|
936
|
+
}
|
|
937
|
+
function fixLines({ paint, all }, first, fix) {
|
|
938
|
+
const label = `${first}${paint.good("fix")} `;
|
|
939
|
+
if (all) {
|
|
940
|
+
return {
|
|
941
|
+
lines: wrapped(fix, label, " ".repeat(textWidth(label))),
|
|
942
|
+
cut: false
|
|
943
|
+
};
|
|
1003
944
|
}
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
945
|
+
const shown = truncateWords(fix, MEASURE - textWidth(label));
|
|
946
|
+
return { lines: [`${label}${shown}`], cut: shown !== fix };
|
|
947
|
+
}
|
|
948
|
+
function renderSection(view, { title, count, examples }) {
|
|
949
|
+
const { paint, all } = view;
|
|
950
|
+
const lines = [`${paint.bold(title)}${paint.dim(`${SEPARATOR}${count}`)}`];
|
|
951
|
+
const shared = sharedFix(examples);
|
|
952
|
+
const shown = all ? examples : examples.slice(0, SHOWN);
|
|
953
|
+
const hidden = examples.length - shown.length;
|
|
954
|
+
let cut = hidden > 0;
|
|
955
|
+
const detailWidth = Math.min(40, Math.max(0, ...shown.map(({ detail }) => textWidth(detail ?? ""))));
|
|
956
|
+
const placeMax = Math.max(30, MEASURE - BRANCH_WIDTH - (detailWidth > 0 ? detailWidth + 2 : 0));
|
|
957
|
+
const places = shown.map(({ place }) => {
|
|
958
|
+
const fitted = all ? place : truncatePath(place, placeMax);
|
|
959
|
+
cut ||= fitted !== place;
|
|
960
|
+
return fitted;
|
|
961
|
+
});
|
|
962
|
+
const placeWidth = Math.max(0, ...places.map(textWidth));
|
|
963
|
+
for (const [i, example] of shown.entries()) {
|
|
964
|
+
const glyph = paint.dim(branch(i, hidden > 0 ? shown.length + 1 : shown.length));
|
|
965
|
+
const place = places[i] ?? "";
|
|
966
|
+
let detail = example.detail ?? "";
|
|
967
|
+
if (detail !== "" && !all) {
|
|
968
|
+
const room = MEASURE - BRANCH_WIDTH - placeWidth - 2;
|
|
969
|
+
const fitted = truncateWords(detail, room);
|
|
970
|
+
cut ||= fitted !== detail;
|
|
971
|
+
detail = fitted;
|
|
1012
972
|
}
|
|
1013
|
-
}
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
973
|
+
lines.push(detail === "" ? ` ${glyph}${place}` : ` ${glyph}${place.padEnd(placeWidth)} ${paint.dim(detail)}`);
|
|
974
|
+
if (example.fix !== undefined && example.fix !== shared) {
|
|
975
|
+
const note = fixLines(view, NOTE_INDENT, example.fix);
|
|
976
|
+
lines.push(...note.lines);
|
|
977
|
+
cut ||= note.cut;
|
|
1018
978
|
}
|
|
1019
979
|
}
|
|
1020
|
-
if (
|
|
1021
|
-
lines.push(
|
|
1022
|
-
|
|
1023
|
-
|
|
980
|
+
if (hidden > 0) {
|
|
981
|
+
lines.push(` ${paint.dim(`└ ${hidden} more`)}`);
|
|
982
|
+
}
|
|
983
|
+
if (shared !== undefined) {
|
|
984
|
+
const line = fixLines(view, " ", shared);
|
|
985
|
+
lines.push(...line.lines);
|
|
986
|
+
cut ||= line.cut;
|
|
987
|
+
}
|
|
988
|
+
return { lines, cut };
|
|
989
|
+
}
|
|
990
|
+
var UNRESOLVED = "unresolved ";
|
|
991
|
+
function countOf(hits, files, noun) {
|
|
992
|
+
return hits === files && noun !== "import" ? plural(files, "file") : `${plural(hits, noun)} in ${plural(files, "file")}`;
|
|
993
|
+
}
|
|
994
|
+
function causeSections(widen) {
|
|
995
|
+
const byCategory = new Map;
|
|
996
|
+
for (const { node, causes, known } of widen) {
|
|
997
|
+
for (const { cause, at, fix } of causes) {
|
|
998
|
+
const category = categoryOf(cause);
|
|
999
|
+
const named = cause.startsWith(UNRESOLVED) ? cause.slice(UNRESOLVED.length) : cause === category ? "" : cause;
|
|
1000
|
+
const detail = [named, known === undefined ? "" : `known: ${known}`].filter((part) => part !== "").join(SEPARATOR);
|
|
1001
|
+
const example = { place: at ?? node, fix };
|
|
1002
|
+
if (detail !== "")
|
|
1003
|
+
example.detail = detail;
|
|
1004
|
+
const hits = byCategory.get(category) ?? [];
|
|
1005
|
+
hits.push({ node, example });
|
|
1006
|
+
byCategory.set(category, hits);
|
|
1024
1007
|
}
|
|
1025
1008
|
}
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1009
|
+
return [...byCategory].map(([category, hits]) => {
|
|
1010
|
+
const files = new Set(hits.map(({ node }) => node)).size;
|
|
1011
|
+
const noun = category.includes("import") ? "import" : "place";
|
|
1012
|
+
return {
|
|
1013
|
+
files,
|
|
1014
|
+
section: {
|
|
1015
|
+
title: category,
|
|
1016
|
+
count: countOf(hits.length, files, noun),
|
|
1017
|
+
examples: hits.map(({ example }) => example)
|
|
1018
|
+
}
|
|
1019
|
+
};
|
|
1020
|
+
}).sort((a, b) => b.files - a.files || (a.section.title < b.section.title ? -1 : 1)).map(({ section }) => section);
|
|
1021
|
+
}
|
|
1022
|
+
var filesIn = (entries) => new Set(entries.map(({ file }) => file)).size;
|
|
1023
|
+
function otherSections(report) {
|
|
1024
|
+
const sections = [];
|
|
1025
|
+
const add = (section) => {
|
|
1026
|
+
if (section.examples.length > 0)
|
|
1027
|
+
sections.push(section);
|
|
1028
|
+
};
|
|
1029
|
+
const spelled = report.undeclaredPaths;
|
|
1030
|
+
add({
|
|
1031
|
+
title: "path spelled in code that nothing declares",
|
|
1032
|
+
count: `${plural(spelled.length, "path")} in ${plural(filesIn(spelled), "file")}`,
|
|
1033
|
+
examples: spelled.map(({ file, path, dir, at, fix }) => ({
|
|
1034
|
+
place: at ?? file,
|
|
1035
|
+
detail: dir ? `${path}/` : path,
|
|
1036
|
+
fix
|
|
1037
|
+
}))
|
|
1038
|
+
});
|
|
1039
|
+
const dead = report.deadGlobs;
|
|
1040
|
+
add({
|
|
1041
|
+
title: "dead glob: a change to what it matches selects nothing",
|
|
1042
|
+
count: `${plural(dead.length, "glob")} in ${plural(new Set(dead.map(({ node }) => node)).size, "file")}`,
|
|
1043
|
+
examples: dead.map((found) => ({
|
|
1044
|
+
place: found.at ?? found.node,
|
|
1045
|
+
detail: `${found.kind} ${found.glob}: ${summary(found)}`,
|
|
1046
|
+
fix: found.fix
|
|
1047
|
+
}))
|
|
1048
|
+
});
|
|
1049
|
+
add({
|
|
1050
|
+
title: "`deadGlobs` entry that matches no dead glob",
|
|
1051
|
+
count: plural(report.staleDeadGlobs.length, "entry", "entries"),
|
|
1052
|
+
examples: report.staleDeadGlobs.map(({ file, glob, fix }) => ({
|
|
1053
|
+
place: file,
|
|
1054
|
+
detail: glob,
|
|
1055
|
+
fix
|
|
1056
|
+
}))
|
|
1057
|
+
});
|
|
1058
|
+
add({
|
|
1059
|
+
title: "unknown not in `knownUnknowns`",
|
|
1060
|
+
count: plural(report.unlistedUnknowns.length, "file"),
|
|
1061
|
+
examples: report.unlistedUnknowns.map((node) => ({
|
|
1062
|
+
place: node,
|
|
1063
|
+
fix: "make its dependencies known, or list it in `knownUnknowns` with a reason"
|
|
1064
|
+
}))
|
|
1065
|
+
});
|
|
1066
|
+
add({
|
|
1067
|
+
title: "`knownUnknowns` entry whose dependencies are known",
|
|
1068
|
+
count: plural(report.staleKnownUnknowns.length, "entry", "entries"),
|
|
1069
|
+
examples: report.staleKnownUnknowns.map(({ file, reason, fix }) => ({
|
|
1070
|
+
place: file,
|
|
1071
|
+
detail: reason,
|
|
1072
|
+
fix
|
|
1073
|
+
}))
|
|
1074
|
+
});
|
|
1075
|
+
for (const suite of new Set(report.unreached.map(({ suite }) => suite))) {
|
|
1076
|
+
const entries = report.unreached.filter((entry) => entry.suite === suite);
|
|
1077
|
+
add({
|
|
1078
|
+
title: "no test reaches: a change runs the whole suite",
|
|
1079
|
+
count: `${plural(entries.length, "file")} in suite ${suite}`,
|
|
1080
|
+
examples: entries.map(({ file, fix }) => ({ place: file, fix }))
|
|
1081
|
+
});
|
|
1048
1082
|
}
|
|
1049
|
-
|
|
1050
|
-
|
|
1083
|
+
add({
|
|
1084
|
+
title: "tsconfig that doesn't parse, whose `paths` are ignored",
|
|
1085
|
+
count: plural(report.unparsedTsconfigs.length, "file"),
|
|
1086
|
+
examples: report.unparsedTsconfigs.map(({ file, reason }) => ({
|
|
1087
|
+
place: file,
|
|
1088
|
+
detail: reason
|
|
1089
|
+
}))
|
|
1090
|
+
});
|
|
1091
|
+
add({
|
|
1092
|
+
title: "read by the regex extractor",
|
|
1093
|
+
count: `${plural(report.fallbacks.length, "file")} the lexer couldn't read`,
|
|
1094
|
+
examples: report.fallbacks.map(({ file, reason }) => ({
|
|
1095
|
+
place: file,
|
|
1096
|
+
detail: reason
|
|
1097
|
+
}))
|
|
1098
|
+
});
|
|
1099
|
+
const idle = report.unknown.filter((node) => node.tests.length === 0);
|
|
1100
|
+
add({
|
|
1101
|
+
title: "unknown, but no test reaches it, so it widens nothing",
|
|
1102
|
+
count: plural(idle.length, "file"),
|
|
1103
|
+
examples: idle.flatMap(({ node, causes, known }) => causes.map(({ cause, at }) => ({
|
|
1104
|
+
place: at ?? node,
|
|
1105
|
+
detail: known === undefined ? cause : `${cause}${SEPARATOR}known: ${known}`
|
|
1106
|
+
})))
|
|
1107
|
+
});
|
|
1108
|
+
return sections;
|
|
1109
|
+
}
|
|
1110
|
+
function verdict(paint, counts) {
|
|
1111
|
+
const { unknown, testsOnAnyChange: tests } = counts;
|
|
1112
|
+
if (unknown === 0) {
|
|
1113
|
+
return paint.good("bivium can follow every file a test reaches");
|
|
1114
|
+
}
|
|
1115
|
+
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`;
|
|
1116
|
+
}
|
|
1117
|
+
function context(counts, max) {
|
|
1118
|
+
const parts = [
|
|
1119
|
+
max === undefined ? "" : `at most ${max} allowed`,
|
|
1120
|
+
counts.undeclaredPaths > 0 ? plural(counts.undeclaredPaths, "undeclared path") : "",
|
|
1121
|
+
counts.deadGlobs > 0 ? plural(counts.deadGlobs, "dead glob") : "",
|
|
1122
|
+
counts.staleDeadGlobs > 0 ? plural(counts.staleDeadGlobs, "stale `deadGlobs` entry", "stale `deadGlobs` entries") : "",
|
|
1123
|
+
counts.unlistedUnknowns > 0 ? `${counts.unlistedUnknowns} not in \`knownUnknowns\`` : "",
|
|
1124
|
+
counts.staleKnownUnknowns > 0 ? plural(counts.staleKnownUnknowns, "stale `knownUnknowns` entry", "stale `knownUnknowns` entries") : "",
|
|
1125
|
+
counts.unreached > 0 ? `${plural(counts.unreached, "file")} no test reaches` : "",
|
|
1126
|
+
counts.unparsedTsconfigs > 0 ? plural(counts.unparsedTsconfigs, "tsconfig that doesn't parse", "tsconfigs that don't parse") : "",
|
|
1127
|
+
counts.fallbacks > 0 ? `${plural(counts.fallbacks, "file")} the lexer couldn't read` : "",
|
|
1128
|
+
counts.unknownNoTestReaches > 0 ? `${counts.unknownNoTestReaches} more unknown no test reaches` : ""
|
|
1129
|
+
].filter((part) => part !== "");
|
|
1130
|
+
const lines = [];
|
|
1131
|
+
for (const part of parts) {
|
|
1132
|
+
const last = lines.at(-1);
|
|
1133
|
+
if (last !== undefined && textWidth(last) + SEPARATOR.length + textWidth(part) <= MEASURE) {
|
|
1134
|
+
lines[lines.length - 1] = `${last}${SEPARATOR}${part}`;
|
|
1135
|
+
} else
|
|
1136
|
+
lines.push(part);
|
|
1051
1137
|
}
|
|
1138
|
+
return lines;
|
|
1139
|
+
}
|
|
1140
|
+
function failures(counts, max) {
|
|
1141
|
+
const parts = [
|
|
1142
|
+
[
|
|
1143
|
+
max !== undefined && counts.unknown > max ? counts.unknown : 0,
|
|
1144
|
+
`unknown, over --max-unknown ${max}`,
|
|
1145
|
+
`unknown, over --max-unknown ${max}`
|
|
1146
|
+
],
|
|
1147
|
+
[counts.unlistedUnknowns, "unlisted unknown", "unlisted unknowns"],
|
|
1148
|
+
[counts.undeclaredPaths, "undeclared path", "undeclared paths"],
|
|
1149
|
+
[counts.deadGlobs, "dead glob", "dead globs"],
|
|
1150
|
+
[
|
|
1151
|
+
counts.staleKnownUnknowns + counts.staleDeadGlobs,
|
|
1152
|
+
"stale entry",
|
|
1153
|
+
"stale entries"
|
|
1154
|
+
]
|
|
1155
|
+
];
|
|
1156
|
+
return parts.flatMap(([count, one, many]) => count > 0 ? [plural(count, one, many)] : []);
|
|
1157
|
+
}
|
|
1158
|
+
function formatReport(report, max, view) {
|
|
1159
|
+
const { paint } = view;
|
|
1160
|
+
const counts = summarize(report);
|
|
1161
|
+
const lines = [
|
|
1162
|
+
verdict(paint, counts),
|
|
1163
|
+
...context(counts, max).map((line) => paint.dim(line)),
|
|
1164
|
+
""
|
|
1165
|
+
];
|
|
1166
|
+
let cut = false;
|
|
1167
|
+
for (const section of [
|
|
1168
|
+
...causeSections(widening(report)),
|
|
1169
|
+
...otherSections(report)
|
|
1170
|
+
]) {
|
|
1171
|
+
const rendered = renderSection(view, section);
|
|
1172
|
+
lines.push(...rendered.lines, "");
|
|
1173
|
+
cut ||= rendered.cut;
|
|
1174
|
+
}
|
|
1175
|
+
const detail = `checked in ${duration(view.elapsedMs)}${cut ? `${SEPARATOR}--all for every file` : ""}`;
|
|
1176
|
+
const failed = failures(counts, max);
|
|
1177
|
+
lines.push(failed.length > 0 ? failLine(paint, failed.join(SEPARATOR), detail) : closingLine(paint, "Check passes", detail));
|
|
1052
1178
|
return lines.join(`
|
|
1053
|
-
`)
|
|
1179
|
+
`);
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
// src/help.ts
|
|
1183
|
+
var row = (p, name, does, preset) => ` ${name.padEnd(21)}${does}${preset === undefined ? "" : p.dim(`${SEPARATOR}${preset}`)}`;
|
|
1184
|
+
var example = (p, intent, command) => ` ${p.dim(`# ${intent}`)}
|
|
1185
|
+
${command}`;
|
|
1186
|
+
function usage(p) {
|
|
1187
|
+
return `${p.bold("bivium")} ${version}${p.dim(`${SEPARATOR}run only the tests your change can reach`)}
|
|
1188
|
+
|
|
1189
|
+
${p.bold("Usage")}
|
|
1190
|
+
bivium <command> [arguments] [options]
|
|
1191
|
+
|
|
1192
|
+
${p.bold("Commands")}
|
|
1193
|
+
${row(p, "plan [base]", "the tests a change can affect, per suite")}
|
|
1194
|
+
${row(p, "run [base]", "run a suite's selected tests")}
|
|
1195
|
+
${row(p, "why <test> [file]", "the chain from a change, or a file, to a test")}
|
|
1196
|
+
${row(p, "check", "what the graph can't read, and dead globs")}
|
|
1197
|
+
${row(p, "graph", "each file's test fan-in, and the graph")}
|
|
1198
|
+
|
|
1199
|
+
base defaults to the config's base, else the upstream default branch.
|
|
1200
|
+
Options go before or after the command, as --flag value or --flag=value.
|
|
1201
|
+
A path is repo-relative, relative to the current directory, or absolute.
|
|
1202
|
+
|
|
1203
|
+
${p.bold("Options")}
|
|
1204
|
+
${row(p, "--config <file>", "the config file", "bivium.config.{ts,mts,mjs,js,json}")}
|
|
1205
|
+
${row(p, "--cwd <dir>", "the repository", "the current directory")}
|
|
1206
|
+
${row(p, "--no-cache", "read every file; skip the extraction cache")}
|
|
1207
|
+
${row(p, "--json", "plan, why, check, graph: print JSON")}
|
|
1208
|
+
${row(p, "--color, --no-color", "force color on or off", "on in a terminal")}
|
|
1209
|
+
${row(p, "-h, --help", "this help")}
|
|
1210
|
+
${row(p, "-v, --version", "the version")}
|
|
1211
|
+
|
|
1212
|
+
${p.bold(" plan")}
|
|
1213
|
+
${row(p, "--list", "the files, as when piped, on a terminal too")}
|
|
1214
|
+
${row(p, "--github", "also write each suite's selection to $GITHUB_OUTPUT")}
|
|
1215
|
+
${p.bold(" run")}
|
|
1216
|
+
${row(p, "--suite <name>", "the suite to run", "the first")}
|
|
1217
|
+
${p.bold(" why")}
|
|
1218
|
+
${row(p, "--base <ref>", "the base ref", "as for plan")}
|
|
1219
|
+
${row(p, "--format <f>", "tree, chain, source or json", "tree")}
|
|
1220
|
+
${row(p, "--all", "every change's chain, not only the shortest")}
|
|
1221
|
+
${p.bold(" check")}
|
|
1222
|
+
${row(p, "--base <ref>", "the base ref", "HEAD")}
|
|
1223
|
+
${row(p, "--all", "every file, and each fix whole")}
|
|
1224
|
+
${row(p, "--max-unknown <n>", "fail past n unknown files that reach a test")}
|
|
1225
|
+
${p.bold(" graph")}
|
|
1226
|
+
${row(p, "--suite <name>", "only this suite", "every suite")}
|
|
1227
|
+
${row(p, "--top <n>", "files to list per suite", "10")}
|
|
1054
1228
|
|
|
1055
|
-
|
|
1229
|
+
${p.bold("Examples")}
|
|
1230
|
+
${example(p, "the tests this branch needs, as data", "bivium plan origin/main --json")}
|
|
1231
|
+
${example(p, "how a change to a file reaches a test", "bivium why tests/Button.test.ts src/Button.svelte --format chain")}
|
|
1232
|
+
${example(p, "fail CI when a file bivium can't follow reaches a test", "bivium check --max-unknown 0")}
|
|
1233
|
+
|
|
1234
|
+
${p.bold("For agents")}
|
|
1235
|
+
Run with --json; read ok and summary. Exit 0 done, 1 check found
|
|
1236
|
+
problems, 2 a usage, config or git error; run exits with its command's.
|
|
1237
|
+
With --json, an error is JSON too: error.kind (usage, config, git or
|
|
1238
|
+
other), error.code and error.message.`;
|
|
1056
1239
|
}
|
|
1057
1240
|
|
|
1058
1241
|
// src/why.ts
|
|
@@ -1324,31 +1507,42 @@ var LIST = 3;
|
|
|
1324
1507
|
function nameOf(node) {
|
|
1325
1508
|
return node.slice(node.indexOf("#") + 1);
|
|
1326
1509
|
}
|
|
1327
|
-
function
|
|
1328
|
-
const width = Math.max(0, ...rows.filter(([, at]) => at !== undefined).map(([
|
|
1329
|
-
return rows.map(([
|
|
1510
|
+
function aligned2(rows, paint) {
|
|
1511
|
+
const width = Math.max(0, ...rows.filter(([, , at]) => at !== undefined).map(([, t]) => t.length));
|
|
1512
|
+
return rows.map(([shown, plain, at]) => at === undefined ? shown : `${shown}${" ".repeat(width - plain.length)} ${paint.dim(at)}`);
|
|
1330
1513
|
}
|
|
1331
|
-
function tree(test, chain) {
|
|
1332
|
-
const rows = [[test, undefined]];
|
|
1514
|
+
function tree(test, chain, paint) {
|
|
1515
|
+
const rows = [[paint.bold(test), test, undefined]];
|
|
1516
|
+
const row = (indent, glyph, shown, plain) => [
|
|
1517
|
+
`${indent}${paint.dim(glyph)}${shown}`,
|
|
1518
|
+
`${indent}${glyph}${plain}`
|
|
1519
|
+
];
|
|
1333
1520
|
const width = Math.max(0, ...chain.links.filter((link) => link.kind !== "unknown").map((link) => link.kind.length));
|
|
1334
1521
|
let indent = "";
|
|
1335
1522
|
for (const [i, link] of chain.links.entries()) {
|
|
1336
1523
|
const next = chain.links[i + 1]?.node ?? chain.root;
|
|
1337
1524
|
if (link.kind === "unknown") {
|
|
1338
|
-
|
|
1525
|
+
const every = "depends on every change:";
|
|
1526
|
+
rows.push([...row(indent, "└─ ", every, every), undefined]);
|
|
1339
1527
|
for (const { cause, at } of chain.unknown) {
|
|
1340
|
-
|
|
1528
|
+
const plain = `${indent} ✗ ${cause}`;
|
|
1529
|
+
rows.push([`${indent} ${paint.bad("✗")} ${cause}`, plain, at]);
|
|
1341
1530
|
}
|
|
1342
1531
|
indent += " ";
|
|
1343
|
-
rows.push([
|
|
1532
|
+
rows.push([...row(indent, "└─ ", next, next), undefined]);
|
|
1344
1533
|
indent += " ";
|
|
1345
1534
|
break;
|
|
1346
1535
|
}
|
|
1347
|
-
|
|
1536
|
+
const kind = link.kind.padEnd(width);
|
|
1537
|
+
rows.push([
|
|
1538
|
+
...row(indent, "└─ ", `${paint.faint(kind)} ${next}`, `${kind} ${next}`),
|
|
1539
|
+
link.at
|
|
1540
|
+
]);
|
|
1348
1541
|
indent += " ";
|
|
1349
1542
|
}
|
|
1350
|
-
|
|
1351
|
-
|
|
1543
|
+
const end = `${indent}● ${chain.cause}`;
|
|
1544
|
+
rows.push([end, end, undefined]);
|
|
1545
|
+
return aligned2(rows, paint);
|
|
1352
1546
|
}
|
|
1353
1547
|
function verbOf(link) {
|
|
1354
1548
|
return link.kind === "export" ? `exported as ${nameOf(link.node)}` : VERBS[link.kind];
|
|
@@ -1359,10 +1553,10 @@ function whereOf(link) {
|
|
|
1359
1553
|
return `${link.node} (${configured})`;
|
|
1360
1554
|
return link.at ?? link.node;
|
|
1361
1555
|
}
|
|
1362
|
-
function steps(links) {
|
|
1556
|
+
function steps(links, paint) {
|
|
1363
1557
|
const rows = [...links].reverse().map((link) => [verbOf(link), whereOf(link)]);
|
|
1364
1558
|
const width = Math.max(0, ...rows.map(([verb]) => verb.length));
|
|
1365
|
-
return rows.map(([verb, where], i) =>
|
|
1559
|
+
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)}`);
|
|
1366
1560
|
}
|
|
1367
1561
|
function header({ root, cause }) {
|
|
1368
1562
|
if (cause === "changed")
|
|
@@ -1371,21 +1565,24 @@ function header({ root, cause }) {
|
|
|
1371
1565
|
return `if ${root} changes`;
|
|
1372
1566
|
return `${root}: ${cause}`;
|
|
1373
1567
|
}
|
|
1374
|
-
function chained(test, chain) {
|
|
1568
|
+
function chained(test, chain, paint) {
|
|
1375
1569
|
const unknownAt = chain.links.findIndex((link) => link.kind === "unknown");
|
|
1376
1570
|
const unknownLink = chain.links[unknownAt];
|
|
1377
1571
|
if (unknownLink === undefined) {
|
|
1378
|
-
return [
|
|
1572
|
+
return [
|
|
1573
|
+
header(chain),
|
|
1574
|
+
...steps(chain.links, paint).map((step) => ` ${step}`)
|
|
1575
|
+
];
|
|
1379
1576
|
}
|
|
1380
1577
|
const node = unknownLink.node;
|
|
1381
1578
|
const where = chain.unknown.map(({ at }) => at ?? node);
|
|
1382
1579
|
const width = Math.max(0, ...where.map((at) => at.length));
|
|
1383
|
-
const lines = [`${test} runs on every change`];
|
|
1580
|
+
const lines = [`${paint.bold(test)} runs on every change`];
|
|
1384
1581
|
for (const { at, cause, fix } of chain.unknown) {
|
|
1385
|
-
lines.push(` because ${(at ?? node).padEnd(width)} ${cause}`, ` fix: ${fix}`);
|
|
1582
|
+
lines.push(` because ${paint.dim((at ?? node).padEnd(width))} ${cause}`, ` ${paint.good("fix")}: ${fix}`);
|
|
1386
1583
|
}
|
|
1387
1584
|
if (unknownAt > 0) {
|
|
1388
|
-
lines.push(` reached via ${node}`, ...steps(chain.links.slice(0, unknownAt)).map((step) => ` ${step}`));
|
|
1585
|
+
lines.push(` reached via ${node}`, ...steps(chain.links.slice(0, unknownAt), paint).map((step) => ` ${step}`));
|
|
1389
1586
|
}
|
|
1390
1587
|
return lines;
|
|
1391
1588
|
}
|
|
@@ -1393,7 +1590,7 @@ var LINE_NUMBER = /:(\d+)$/;
|
|
|
1393
1590
|
function lineNumber(at) {
|
|
1394
1591
|
return at === undefined ? undefined : LINE_NUMBER.exec(at)?.[1];
|
|
1395
1592
|
}
|
|
1396
|
-
function sourced(test, chain) {
|
|
1593
|
+
function sourced(test, chain, paint) {
|
|
1397
1594
|
const unknownAt = chain.links.findIndex((link) => link.kind === "unknown");
|
|
1398
1595
|
const unknownLink = chain.links[unknownAt];
|
|
1399
1596
|
const hops = unknownLink === undefined ? chain.links : chain.links.slice(0, unknownAt);
|
|
@@ -1402,20 +1599,20 @@ function sourced(test, chain) {
|
|
|
1402
1599
|
const gutter = " ".repeat(width);
|
|
1403
1600
|
const cited = (at, text) => {
|
|
1404
1601
|
const n = lineNumber(at);
|
|
1405
|
-
return n === undefined || text === undefined ? [] : [`${n.padStart(width)}
|
|
1602
|
+
return n === undefined || text === undefined ? [] : [`${paint.dim(`${n.padStart(width)} |`)} ${text}`];
|
|
1406
1603
|
};
|
|
1407
1604
|
const count = plural(hops.length, "hop", "hops");
|
|
1408
1605
|
const lines = unknownLink === undefined ? [
|
|
1409
|
-
`selected: ${test} ← ${chain.root} (${count}${chain.cause === "changed" ? "" : `, ${chain.cause}`})`
|
|
1606
|
+
`selected: ${paint.bold(test)} ← ${chain.root} (${count}${chain.cause === "changed" ? "" : `, ${chain.cause}`})`
|
|
1410
1607
|
] : [
|
|
1411
|
-
`selected: ${test} ← every change (${unknownAt === 0 ? "its dependencies can't be read" : `${count} to ${unknownLink.node}, whose dependencies can't be read`})`
|
|
1608
|
+
`selected: ${paint.bold(test)} ← every change (${unknownAt === 0 ? "its dependencies can't be read" : `${count} to ${unknownLink.node}, whose dependencies can't be read`})`
|
|
1412
1609
|
];
|
|
1413
1610
|
lines.push("");
|
|
1414
1611
|
for (const link of hops) {
|
|
1415
1612
|
const configured = CONFIGURED[link.kind];
|
|
1416
1613
|
const where = configured === undefined ? link.at ?? link.node : link.node;
|
|
1417
1614
|
const kind = configured ?? link.kind;
|
|
1418
|
-
lines.push(`${gutter}--> ${where} ${kind}`, ...cited(link.at, link.text));
|
|
1615
|
+
lines.push(`${gutter}${paint.dim("-->")} ${paint.dim(where)} ${paint.faint(kind)}`, ...cited(link.at, link.text));
|
|
1419
1616
|
}
|
|
1420
1617
|
if (unknownLink === undefined) {
|
|
1421
1618
|
lines.push(`${gutter}● ${chain.root}: ${chain.cause}`);
|
|
@@ -1423,7 +1620,7 @@ function sourced(test, chain) {
|
|
|
1423
1620
|
}
|
|
1424
1621
|
const node = unknownLink.node;
|
|
1425
1622
|
for (const { cause, at, text, fix } of chain.unknown) {
|
|
1426
|
-
lines.push(`${gutter}✗ ${at ?? node} ${cause}`, ...cited(at, text), `${gutter} = fix: ${fix}`);
|
|
1623
|
+
lines.push(`${gutter}${paint.bad("✗")} ${paint.dim(at ?? node)} ${cause}`, ...cited(at, text), `${gutter} = ${paint.good("fix")}: ${fix}`);
|
|
1427
1624
|
}
|
|
1428
1625
|
return lines;
|
|
1429
1626
|
}
|
|
@@ -1452,7 +1649,7 @@ function unselectedMessage(explanation) {
|
|
|
1452
1649
|
].join(`
|
|
1453
1650
|
`);
|
|
1454
1651
|
}
|
|
1455
|
-
function formatExplanation(explanation, format = "tree") {
|
|
1652
|
+
function formatExplanation(explanation, format = "tree", paint = PLAIN) {
|
|
1456
1653
|
if (explanation.kind === "full") {
|
|
1457
1654
|
return [
|
|
1458
1655
|
`The ${explanation.suite} suite runs in full:`,
|
|
@@ -1464,60 +1661,17 @@ function formatExplanation(explanation, format = "tree") {
|
|
|
1464
1661
|
return unselectedMessage(explanation);
|
|
1465
1662
|
const { test, more, others } = explanation;
|
|
1466
1663
|
const render = RENDER[format];
|
|
1467
|
-
const lines = render(test, explanation);
|
|
1468
|
-
if (more.length > 0)
|
|
1469
|
-
lines.push(moreLine(more, others.length > 0));
|
|
1664
|
+
const lines = render(test, explanation, paint);
|
|
1665
|
+
if (more.length > 0) {
|
|
1666
|
+
lines.push(paint.dim(moreLine(more, others.length > 0)));
|
|
1667
|
+
}
|
|
1470
1668
|
for (const other of others)
|
|
1471
|
-
lines.push("", ...render(test, other));
|
|
1669
|
+
lines.push("", ...render(test, other, paint));
|
|
1472
1670
|
return lines.join(`
|
|
1473
1671
|
`);
|
|
1474
1672
|
}
|
|
1475
1673
|
|
|
1476
1674
|
// src/main.ts
|
|
1477
|
-
var USAGE = `Usage:
|
|
1478
|
-
bivium plan [base] [--json] [--github] print the tests a change can affect
|
|
1479
|
-
bivium run [base] [--suite <name>] run a suite's selected tests
|
|
1480
|
-
bivium why <test> [file] [--base <ref>] the chain from a change (or file) to a test
|
|
1481
|
-
[--format <f>] [--all] [--json]
|
|
1482
|
-
bivium check [--max-unknown <n>] [--json]
|
|
1483
|
-
what the graph can't read, and dead globs
|
|
1484
|
-
bivium graph [--suite <name>] [--top <n>] [--json]
|
|
1485
|
-
each file's test fan-in, and the graph
|
|
1486
|
-
bivium --version print the version
|
|
1487
|
-
bivium --help print this
|
|
1488
|
-
|
|
1489
|
-
base defaults to the config's base, else the upstream default branch.
|
|
1490
|
-
Options go before or after the command, as --flag value or --flag=value.
|
|
1491
|
-
Paths may be repo-relative, relative to the current directory, or
|
|
1492
|
-
absolute.
|
|
1493
|
-
|
|
1494
|
-
Options:
|
|
1495
|
-
--config <file> config file (default: bivium.config.{ts,mts,mjs,js,json})
|
|
1496
|
-
--cwd <dir> repository (default: the current directory)
|
|
1497
|
-
--no-cache read every file; don't read or write the extraction cache
|
|
1498
|
-
--suite <name> for run: the suite (default: the first one); for graph:
|
|
1499
|
-
only this suite (default: every one)
|
|
1500
|
-
--json for plan, why, check and graph: print JSON
|
|
1501
|
-
--github for plan: also write each suite's selection to
|
|
1502
|
-
$GITHUB_OUTPUT as <output>=all|none|<files>
|
|
1503
|
-
--base <ref> for why and check: the base ref
|
|
1504
|
-
--format <f> for why: tree (default), chain (from the change to the
|
|
1505
|
-
test), source (each hop's line of source) or json
|
|
1506
|
-
--all for why: every change's chain to the test, not only the
|
|
1507
|
-
shortest
|
|
1508
|
-
--max-unknown <n> for check: fail when more unknown nodes than this reach
|
|
1509
|
-
a test
|
|
1510
|
-
--top <n> for graph: how many files to list per suite (default: 10)
|
|
1511
|
-
|
|
1512
|
-
Exit status: 0 done; 1 check found problems; 2 a usage, configuration or
|
|
1513
|
-
git error. run exits with its command's status. With --json, an error is
|
|
1514
|
-
also printed to stdout as { "schemaVersion", "error": { "kind", "message" } },
|
|
1515
|
-
kind one of usage, config, git or other.
|
|
1516
|
-
|
|
1517
|
-
Examples:
|
|
1518
|
-
bivium plan origin/main --json
|
|
1519
|
-
bivium why tests/Button.test.ts src/Button.svelte --format chain
|
|
1520
|
-
bivium check --json`;
|
|
1521
1675
|
var WHITESPACE = /\s+/;
|
|
1522
1676
|
var VALUED = new Set([
|
|
1523
1677
|
"--config",
|
|
@@ -1528,12 +1682,12 @@ var VALUED = new Set([
|
|
|
1528
1682
|
"--max-unknown",
|
|
1529
1683
|
"--top"
|
|
1530
1684
|
]);
|
|
1531
|
-
var COMMON = ["--config", "--cwd", "--no-cache"];
|
|
1685
|
+
var COMMON = ["--config", "--cwd", "--no-cache", "--color", "--no-color"];
|
|
1532
1686
|
var FLAGS = {
|
|
1533
|
-
plan: [...COMMON, "--json", "--github"],
|
|
1687
|
+
plan: [...COMMON, "--json", "--github", "--list"],
|
|
1534
1688
|
run: [...COMMON, "--suite"],
|
|
1535
1689
|
why: [...COMMON, "--base", "--json", "--format", "--all"],
|
|
1536
|
-
check: [...COMMON, "--base", "--max-unknown", "--json"],
|
|
1690
|
+
check: [...COMMON, "--base", "--max-unknown", "--json", "--all"],
|
|
1537
1691
|
graph: [...COMMON, "--suite", "--top", "--json"]
|
|
1538
1692
|
};
|
|
1539
1693
|
var POSITIONAL = {
|
|
@@ -1543,6 +1697,33 @@ var POSITIONAL = {
|
|
|
1543
1697
|
check: 0,
|
|
1544
1698
|
graph: 0
|
|
1545
1699
|
};
|
|
1700
|
+
var PIPED = {
|
|
1701
|
+
stdout: false,
|
|
1702
|
+
stderr: false,
|
|
1703
|
+
env: {},
|
|
1704
|
+
writeError: () => {}
|
|
1705
|
+
};
|
|
1706
|
+
function colorAsked(argv) {
|
|
1707
|
+
let forced;
|
|
1708
|
+
for (const arg of argv) {
|
|
1709
|
+
if (arg === "--color" || arg === "--color=true")
|
|
1710
|
+
forced = true;
|
|
1711
|
+
if (arg === "--no-color" || arg === "--no-color=true")
|
|
1712
|
+
forced = false;
|
|
1713
|
+
}
|
|
1714
|
+
return forced;
|
|
1715
|
+
}
|
|
1716
|
+
function outFor(io, argv, startMs) {
|
|
1717
|
+
const agent = agentVariable(io.env) !== undefined;
|
|
1718
|
+
const forced = colorAsked(argv);
|
|
1719
|
+
return {
|
|
1720
|
+
io,
|
|
1721
|
+
paint: wantsColor(forced, io.stdout, io.env, agent) ? createPaint() : PLAIN,
|
|
1722
|
+
errorPaint: wantsColor(forced, io.stderr, io.env, agent) ? createPaint() : PLAIN,
|
|
1723
|
+
agent,
|
|
1724
|
+
startMs
|
|
1725
|
+
};
|
|
1726
|
+
}
|
|
1546
1727
|
var SCHEMA_VERSION = 1;
|
|
1547
1728
|
function usageError(code, message) {
|
|
1548
1729
|
return new BiviumError2(code, message);
|
|
@@ -1637,6 +1818,100 @@ function logPlan({ plan, narrowed }) {
|
|
|
1637
1818
|
logSelection(`${name} tests`, selection, total, plan.full.includes(name));
|
|
1638
1819
|
}
|
|
1639
1820
|
}
|
|
1821
|
+
function verdictOf(name, selection, total, { plan, changed }, used, paint) {
|
|
1822
|
+
const suite = Object.keys(plan.suites).length > 1 ? ` (${name})` : "";
|
|
1823
|
+
if (selection.mode === "some") {
|
|
1824
|
+
return `${paint.bold(String(selection.files.length))} of ${plural(total, "test")} run for ${plural(changed.length, "changed file")}${suite}`;
|
|
1825
|
+
}
|
|
1826
|
+
if (selection.mode === "none") {
|
|
1827
|
+
const why = changed.length === 0 ? "nothing changed" : "no change reaches a test";
|
|
1828
|
+
return `no tests run${suite}: ${why}`;
|
|
1829
|
+
}
|
|
1830
|
+
const cause = plan.full.includes(name) ? plan.causes.find((candidate) => candidate.suites.includes(name)) : undefined;
|
|
1831
|
+
if (cause !== undefined)
|
|
1832
|
+
used.add(cause);
|
|
1833
|
+
const why = plan.full.includes(name) ? cause?.message ?? "a full run" : "each reaches a change";
|
|
1834
|
+
const every = total === 1 ? "the 1 test runs" : `all ${total} tests run`;
|
|
1835
|
+
return `${every}${suite}: ${why}`;
|
|
1836
|
+
}
|
|
1837
|
+
function commonDir(files) {
|
|
1838
|
+
const [first = ""] = files;
|
|
1839
|
+
let prefix = first.slice(0, first.lastIndexOf("/") + 1);
|
|
1840
|
+
for (const file of files) {
|
|
1841
|
+
while (!file.startsWith(prefix)) {
|
|
1842
|
+
prefix = prefix.slice(0, prefix.lastIndexOf("/", prefix.length - 2) + 1);
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
return prefix;
|
|
1846
|
+
}
|
|
1847
|
+
function dirOf(file, prefix) {
|
|
1848
|
+
const slash = file.indexOf("/", prefix.length);
|
|
1849
|
+
return slash === -1 ? `${prefix}*` : file.slice(0, slash + 1);
|
|
1850
|
+
}
|
|
1851
|
+
function countTable(paint, { plan }) {
|
|
1852
|
+
const suites = Object.entries(plan.suites).flatMap(([name, selection]) => selection.mode === "some" ? [{ name, files: selection.files }] : []);
|
|
1853
|
+
const prefix = commonDir(suites.flatMap(({ files }) => files));
|
|
1854
|
+
const counts = new Map;
|
|
1855
|
+
for (const [i, { files }] of suites.entries()) {
|
|
1856
|
+
for (const file of files) {
|
|
1857
|
+
const dir = dirOf(file, prefix);
|
|
1858
|
+
const row = counts.get(dir) ?? suites.map(() => 0);
|
|
1859
|
+
row[i] = (row[i] ?? 0) + 1;
|
|
1860
|
+
counts.set(dir, row);
|
|
1861
|
+
}
|
|
1862
|
+
}
|
|
1863
|
+
if (counts.size < 2)
|
|
1864
|
+
return [];
|
|
1865
|
+
const sum = (row) => row.reduce((a, b) => a + b, 0);
|
|
1866
|
+
const rows = [...counts].sort(([a, x], [b, y]) => sum(y) - sum(x) || (a < b ? -1 : 1));
|
|
1867
|
+
const several = Object.keys(plan.suites).length > 1;
|
|
1868
|
+
const heads = several ? suites.map(({ name }) => name) : ["tests"];
|
|
1869
|
+
const right = heads.map((_, i) => i + 1);
|
|
1870
|
+
return aligned([
|
|
1871
|
+
headerRow(paint, ["directory", ...heads]),
|
|
1872
|
+
...rows.map(([dir, row]) => [truncatePath(dir, 48), ...row.map(String)]),
|
|
1873
|
+
["", ...heads.map(() => RULE)],
|
|
1874
|
+
[
|
|
1875
|
+
paint.dim(plural(rows.length, "directory", "directories")),
|
|
1876
|
+
...suites.map(({ files }) => String(files.length))
|
|
1877
|
+
]
|
|
1878
|
+
], right, paint.dim);
|
|
1879
|
+
}
|
|
1880
|
+
async function showPlan(out, base, config, computed) {
|
|
1881
|
+
const { paint } = out;
|
|
1882
|
+
const { plan, narrowed } = computed;
|
|
1883
|
+
const used = new Set;
|
|
1884
|
+
const lines = [];
|
|
1885
|
+
let planned = 0;
|
|
1886
|
+
for (const [name, selection] of Object.entries(plan.suites)) {
|
|
1887
|
+
const total = plan.totals[name] ?? 0;
|
|
1888
|
+
lines.push(verdictOf(name, selection, total, computed, used, paint));
|
|
1889
|
+
if (selection.mode === "some")
|
|
1890
|
+
planned += selection.files.length;
|
|
1891
|
+
if (selection.mode === "all")
|
|
1892
|
+
planned += total;
|
|
1893
|
+
}
|
|
1894
|
+
const mergeBase = (await computed.mergeBase()).slice(0, 7);
|
|
1895
|
+
const notes = [
|
|
1896
|
+
[
|
|
1897
|
+
`since ${base} (merge base ${mergeBase})`,
|
|
1898
|
+
...config.file ? [`config ${config.file}`] : []
|
|
1899
|
+
].join(SEPARATOR),
|
|
1900
|
+
...plan.causes.filter((cause) => !used.has(cause)).map((cause) => `full run: ${cause.message}`),
|
|
1901
|
+
...plan.packages,
|
|
1902
|
+
...plan.settings,
|
|
1903
|
+
...narrowed.flatMap(({ narrower, result }) => ("note" in result) && result.note ? [`${narrower.name}: ${result.note}`] : [])
|
|
1904
|
+
];
|
|
1905
|
+
lines.push(...notes.map((note) => paint.dim(truncateWords(note, MEASURE))));
|
|
1906
|
+
const table = countTable(paint, computed);
|
|
1907
|
+
if (table.length > 0)
|
|
1908
|
+
lines.push("", ...table);
|
|
1909
|
+
const some = Object.values(plan.suites).some(({ mode }) => mode === "some");
|
|
1910
|
+
const cost = duration(nowMs() - out.startMs);
|
|
1911
|
+
lines.push("", closingLine(paint, `Planned ${plural(planned, "test")}`, some ? `${cost}${SEPARATOR}--list for the files` : cost));
|
|
1912
|
+
console.log(lines.join(`
|
|
1913
|
+
`));
|
|
1914
|
+
}
|
|
1640
1915
|
function githubOutput() {
|
|
1641
1916
|
const output = githubOutputPath();
|
|
1642
1917
|
if (!output) {
|
|
@@ -1691,7 +1966,7 @@ function repoPath(root, arg, known) {
|
|
|
1691
1966
|
return asWritten;
|
|
1692
1967
|
return fromCwd !== undefined && known(fromCwd) ? fromCwd : asWritten;
|
|
1693
1968
|
}
|
|
1694
|
-
async function inspect(command, { flags, positional }) {
|
|
1969
|
+
async function inspect(command, { flags, positional }, out) {
|
|
1695
1970
|
const format = flags.get("--format") ?? "tree";
|
|
1696
1971
|
if (format !== "json" && !isFormat(format)) {
|
|
1697
1972
|
throw usageError("BV_BAD_OPTION_VALUE", `bivium why: --format takes tree, chain, source or json${didYouMean(format, ["tree", "chain", "source", "json"])}`);
|
|
@@ -1701,24 +1976,40 @@ async function inspect(command, { flags, positional }) {
|
|
|
1701
1976
|
if (max !== undefined && !Number.isInteger(max)) {
|
|
1702
1977
|
throw usageError("BV_BAD_OPTION_VALUE", "bivium check: --max-unknown takes a whole number");
|
|
1703
1978
|
}
|
|
1704
|
-
const [testArg
|
|
1979
|
+
const [testArg] = positional;
|
|
1705
1980
|
if (command === "why" && testArg === undefined) {
|
|
1706
1981
|
throw usageError("BV_MISSING_ARGUMENT", "bivium why: which test? bivium why <test> [file]");
|
|
1707
1982
|
}
|
|
1708
|
-
const
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1983
|
+
const progress = command === "check" && out.io.stderr && !out.agent && !flags.has("--json") ? createProgress(out.io.writeError, out.errorPaint, nowMs) : SILENT;
|
|
1984
|
+
progress.step("building the graph", 0);
|
|
1985
|
+
try {
|
|
1986
|
+
const { root, base, config } = await prepare({
|
|
1987
|
+
cwd: flags.get("--cwd"),
|
|
1988
|
+
base: flags.get("--base") ?? (command === "check" ? "HEAD" : undefined),
|
|
1989
|
+
configFile: flags.get("--config")
|
|
1990
|
+
});
|
|
1991
|
+
const computed = await computePlan(root, base, config, {
|
|
1992
|
+
...flags.has("--no-cache") && { cache: false }
|
|
1993
|
+
});
|
|
1994
|
+
if (command === "check") {
|
|
1995
|
+
const report = await check(root, config, computed, (what, done) => progress.step(what, done));
|
|
1996
|
+
progress.clear();
|
|
1997
|
+
const ok = !failed(report, max);
|
|
1998
|
+
console.log(flags.has("--json") ? json({ ok, summary: summarize(report), ...report }) : formatReport(report, max, {
|
|
1999
|
+
paint: out.paint,
|
|
2000
|
+
all: flags.has("--all"),
|
|
2001
|
+
elapsedMs: nowMs() - out.startMs
|
|
2002
|
+
}));
|
|
2003
|
+
return ok ? 0 : 1;
|
|
2004
|
+
}
|
|
2005
|
+
return await whyCommand(root, config, computed, flags, positional, out);
|
|
2006
|
+
} catch (error) {
|
|
2007
|
+
progress.clear();
|
|
2008
|
+
throw error;
|
|
1721
2009
|
}
|
|
2010
|
+
}
|
|
2011
|
+
async function whyCommand(root, config, computed, flags, [testArg, fileArg], out) {
|
|
2012
|
+
const format = flags.get("--format") ?? "tree";
|
|
1722
2013
|
const inGraph = (path) => computed.graph.has(path);
|
|
1723
2014
|
const isTest = (path) => config.suites.some((suite) => suite.isTest(path));
|
|
1724
2015
|
const test = repoPath(root, testArg ?? "", (path) => inGraph(path) && isTest(path));
|
|
@@ -1733,7 +2024,7 @@ async function inspect(command, { flags, positional }) {
|
|
|
1733
2024
|
const explanation = await explain(root, config, computed, test, file, {
|
|
1734
2025
|
all: flags.has("--all")
|
|
1735
2026
|
});
|
|
1736
|
-
console.log(flags.has("--json") || format === "json" ? json(explanation) : formatExplanation(explanation, format));
|
|
2027
|
+
console.log(flags.has("--json") || format === "json" ? json(explanation) : formatExplanation(explanation, isFormat(format) ? format : "tree", out.paint));
|
|
1737
2028
|
return 0;
|
|
1738
2029
|
}
|
|
1739
2030
|
async function graphCommand({ flags }) {
|
|
@@ -1755,7 +2046,7 @@ async function graphCommand({ flags }) {
|
|
|
1755
2046
|
console.log(flags.has("--json") ? json(report) : formatGraph(report, top));
|
|
1756
2047
|
return 0;
|
|
1757
2048
|
}
|
|
1758
|
-
async function runCommand(command, { flags, positional }) {
|
|
2049
|
+
async function runCommand(command, { flags, positional }, out) {
|
|
1759
2050
|
const output = flags.has("--github") ? githubOutput() : undefined;
|
|
1760
2051
|
const { root, base, config } = await prepare({
|
|
1761
2052
|
cwd: flags.get("--cwd"),
|
|
@@ -1793,6 +2084,8 @@ async function runCommand(command, { flags, positional }) {
|
|
|
1793
2084
|
packages,
|
|
1794
2085
|
narrowed
|
|
1795
2086
|
}));
|
|
2087
|
+
} else if (out.io.stdout && !out.agent && !flags.has("--list")) {
|
|
2088
|
+
await showPlan(out, base, config, computed);
|
|
1796
2089
|
} else {
|
|
1797
2090
|
await logContext(base, config, computed);
|
|
1798
2091
|
logPlan(computed);
|
|
@@ -1839,9 +2132,10 @@ function fail(argv, error) {
|
|
|
1839
2132
|
}
|
|
1840
2133
|
return 2;
|
|
1841
2134
|
}
|
|
1842
|
-
async function main(argv) {
|
|
2135
|
+
async function main(argv, io = PIPED) {
|
|
2136
|
+
const out = outFor(io, argv, nowMs());
|
|
1843
2137
|
if (argv.includes("--help") || argv.includes("-h")) {
|
|
1844
|
-
console.log(
|
|
2138
|
+
console.log(usage(out.paint));
|
|
1845
2139
|
return 0;
|
|
1846
2140
|
}
|
|
1847
2141
|
if (argv.includes("--version") || argv.includes("-v")) {
|
|
@@ -1851,7 +2145,7 @@ async function main(argv) {
|
|
|
1851
2145
|
const at = commandAt(argv);
|
|
1852
2146
|
const command = at === -1 ? undefined : argv[at];
|
|
1853
2147
|
if (command === "help") {
|
|
1854
|
-
console.log(
|
|
2148
|
+
console.log(usage(out.paint));
|
|
1855
2149
|
return 0;
|
|
1856
2150
|
}
|
|
1857
2151
|
if (command === undefined) {
|
|
@@ -1864,10 +2158,10 @@ async function main(argv) {
|
|
|
1864
2158
|
try {
|
|
1865
2159
|
const args = parseArgs(command, rest);
|
|
1866
2160
|
if (command === "why" || command === "check")
|
|
1867
|
-
return await inspect(command, args);
|
|
2161
|
+
return await inspect(command, args, out);
|
|
1868
2162
|
if (command === "graph")
|
|
1869
2163
|
return await graphCommand(args);
|
|
1870
|
-
return await runCommand(command, args);
|
|
2164
|
+
return await runCommand(command, args, out);
|
|
1871
2165
|
} catch (error) {
|
|
1872
2166
|
if (error instanceof BiviumError2)
|
|
1873
2167
|
return fail(argv, error);
|
|
@@ -1877,4 +2171,10 @@ async function main(argv) {
|
|
|
1877
2171
|
}
|
|
1878
2172
|
|
|
1879
2173
|
// src/cli.ts
|
|
1880
|
-
|
|
2174
|
+
var io = {
|
|
2175
|
+
stdout: process.stdout.isTTY === true,
|
|
2176
|
+
stderr: process.stderr.isTTY === true,
|
|
2177
|
+
env: environment(),
|
|
2178
|
+
writeError: (text) => process.stderr.write(text)
|
|
2179
|
+
};
|
|
2180
|
+
process.exitCode = await main(process.argv.slice(2), io);
|