From 4bd15f8a3d7e159f61e8fe2d465cb20f4b0629c6 Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Sun, 22 Nov 2026 04:32:40 -0800 Subject: [PATCH] Test selection finds tests for NodeNext .js imports and Rails specs (#4679) In TypeScript repos that import `./x.js` for `x.ts` (NodeNext) and in Rails repos, touched source files now get their tests instead of none. Why: `/verify` and `/ship` found no tests for these files even though tests covered the files (#4398, #4211). Nothing to do: a Rails file maps only when its spec exists at the conventional path. Co-authored-by: mikhailsal <14850941+mikhailsal@users.noreply.github.com> Co-authored-by: Ikraam Ghoor <34813339+ikraamg@users.noreply.github.com> Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1v/adversarial-native#1 reader=adversarial-native Review-Finding: p1v/adversarial-native#2 reader=adversarial-native Review-Finding: p1v/adversarial-native#3 reader=adversarial-native Review-Finding: p1v/outside-voice#2 reader=outside-voice Review-Finding: p1v/outside-voice#3 reader=outside-voice --- .../pregate-nodenext-and-rspec-test-lanes.md | 1 + docs/pregate.md | 8 ++- lib/pregate/import-graph.ts | 20 ++++++- test/pregate-import-graph-paths.test.ts | 28 +++++++++ test/pregate-rspec-convention.test.ts | 60 +++++++++++++++++++ 5 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 changelog.d/pregate-nodenext-and-rspec-test-lanes.md create mode 100644 test/pregate-rspec-convention.test.ts diff --git a/changelog.d/pregate-nodenext-and-rspec-test-lanes.md b/changelog.d/pregate-nodenext-and-rspec-test-lanes.md new file mode 100644 index 000000000..8df7b13ee --- /dev/null +++ b/changelog.d/pregate-nodenext-and-rspec-test-lanes.md @@ -0,0 +1 @@ +- **`/verify` and `/ship` find the tests for two more kinds of source file.** In a TypeScript repo that uses NodeNext imports (`./x.js` for `x.ts`), the test selector now follows those imports to the `.ts` or `.tsx` file, as TypeScript does, even when a compiled `.js` sits beside it (#4398). In a Rails repo, `app/.rb` now selects `spec/_spec.rb` and `lib/.rb` selects `spec/lib/_spec.rb` when that spec exists, at the repo root or under an engine's own root (#4211). Request and system specs, which name no single source file, are not mapped. diff --git a/docs/pregate.md b/docs/pregate.md index 527e21d5e..571f65b27 100644 --- a/docs/pregate.md +++ b/docs/pregate.md @@ -324,7 +324,9 @@ Two stages with separate clocks, both printed: - `lanes`: touched files map to tests through `lib/pregate/import-graph.ts` (static imports ∪ repo-path string literals ∪ the `bin/` → - `test/*.test.ts` convention ∪ `dependencies`; Dart `import`/`export`/`part` + `test/*.test.ts` convention ∪ RSpec's `[/]spec/_spec.rb` → + `[/]app/.rb` or `[/].rb` convention, for whichever exists + (request and system specs name no single source file and map to none) ∪ `dependencies`; Dart `import`/`export`/`part` directives resolve `package:/x` through every tracked `pubspec.yaml`, and `*_test.dart` files under a `test/` directory are tests, except under `integration_test/` and `test_driver/`: they need a device, so pregate never runs them, and a touched @@ -377,6 +379,10 @@ Two stages with separate clocks, both printed: `baseUrl`) tries the exact path, then `.ts`, `.tsx`, `.js`, `.jsx`, `.mjs`, `.cjs`, `.mts`, `.cts`, then `index.ts`, `index.tsx`, `index.js`, `index.jsx` and `index.mjs` inside the directory; the first file that exists wins. + An import written with the emitted extension (NodeNext: `./x.js` for `x.ts` + or `x.tsx`, `.jsx` → `.tsx`, `.mjs` → `.mts`, `.cjs` → `.cts`) resolves to + its TypeScript source first, as tsc does, even when a compiled `.js` sits + beside it, then as written; a path string literal always names the file as written. A touched source file with no lane is `FAIL lanes — has no test lane` once a policy that [declares lanes](#lane-requirements) exists (the base copy, the user-owned file or `--policy-from head`); with no policy anywhere, or one diff --git a/lib/pregate/import-graph.ts b/lib/pregate/import-graph.ts index 2e37b3fd3..1ac1f0c41 100644 --- a/lib/pregate/import-graph.ts +++ b/lib/pregate/import-graph.ts @@ -10,12 +10,18 @@ * import) and literal directories (`test/fixtures/x` → `x/**`), and in * Python, dotted `mock.patch` targets (`'pkg.mod.Class.method'`); * 3. the `bin/` → `test/*.test.*` convention (with and without - * the `gstack-` prefix); + * the `gstack-` prefix), and RSpec's `[/]spec/_spec.rb` → + * `[/]app/.rb` or `[/].rb` (Rails autoloads constants, + * so no require names the file); * 4. explicit `.gstack/pregate.json` `dependencies` declarations; * 5. in a Django project, the files Django loads by name (lib/pregate/django.ts); * 6. in a Cargo crate, every `.rs` file and the Cargo.toml select the crate's * tests and its dependents' (lib/pregate/rust.ts); in-source * `#[cfg(test)]` files and `/tests/` files are the tests. + * A module specifier written with the emitted extension (NodeNext: `./x.js` + * for `x.ts` or `x.tsx`, `.jsx`→`.tsx`, `.mjs`→`.mts`, `.cjs`→`.cts`) resolves to + * its TypeScript source first, as tsc does, then as written; a filesystem + * literal always names the path as written. * Dart directives (`import`/`export`/`part`, conditional URIs included) resolve * `package:/x` through every tracked pubspec.yaml to `/lib/x`. * Uncertainty selects more tests, never fewer: a dependency on a directory @@ -316,6 +322,8 @@ function matchAliases(aliases: readonly PathAlias[], ref: string): { chosen: str /** One candidate repo path → the file it names (trying extensions), `dir/**`, a bare bin, or null. */ function resolveCandidate(files: Set, dirs: Set, c: string, literal: boolean): string | null { if (c.startsWith('../') || c === '..' || c === '.' || c === '') return null; + const ts = literal ? null : /\.([mc]?)js(x?)$/.exec(c); + if (ts) for (const t of ts[1] || ts[2] ? [`.${ts[1]}ts${ts[2]}`] : ['.ts', '.tsx']) { const f = c.slice(0, -ts[0].length) + t; if (files.has(f)) return f; } for (const ext of RESOLVE_EXT) if (files.has(c + ext)) return c + ext; // A directory literal needs two segments (`test/fixtures/x`): a bare word is a name, not a path. if (dirs.has(c) && (!literal || c.includes('/'))) return `${c}/**`; @@ -507,6 +515,12 @@ function conventionBins(test: string, files: Set): string[] { return out; } +/** The Ruby sources an RSpec spec names by convention: spec/models/user_spec.rb names app/models/user.rb, spec/lib/parser_spec.rb names lib/parser.rb, and an engine's engines/x/spec/... names engines/x/app/... the same way. */ +function conventionRubySubjects(test: string, files: Set): string[] { + const m = /^((?:.+\/)?)spec\/(.+)_spec\.rb$/.exec(test); + return m ? [`${m[1]}app/${m[2]}.rb`, `${m[1]}${m[2]}.rb`].filter(f => files.has(f)) : []; +} + export interface BuildOptions { declarations?: Record; maxBytes?: number; /** Paths that no longer exist (deleted or renamed-from) but that tests may still name; they resolve as dependencies so their consumers are found, and are never tests themselves (`git ls-files` still lists an unstaged deletion). */ @@ -541,12 +555,12 @@ export function buildGraph(repoRoot: string, opts: BuildOptions = {}): Graph { for (const e of rust.edges) addEdge(graph, e.dependency, e.dependent, e.via); const django = djangoEdges({ files: [...files], tests: graph.tests, read: rel => pySources.get(rel) ?? readRepoFile(repoRoot, rel, maxBytes), code: pythonCode, resolveDotted: dotted => pythonPatchTarget(files, dotted) }); for (const e of django) addEdge(graph, e.dependency, e.dependent, 'framework'); - for (const test of graph.tests) for (const bin of conventionBins(test, files)) addEdge(graph, bin, test, 'convention'); + for (const test of graph.tests) for (const subject of [...conventionBins(test, files), ...conventionRubySubjects(test, files)]) addEdge(graph, subject, test, 'convention'); if (opts.goneSources) { const present = new Set(all); graph.goneSubjects = new Map(); for (const [rel, source] of opts.goneSources) { - const targets = [...referencesIn(files, dirs, rel, source, aliases.aliasesFor(rel), undefined, packages).map(r => r.target), ...conventionBins(rel, files)]; + const targets = [...referencesIn(files, dirs, rel, source, aliases.aliasesFor(rel), undefined, packages).map(r => r.target), ...conventionBins(rel, files), ...conventionRubySubjects(rel, files)]; graph.goneSubjects.set(rel, [...new Set(targets)].filter(t => present.has(t) && !isTestFile(t) && !TEST_TREE.test(t)).sort()); } } diff --git a/test/pregate-import-graph-paths.test.ts b/test/pregate-import-graph-paths.test.ts index f7586dd0d..00cb84532 100644 --- a/test/pregate-import-graph-paths.test.ts +++ b/test/pregate-import-graph-paths.test.ts @@ -303,3 +303,31 @@ describe('import graph: alias precedence, extends and the no-lane explanation', expect(text).toContain('declare the file under dependencies in .gstack/pregate.json, or report the resolver gap'); }); }); + +describe('import graph: NodeNext specifiers written with the emitted extension (#4398)', () => { + // Value: protects=NodeNext `./x.js` specifiers map to x.ts or x.tsx (and .jsx→.tsx, .mjs→.mts), directly and transitively, the + // import resolves to the .ts source over a .js twin as in tsc (the twin keeps its literal edge), and a filesystem literal naming + // a `.js` directory keeps that directory; + // fails_when=the .js→.ts rewrite is removed, runs after the as-written lookup, or applies to literals; + // why_new=no case covered emitted-extension imports; seam=none; no_claim=bundler-specific resolution beyond the TS rewrite + test('a NodeNext import written with the emitted .js extension maps to its .ts source', () => { + const repo = fixture(); + commit(repo, { + 'src/leaf.ts': 'export const leaf = 1;\n', + 'src/mid.ts': "import { leaf } from './leaf.js';\nexport const mid = leaf;\n", + 'src/view.tsx': 'export const View = () => null;\n', + 'src/esm.mts': 'export const esm = 1;\n', + 'src/both.js': 'export const both = 1;\n', + 'src/both.ts': 'export const both = 2;\n', + 'src/panel.tsx': 'export const Panel = () => null;\n', + 'tests/fixtures/config.js/a.json': '{}\n', + 'tests/fixtures/config.ts': 'export const config = 1;\n', + 'tests/fixture.ts': "export { mid } from '../src/mid.js';\n", + 'tests/app.test.ts': "import { readdirSync } from 'node:fs';\nimport { mid } from './fixture.js';\nimport { View } from '../src/view.jsx';\nimport { esm } from '../src/esm.mjs';\nimport { both } from '../src/both.js';\nimport { Panel } from '../src/panel.js';\nvoid [mid, View, esm, both, Panel, readdirSync('tests/fixtures/config.js')];\n", + }); + const g = buildGraph(repo.work); + for (const f of ['tests/fixture.ts', 'src/mid.ts', 'src/leaf.ts', 'src/view.tsx', 'src/esm.mts', 'src/both.ts', 'src/both.js', 'src/panel.tsx', 'tests/fixtures/config.js/a.json']) expect(testsFor(g, f).tests).toEqual(['tests/app.test.ts']); + expect(testsFor(g, 'src/both.ts').provenance).toEqual(['tests/app.test.ts ← src/both.ts (import)']); + expect(testsFor(g, 'tests/fixtures/config.ts').tests).toEqual([]); + }); +}); diff --git a/test/pregate-rspec-convention.test.ts b/test/pregate-rspec-convention.test.ts new file mode 100644 index 000000000..4d51a096f --- /dev/null +++ b/test/pregate-rspec-convention.test.ts @@ -0,0 +1,60 @@ +/** + * Rails autoloads constants, so a Ruby source names no require a spec could + * follow. The RSpec convention maps it instead: `app/.rb` and + * `lib/.rb` select `spec/_spec.rb` and `spec/lib/_spec.rb` + * when that spec exists, at the repo root or under an engine's own root. + */ +import { afterEach, describe, expect, test } from 'bun:test'; +import { buildGraph, testsFor } from '../lib/pregate/import-graph'; +import { cleanup, commit, makeFixtureRepo, type FixtureRepo } from './helpers/pregate-fixture'; + +const repos: FixtureRepo[] = []; +const fixture = () => { const r = makeFixtureRepo(); repos.push(r); return r; }; +afterEach(() => { while (repos.length) cleanup(repos.pop()!); }); + +describe('import graph: RSpec specs mirror app/ and lib/', () => { + // Value: protects=a Rails source under app/ or lib/ selects the spec at its conventional path, though no require names it; + // fails_when=the RSpec convention edge is removed; why_new=Ruby sources had no test lane in a Rails repo (#4211); + // seam=none; no_claim=running RSpec (graph only) + test('app/, a class-named directory and lib/ files select the spec at the conventional path', () => { + const repo = fixture(); + commit(repo, { + 'app/services/plugins/weather.rb': 'class Plugins::Weather; end\n', + 'app/services/plugins/weather/api.rb': 'class Plugins::Weather::Api; end\n', + 'app/models/user.rb': 'class User; end\n', + 'lib/parser.rb': 'class Parser; end\n', + 'spec/rails_helper.rb': '# loads the app\n', + 'spec/services/plugins/weather_spec.rb': "require 'rails_helper'\nRSpec.describe Plugins::Weather do; end\n", + 'spec/services/plugins/weather/api_spec.rb': "require 'rails_helper'\nRSpec.describe Plugins::Weather::Api do; end\n", + 'spec/models/user_spec.rb': "require 'rails_helper'\nRSpec.describe User do; end\n", + 'spec/lib/parser_spec.rb': "require 'rails_helper'\nRSpec.describe Parser do; end\n", + }); + const g = buildGraph(repo.work); + expect(testsFor(g, 'app/services/plugins/weather.rb').tests).toEqual(['spec/services/plugins/weather_spec.rb']); + expect(testsFor(g, 'app/services/plugins/weather/api.rb').tests).toEqual(['spec/services/plugins/weather/api_spec.rb']); + expect(testsFor(g, 'app/models/user.rb').tests).toEqual(['spec/models/user_spec.rb']); + expect(testsFor(g, 'lib/parser.rb').tests).toEqual(['spec/lib/parser_spec.rb']); + }); + + test('a spec under an engine or package root selects the source under that root', () => { + const repo = fixture(); + commit(repo, { + 'engines/billing/app/models/invoice.rb': 'class Invoice; end\n', + 'engines/billing/lib/billing.rb': 'module Billing; end\n', + 'engines/billing/spec/models/invoice_spec.rb': 'RSpec.describe Invoice do; end\n', + 'engines/billing/spec/lib/billing_spec.rb': 'RSpec.describe Billing do; end\n', + }); + const g = buildGraph(repo.work); + expect(testsFor(g, 'engines/billing/app/models/invoice.rb').tests).toEqual(['engines/billing/spec/models/invoice_spec.rb']); + expect(testsFor(g, 'engines/billing/lib/billing.rb').tests).toEqual(['engines/billing/spec/lib/billing_spec.rb']); + }); + + test('no edge when the conventional spec does not exist', () => { + const repo = fixture(); + commit(repo, { + 'app/models/order.rb': 'class Order; end\n', + 'spec/models/user_spec.rb': 'RSpec.describe User do; end\n', + }); + expect(testsFor(buildGraph(repo.work), 'app/models/order.rb').tests).toEqual([]); + }); +});