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.
@@ -0,0 +1,12 @@
1
+ // src/env.ts
2
+ function githubOutputPath() {
3
+ return process.env.GITHUB_OUTPUT;
4
+ }
5
+ function nowMs() {
6
+ return Date.now();
7
+ }
8
+ function environment() {
9
+ return process.env;
10
+ }
11
+
12
+ export { githubOutputPath, nowMs, environment };
@@ -0,0 +1,149 @@
1
+ import {
2
+ PLAIN2,
3
+ createPaint2,
4
+ wantsColor2,
5
+ isAgent2
6
+ } from "./shared-a8ebfhzf.js";
7
+ import {
8
+ BiviumError2,
9
+ didYouMean2
10
+ } from "./shared-118ck8qt.js";
11
+
12
+ // src/args.ts
13
+ var VALUED = new Set([
14
+ "--config",
15
+ "--cwd",
16
+ "--suite",
17
+ "--base",
18
+ "--format",
19
+ "--max-unknown",
20
+ "--top"
21
+ ]);
22
+ var COMMON = ["--config", "--cwd", "--no-cache", "--color", "--no-color"];
23
+ var FLAGS2 = {
24
+ plan: [...COMMON, "--json", "--github", "--list"],
25
+ run: [...COMMON, "--suite"],
26
+ why: [...COMMON, "--base", "--json", "--format", "--all"],
27
+ check: [...COMMON, "--base", "--max-unknown", "--json", "--all"],
28
+ graph: [...COMMON, "--suite", "--top", "--json"]
29
+ };
30
+ var POSITIONAL = {
31
+ plan: 1,
32
+ run: 1,
33
+ why: 2,
34
+ check: 0,
35
+ graph: 0
36
+ };
37
+ function colorAsked(argv) {
38
+ let forced;
39
+ for (const arg of argv) {
40
+ if (arg === "--color" || arg === "--color=true")
41
+ forced = true;
42
+ if (arg === "--no-color" || arg === "--no-color=true")
43
+ forced = false;
44
+ }
45
+ return forced;
46
+ }
47
+ function outFor2(io, argv, startMs) {
48
+ const agent = isAgent2(io.env);
49
+ const forced = colorAsked(argv);
50
+ return {
51
+ io,
52
+ paint: wantsColor2(forced, io.stdout, io.env, agent) ? createPaint2() : PLAIN2,
53
+ errorPaint: wantsColor2(forced, io.stderr, io.env, agent) ? createPaint2() : PLAIN2,
54
+ agent,
55
+ startMs
56
+ };
57
+ }
58
+ var SCHEMA_VERSION = 1;
59
+ function usageError2(code, message) {
60
+ return new BiviumError2(code, message);
61
+ }
62
+ function unknownOption(command, flag) {
63
+ const known = FLAGS2[command] ?? [];
64
+ const others = Object.keys(FLAGS2).filter((other) => other !== command && FLAGS2[other]?.includes(flag));
65
+ const hint = others.length > 0 ? ` (${others.join(" and ")} ${others.length === 1 ? "takes" : "take"} it)` : didYouMean2(flag, known);
66
+ return usageError2("BV_UNKNOWN_OPTION", `bivium ${command}: unknown option ${flag}${hint}`);
67
+ }
68
+ function parseArgs2(command, argv) {
69
+ const known = FLAGS2[command] ?? [];
70
+ const flags = new Map;
71
+ const positional = [];
72
+ for (let i = 0;i < argv.length; i++) {
73
+ const arg = argv[i];
74
+ if (arg === undefined)
75
+ continue;
76
+ if (!arg.startsWith("-") || arg === "-") {
77
+ positional.push(arg);
78
+ continue;
79
+ }
80
+ const equals = arg.startsWith("--") ? arg.indexOf("=") : -1;
81
+ const flag = equals === -1 ? arg : arg.slice(0, equals);
82
+ if (!known.includes(flag))
83
+ throw unknownOption(command, flag);
84
+ if (!VALUED.has(flag)) {
85
+ const on = equals === -1 ? "true" : arg.slice(equals + 1);
86
+ if (on !== "true" && on !== "false") {
87
+ throw usageError2("BV_BAD_OPTION_VALUE", `bivium ${command}: ${flag} is a switch: give it alone, or as ${flag}=true or ${flag}=false`);
88
+ }
89
+ if (on === "true")
90
+ flags.set(flag, "");
91
+ continue;
92
+ }
93
+ const value = equals === -1 ? argv[++i] : arg.slice(equals + 1);
94
+ if (value === undefined || value === "" || value.startsWith("--")) {
95
+ throw usageError2("BV_BAD_OPTION_VALUE", `bivium: ${flag} needs a value`);
96
+ }
97
+ flags.set(flag, value);
98
+ }
99
+ if (positional.length > (POSITIONAL[command] ?? 0)) {
100
+ throw usageError2("BV_UNEXPECTED_ARGUMENT", `bivium ${command}: unexpected ${positional.at(-1)}`);
101
+ }
102
+ return { flags, positional };
103
+ }
104
+ function json2(value) {
105
+ return JSON.stringify({ schemaVersion: SCHEMA_VERSION, ...value }, null, 2);
106
+ }
107
+ function commandAt2(argv) {
108
+ for (let i = 0;i < argv.length; i++) {
109
+ const arg = argv[i];
110
+ if (arg === undefined)
111
+ continue;
112
+ if (!arg.startsWith("-"))
113
+ return i;
114
+ if (VALUED.has(arg))
115
+ i++;
116
+ }
117
+ return -1;
118
+ }
119
+ var COMMANDS2 = Object.keys(FLAGS2);
120
+ var EVERY_FLAG2 = [
121
+ ...new Set([...Object.values(FLAGS2).flat(), "--help", "--version"])
122
+ ];
123
+ function unknownFlag2(argv) {
124
+ for (let i = 0;i < argv.length; i++) {
125
+ const arg = argv[i];
126
+ if (arg === undefined || !arg.startsWith("-") || arg === "-")
127
+ continue;
128
+ const equals = arg.startsWith("--") ? arg.indexOf("=") : -1;
129
+ const flag = equals === -1 ? arg : arg.slice(0, equals);
130
+ if (!EVERY_FLAG2.includes(flag))
131
+ return flag;
132
+ if (VALUED.has(arg))
133
+ i++;
134
+ }
135
+ return;
136
+ }
137
+ function wantsJson(argv) {
138
+ return argv.some((arg, i) => arg === "--json" || arg === "--json=true" || arg === "--format=json" || arg === "--format" && argv[i + 1] === "json");
139
+ }
140
+ function fail2(argv, error) {
141
+ console.error(error.message);
142
+ if (wantsJson(argv)) {
143
+ const { kind, code, message } = error;
144
+ console.log(json2({ error: { kind, code, message } }));
145
+ }
146
+ return 2;
147
+ }
148
+
149
+ export { FLAGS2, outFor2, usageError2, parseArgs2, json2, commandAt2, COMMANDS2, EVERY_FLAG2, unknownFlag2, fail2 };
@@ -0,0 +1,26 @@
1
+ import {
2
+ FLAGS2,
3
+ outFor2,
4
+ usageError2,
5
+ parseArgs2,
6
+ json2,
7
+ commandAt2,
8
+ COMMANDS2,
9
+ EVERY_FLAG2,
10
+ unknownFlag2,
11
+ fail2
12
+ } from "./shared-ke52sg37.js";
13
+ import"./shared-118ck8qt.js";
14
+ import"./shared-a8ebfhzf.js";
15
+ export {
16
+ COMMANDS2 as COMMANDS,
17
+ EVERY_FLAG2 as EVERY_FLAG,
18
+ FLAGS2 as FLAGS,
19
+ commandAt2 as commandAt,
20
+ fail2 as fail,
21
+ json2 as json,
22
+ outFor2 as outFor,
23
+ parseArgs2 as parseArgs,
24
+ unknownFlag2 as unknownFlag,
25
+ usageError2 as usageError
26
+ };
@@ -0,0 +1,164 @@
1
+ import {
2
+ plural2
3
+ } from "./shared-a8ebfhzf.js";
4
+ import {
5
+ walksByTypes,
6
+ importersOf,
7
+ sorted
8
+ } from "./shared-70w84x4c.js";
9
+
10
+ // src/fanin.ts
11
+ function fanIn2(graph, suite) {
12
+ const ids = new Map;
13
+ const nodes = [...graph.keys()];
14
+ for (const [i, node] of nodes.entries())
15
+ ids.set(node, i);
16
+ const matches = new Map;
17
+ const matching = (re) => {
18
+ let found = matches.get(re.source);
19
+ if (!found) {
20
+ found = [];
21
+ for (const [i, node] of nodes.entries())
22
+ if (re.test(node))
23
+ found.push(i);
24
+ matches.set(re.source, found);
25
+ }
26
+ return found;
27
+ };
28
+ const forward = [];
29
+ const direct = [];
30
+ for (const node of nodes) {
31
+ const deps = graph.get(node);
32
+ const out = [];
33
+ const read = [];
34
+ if (deps) {
35
+ for (const file of deps.files) {
36
+ if (!suite.types && deps.types?.has(file))
37
+ continue;
38
+ const id = ids.get(file);
39
+ if (id !== undefined)
40
+ out.push(id);
41
+ }
42
+ for (const re of deps.patterns)
43
+ out.push(...matching(re));
44
+ for (const re of [...deps.reads, ...deps.lists ?? []]) {
45
+ read.push(...matching(re));
46
+ }
47
+ }
48
+ forward.push(out);
49
+ direct.push(read);
50
+ }
51
+ const counts = new Uint32Array(nodes.length);
52
+ const stamp = new Int32Array(nodes.length).fill(-1);
53
+ const stack = [];
54
+ for (const [t, test] of nodes.entries()) {
55
+ if (!suite.isTest(test))
56
+ continue;
57
+ const reached = [];
58
+ const visit = (id) => {
59
+ if (stamp[id] === t)
60
+ return;
61
+ stamp[id] = t;
62
+ reached.push(id);
63
+ };
64
+ visit(t);
65
+ stack.push(t);
66
+ for (let at = stack.pop();at !== undefined; at = stack.pop()) {
67
+ for (const next of byId(forward, at)) {
68
+ if (stamp[next] === t)
69
+ continue;
70
+ visit(next);
71
+ stack.push(next);
72
+ }
73
+ }
74
+ for (const id of [...reached]) {
75
+ for (const read of byId(direct, id))
76
+ visit(read);
77
+ }
78
+ for (const id of reached)
79
+ counts[id] = byId(counts, id) + 1;
80
+ }
81
+ const result = new Map;
82
+ for (const [i, node] of nodes.entries())
83
+ result.set(node, byId(counts, i));
84
+ return result;
85
+ }
86
+ function byId(list, id) {
87
+ const value = list[id];
88
+ if (value === undefined) {
89
+ throw new Error(`bivium: fanin: no node with id ${id}.`);
90
+ }
91
+ return value;
92
+ }
93
+ function graphReport2(graph, suites) {
94
+ const counts = suites.map((suite) => [suite.name, fanIn2(graph, suite)]);
95
+ const unknown = [...graph].filter(([, deps]) => deps.unknown.length > 0).map(([node]) => node);
96
+ const everyChange = walksByTypes(graph, unknown, {
97
+ includeUnknown: false,
98
+ readers: false
99
+ });
100
+ return {
101
+ suites: Object.fromEntries(suites.map((suite) => [
102
+ suite.name,
103
+ {
104
+ total: [...graph.keys()].filter(suite.isTest).length,
105
+ everyChange: sorted(everyChange(suite.types)).filter(suite.isTest)
106
+ }
107
+ ])),
108
+ nodes: [...graph].map(([node, deps]) => ({
109
+ node,
110
+ imports: sorted(deps.files),
111
+ importers: sorted(importersOf(graph, node)),
112
+ fanIn: Object.fromEntries(counts.map(([name, count]) => [name, count.get(node) ?? 0])),
113
+ unknown: deps.unknown.length > 0
114
+ }))
115
+ };
116
+ }
117
+ function buckets(total) {
118
+ const list = [
119
+ [0, 0],
120
+ [1, 1]
121
+ ];
122
+ for (let low = 2, high = 10;low < total; low = high + 1, high *= 10) {
123
+ list.push([low, Math.min(high, total - 1)]);
124
+ }
125
+ if (total > 1)
126
+ list.push([total, total]);
127
+ return list;
128
+ }
129
+ function formatGraph2(report, top) {
130
+ const lines = [];
131
+ const files = report.nodes.filter(({ node }) => !node.startsWith("pkg:") && !node.includes("#"));
132
+ for (const [name, { total: tests, everyChange }] of Object.entries(report.suites)) {
133
+ const any = everyChange.length;
134
+ lines.push(`${name}: ${plural2(tests, "test")}${any > 0 ? `; ${any} run on any change (\`bivium check\` says why)` : ""}`);
135
+ 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));
136
+ const width = Math.max(String(tests).length, 5);
137
+ if (ranked.length > 0) {
138
+ lines.push(` ${"tests".padStart(width)} file`);
139
+ for (const [id, count] of ranked.slice(0, top)) {
140
+ lines.push(` ${String(count).padStart(width)} ${id}`);
141
+ }
142
+ if (ranked.length > top) {
143
+ lines.push(` … ${ranked.length - top} more (--top <n> shows more)`);
144
+ }
145
+ }
146
+ lines.push(` files by how many ${name} tests a change to one selects:`);
147
+ const rows = buckets(tests).map(([low, high]) => {
148
+ const label = low === high ? String(low) : `${low}–${high}`;
149
+ const count = files.filter(({ fanIn: reach }) => {
150
+ const n = reach[name] ?? 0;
151
+ return n >= low && n <= high;
152
+ }).length;
153
+ return [label, count];
154
+ });
155
+ const labelWidth = Math.max(...rows.map(([label]) => label.length));
156
+ for (const [label, count] of rows) {
157
+ lines.push(` ${label.padStart(labelWidth)} ${count}`);
158
+ }
159
+ }
160
+ return lines.join(`
161
+ `);
162
+ }
163
+
164
+ export { fanIn2, graphReport2, formatGraph2 };
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  BiviumError2,
3
3
  didYouMean2
4
- } from "./shared-211m8fjt.js";
4
+ } from "./shared-118ck8qt.js";
5
5
 
6
6
  // src/config.ts
7
7
  import { existsSync, readFileSync } from "node:fs";
@@ -0,0 +1,19 @@
1
+ import {
2
+ fixFor2,
3
+ check2,
4
+ failed2,
5
+ summarize2,
6
+ formatReport2
7
+ } from "./shared-byhfswm9.js";
8
+ import"./shared-my87dhfd.js";
9
+ import"./shared-yfvpkeff.js";
10
+ import"./shared-px5a17nj.js";
11
+ import"./shared-118ck8qt.js";
12
+ import"./shared-a8ebfhzf.js";
13
+ export {
14
+ check2 as check,
15
+ failed2 as failed,
16
+ fixFor2 as fixFor,
17
+ formatReport2 as formatReport,
18
+ summarize2 as summarize
19
+ };