bivium 0.7.0 → 0.8.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/cli.js CHANGED
@@ -1,15 +1,18 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
- version,
3
+ BiviumError2,
4
+ didYouMean2
5
+ } from "./shared-211m8fjt.js";
6
+ import {
7
+ globToRegExp,
8
+ inRoot2
9
+ } from "./shared-550cvwt2.js";
10
+ import {
4
11
  githubOutputPath,
5
12
  nowMs,
6
13
  environment,
7
14
  agentVariable,
8
- BiviumError2,
9
- didYouMean,
10
- globToRegExp,
11
- inRoot,
12
- sorted,
15
+ version,
13
16
  QUERY,
14
17
  UNDECLARED_READS,
15
18
  RELATIVE,
@@ -24,11 +27,11 @@ import {
24
27
  walksByTypes,
25
28
  importersOf,
26
29
  affectedBy,
27
- formatSelection,
28
- orphanOf,
30
+ sorted,
29
31
  PLAIN,
30
32
  createPaint,
31
33
  wantsColor,
34
+ isDumb,
32
35
  SEPARATOR,
33
36
  branch,
34
37
  NOTE_INDENT,
@@ -44,9 +47,11 @@ import {
44
47
  plural,
45
48
  SILENT,
46
49
  createProgress,
47
- computePlan,
48
- prepare
49
- } from "./shared.js";
50
+ formatSelection,
51
+ orphanOf,
52
+ computePlan2,
53
+ prepare2
54
+ } from "./shared-9qcbzxw3.js";
50
55
 
51
56
  // src/main.ts
52
57
  import { spawnSync } from "node:child_process";
@@ -439,7 +444,7 @@ function spellers(config, { graph }) {
439
444
  continue;
440
445
  if (node.startsWith("pkg:") || node.includes("#"))
441
446
  continue;
442
- if (suite.isTest(node) || (reached.get(node) ?? 0) > 0 && suite.roots.some((root) => inRoot(node, root))) {
447
+ if (suite.isTest(node) || (reached.get(node) ?? 0) > 0 && suite.roots.some((root) => inRoot2(node, root))) {
443
448
  const suites = found.get(node);
444
449
  suites ? suites.push(suite) : found.set(node, [suite]);
445
450
  }
@@ -1731,7 +1736,7 @@ function usageError(code, message) {
1731
1736
  function unknownOption(command, flag) {
1732
1737
  const known = FLAGS[command] ?? [];
1733
1738
  const others = Object.keys(FLAGS).filter((other) => other !== command && FLAGS[other]?.includes(flag));
1734
- const hint = others.length > 0 ? ` (${others.join(" and ")} ${others.length === 1 ? "takes" : "take"} it)` : didYouMean(flag, known);
1739
+ const hint = others.length > 0 ? ` (${others.join(" and ")} ${others.length === 1 ? "takes" : "take"} it)` : didYouMean2(flag, known);
1735
1740
  return usageError("BV_UNKNOWN_OPTION", `bivium ${command}: unknown option ${flag}${hint}`);
1736
1741
  }
1737
1742
  function parseArgs(command, argv) {
@@ -1940,7 +1945,7 @@ function findSuite(config, name) {
1940
1945
  if (suite)
1941
1946
  return suite;
1942
1947
  const names = config.suites.map((candidate) => candidate.name);
1943
- throw usageError("BV_UNKNOWN_SUITE", `bivium: no suite "${name}"${didYouMean(name, names)}; the config has ${names.join(", ")}`);
1948
+ throw usageError("BV_UNKNOWN_SUITE", `bivium: no suite "${name}"${didYouMean2(name, names)}; the config has ${names.join(", ")}`);
1944
1949
  }
1945
1950
  function real(path) {
1946
1951
  try {
@@ -1969,7 +1974,7 @@ function repoPath(root, arg, known) {
1969
1974
  async function inspect(command, { flags, positional }, out) {
1970
1975
  const format = flags.get("--format") ?? "tree";
1971
1976
  if (format !== "json" && !isFormat(format)) {
1972
- throw usageError("BV_BAD_OPTION_VALUE", `bivium why: --format takes tree, chain, source or json${didYouMean(format, ["tree", "chain", "source", "json"])}`);
1977
+ throw usageError("BV_BAD_OPTION_VALUE", `bivium why: --format takes tree, chain, source or json${didYouMean2(format, ["tree", "chain", "source", "json"])}`);
1973
1978
  }
1974
1979
  const maxFlag = flags.get("--max-unknown");
1975
1980
  const max = maxFlag === undefined ? undefined : Number(maxFlag);
@@ -1980,15 +1985,15 @@ async function inspect(command, { flags, positional }, out) {
1980
1985
  if (command === "why" && testArg === undefined) {
1981
1986
  throw usageError("BV_MISSING_ARGUMENT", "bivium why: which test? bivium why <test> [file]");
1982
1987
  }
1983
- const progress = command === "check" && out.io.stderr && !out.agent && !flags.has("--json") ? createProgress(out.io.writeError, out.errorPaint, nowMs) : SILENT;
1988
+ const progress = command === "check" && out.io.stderr && !isDumb(out.io.env) && !out.agent && !flags.has("--json") ? createProgress(out.io.writeError, out.errorPaint, nowMs) : SILENT;
1984
1989
  progress.step("building the graph", 0);
1985
1990
  try {
1986
- const { root, base, config } = await prepare({
1991
+ const { root, base, config } = await prepare2({
1987
1992
  cwd: flags.get("--cwd"),
1988
1993
  base: flags.get("--base") ?? (command === "check" ? "HEAD" : undefined),
1989
1994
  configFile: flags.get("--config")
1990
1995
  });
1991
- const computed = await computePlan(root, base, config, {
1996
+ const computed = await computePlan2(root, base, config, {
1992
1997
  ...flags.has("--no-cache") && { cache: false }
1993
1998
  });
1994
1999
  if (command === "check") {
@@ -2016,10 +2021,10 @@ async function whyCommand(root, config, computed, flags, [testArg, fileArg], out
2016
2021
  const file = fileArg === undefined ? undefined : repoPath(root, fileArg, inGraph);
2017
2022
  if (!isTest(test) || !inGraph(test)) {
2018
2023
  const tests = [...computed.graph.keys()].filter(isTest);
2019
- throw usageError(inGraph(test) ? "BV_UNKNOWN_TEST" : "BV_UNKNOWN_FILE", inGraph(test) ? `bivium why: ${test} is no suite's test: name a test first, as in bivium why <test> ${test}` : `bivium why: no test ${test}${didYouMean(test, tests)}`);
2024
+ throw usageError(inGraph(test) ? "BV_UNKNOWN_TEST" : "BV_UNKNOWN_FILE", inGraph(test) ? `bivium why: ${test} is no suite's test: name a test first, as in bivium why <test> ${test}` : `bivium why: no test ${test}${didYouMean2(test, tests)}`);
2020
2025
  }
2021
2026
  if (file !== undefined && !inGraph(file)) {
2022
- throw usageError("BV_UNKNOWN_FILE", `bivium why: no file ${file}${didYouMean(file, computed.graph.keys())}`);
2027
+ throw usageError("BV_UNKNOWN_FILE", `bivium why: no file ${file}${didYouMean2(file, computed.graph.keys())}`);
2023
2028
  }
2024
2029
  const explanation = await explain(root, config, computed, test, file, {
2025
2030
  all: flags.has("--all")
@@ -2032,14 +2037,14 @@ async function graphCommand({ flags }) {
2032
2037
  if (!Number.isInteger(top) || top < 0) {
2033
2038
  throw usageError("BV_BAD_OPTION_VALUE", "bivium graph: --top takes a whole number");
2034
2039
  }
2035
- const { root, base, config } = await prepare({
2040
+ const { root, base, config } = await prepare2({
2036
2041
  cwd: flags.get("--cwd"),
2037
2042
  base: "HEAD",
2038
2043
  configFile: flags.get("--config")
2039
2044
  });
2040
2045
  const one = findSuite(config, flags.get("--suite"));
2041
2046
  const suites = one ? [one] : config.suites;
2042
- const { graph } = await computePlan(root, base, config, {
2047
+ const { graph } = await computePlan2(root, base, config, {
2043
2048
  ...flags.has("--no-cache") && { cache: false }
2044
2049
  });
2045
2050
  const report = graphReport(graph, suites);
@@ -2048,7 +2053,7 @@ async function graphCommand({ flags }) {
2048
2053
  }
2049
2054
  async function runCommand(command, { flags, positional }, out) {
2050
2055
  const output = flags.has("--github") ? githubOutput() : undefined;
2051
- const { root, base, config } = await prepare({
2056
+ const { root, base, config } = await prepare2({
2052
2057
  cwd: flags.get("--cwd"),
2053
2058
  base: positional[0],
2054
2059
  configFile: flags.get("--config")
@@ -2060,7 +2065,7 @@ async function runCommand(command, { flags, positional }, out) {
2060
2065
  if (command === "run" && !suite.run) {
2061
2066
  throw usageError("BV_NO_RUN_COMMAND", `bivium run: suite "${suite.name}" has no run command: add suites.${suite.name}.run to the config`);
2062
2067
  }
2063
- const computed = await computePlan(root, base, config, {
2068
+ const computed = await computePlan2(root, base, config, {
2064
2069
  ...flags.has("--no-cache") && { cache: false },
2065
2070
  ...command === "run" && { suites: [suite.name] },
2066
2071
  mergeBase: true
@@ -2094,19 +2099,68 @@ async function runCommand(command, { flags, positional }, out) {
2094
2099
  writeOutputs(output, config, computed);
2095
2100
  return 0;
2096
2101
  }
2097
- await logContext(base, config, computed);
2098
- logPlan(computed);
2102
+ const human = out.io.stdout && !out.agent;
2103
+ const others = flags.has("--suite") ? [] : config.suites.slice(1).map((s) => `--suite=${s.name}`);
2104
+ if (human)
2105
+ await showRun(out, base, config, computed, suite.name, others);
2106
+ else {
2107
+ await logContext(base, config, computed);
2108
+ logPlan(computed);
2109
+ }
2099
2110
  const selection = computed.plan.suites[suite.name];
2100
2111
  if (selection === undefined || selection.mode === "none") {
2101
- console.log(`No ${suite.name} tests affected against ${base}; skipping.`);
2112
+ if (human) {
2113
+ console.log(closingLine(out.paint, `Nothing to run for ${suite.name}`));
2114
+ } else {
2115
+ console.log(`No ${suite.name} tests affected against ${base}; skipping.`);
2116
+ }
2102
2117
  return 0;
2103
2118
  }
2104
- const [program, ...args] = commandFor(suite.run ?? "", selection.mode === "some" ? selection.files : []);
2119
+ const [program, ...args] = selection.mode === "some" ? commandFor(suite.run ?? "", selection.files) : suite.runAll !== undefined ? commandFor(suite.runAll, []) : commandFor(suite.run ?? "", suite.roots.filter((root) => root !== "."));
2105
2120
  if (program === undefined) {
2106
2121
  throw usageError("BV_NO_RUN_COMMAND", `bivium run: suite "${suite.name}" has no run command: add suites.${suite.name}.run to the config`);
2107
2122
  }
2123
+ if (human) {
2124
+ const files = selection.mode === "some" ? selection.files.length : 0;
2125
+ const bare = commandFor(suite.run ?? "", []);
2126
+ console.log(`${out.paint.dim(shownCommand([program, ...args], bare, files))}
2127
+ `);
2128
+ }
2129
+ const started = nowMs();
2108
2130
  const result = spawnSync(program, args, { cwd: root, stdio: "inherit" });
2109
- return result.status ?? 1;
2131
+ const interrupted = result.signal === "SIGINT";
2132
+ const status = interrupted ? 130 : result.status ?? 1;
2133
+ if (human) {
2134
+ const took = duration(nowMs() - started);
2135
+ const name = suite.name;
2136
+ console.log(`
2137
+ ${interrupted ? failLine(out.paint, `${name} interrupted`, took) : status === 0 ? closingLine(out.paint, `${name} passed`, took) : failLine(out.paint, `${name} failed`, `${result.signal ?? `exit ${status}`}${SEPARATOR}${took}`)}`);
2138
+ }
2139
+ return status;
2140
+ }
2141
+ function shownCommand(whole, bare, files) {
2142
+ const line = `$ ${whole.join(" ")}`;
2143
+ if (textWidth(line) <= MEASURE)
2144
+ return line;
2145
+ return `$ ${bare.join(" ")} ${SEPARATOR.trim()} ${plural(files, "file")}, as bivium plan --list names them`;
2146
+ }
2147
+ async function showRun(out, base, config, computed, name, others) {
2148
+ const { paint } = out;
2149
+ const selection = computed.plan.suites[name];
2150
+ const total = computed.plan.totals[name] ?? 0;
2151
+ const used = new Set;
2152
+ const verdict = selection === undefined ? `no ${name} tests` : verdictOf(name, selection, total, computed, used, paint);
2153
+ const mergeBase = (await computed.mergeBase()).slice(0, 7);
2154
+ const context = [
2155
+ `since ${base} (merge base ${mergeBase})`,
2156
+ ...config.file ? [`config ${config.file}`] : []
2157
+ ].join(SEPARATOR);
2158
+ const lines = [verdict, paint.dim(context)];
2159
+ if (others.length > 0) {
2160
+ lines.push(paint.dim(`running ${name}${SEPARATOR}${plural(others.length, "more suite")}: ${others.join(", ")}`));
2161
+ }
2162
+ console.log(lines.join(`
2163
+ `));
2110
2164
  }
2111
2165
  function commandAt(argv) {
2112
2166
  for (let i = 0;i < argv.length; i++) {
@@ -2152,7 +2206,7 @@ async function main(argv, io = PIPED) {
2152
2206
  return fail(argv, usageError("BV_MISSING_ARGUMENT", `bivium: which command? ${COMMANDS.join(", ")} (bivium --help for usage)`));
2153
2207
  }
2154
2208
  if (!(command in FLAGS)) {
2155
- return fail(argv, usageError("BV_UNKNOWN_COMMAND", `bivium: unknown command "${command}"${didYouMean(command, COMMANDS)}; the commands are ${COMMANDS.join(", ")} (bivium --help for usage)`));
2209
+ return fail(argv, usageError("BV_UNKNOWN_COMMAND", `bivium: unknown command "${command}"${didYouMean2(command, COMMANDS)}; the commands are ${COMMANDS.join(", ")} (bivium --help for usage)`));
2156
2210
  }
2157
2211
  const rest = [...argv.slice(0, at), ...argv.slice(at + 1)];
2158
2212
  try {
package/compute.js ADDED
@@ -0,0 +1,20 @@
1
+ import {
2
+ readSources2,
3
+ collectPackages2,
4
+ extractRevision2,
5
+ graphView2,
6
+ computePlan2,
7
+ prepare2,
8
+ plan2
9
+ } from "./shared-9qcbzxw3.js";
10
+ import"./shared-550cvwt2.js";
11
+ import"./shared-211m8fjt.js";
12
+ export {
13
+ collectPackages2 as collectPackages,
14
+ computePlan2 as computePlan,
15
+ extractRevision2 as extractRevision,
16
+ graphView2 as graphView,
17
+ plan2 as plan,
18
+ prepare2 as prepare,
19
+ readSources2 as readSources
20
+ };
package/config.js ADDED
@@ -0,0 +1,15 @@
1
+ import {
2
+ defineConfig2,
3
+ resolveConfig2,
4
+ inRoot2,
5
+ checkConfig2,
6
+ loadConfig2
7
+ } from "./shared-550cvwt2.js";
8
+ import"./shared-211m8fjt.js";
9
+ export {
10
+ checkConfig2 as checkConfig,
11
+ defineConfig2 as defineConfig,
12
+ inRoot2 as inRoot,
13
+ loadConfig2 as loadConfig,
14
+ resolveConfig2 as resolveConfig
15
+ };
package/docs/cli.md CHANGED
@@ -103,12 +103,31 @@ bivium run [base] [--suite <name>]
103
103
  - `--suite <name>` picks the suite.
104
104
  Default: the first one in the config.
105
105
 
106
- It prints the plan first, as `plan` does.
107
106
  The selected files go where the command says `{files}`, or at its end.
108
- When every test is selected, no files are passed,
109
- so the runner runs them all.
110
- When none is, `run` skips the command and exits 0.
111
- Otherwise it exits with the command's status.
107
+ When every test is selected, the suite's `root` goes there instead,
108
+ so a runner that would run every suite's tests runs only this one's;
109
+ `runAll` in the config replaces the command for that case.
110
+ When no test is selected, `run` skips the command and exits 0.
111
+ Otherwise it exits with the command's status,
112
+ or 130 when Ctrl-C interrupted it.
113
+
114
+ On a terminal, it opens as `plan` does, names the suites it didn't run,
115
+ echoes the command, then closes with the result:
116
+
117
+ ```console
118
+ $ bivium run main
119
+ 1 of 2 tests run for 1 changed file (unit)
120
+ since main (merge base f601cb4) · config bivium.config.ts
121
+ running unit · 2 more suites: --suite=e2e, --suite=visual
122
+ $ bun test tests/unit/name.test.ts
123
+
124
+ …the runner's output…
125
+
126
+ ✓ unit passed · 247 ms
127
+ ```
128
+
129
+ Piped, or for a coding agent, it prints the plan as text,
130
+ then the runner's output.
112
131
 
113
132
  ```sh
114
133
  bivium run --suite e2e
package/docs/config.md CHANGED
@@ -44,6 +44,7 @@ a set of test files and the command that runs them.
44
44
  |:---|:---|:---|
45
45
  | `tests` | The suite's test files | required |
46
46
  | `run` | The command `bivium run` runs | none; `bivium run` needs one |
47
+ | `runAll` | The command for a full run, when every test is selected | `run`, with `root` for `{files}` |
47
48
  | `root` | Where the orphan rule applies | the whole repository |
48
49
  | `urls` | URL pattern → the page a `page.goto()` opens | none |
49
50
  | `pages` | Pages only a `goto()` opens | none |
@@ -57,9 +58,12 @@ a set of test files and the command that runs them.
57
58
 
58
59
  `bivium run` puts the selected files where `{files}` is:
59
60
  `vitest run {files}` runs `vitest run tests/a.test.ts tests/b.test.ts`.
60
- When the whole suite is selected, `{files}` becomes nothing,
61
- and the command runs with no files.
61
+ When the whole suite is selected, `{files}` becomes the suite's `root`,
62
+ `vitest run tests`, since a bare `vitest run` or `bun test` would run
63
+ every suite's tests; without a `root`, it becomes nothing.
62
64
  Without `{files}`, the files go at the end of the command.
65
+ When the runner needs another command for a full run,
66
+ set `runAll`: `runAll: "playwright test --project=e2e"`.
63
67
 
64
68
  ### `root` and the orphan rule
65
69
 
package/errors.js ADDED
@@ -0,0 +1,8 @@
1
+ import {
2
+ BiviumError2,
3
+ didYouMean2
4
+ } from "./shared-211m8fjt.js";
5
+ export {
6
+ BiviumError2 as BiviumError,
7
+ didYouMean2 as didYouMean
8
+ };
package/index.d.ts CHANGED
@@ -144,9 +144,16 @@ export interface SuiteConfig {
144
144
  tests: Globs;
145
145
  /**
146
146
  * The command `bivium run` runs. `{files}` is replaced by the selected
147
- * files, or nothing for all of them; without it, they're appended.
147
+ * files; without it, they're appended. When every test is selected,
148
+ * `{files}` is the suite's `root`, or nothing without one.
148
149
  */
149
150
  run?: string;
151
+ /**
152
+ * The command for a full run of the suite, when every test is selected
153
+ * and `run` with the suite's root wouldn't run exactly this suite.
154
+ * Default: `run`, with the root for `{files}`.
155
+ */
156
+ runAll?: string;
150
157
  /**
151
158
  * Where the orphan rule applies: a changed file under one of these paths
152
159
  * that no test of the suite reaches is wired some other way (a setup
package/index.js CHANGED
@@ -1,10 +1,11 @@
1
+ // src/index.ts
2
+ import { plan } from "./compute.js";
3
+ import { defineConfig } from "./config.js";
1
4
  import {
2
- BiviumError2,
3
- defineConfig2,
4
- plan2
5
- } from "./shared.js";
5
+ BiviumError
6
+ } from "./errors.js";
6
7
  export {
7
- BiviumError2 as BiviumError,
8
- defineConfig2 as defineConfig,
9
- plan2 as plan
8
+ BiviumError,
9
+ defineConfig,
10
+ plan
10
11
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bivium",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Run only the tests your change can reach.",
5
5
  "license": "MIT",
6
6
  "author": "Eric Liu (https://github.com/metonym)",
@@ -0,0 +1,69 @@
1
+ // src/errors.ts
2
+ var KINDS = {
3
+ BV_MISSING_ARGUMENT: "usage",
4
+ BV_UNKNOWN_COMMAND: "usage",
5
+ BV_UNKNOWN_OPTION: "usage",
6
+ BV_BAD_OPTION_VALUE: "usage",
7
+ BV_UNEXPECTED_ARGUMENT: "usage",
8
+ BV_UNKNOWN_SUITE: "usage",
9
+ BV_UNKNOWN_TEST: "usage",
10
+ BV_UNKNOWN_FILE: "usage",
11
+ BV_NO_RUN_COMMAND: "usage",
12
+ BV_NO_GITHUB_OUTPUT: "usage",
13
+ BV_CONFIG_NOT_FOUND: "config",
14
+ BV_CONFIG_UNREADABLE: "config",
15
+ BV_CONFIG_INVALID: "config",
16
+ BV_GIT_NO_BASE: "git",
17
+ BV_GIT_NO_MERGE_BASE: "git",
18
+ BV_GIT_FAILED: "git",
19
+ BV_INTERNAL: "other"
20
+ };
21
+
22
+ class BiviumError2 extends Error {
23
+ code;
24
+ kind;
25
+ constructor(code, message, options) {
26
+ super(message, options);
27
+ this.code = code;
28
+ this.kind = KINDS[code];
29
+ }
30
+ }
31
+ function distance(a, b) {
32
+ const width = b.length + 1;
33
+ const cells = new Uint32Array((a.length + 1) * width);
34
+ const d = (i, j) => cells[i * width + j] ?? 0;
35
+ for (let i = 0;i <= a.length; i++)
36
+ cells[i * width] = i;
37
+ for (let j = 0;j <= b.length; j++)
38
+ cells[j] = j;
39
+ for (let i = 1;i <= a.length; i++) {
40
+ for (let j = 1;j <= b.length; j++) {
41
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
42
+ let best = Math.min(d(i - 1, j) + 1, d(i, j - 1) + 1, d(i - 1, j - 1) + cost);
43
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
44
+ best = Math.min(best, d(i - 2, j - 2) + 1);
45
+ }
46
+ cells[i * width + j] = best;
47
+ }
48
+ }
49
+ return d(a.length, b.length);
50
+ }
51
+ function closest(input, candidates) {
52
+ const most = Math.max(1, Math.floor(input.length / 3));
53
+ let best;
54
+ let bestDistance = most + 1;
55
+ for (const candidate of candidates) {
56
+ const d = distance(input, candidate);
57
+ if (d < bestDistance) {
58
+ best = candidate;
59
+ bestDistance = d;
60
+ }
61
+ }
62
+ return best;
63
+ }
64
+ function didYouMean2(input, candidates) {
65
+ const match = closest(input, candidates);
66
+ return match === undefined ? "" : ` (did you mean ${match}?)`;
67
+ }
68
+
69
+ export { BiviumError2, didYouMean2 };