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

+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());
}
}