jev-test-filter 0.1.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/dist/cargo.js ADDED
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Rust's tests: names from cargo, locations from the source.
3
+ *
4
+ * `cargo test -- --exact` is unforgiving. A module path reconstructed one
5
+ * segment wrong matches nothing, the run is green, and nothing says a test was
6
+ * skipped -- so the names that reach the filter are the ones cargo printed,
7
+ * never ones this tool assembled. Cargo has to build the test targets to list
8
+ * them, which is work a run would do anyway.
9
+ *
10
+ * The source still has something cargo's listing does not: where each test
11
+ * is. Matching the two by full path gives a file and a line, which is what
12
+ * lets a test whose own body sits in the diff be selected without being
13
+ * asked about. A name cargo listed and no `#[test]` function accounts for --
14
+ * anything a macro generated -- keeps no location and is scored instead.
15
+ */
16
+ import { execFile } from "node:child_process";
17
+ import { readFile } from "node:fs/promises";
18
+ import { join } from "node:path";
19
+ import { promisify } from "node:util";
20
+ import { parse } from "@ast-grep/napi";
21
+ import { registerLanguages } from "./languages.js";
22
+ const execFileAsync = promisify(execFile);
23
+ /** `#[test]`, `#[tokio::test]`, `#[async_std::test]` -- but not `#[cfg(test)]`. */
24
+ const TEST_ATTRIBUTE = /^#\[\s*(?:[A-Za-z_]\w*\s*::\s*)*test\s*\]$/;
25
+ export function isTestAttribute(text) {
26
+ return TEST_ATTRIBUTE.test(text);
27
+ }
28
+ /** The `name: test` lines of `cargo test -- --list`, without the suffix. */
29
+ export function parseCargoList(stdout) {
30
+ const out = [];
31
+ for (const line of stdout.split("\n")) {
32
+ const m = /^(\S+): test$/.exec(line.trim());
33
+ if (m)
34
+ out.push(m[1]);
35
+ }
36
+ return out;
37
+ }
38
+ /**
39
+ * The module path a file contributes, by Rust's file-to-module rules.
40
+ *
41
+ * `src/lib.rs` and `src/main.rs` are crate roots and contribute nothing;
42
+ * `mod.rs` names its directory; every file under `tests/`, `benches/` and
43
+ * `examples/` is its own crate root. A `#[path]` attribute can override all of
44
+ * this, and is rare enough that a name it moves simply goes unlocated.
45
+ */
46
+ export function modulePrefix(file) {
47
+ const m = /^(src|tests|benches|examples)\/(.+)\.rs$/.exec(file);
48
+ if (!m)
49
+ return [];
50
+ if (m[1] !== "src")
51
+ return [];
52
+ const parts = m[2].split("/");
53
+ const last = parts.at(-1);
54
+ if (last === "lib" || last === "main" || last === "mod")
55
+ parts.pop();
56
+ return parts;
57
+ }
58
+ /** Every `#[test]` function in one file, keyed by its full module path. */
59
+ export function rustTestsIn(source, file) {
60
+ registerLanguages();
61
+ const root = parse("rust", source).root();
62
+ const out = new Map();
63
+ const mods = root.findAll({ rule: { kind: "mod_item" } }).map((n) => ({
64
+ name: n.field("name")?.text() ?? "",
65
+ start: n.range().start.index,
66
+ end: n.range().end.index,
67
+ }));
68
+ for (const fn of root.findAll({ rule: { kind: "function_item" } })) {
69
+ // Attributes are siblings preceding the item, nearest last.
70
+ let isTest = false;
71
+ for (const prev of fn.prevAll()) {
72
+ if (prev.kind() !== "attribute_item")
73
+ break;
74
+ if (isTestAttribute(prev.text()))
75
+ isTest = true;
76
+ }
77
+ if (!isTest)
78
+ continue;
79
+ const range = fn.range();
80
+ const chain = mods
81
+ .filter((m) => m.start <= range.start.index && m.end >= range.end.index)
82
+ .sort((a, b) => a.start - b.start)
83
+ .map((m) => m.name);
84
+ const path = [...modulePrefix(file), ...chain, fn.field("name")?.text() ?? ""].join("::");
85
+ out.set(path, { file, line: range.start.line + 1, endLine: range.end.line + 1 });
86
+ }
87
+ return out;
88
+ }
89
+ /** `git ls-files` for the crate's Rust sources. */
90
+ async function rustFiles(cwd) {
91
+ const { stdout } = await execFileAsync("git", ["ls-files", "-z", "--", "*.rs"], {
92
+ cwd,
93
+ maxBuffer: 64 * 1024 * 1024,
94
+ });
95
+ return stdout.split("\0").filter((f) => f !== "");
96
+ }
97
+ /**
98
+ * Ask cargo what tests exist. This builds the test targets.
99
+ *
100
+ * `--quiet` keeps cargo's own progress off stdout; the listing is what is
101
+ * left. A failure to build is not something to work around -- it is reported
102
+ * so the run can fall back to everything.
103
+ */
104
+ export async function cargoList(cwd) {
105
+ const { stdout } = await execFileAsync("cargo", ["test", "--quiet", "--", "--list"], {
106
+ cwd,
107
+ maxBuffer: 64 * 1024 * 1024,
108
+ });
109
+ return stdout;
110
+ }
111
+ export async function listRustTests(cwd) {
112
+ const names = parseCargoList(await cargoList(cwd));
113
+ const located = new Map();
114
+ for (const file of await rustFiles(cwd)) {
115
+ const source = await readFile(join(cwd, file), "utf8");
116
+ for (const [path, at] of rustTestsIn(source, file))
117
+ located.set(path, at);
118
+ }
119
+ return names.map((name) => {
120
+ const at = located.get(name);
121
+ return {
122
+ file: at?.file ?? "",
123
+ titlePath: name.split("::"),
124
+ line: at?.line ?? 0,
125
+ endLine: at?.endLine ?? 0,
126
+ framework: "rust",
127
+ dynamic: false,
128
+ };
129
+ });
130
+ }
package/dist/cli.d.ts ADDED
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env node
2
+ import type { RunRecord, RunResult } from "./run.ts";
3
+ import type { Framework } from "./types.ts";
4
+ /** Where a run leaves its answers, and what a bare `--replay` means. */
5
+ export declare const DEFAULT_RECORD_PATH = ".jev-test-filter/last.json";
6
+ export interface CliArgs {
7
+ base: string | null;
8
+ staged: boolean;
9
+ paths: string[];
10
+ format: Framework | null;
11
+ cutoff: number | undefined;
12
+ concurrency: number | undefined;
13
+ json: boolean;
14
+ dryRun: boolean;
15
+ replayPath: string | null;
16
+ exec: string[] | null;
17
+ help: boolean;
18
+ }
19
+ /**
20
+ * `--exec` takes everything after the `--` that follows it, so the runner's
21
+ * own flags never have to be escaped past this parser.
22
+ */
23
+ export declare function parseCliArgs(argv: string[]): CliArgs;
24
+ export declare function renderLine(res: RunResult): string;
25
+ /** The command to run, or null when nothing was selected. */
26
+ export declare function execArgv(cmd: string[], res: RunResult): string[] | null;
27
+ /**
28
+ * The status the stdout form exits with when nothing was selected.
29
+ *
30
+ * An empty argv is what "everything was selected" and "nothing was selected"
31
+ * both look like on stdout, and the second must not read as the first: a
32
+ * shell that runs `eval "vitest run $(jev-test-filter)"` on an empty line
33
+ * runs the WHOLE suite, which is the opposite of what the tool decided.
34
+ * `--exec` has no such ambiguity, which is why it is the shape to reach for.
35
+ */
36
+ export declare const EXIT_NOTHING_SELECTED = 3;
37
+ /** The status of the stdout form, which is the only shape that needs one. */
38
+ export declare function stdoutExitCode(res: RunResult): number;
39
+ /**
40
+ * Whether this run's record is worth keeping.
41
+ *
42
+ * A fallback record holds no answers -- the run never got any, or gave up on
43
+ * the ones it had. Writing it over the last good record destroys exactly the
44
+ * material `--replay` exists to re-gate, and it was measured happening: one
45
+ * `HTTP 529 system_overloaded` replaced a complete scoring, and the next
46
+ * `--replay` reported the 529 instead of re-gating the earlier answers.
47
+ */
48
+ export declare function shouldSaveRecord(record: RunRecord): boolean;
49
+ export declare function renderJson(res: RunResult): string;
package/dist/cli.js ADDED
@@ -0,0 +1,265 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * The command line.
4
+ *
5
+ * Three shapes, because "easy to wire in" means different things in a
6
+ * Makefile, in a shell and in another program:
7
+ *
8
+ * jev-test-filter --base main --exec -- vitest run
9
+ * jev-test-filter --base main --json -> the whole scoring
10
+ * jev-test-filter --base main -> arguments on stdout
11
+ *
12
+ * `--exec` is the one to reach for, and not merely out of convenience: it
13
+ * hands the arguments to `spawn` with no shell in between, so nothing can be
14
+ * re-split. The stdout form cannot offer that. A test name contains spaces
15
+ * almost by definition, `renderLine` therefore quotes it, and an unquoted
16
+ * `$(...)` word-splits without re-parsing those quotes -- so
17
+ * `vitest run $(jev-test-filter)` hands vitest five arguments where one was
18
+ * meant. The stdout form is for reading and for `eval`, and the help text
19
+ * says so.
20
+ *
21
+ * Only this file writes to a stream or exits.
22
+ */
23
+ import { spawn } from "node:child_process";
24
+ import { parseArgs } from "node:util";
25
+ import { displayName } from "./questions.js";
26
+ import { fullName } from "./filter.js";
27
+ import { loadRecord, RECORD_DIR, RECORD_FILE, replay, run, saveRecord } from "./run.js";
28
+ const FORMATS = ["vitest", "jest", "node", "playwright", "rust", "go", "auto"];
29
+ /** Where a run leaves its answers, and what a bare `--replay` means. */
30
+ export const DEFAULT_RECORD_PATH = `${RECORD_DIR}/${RECORD_FILE}`;
31
+ /**
32
+ * `--exec` takes everything after the `--` that follows it, so the runner's
33
+ * own flags never have to be escaped past this parser.
34
+ */
35
+ export function parseCliArgs(argv) {
36
+ const execAt = argv.indexOf("--exec");
37
+ const own = execAt === -1 ? argv : argv.slice(0, execAt);
38
+ let exec = null;
39
+ if (execAt !== -1) {
40
+ const rest = argv.slice(execAt + 1);
41
+ exec = rest[0] === "--" ? rest.slice(1) : rest;
42
+ if (exec.length === 0)
43
+ throw new Error("--exec needs a command, e.g. --exec -- vitest run");
44
+ }
45
+ // `--replay` may be given without a path. parseArgs has no notion of an
46
+ // optional value -- a bare one is an error, and only `--replay=` reaches a
47
+ // default -- so a bare one is rewritten before parseArgs sees it. A path is
48
+ // a path; anything starting with `-` is the next option.
49
+ const args = own.map((a, i) => a === "--replay" && (i === own.length - 1 || own[i + 1].startsWith("-"))
50
+ ? `--replay=${DEFAULT_RECORD_PATH}`
51
+ : a);
52
+ const { values, positionals } = parseArgs({
53
+ args,
54
+ allowPositionals: true,
55
+ options: {
56
+ base: { type: "string" },
57
+ staged: { type: "boolean", default: false },
58
+ format: { type: "string", default: "auto" },
59
+ cutoff: { type: "string" },
60
+ concurrency: { type: "string" },
61
+ json: { type: "boolean", default: false },
62
+ "dry-run": { type: "boolean", default: false },
63
+ replay: { type: "string" },
64
+ help: { type: "boolean", short: "h", default: false },
65
+ },
66
+ });
67
+ const format = String(values.format);
68
+ if (!FORMATS.includes(format)) {
69
+ throw new Error(`unknown --format ${format}; expected one of ${FORMATS.join(", ")}`);
70
+ }
71
+ return {
72
+ base: values.base === undefined ? null : String(values.base),
73
+ staged: Boolean(values.staged),
74
+ paths: positionals,
75
+ format: format === "auto" ? null : format,
76
+ cutoff: values.cutoff === undefined ? undefined : Number(values.cutoff),
77
+ concurrency: values.concurrency === undefined ? undefined : Number(values.concurrency),
78
+ json: Boolean(values.json),
79
+ dryRun: Boolean(values["dry-run"]),
80
+ replayPath: values.replay === undefined ? null : String(values.replay),
81
+ exec,
82
+ help: Boolean(values.help),
83
+ };
84
+ }
85
+ /**
86
+ * Shell-safe single quoting, for a line a human will read or `eval`.
87
+ *
88
+ * Not for an unquoted `$(...)`: that word-splits without re-parsing quotes,
89
+ * so a quoted test name arrives as several arguments. `--exec` exists so
90
+ * that nobody has to get this right.
91
+ */
92
+ function quote(arg) {
93
+ return /^[A-Za-z0-9_@%+=:,./-]+$/.test(arg) ? arg : `'${arg.replace(/'/g, `'\\''`)}'`;
94
+ }
95
+ export function renderLine(res) {
96
+ return res.filter.argv.map(quote).join(" ");
97
+ }
98
+ /** The command to run, or null when nothing was selected. */
99
+ export function execArgv(cmd, res) {
100
+ if (res.filter.mode === "none")
101
+ return null;
102
+ return [...cmd, ...res.filter.argv];
103
+ }
104
+ /**
105
+ * The status the stdout form exits with when nothing was selected.
106
+ *
107
+ * An empty argv is what "everything was selected" and "nothing was selected"
108
+ * both look like on stdout, and the second must not read as the first: a
109
+ * shell that runs `eval "vitest run $(jev-test-filter)"` on an empty line
110
+ * runs the WHOLE suite, which is the opposite of what the tool decided.
111
+ * `--exec` has no such ambiguity, which is why it is the shape to reach for.
112
+ */
113
+ export const EXIT_NOTHING_SELECTED = 3;
114
+ /** The status of the stdout form, which is the only shape that needs one. */
115
+ export function stdoutExitCode(res) {
116
+ return res.filter.mode === "none" ? EXIT_NOTHING_SELECTED : 0;
117
+ }
118
+ /**
119
+ * Whether this run's record is worth keeping.
120
+ *
121
+ * A fallback record holds no answers -- the run never got any, or gave up on
122
+ * the ones it had. Writing it over the last good record destroys exactly the
123
+ * material `--replay` exists to re-gate, and it was measured happening: one
124
+ * `HTTP 529 system_overloaded` replaced a complete scoring, and the next
125
+ * `--replay` reported the 529 instead of re-gating the earlier answers.
126
+ */
127
+ export function shouldSaveRecord(record) {
128
+ return record.fallback === null;
129
+ }
130
+ export function renderJson(res) {
131
+ return `${JSON.stringify({
132
+ framework: res.framework,
133
+ mode: res.filter.mode,
134
+ argv: res.filter.argv,
135
+ fallback: res.selection.fallback,
136
+ selected: res.selection.selected.length,
137
+ total: res.selection.all.length,
138
+ spent: res.spent,
139
+ tests: res.selection.verdicts.map((v) => ({
140
+ file: v.test.file,
141
+ name: displayName(v.test),
142
+ pattern_name: fullName(v.test),
143
+ line: v.test.line,
144
+ selected: v.selected,
145
+ reason: v.reason,
146
+ score: v.answer?.value ?? null,
147
+ confidence: v.answer?.confidence ?? null,
148
+ })),
149
+ }, null, 2)}\n`;
150
+ }
151
+ const HELP = `jev-test-filter — score every test against a git diff and emit runner arguments
152
+
153
+ Usage:
154
+ jev-test-filter [options] --exec -- <command...>
155
+ jev-test-filter [options] [paths...]
156
+
157
+ Options:
158
+ --base <ref> compare against the merge base with <ref>, as a pull request does
159
+ --staged use the staged change instead of the working tree
160
+ --format <name> vitest | jest | node | playwright | rust | go | auto (default: auto)
161
+ --cutoff <n> select at or above this score level (default: 2)
162
+ --concurrency <n> requests in flight at once
163
+ --json print the full scoring instead of the arguments
164
+ --dry-run extract and report without calling Jev
165
+ --replay <file> re-gate a recorded run offline (default: .jev-test-filter/last.json)
166
+ --exec -- <cmd...> append the arguments to <cmd...> and run it
167
+ -h, --help this text
168
+
169
+ Examples:
170
+ jev-test-filter --base main --exec -- vitest run
171
+ jev-test-filter --base main --format node --exec -- node --test
172
+ jev-test-filter --base main --format go --exec -- go test
173
+ jev-test-filter --base main --format rust --exec -- cargo test
174
+ jev-test-filter --base main --json > selection.json
175
+
176
+ Rust is never discovered automatically: listing its tests builds the test
177
+ targets, so it happens only under --format rust.
178
+
179
+ Without --exec the arguments are written to stdout, shell-quoted. They are
180
+ meant to be read, or passed through eval:
181
+
182
+ eval "vitest run $(jev-test-filter --base main)"
183
+
184
+ An unquoted $(...) will not work: a test name contains spaces, so the
185
+ arguments are quoted, and the shell word-splits them without re-parsing the
186
+ quotes. Use --exec instead of working around it.
187
+
188
+ Environment:
189
+ TYPESAFE_API_KEY required unless --dry-run or --replay
190
+ `;
191
+ async function main() {
192
+ let args;
193
+ try {
194
+ args = parseCliArgs(process.argv.slice(2));
195
+ }
196
+ catch (err) {
197
+ process.stderr.write(`jev-test-filter: ${err instanceof Error ? err.message : String(err)}\n`);
198
+ return 2;
199
+ }
200
+ if (args.help) {
201
+ process.stdout.write(HELP);
202
+ return 0;
203
+ }
204
+ let res;
205
+ if (args.replayPath !== null) {
206
+ const record = await loadRecord(args.replayPath);
207
+ const selection = replay(record, args.cutoff === undefined ? {} : { cutoff: args.cutoff });
208
+ const { buildFilter } = await import("./filter.js");
209
+ res = {
210
+ selection,
211
+ framework: record.framework,
212
+ filter: selection.fallback === null ? buildFilter(selection, record.framework) : { mode: "all", argv: [] },
213
+ record,
214
+ spent: null,
215
+ };
216
+ }
217
+ else {
218
+ res = await run({
219
+ base: args.base,
220
+ staged: args.staged,
221
+ paths: args.paths,
222
+ format: args.format,
223
+ dryRun: args.dryRun,
224
+ ...(args.cutoff === undefined ? {} : { cutoff: args.cutoff }),
225
+ ...(args.concurrency === undefined ? {} : { concurrency: args.concurrency }),
226
+ });
227
+ if (shouldSaveRecord(res.record))
228
+ await saveRecord(process.cwd(), res.record);
229
+ }
230
+ if (res.selection.fallback !== null) {
231
+ process.stderr.write(`jev-test-filter: running everything (${res.selection.fallback})\n`);
232
+ }
233
+ else {
234
+ const { selected, all } = res.selection;
235
+ process.stderr.write(`jev-test-filter: ${selected.length}/${all.length} tests selected (${res.filter.mode})\n`);
236
+ }
237
+ if (args.json) {
238
+ process.stdout.write(renderJson(res));
239
+ return 0;
240
+ }
241
+ if (args.exec) {
242
+ const argv = execArgv(args.exec, res);
243
+ if (argv === null) {
244
+ process.stderr.write("jev-test-filter: nothing selected; not running\n");
245
+ return 0;
246
+ }
247
+ return await new Promise((resolve) => {
248
+ const child = spawn(argv[0], argv.slice(1), { stdio: "inherit" });
249
+ child.on("close", (code) => resolve(code ?? 1));
250
+ child.on("error", (err) => {
251
+ process.stderr.write(`jev-test-filter: ${err.message}\n`);
252
+ resolve(127);
253
+ });
254
+ });
255
+ }
256
+ process.stdout.write(`${renderLine(res)}\n`);
257
+ return stdoutExitCode(res);
258
+ }
259
+ // Only run when invoked as a program, so the tests can import the renderers.
260
+ if (process.argv[1] && /cli\.(ts|js)$/.test(process.argv[1])) {
261
+ main().then((code) => process.exit(code), (err) => {
262
+ process.stderr.write(`jev-test-filter: ${err instanceof Error ? err.message : String(err)}\n`);
263
+ process.exit(1);
264
+ });
265
+ }
package/dist/diff.d.ts ADDED
@@ -0,0 +1,52 @@
1
+ /** Post-image line ranges per file, inclusive at both ends. */
2
+ export type ChangedRanges = Map<string, Array<[number, number]>>;
3
+ /**
4
+ * Parse the `@@ -a,b +c,d @@` headers of a unified diff.
5
+ *
6
+ * A file header is only read before the section's first hunk. Inside a hunk
7
+ * every line carries an added or removed marker, so adding the C++ line
8
+ * `++ x;` produces a body line spelled `+++ x;` -- indistinguishable from a
9
+ * file header to anything that does not track where it is.
10
+ */
11
+ export declare function parseUnifiedDiff(text: string): ChangedRanges;
12
+ /** Does `[line, endLine]` intersect any changed range for this file? */
13
+ export declare function touchesChange(ranges: ChangedRanges, file: string, line: number, endLine: number): boolean;
14
+ /**
15
+ * The diff cut into one section per file, keyed by post-image path -- or, for
16
+ * a file the change deletes, by its pre-image path.
17
+ *
18
+ * This is where `parseUnifiedDiff` and this function deliberately part ways.
19
+ * Ranges answer "which test bodies did the change touch", and a deleted file
20
+ * has no post-image line any test body can sit on. Sections answer "what
21
+ * should the model see", and deleting a source file is one of the changes
22
+ * most likely to break a test -- dropping it would hide the change from the
23
+ * judgment it matters most to.
24
+ *
25
+ * The state builder drops whole sections to fit its budget, and a section is
26
+ * the smallest piece that still reads as a diff.
27
+ */
28
+ export declare function splitDiffByFile(text: string): Map<string, string>;
29
+ export interface DiffOptions {
30
+ cwd?: string;
31
+ /** Compare against the merge base with this ref, the way a pull request does. */
32
+ base?: string | null;
33
+ staged?: boolean;
34
+ }
35
+ export interface Diff {
36
+ /** The unified diff, zero context, for the `state`. */
37
+ text: string;
38
+ /** `git diff --stat`, which survives truncation of the text. */
39
+ stat: string;
40
+ ranges: ChangedRanges;
41
+ }
42
+ /**
43
+ * The change under review.
44
+ *
45
+ * With `base`, `base...HEAD` -- commits that landed on the base branch
46
+ * meanwhile are not this change's doing. Without it, the working tree against
47
+ * HEAD, so a selection can be taken before anything is committed.
48
+ *
49
+ * The prefixes are pinned because a user's `diff.mnemonicPrefix` would
50
+ * otherwise rename them and the parser's paths would match no file.
51
+ */
52
+ export declare function loadDiff({ cwd, base, staged }?: DiffOptions): Promise<Diff>;
package/dist/diff.js ADDED
@@ -0,0 +1,168 @@
1
+ /**
2
+ * What the change touched.
3
+ *
4
+ * Two things come out of one `git diff`: the post-image line ranges, which
5
+ * decide which tests are selected without asking, and the diff text itself,
6
+ * which is the `state` every question is asked against.
7
+ */
8
+ import { execFile } from "node:child_process";
9
+ import { promisify } from "node:util";
10
+ const execFileAsync = promisify(execFile);
11
+ /** git's own prefixes, including the `diff.mnemonicPrefix` spellings. */
12
+ const PREFIX = /^[abciwo]\//;
13
+ /**
14
+ * Parse the `@@ -a,b +c,d @@` headers of a unified diff.
15
+ *
16
+ * A file header is only read before the section's first hunk. Inside a hunk
17
+ * every line carries an added or removed marker, so adding the C++ line
18
+ * `++ x;` produces a body line spelled `+++ x;` -- indistinguishable from a
19
+ * file header to anything that does not track where it is.
20
+ */
21
+ export function parseUnifiedDiff(text) {
22
+ const byFile = new Map();
23
+ let file = null;
24
+ let inHunk = false;
25
+ for (const line of text.split("\n")) {
26
+ if (line.startsWith("diff --git ")) {
27
+ file = null;
28
+ inHunk = false;
29
+ continue;
30
+ }
31
+ if (!inHunk && line.startsWith("+++ ")) {
32
+ const path = line.slice(4).trim();
33
+ // A post-image of /dev/null is a deletion: nothing in the new tree
34
+ // changed, so there is no line here a test's body can overlap. The
35
+ // deletion still reaches the model, through `splitDiffByFile`.
36
+ file = path === "/dev/null" ? null : path.replace(PREFIX, "");
37
+ if (file && !byFile.has(file))
38
+ byFile.set(file, []);
39
+ continue;
40
+ }
41
+ if (!line.startsWith("@@"))
42
+ continue;
43
+ inHunk = true;
44
+ if (!file)
45
+ continue;
46
+ const m = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/.exec(line);
47
+ if (!m)
48
+ continue;
49
+ const start = Number(m[1]);
50
+ const count = m[2] === undefined ? 1 : Number(m[2]);
51
+ if (count === 0)
52
+ continue;
53
+ byFile.get(file).push([start, start + count - 1]);
54
+ }
55
+ for (const [k, ranges] of byFile) {
56
+ if (ranges.length === 0)
57
+ byFile.delete(k);
58
+ else
59
+ byFile.set(k, merge(ranges));
60
+ }
61
+ return byFile;
62
+ }
63
+ function merge(ranges) {
64
+ const sorted = [...ranges].sort((a, b) => a[0] - b[0]);
65
+ const out = [sorted[0]];
66
+ for (const [s, e] of sorted.slice(1)) {
67
+ const last = out.at(-1);
68
+ if (s <= last[1] + 1)
69
+ last[1] = Math.max(last[1], e);
70
+ else
71
+ out.push([s, e]);
72
+ }
73
+ return out;
74
+ }
75
+ /** Does `[line, endLine]` intersect any changed range for this file? */
76
+ export function touchesChange(ranges, file, line, endLine) {
77
+ const list = ranges.get(file);
78
+ if (!list)
79
+ return false;
80
+ return list.some(([s, e]) => line <= e && endLine >= s);
81
+ }
82
+ /**
83
+ * The diff cut into one section per file, keyed by post-image path -- or, for
84
+ * a file the change deletes, by its pre-image path.
85
+ *
86
+ * This is where `parseUnifiedDiff` and this function deliberately part ways.
87
+ * Ranges answer "which test bodies did the change touch", and a deleted file
88
+ * has no post-image line any test body can sit on. Sections answer "what
89
+ * should the model see", and deleting a source file is one of the changes
90
+ * most likely to break a test -- dropping it would hide the change from the
91
+ * judgment it matters most to.
92
+ *
93
+ * The state builder drops whole sections to fit its budget, and a section is
94
+ * the smallest piece that still reads as a diff.
95
+ */
96
+ export function splitDiffByFile(text) {
97
+ const out = new Map();
98
+ const lines = text.split("\n");
99
+ let buf = [];
100
+ let post = null;
101
+ let pre = null;
102
+ let inHunk = false;
103
+ const flush = () => {
104
+ const key = post ?? pre;
105
+ if (key && buf.length > 0)
106
+ out.set(key, buf.join("\n"));
107
+ buf = [];
108
+ post = null;
109
+ pre = null;
110
+ inHunk = false;
111
+ };
112
+ for (const line of lines) {
113
+ if (line.startsWith("diff --git ")) {
114
+ flush();
115
+ buf.push(line);
116
+ continue;
117
+ }
118
+ buf.push(line);
119
+ if (line.startsWith("@@")) {
120
+ inHunk = true;
121
+ continue;
122
+ }
123
+ // Headers only before the first hunk; see `parseUnifiedDiff`.
124
+ if (inHunk)
125
+ continue;
126
+ if (line.startsWith("--- ")) {
127
+ const path = line.slice(4).trim();
128
+ pre = path === "/dev/null" ? null : path.replace(PREFIX, "");
129
+ }
130
+ else if (line.startsWith("+++ ")) {
131
+ const path = line.slice(4).trim();
132
+ post = path === "/dev/null" ? null : path.replace(PREFIX, "");
133
+ }
134
+ }
135
+ flush();
136
+ return out;
137
+ }
138
+ async function git(args, cwd) {
139
+ const { stdout } = await execFileAsync("git", args, { cwd, maxBuffer: 256 * 1024 * 1024 });
140
+ return stdout;
141
+ }
142
+ /**
143
+ * The change under review.
144
+ *
145
+ * With `base`, `base...HEAD` -- commits that landed on the base branch
146
+ * meanwhile are not this change's doing. Without it, the working tree against
147
+ * HEAD, so a selection can be taken before anything is committed.
148
+ *
149
+ * The prefixes are pinned because a user's `diff.mnemonicPrefix` would
150
+ * otherwise rename them and the parser's paths would match no file.
151
+ */
152
+ export async function loadDiff({ cwd = process.cwd(), base = null, staged = false } = {}) {
153
+ // Deletions are NOT filtered out. `parseUnifiedDiff` already drops them
154
+ // from the ranges, because a deleted file has no post-image line for a test
155
+ // body to overlap; but removing a source file is exactly the kind of change
156
+ // the model has to see, so it stays in the text.
157
+ const common = ["--no-color", "--no-ext-diff", "--src-prefix=a/", "--dst-prefix=b/"];
158
+ const range = [];
159
+ if (staged)
160
+ range.push("--cached");
161
+ if (base)
162
+ range.push(`${base}...HEAD`);
163
+ else if (!staged)
164
+ range.push("HEAD");
165
+ const text = await git(["diff", "--unified=0", ...common, ...range], cwd);
166
+ const stat = await git(["diff", "--stat", ...common, ...range], cwd);
167
+ return { text, stat, ranges: parseUnifiedDiff(text) };
168
+ }
@@ -0,0 +1,2 @@
1
+ import type { Framework, TestCase } from "./types.ts";
2
+ export declare function discoverTests(cwd: string, paths: string[], format: Framework | null): Promise<TestCase[]>;