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
This commit is contained in:
authored and GitHub committed 2026-11-22 12:32:40 +00:00
1 parent 7530280407
commit 4bd15f8a3d
5 files changed
+113 -4

No files matched your search

@@ -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/<path>.rb` now selects `spec/<path>_spec.rb` and `lib/<path>.rb` selects `spec/lib/<path>_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.
+7 -1
View File
@@ -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/<name>` →
`test/<name>*.test.ts` convention ∪ `dependencies`; Dart `import`/`export`/`part`
`test/<name>*.test.ts` convention ∪ RSpec's `[<root>/]spec/<path>_spec.rb` →
`[<root>/]app/<path>.rb` or `[<root>/]<path>.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:<name>/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 — <file> 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
+17 -3
View File
@@ -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/<name>` → `test/<name>*.test.*` convention (with and without
* the `gstack-` prefix);
* the `gstack-` prefix), and RSpec's `[<root>/]spec/<path>_spec.rb` →
* `[<root>/]app/<path>.rb` or `[<root>/]<path>.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 `<crate>/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:<name>/x` through every tracked pubspec.yaml to `<root>/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<string>, dirs: Set<string>, 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>): 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>): 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<string, string[]>; 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());
}
}
+28
View File
@@ -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([]);
});
});
+60
View File
@@ -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/<path>.rb` and
* `lib/<path>.rb` select `spec/<path>_spec.rb` and `spec/lib/<path>_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([]);
});
});