jev-test-filter 0.1.3 → 0.1.5

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 CHANGED
@@ -4,7 +4,7 @@
4
4
  alter the outcome of every single test in the repository, and prints the filter
5
5
  arguments your test runner already understands. It hands those arguments to the
6
6
  runner you already use — vitest, jest, `node --test`, `bun test`, Playwright,
7
- `cargo test` or `go test` — so the run covers the tests the change could plausibly break
7
+ `cargo test`, `go test` or `rspec` — so the run covers the tests the change could plausibly break
8
8
  instead of all of them.
9
9
 
10
10
  It uses [Jev](https://typesafe.ai), TypeSafe's System One model: one shared
@@ -14,7 +14,8 @@ loop and no file reading by the model.
14
14
  ## Install
15
15
 
16
16
  Requires Node 24 or newer, `git`, and a TypeSafe API key. `--format rust` and
17
- `--format go` additionally need `cargo` and `go` on the `PATH`.
17
+ `--format go` additionally need `cargo` and `go` on the `PATH`; `--format
18
+ rspec` needs nothing beyond the `rspec` you already run.
18
19
 
19
20
  ### The command
20
21
 
@@ -240,13 +241,19 @@ not considered in that mode. Playwright with `--format playwright --exec` uses
240
241
  Playwright's own collected list instead. For source discovery, a file counts
241
242
  as a test file when it is named
242
243
  `*.test.*`, `*.vitest.*` or `*.spec.*` with a `.js`, `.jsx`, `.ts`, `.tsx`, `.mjs`, `.cjs`,
243
- `.mts` or `.cts` extension, or when it ends in `_test.go`, which is Go's own
244
- convention and the only one `go test` compiles into a test binary.
244
+ `.mts` or `.cts` extension, when it ends in `_test.go`, which is Go's own
245
+ convention and the only one `go test` compiles into a test binary. Under
246
+ `--format rspec`, a file ending in `_spec.rb` counts too, which is what
247
+ RSpec's default `--pattern` looks for.
245
248
 
246
- **Rust is the one exception: it is never discovered automatically.** Listing a
249
+ **Rust and RSpec are never discovered automatically.** Listing a
247
250
  crate's tests means running `cargo test -- --list`, and that builds the test
248
251
  targets. A tool that triggers a compile nobody asked for is a tool that gets
249
252
  removed from the workflow, so it happens only when you write `--format rust`.
253
+ RSpec examples cost only a parse, but a Rails application with Vitest or Jest
254
+ tests for its front end is common, and discovering its `_spec.rb` files unasked
255
+ would turn that repository's `--exec -- vitest run` into a mixed-framework
256
+ error. They are read only when you write `--format rspec`.
250
257
  Every other language is read out of the source and costs nothing but a parse.
251
258
 
252
259
  ### Rust and Go
@@ -318,6 +325,41 @@ test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 3 filtered out
318
325
  It emitted `-- --exact cart::tests::apply_discount::clamps_at_zero
319
326
  cart::tests::apply_discount::halves_the_total`.
320
327
 
328
+ ### RSpec
329
+
330
+ ```
331
+ $ jev-test-filter --format rspec --exec -- bundle exec rspec
332
+ ```
333
+
334
+ Examples are read out of the `*_spec.rb` sources, not listed by
335
+ `rspec --dry-run`: that loads every spec file, and in a Rails application it
336
+ boots the application. `--format rspec` is required, even in a repository with
337
+ no other tests. Do not give `rspec` its own paths; the tool appends the
338
+ locations it chose.
339
+
340
+ The same cart again, in Ruby, with the discount broken (rspec-core 3.13.6,
341
+ ruby 3.3.8):
342
+
343
+ ```
344
+ $ rspec # unfiltered
345
+ 5 examples, 1 failure
346
+
347
+ $ jev-test-filter --format rspec --exec -- rspec
348
+ jev-test-filter: 2/5 tests selected (locations)
349
+ Run options: include {:locations=>{"./spec/cart_spec.rb"=>[7, 11]}}
350
+ F.
351
+ 2 examples, 1 failure
352
+
353
+ Failed examples:
354
+
355
+ rspec ./spec/cart_spec.rb:5 # Cart.apply_discount halves the total
356
+ ```
357
+
358
+ It emitted `spec/cart_spec.rb:7:11`, the last lines of the two
359
+ `.apply_discount` examples; `spec/ship_spec.rb` was not loaded at all. Why
360
+ the last line rather than the first is in
361
+ [What it emits, per framework](#what-it-emits-per-framework).
362
+
321
363
  `--base <ref>` compares `<ref>...HEAD`, the way a pull request does. Without
322
364
  it, the working tree against `HEAD` is used; `--staged` uses the index.
323
365
 
@@ -337,6 +379,7 @@ before you hand-edit anything it prints.
337
379
  | `playwright` | runner's test title and project | `--test-list .jev-test-filter/playwright-….txt` with `--exec`; otherwise `file:line` | exact list or positionals |
338
380
  | `rust` | `"::"` — `cart::tests::halves_the_total` | `-- --exact <name> <name>…`, one invocation | after a literal `--` |
339
381
  | `go` | `"/"` — `TestApplyDiscount/halves the total` | `-run '^(?:TestA\|TestB)$'` plus the `./pkg` directories | pattern **before** the packages |
382
+ | `rspec` | a space, none before `#`/`.`/`::` after a class — `Cart#total sums` | `spec/a_spec.rb:12:30`, one argument per file | positionals |
340
383
 
341
384
  Bun 1.3.5 prints `Cart > totals` in its reporter, but `--test-name-pattern`
342
385
  matches `Cart totals`. The pattern above was verified by running Bun itself.
@@ -394,15 +437,46 @@ cargo 1.98.0 and go 1.26.2:
394
437
  all. `--json` still reports the per-subtest score, which is the finer signal
395
438
  and what a reader wants to see.
396
439
 
397
- The vitest, node:test, Bun, Playwright, Rust and Go spellings were each verified
398
- against the real runner. `jest` is emitted with the vitest shape and was not.
440
+ And for RSpec, measured on rspec-core 3.13.6 and ruby 3.3.8:
441
+
442
+ - **An example is named by its last line.** RSpec runs, for a requested line,
443
+ the example or group declared nearest at or above it, and it records a
444
+ multi-line call at a line of its own choosing: `it "a",\n :slow do` at its
445
+ first line, `it(\n "a"\n) do` at its last. The first line of the second
446
+ shape resolves to the example *before* it — a wrong test, run green. The last
447
+ line cannot, because nothing is declared inside an example body.
448
+ - **It is never selected by name.** A full description depends on whether a
449
+ group was described by a class or a string (`Cart#total` against
450
+ `Cart #total`), and a name pattern spelled one way matches nothing spelled
451
+ the other. A location has no spelling, so an interpolated title, a
452
+ description-less one-liner (`it { is_expected.to … }`) and the rows of an
453
+ `.each` loop are all scored like any other test rather than selected unasked.
454
+ - **Shared examples** are scored as one test per inclusion. `it_behaves_like`
455
+ declares a nested group at its own line (the opening of its block, when
456
+ present), so that line runs everything it brought in, including examples
457
+ in a customization block. `include_examples` and `include_context` copy
458
+ the examples into the enclosing group
459
+ with the lines of the file that defined them, so it is named by the line
460
+ that group's block opens on, which runs the whole group.
461
+ - **A `shared_examples` block defined in a `_spec.rb` file is reached only
462
+ through that file.** Its examples are named by their own lines, which run
463
+ the inclusions in the same file; an `it_behaves_like` in another file is a
464
+ test of its own and is scored like any other. Shared examples usually live
465
+ in `spec/support`, which is not a spec file, so this rarely matters.
466
+
467
+ The name `--json` reports is for reading only: the class-versus-string
468
+ distinction is gone once a title is a string, so a group described by the
469
+ string `"Cart"` is joined as if it were the class.
470
+
471
+ The vitest, node:test, Bun, Playwright, Rust, Go and RSpec spellings were each
472
+ verified against the real runner. `jest` is emitted with the vitest shape and was not.
399
473
 
400
474
  The `mode` field in `--json` names the emitted filter shape:
401
475
 
402
476
  | `mode` | `argv` | Meaning |
403
477
  | --- | --- | --- |
404
478
  | `pattern` | flag, pattern, files | The normal case. |
405
- | `locations` | `file:line`… | Playwright. |
479
+ | `locations` | `file:line`… | Playwright, and RSpec (`file:line:line`, one per file). |
406
480
  | `test-list` | `--test-list <file>` | Playwright with runner discovery; `test_list` in JSON holds the file's lines. |
407
481
  | `exact` | `--`, `--exact`, names… | Rust. Every name came from `cargo test -- --list`. |
408
482
  | `files` | files | A name pattern could not express the selection, so whole files were chosen. This happens when a selected test has a non-literal title, or when more than 80% of the suite was selected and the alternation would not be worth it. |
@@ -488,7 +562,7 @@ framework with no `--format`, which exits 1 (see Known limitations).
488
562
  ```
489
563
  --base <ref> compare against <ref>...HEAD, as a pull request does
490
564
  --staged use the staged change instead of the working tree
491
- --format <name> vitest | jest | node | bun | playwright | rust | go | auto (default: auto)
565
+ --format <name> vitest | jest | node | bun | playwright | rust | go | rspec | auto (default: auto)
492
566
  --cutoff <n> select at or above this score level (default: 2)
493
567
  --unsure-below <n> a confidence under this counts as unsure (default: 0.5)
494
568
  --unsure-margin <n> rescue an unsure answer this far under the cutoff (default: 1)
package/dist/cli.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env node
2
- import type { SnapshotReview } from "./snapshot.ts";
2
+ import type { Diff } from "./diff.ts";
3
+ import type { SnapshotChange, SnapshotReview, SnapshotTestSources } from "./snapshot.ts";
3
4
  import type { RunRecord, RunResult } from "./run.ts";
4
5
  import type { GateOptions } from "./gate.ts";
5
6
  import type { Framework } from "./types.ts";
@@ -64,3 +65,12 @@ export declare function stdoutExitCode(res: RunResult): number;
64
65
  export declare function shouldSaveRecord(record: RunRecord): boolean;
65
66
  export declare function renderJson(res: RunResult): string;
66
67
  export declare function renderSnapshotReport(review: SnapshotReview): string;
68
+ /**
69
+ * The snapshot changes under review, narrowed to the paths asked for, with the
70
+ * test sources that explain them.
71
+ */
72
+ export declare function collectSnapshotChanges(args: Pick<CliArgs, "base" | "staged" | "paths">, cwd?: string): Promise<{
73
+ diff: Diff;
74
+ changes: SnapshotChange[];
75
+ context: SnapshotTestSources;
76
+ }>;
package/dist/cli.js CHANGED
@@ -23,7 +23,7 @@
23
23
  import { spawn } from "node:child_process";
24
24
  import { realpathSync } from "node:fs";
25
25
  import { mkdir, writeFile } from "node:fs/promises";
26
- import { dirname, join } from "node:path";
26
+ import { dirname, join, posix } from "node:path";
27
27
  import { fileURLToPath } from "node:url";
28
28
  import { parseArgs } from "node:util";
29
29
  import { displayName } from "./questions.js";
@@ -32,7 +32,7 @@ import { loadDiff } from "./diff.js";
32
32
  import { assessSnapshots, findSnapshotChangesInWorktree, loadSnapshotTestSources, snapshotTestFile } from "./snapshot.js";
33
33
  import { loadContext } from "./context.js";
34
34
  import { loadRecord, RECORD_DIR, RECORD_FILE, replay, run, saveRecord } from "./run.js";
35
- const FORMATS = ["vitest", "jest", "node", "bun", "playwright", "rust", "go", "auto"];
35
+ const FORMATS = ["vitest", "jest", "node", "bun", "playwright", "rust", "go", "rspec", "auto"];
36
36
  /** Where a run leaves its answers, and what a bare `--replay` means. */
37
37
  export const DEFAULT_RECORD_PATH = `${RECORD_DIR}/${RECORD_FILE}`;
38
38
  /**
@@ -231,7 +231,7 @@ Usage:
231
231
  Options:
232
232
  --base <ref> compare against the merge base with <ref>, as a pull request does
233
233
  --staged use the staged change instead of the working tree
234
- --format <name> vitest | jest | node | bun | playwright | rust | go | auto (default: auto)
234
+ --format <name> vitest | jest | node | bun | playwright | rust | go | rspec | auto (default: auto)
235
235
  --cutoff <n> select at or above this score level (default: 2)
236
236
  --unsure-below <n> a confidence under this counts as unsure (default: 0.5)
237
237
  --unsure-margin <n> rescue an unsure answer this far under the cutoff (default: 1)
@@ -252,6 +252,7 @@ Examples:
252
252
  jev-test-filter --verify-snapshots --json
253
253
  jev-test-filter --base main --format go --exec -- go test
254
254
  jev-test-filter --base main --format rust --exec -- cargo test
255
+ jev-test-filter --base main --format rspec --exec -- bundle exec rspec
255
256
  jev-test-filter --base main --json > selection.json
256
257
 
257
258
  Rust is never discovered automatically: listing its tests builds the test
@@ -269,6 +270,26 @@ quotes. Use --exec instead of working around it.
269
270
  Environment:
270
271
  TYPESAFE_API_KEY required unless --dry-run or --replay
271
272
  `;
273
+ /**
274
+ * The snapshot changes under review, narrowed to the paths asked for, with the
275
+ * test sources that explain them.
276
+ */
277
+ export async function collectSnapshotChanges(args, cwd = process.cwd()) {
278
+ const diff = await loadDiff({ cwd, base: args.base, staged: args.staged });
279
+ const revision = args.base ? "HEAD" : args.staged ? ":" : null;
280
+ // The diff names files from the repository root, so they are read from there;
281
+ // the paths asked for are relative to cwd and are rebased to match.
282
+ const changes = (await findSnapshotChangesInWorktree(diff.text, diff.root, revision)).filter((change) => args.paths.length === 0 || args.paths.some((path) => {
283
+ const normalized = posix.normalize(`${diff.prefix}${path}`).replace(/\/$/, "");
284
+ if (normalized === ".")
285
+ return true;
286
+ const testFile = snapshotTestFile(change);
287
+ return change.file === normalized || change.file.startsWith(`${normalized}/`)
288
+ || testFile === normalized || testFile?.startsWith(`${normalized}/`);
289
+ }));
290
+ const context = await loadSnapshotTestSources(diff.root, changes, 24_000, revision);
291
+ return { diff, changes, context };
292
+ }
272
293
  async function main() {
273
294
  let args;
274
295
  try {
@@ -283,15 +304,7 @@ async function main() {
283
304
  return 0;
284
305
  }
285
306
  if (args.verifySnapshots) {
286
- const diff = await loadDiff({ cwd: process.cwd(), base: args.base, staged: args.staged });
287
- const revision = args.base ? "HEAD" : args.staged ? ":" : null;
288
- const changes = (await findSnapshotChangesInWorktree(diff.text, process.cwd(), revision)).filter((change) => args.paths.length === 0 || args.paths.some((path) => {
289
- const normalized = path.replace(/^\.\//, "").replace(/\/$/, "");
290
- const testFile = snapshotTestFile(change);
291
- return change.file === normalized || change.file.startsWith(`${normalized}/`)
292
- || testFile === normalized || testFile?.startsWith(`${normalized}/`);
293
- }));
294
- const context = await loadSnapshotTestSources(process.cwd(), changes, 24_000, revision);
307
+ const { diff, changes, context } = await collectSnapshotChanges(args);
295
308
  const review = args.dryRun
296
309
  ? { entries: changes.map((change) => ({ ...change, status: "unknown", score: null, confidence: null })), error: null, spent: null }
297
310
  : await assessSnapshots(diff.text, diff.stat, undefined, changes, context);
package/dist/diff.d.ts CHANGED
@@ -37,7 +37,12 @@ export interface Diff {
37
37
  text: string;
38
38
  /** `git diff --stat`, which survives truncation of the text. */
39
39
  stat: string;
40
+ /** Post-image ranges keyed relative to the requested cwd, like discovered tests. */
40
41
  ranges: ChangedRanges;
42
+ /** The requested cwd relative to the repository root: "" at the root, else ending in "/". */
43
+ prefix: string;
44
+ /** The repository root. Paths in `text` and `stat` are relative to this, not to cwd. */
45
+ root: string;
41
46
  }
42
47
  /**
43
48
  * The change under review.
package/dist/diff.js CHANGED
@@ -6,6 +6,7 @@
6
6
  * which is the `state` every question is asked against.
7
7
  */
8
8
  import { execFile } from "node:child_process";
9
+ import { resolve } from "node:path";
9
10
  import { promisify } from "node:util";
10
11
  const execFileAsync = promisify(execFile);
11
12
  /** git's own prefixes, including the `diff.mnemonicPrefix` spellings. */
@@ -154,7 +155,7 @@ export async function loadDiff({ cwd = process.cwd(), base = null, staged = fals
154
155
  // from the ranges, because a deleted file has no post-image line for a test
155
156
  // body to overlap; but removing a source file is exactly the kind of change
156
157
  // 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 common = ["--no-relative", "--no-color", "--no-ext-diff", "--src-prefix=a/", "--dst-prefix=b/"];
158
159
  const range = [];
159
160
  if (staged)
160
161
  range.push("--cached");
@@ -164,7 +165,15 @@ export async function loadDiff({ cwd = process.cwd(), base = null, staged = fals
164
165
  range.push("HEAD");
165
166
  const text = await git(["diff", "--unified=0", ...common, ...range], cwd);
166
167
  const stat = await git(["diff", "--stat", ...common, ...range], cwd);
167
- return { text, stat, ranges: parseUnifiedDiff(text) };
168
+ // Git diff paths are repository-relative, while test discovery uses cwd.
169
+ // Rebase only the lookup keys: sibling-workspace changes must stay in the
170
+ // text and stat passed to the model.
171
+ const prefix = (await git(["rev-parse", "--show-prefix"], cwd)).replace(/\r?\n$/, "");
172
+ const parsed = parseUnifiedDiff(text);
173
+ const ranges = prefix === "" ? parsed : new Map([...parsed].filter(([file]) => file.startsWith(prefix))
174
+ .map(([file, changed]) => [file.slice(prefix.length), changed]));
175
+ const root = resolve(cwd, ...prefix.split("/").filter(Boolean).map(() => ".."));
176
+ return { text, stat, ranges, prefix, root };
168
177
  }
169
178
  /**
170
179
  * The commit a ref names, or null when it names none -- no repository, no
package/dist/discover.js CHANGED
@@ -1,16 +1,22 @@
1
1
  /**
2
2
  * Every test in the repository, whatever language it is written in.
3
3
  *
4
- * ECMAScript and Go come out of the source and cost a parse. Rust has to be
4
+ * ECMAScript, Go and RSpec come out of the source and cost a parse. Rust has to be
5
5
  * asked for: its names come from `cargo test -- --list`, which builds the test
6
6
  * targets, and a tool that triggers a compile nobody asked for is a tool that
7
7
  * gets removed from the workflow. `--format rust` is that asking.
8
+ *
9
+ * RSpec has to be asked for too, for a different reason: `_spec.rb` files were
10
+ * never discovered before, and a Rails application with Vitest or Jest tests
11
+ * for its front end is common. Discovering them unasked would turn that
12
+ * repository's existing `--exec -- vitest run` into a mixed-framework error.
8
13
  */
9
14
  import { readFile } from "node:fs/promises";
10
15
  import { join } from "node:path";
11
- import { detectFramework, findTestFiles, isGoTestFile } from "./framework.js";
16
+ import { detectFramework, findTestFiles, isGoTestFile, isRSpecFile } from "./framework.js";
12
17
  import { extractTests } from "./extract.js";
13
18
  import { extractGoTests } from "./extract-go.js";
19
+ import { extractRSpecTests } from "./extract-rspec.js";
14
20
  import { listRustTests } from "./cargo.js";
15
21
  export async function discoverTests(cwd, paths, format) {
16
22
  const out = [];
@@ -21,6 +27,11 @@ export async function discoverTests(cwd, paths, format) {
21
27
  out.push(...extractGoTests(source, file));
22
28
  continue;
23
29
  }
30
+ if (isRSpecFile(file)) {
31
+ if (format === "rspec")
32
+ out.push(...extractRSpecTests(source, file));
33
+ continue;
34
+ }
24
35
  const framework = detectFramework(source, file);
25
36
  if (format !== null && framework !== format)
26
37
  continue;
@@ -0,0 +1,20 @@
1
+ import type { SgNode } from "@ast-grep/napi";
2
+ import type { TestCase } from "./types.ts";
3
+ /**
4
+ * What RSpec prints for one level of the chain.
5
+ *
6
+ * `describe Cart, "#total"` is one description, "Cart#total": a second
7
+ * string argument is appended by the same rule that joins a class to its
8
+ * children. Symbols after the description are metadata and are dropped.
9
+ */
10
+ export declare function description(call: SgNode): string;
11
+ /**
12
+ * The full description, the way RSpec prints it.
13
+ *
14
+ * Only for reading: the class-versus-string distinction the separator turns
15
+ * on is gone by the time a title is a string, so a group described by the
16
+ * string `"Cart"` reads as if it were the class. Nothing is ever selected by
17
+ * this name.
18
+ */
19
+ export declare function rspecFullName(titlePath: readonly string[]): string;
20
+ export declare function extractRSpecTests(source: string, file: string): TestCase[];
@@ -0,0 +1,177 @@
1
+ /**
2
+ * RSpec's examples, read out of the source.
3
+ *
4
+ * Asking RSpec instead (`rspec --dry-run`) would load every spec file, and in
5
+ * a Rails application that means booting the application -- work nobody asked
6
+ * this tool to do, for the same reason Rust is never listed unasked. The
7
+ * source has everything a location filter needs, so the source is read.
8
+ *
9
+ * Selection is by location rather than by name, and every choice below
10
+ * follows from how RSpec resolves a `file:line`, which was measured (RSpec
11
+ * 3.13, Ruby 3.3) rather than assumed:
12
+ *
13
+ * - A requested line runs the declaration nearest at or above it. RSpec
14
+ * records an example at a line of its own choosing -- `it "a",\n :slow do`
15
+ * at the first line, but `it(\n "a"\n) do` at the last -- so the first
16
+ * line of a multi-line call can resolve to the example BEFORE it, which
17
+ * is a wrong test run green. The last line of an example cannot: nothing
18
+ * is declared inside an example body. So an example is named by its last
19
+ * line.
20
+ * - A full description is not something to assemble. RSpec joins a group
21
+ * and its child with no space when the group is a class and the child
22
+ * starts with `#`, `.` or `::` (`Cart#total`), and with one otherwise
23
+ * (`"Cart" #total`). A name pattern spelled one way matches nothing
24
+ * spelled the other, and says nothing about it. A location has no
25
+ * spelling to get wrong.
26
+ * - Because of that, `dynamic` is never set. An interpolated title, a
27
+ * one-liner with no description at all and a row of an `.each` loop are
28
+ * all reachable by their line, so none of them has to be selected unasked.
29
+ */
30
+ import { parse } from "@ast-grep/napi";
31
+ import { registerLanguages } from "./languages.js";
32
+ /** `describe` and its spellings, including shared groups, which declare examples too. */
33
+ const GROUP = /^[xf]?(?:describe|context|feature|example_group)$|^shared_(?:examples(?:_for)?|context)$/;
34
+ /** `it` and its spellings. A call with no block declares nothing that runs. */
35
+ const EXAMPLE = /^[xf]?(?:it|specify|example|scenario|its)$|^(?:focus|pending|skip)$/;
36
+ /**
37
+ * A nested group the shared examples are copied into. RSpec declares that
38
+ * group at the call itself, so its line reaches every example it holds.
39
+ */
40
+ const IT_BEHAVES_LIKE = /^it_(?:behaves|should_behave)_like$/;
41
+ /**
42
+ * Shared examples copied straight into the enclosing group. They keep the
43
+ * lines of the file that defined them, which is usually a support file, so
44
+ * no line of this file reaches them -- except the enclosing group's own.
45
+ */
46
+ const INCLUDE_SHARED = /^include_(?:examples|context)$/;
47
+ /** The receiver a group may be called on: none, or `RSpec`. */
48
+ function receiverAllowed(call) {
49
+ const recv = call.field("receiver");
50
+ return recv === null || /^(?:::)?RSpec$/.test(recv.text());
51
+ }
52
+ /** The positional arguments, without parentheses, commas or a trailing hash. */
53
+ function args(call) {
54
+ return (call.field("arguments")?.children() ?? []).filter((n) => n.isNamed() && n.kind() !== "comment");
55
+ }
56
+ /**
57
+ * A string literal's text, with interpolations left as written. The grammar
58
+ * has no escape nodes inside single quotes, where only `\\` and `\'` are
59
+ * escapes at all.
60
+ */
61
+ function stringText(node) {
62
+ const text = node
63
+ .children()
64
+ .filter((n) => n.isNamed())
65
+ .map((n) => (n.kind() === "escape_sequence" ? n.text().slice(1) : n.text()))
66
+ .join("");
67
+ return node.child(0)?.text() === "'" ? text.replace(/\\([\\'])/g, "$1") : text;
68
+ }
69
+ /** A class or module, as RSpec prints it: the constant path, exactly as written. */
70
+ function isConstant(node) {
71
+ return node.kind() === "constant" || node.kind() === "scope_resolution";
72
+ }
73
+ /**
74
+ * What RSpec prints for one level of the chain.
75
+ *
76
+ * `describe Cart, "#total"` is one description, "Cart#total": a second
77
+ * string argument is appended by the same rule that joins a class to its
78
+ * children. Symbols after the description are metadata and are dropped.
79
+ */
80
+ export function description(call) {
81
+ const [first, second] = args(call);
82
+ if (!first)
83
+ return "";
84
+ let text;
85
+ if (first.kind() === "string")
86
+ text = stringText(first);
87
+ else if (first.kind() === "simple_symbol")
88
+ text = first.text().slice(1);
89
+ else if (first.kind() === "pair" || first.kind() === "hash")
90
+ return "";
91
+ else
92
+ text = first.text();
93
+ if (second?.kind() === "string")
94
+ text = joinDescriptions(text, stringText(second));
95
+ return text;
96
+ }
97
+ /** RSpec's own separator: none between a class and a `#`, `.` or `::` child. */
98
+ function joinDescriptions(parent, child) {
99
+ if (child === "")
100
+ return parent;
101
+ if (parent === "")
102
+ return child;
103
+ return CONSTANT_PATH.test(parent) && /^(?:#|\.|::)/.test(child) ? `${parent}${child}` : `${parent} ${child}`;
104
+ }
105
+ const CONSTANT_PATH = /^(?:::)?[A-Z]\w*(?:::[A-Z]\w*)*$/;
106
+ /**
107
+ * The full description, the way RSpec prints it.
108
+ *
109
+ * Only for reading: the class-versus-string distinction the separator turns
110
+ * on is gone by the time a title is a string, so a group described by the
111
+ * string `"Cart"` reads as if it were the class. Nothing is ever selected by
112
+ * this name.
113
+ */
114
+ export function rspecFullName(titlePath) {
115
+ return titlePath.reduce((acc, part) => joinDescriptions(acc, part), "");
116
+ }
117
+ /** The line a block opens on, which is where RSpec records a group. */
118
+ function blockLine(call) {
119
+ const block = call.field("block");
120
+ return (block ?? call).range().start.line + 1;
121
+ }
122
+ export function extractRSpecTests(source, file) {
123
+ registerLanguages();
124
+ const root = parse("ruby", source).root();
125
+ const out = [];
126
+ const push = (call, titlePath, runnerLine) => {
127
+ const range = call.range();
128
+ out.push({
129
+ file,
130
+ titlePath,
131
+ line: range.start.line + 1,
132
+ endLine: range.end.line + 1,
133
+ framework: "rspec",
134
+ runnerLine,
135
+ dynamic: false,
136
+ });
137
+ };
138
+ // A walk rather than range containment, because where a call sits is what
139
+ // decides whether it means anything: `skip "reason"` inside an example body
140
+ // is a statement, not an example, so the walk never descends into one.
141
+ const walk = (node, chain, groupLine) => {
142
+ if (node.kind() === "call") {
143
+ const method = node.field("method")?.text() ?? "";
144
+ const hasBlock = node.field("block") !== null;
145
+ if (GROUP.test(method) && hasBlock && receiverAllowed(node)) {
146
+ const next = [...chain, description(node)];
147
+ for (const child of node.children())
148
+ walk(child, next, blockLine(node));
149
+ return;
150
+ }
151
+ if (node.field("receiver") === null && chain.length > 0) {
152
+ if (EXAMPLE.test(method) && hasBlock) {
153
+ const title = description(node) || `example at ./${file}:${node.range().start.line + 1}`;
154
+ push(node, [...chain, title], node.range().end.line + 1);
155
+ return;
156
+ }
157
+ if (IT_BEHAVES_LIKE.test(method)) {
158
+ const verb = method === "it_behaves_like" ? "behaves like" : "it should behave like";
159
+ // A customization block may declare its own examples. Its last
160
+ // line would select only the last of those, dropping the shared
161
+ // examples. Select the nested group's declaration instead.
162
+ const runnerLine = hasBlock ? blockLine(node) : node.range().end.line + 1;
163
+ push(node, [...chain, `${verb} ${description(node)}`], runnerLine);
164
+ return;
165
+ }
166
+ if (INCLUDE_SHARED.test(method)) {
167
+ push(node, [...chain, `${method} ${description(node)}`], groupLine ?? node.range().start.line + 1);
168
+ return;
169
+ }
170
+ }
171
+ }
172
+ for (const child of node.children())
173
+ walk(child, chain, groupLine);
174
+ };
175
+ walk(root, [], null);
176
+ return out;
177
+ }
package/dist/filter.d.ts CHANGED
@@ -11,6 +11,9 @@
11
11
  * alternation of anchored full names, is the only safe shape.
12
12
  * - Playwright's own --list supplies project-aware names for --test-list.
13
13
  * Source-only discovery retains file:line as its conservative fallback.
14
+ * - RSpec is selected by `file:line` only. Its full description depends on
15
+ * whether a group was described by a class or by a string, which a name
16
+ * read out of the source cannot always tell apart.
14
17
  */
15
18
  import type { Framework, Selection, TestCase } from "./types.ts";
16
19
  export type FilterMode =
package/dist/filter.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { createHash } from "node:crypto";
2
+ import { rspecFullName } from "./extract-rspec.js";
2
3
  export function escapeRegExp(s) {
3
4
  return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
4
5
  }
@@ -8,6 +9,8 @@ export function fullName(t) {
8
9
  return t.titlePath.join("::");
9
10
  if (t.framework === "go")
10
11
  return t.titlePath.join("/");
12
+ if (t.framework === "rspec")
13
+ return rspecFullName(t.titlePath);
11
14
  const sep = t.framework === "node" || t.framework === "bun" ? " " : " > ";
12
15
  return t.titlePath.join(sep);
13
16
  }
@@ -64,6 +67,20 @@ export function buildFilter(sel, framework, { fileThreshold = 0.8 } = {}) {
64
67
  // both, which costs time and cannot lose a test.
65
68
  return { mode: "exact", argv: ["--", "--exact", ...selected.map(fullName)] };
66
69
  }
70
+ if (framework === "rspec") {
71
+ const files = uniqueFiles(selected);
72
+ if (selected.length / all.length > fileThreshold)
73
+ return { mode: "files", argv: files };
74
+ // One argument per file, `spec/a_spec.rb:12:30`: RSpec takes several
75
+ // lines after one path, and a line filter applies to its own file only.
76
+ const lines = new Map(files.map((f) => [f, new Set()]));
77
+ for (const t of selected)
78
+ lines.get(t.file).add(t.runnerLine ?? t.line);
79
+ return {
80
+ mode: "locations",
81
+ argv: files.map((f) => [f, ...[...lines.get(f)].sort((a, b) => a - b)].join(":")),
82
+ };
83
+ }
67
84
  if (framework === "go") {
68
85
  // Top-level functions, never subtests. `-run` takes one hierarchical
69
86
  // pattern and a second `-run` replaces the first, so "all of TestA, but
@@ -3,6 +3,7 @@ import type { Framework } from "./types.ts";
3
3
  /** The grammar to parse a file under, by extension. */
4
4
  export declare function langFor(file: string): Lang;
5
5
  export declare function isGoTestFile(file: string): boolean;
6
+ export declare function isRSpecFile(file: string): boolean;
6
7
  export declare function isTestFile(file: string): boolean;
7
8
  /**
8
9
  * Playwright wins a tie because a spec that imports `expect` from elsewhere
package/dist/framework.js CHANGED
@@ -27,8 +27,13 @@ const GO_TEST_FILE = /_test\.go$/;
27
27
  export function isGoTestFile(file) {
28
28
  return GO_TEST_FILE.test(file);
29
29
  }
30
+ /** RSpec's own convention, and the suffix its default `--pattern` looks for. */
31
+ const RSPEC_TEST_FILE = /_spec\.rb$/;
32
+ export function isRSpecFile(file) {
33
+ return RSPEC_TEST_FILE.test(file);
34
+ }
30
35
  export function isTestFile(file) {
31
- return TEST_FILE.test(file) || GO_TEST_FILE.test(file);
36
+ return TEST_FILE.test(file) || GO_TEST_FILE.test(file) || RSPEC_TEST_FILE.test(file);
32
37
  }
33
38
  /** `require("x")`, with the specifier captured. */
34
39
  const REQUIRE_RULE = {
package/dist/languages.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * The grammars ast-grep does not bundle.
3
3
  *
4
- * `@ast-grep/napi` ships the web languages only; Rust and Go arrive as
4
+ * `@ast-grep/napi` ships the web languages only; Rust, Go and Ruby arrive as
5
5
  * separate packages and have to be registered before the first `parse` that
6
6
  * names them. Registration is process-wide and rejects a second call for the
7
7
  * same language, so it happens once behind a flag rather than per file.
@@ -9,10 +9,11 @@
9
9
  import { registerDynamicLanguage } from "@ast-grep/napi";
10
10
  import rust from "@ast-grep/lang-rust";
11
11
  import go from "@ast-grep/lang-go";
12
+ import ruby from "@ast-grep/lang-ruby";
12
13
  let registered = false;
13
14
  export function registerLanguages() {
14
15
  if (registered)
15
16
  return;
16
- registerDynamicLanguage({ rust, go });
17
+ registerDynamicLanguage({ rust, go, ruby });
17
18
  registered = true;
18
19
  }
@@ -1,24 +1,3 @@
1
- /**
2
- * One question per test.
3
- *
4
- * `score` rather than `choice` because the answer is an ordered conclusion:
5
- * asking "how much can this change break this test" as an unordered choice
6
- * discards the ordering, splits the probability mass between adjacent levels,
7
- * and returns a low confidence that is indistinguishable from real
8
- * uncertainty.
9
- *
10
- * No threshold ever appears in a question. The cutoff is a decision the gate
11
- * makes from the answer; writing it into the question would mean every
12
- * recalibration rewrote the question, and no run could be compared with an
13
- * earlier one.
14
- *
15
- * The one thing a question may carry beyond the test's own identity is
16
- * history from flaker (`--context`): the files this test failed with before,
17
- * as one plain sentence under `instructions.history`. It is evidence, not a
18
- * rule -- no count, no rate -- and it is absent, key and all, for a test the
19
- * context says nothing about, so such a test is asked exactly what it was
20
- * asked before contexts existed.
21
- */
22
1
  import type { TestCase } from "./types.ts";
23
2
  export interface ScoreQuestion {
24
3
  type: "score";
package/dist/questions.js CHANGED
@@ -1,3 +1,25 @@
1
+ /**
2
+ * One question per test.
3
+ *
4
+ * `score` rather than `choice` because the answer is an ordered conclusion:
5
+ * asking "how much can this change break this test" as an unordered choice
6
+ * discards the ordering, splits the probability mass between adjacent levels,
7
+ * and returns a low confidence that is indistinguishable from real
8
+ * uncertainty.
9
+ *
10
+ * No threshold ever appears in a question. The cutoff is a decision the gate
11
+ * makes from the answer; writing it into the question would mean every
12
+ * recalibration rewrote the question, and no run could be compared with an
13
+ * earlier one.
14
+ *
15
+ * The one thing a question may carry beyond the test's own identity is
16
+ * history from flaker (`--context`): the files this test failed with before,
17
+ * as one plain sentence under `instructions.history`. It is evidence, not a
18
+ * rule -- no count, no rate -- and it is absent, key and all, for a test the
19
+ * context says nothing about, so such a test is asked exactly what it was
20
+ * asked before contexts existed.
21
+ */
22
+ import { rspecFullName } from "./extract-rspec.js";
1
23
  const TASK = "The state is a git diff. Judge only the one test identified below, and only " +
2
24
  "for whether this change can alter its outcome -- other problems with the test " +
3
25
  "or with the change are not your concern here.";
@@ -23,6 +45,8 @@ export function displayName(t) {
23
45
  return t.titlePath.join("::");
24
46
  if (t.framework === "go")
25
47
  return t.titlePath.join("/");
48
+ if (t.framework === "rspec")
49
+ return rspecFullName(t.titlePath);
26
50
  const sep = t.framework === "node" ? " " : " > ";
27
51
  return t.titlePath.join(sep);
28
52
  }
package/dist/types.d.ts CHANGED
@@ -12,7 +12,7 @@
12
12
  * `jest` is separate from `vitest` only so a report can name it; the two share
13
13
  * a filter shape exactly.
14
14
  */
15
- export type Framework = "vitest" | "jest" | "node" | "bun" | "playwright" | "rust" | "go" | "unknown";
15
+ export type Framework = "vitest" | "jest" | "node" | "bun" | "playwright" | "rust" | "go" | "rspec" | "unknown";
16
16
  /** One test, as the source declares it. */
17
17
  export interface TestCase {
18
18
  /**
@@ -34,6 +34,14 @@ export interface TestCase {
34
34
  /** Playwright project and path relative to its configured rootDir, when listed by the runner. */
35
35
  project?: string;
36
36
  runnerFile?: string;
37
+ /**
38
+ * The line a `file:line` filter names this test by, when it is not `line`.
39
+ * RSpec resolves a requested line to the nearest declaration at or above
40
+ * it and records a multi-line call at a line of its own choosing, so an
41
+ * example is named by its last line and shared examples copied into a
42
+ * group by the line that group's block opens on.
43
+ */
44
+ runnerLine?: number;
37
45
  /**
38
46
  * The title could not be read statically -- a template with an
39
47
  * interpolation, or a `.each` row. Such a test can never be named in a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jev-test-filter",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Score every test against a git diff with Jev, and emit the filter arguments your runner already understands",
5
5
  "keywords": [
6
6
  "test",
@@ -9,6 +9,7 @@
9
9
  "playwright",
10
10
  "test-selection",
11
11
  "jev",
12
+ "rspec",
12
13
  "ast-grep"
13
14
  ],
14
15
  "license": "MIT",
@@ -61,13 +62,13 @@
61
62
  },
62
63
  "dependencies": {
63
64
  "@ast-grep/lang-go": "^0.0.6",
65
+ "@ast-grep/lang-ruby": "^0.0.7",
64
66
  "@ast-grep/lang-rust": "^0.0.7",
65
- "@ast-grep/napi": "^0.40.0"
67
+ "@ast-grep/napi": "^0.51.3"
66
68
  },
67
69
  "devDependencies": {
68
- "@secretlint/secretlint-rule-preset-recommend": "^9.0.0",
69
- "@types/node": "^24.0.0",
70
- "secretlint": "^9.0.0",
71
- "typescript": "^5.9.0"
70
+ "@types/node": "^24",
71
+ "secretlint": "^14.0.1",
72
+ "typescript": "^7.0.2"
72
73
  }
73
74
  }