From cd744b1bfbca7d0951762d9711c6a1263dfe4bfd Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Fri, 27 Nov 2026 09:54:43 +0800 Subject: [PATCH] fix(ci): require complete path references for test coverage (#14819) --- scripts/ci/test-selection.mjs | 58 +++++++++++------------- tests/evaluation/ciTestSelection.test.ts | 25 ++++++++++ 2 files changed, 51 insertions(+), 32 deletions(-) diff --git a/scripts/ci/test-selection.mjs b/scripts/ci/test-selection.mjs index c522d04661d..2607827c682 100644 --- a/scripts/ci/test-selection.mjs +++ b/scripts/ci/test-selection.mjs @@ -380,18 +380,25 @@ export function traceAffectedTests({ files, snapshot }) { isTest(path) || path.startsWith("scripts/") || path.startsWith("tests/fixtures/"); + const mentionsPath = (source, path) => source.includes(path); + // A possible mention can select extra tests. Only a complete path literal + // or a traced import establishes coverage for an individual input. + const quotesPath = (source, path) => + ["'", '"', "`"].some((quote) => source.includes(quote + path + quote)); + const names = (file, paths, matches = mentionsPath) => + paths.some((path) => matches(sources.get(file) ?? "", path)); + // A seed and every helper that imports or names it, directly or through another. - const throughHelpers = (seed) => { + const throughHelpers = (seed, matches = mentionsPath) => { const reached = new Set(seed); for (let grew = true; grew;) { grew = false; const namedReached = [...reached].filter(isNamedTarget); for (const helper of helpers) { if (reached.has(helper)) continue; - const source = sources.get(helper) ?? ""; if ( imports.get(helper).some((target) => reached.has(target)) || - namedReached.some((target) => source.includes(target)) + names(helper, namedReached, matches) ) { reached.add(helper); grew = true; @@ -415,38 +422,25 @@ export function traceAffectedTests({ files, snapshot }) { const named = (reached) => [ ...new Set([...files, ...[...reached].filter(isNamedTarget)]), ]; - const names = (test, paths) => - paths.some((path) => (sources.get(test) ?? "").includes(path)); - // A fixture without a traced import or literal path may be loaded through - // a computed locator. Other selected files do not establish fixture coverage. - for (const fixture of files.filter(isTraceableFixture)) { - const reached = throughHelpers([fixture]); - const paths = [fixture, ...[...reached].filter(isNamedTarget)]; - if ( - !tests.some( - (test) => - imports.get(test).some((target) => reached.has(target)) || - names(test, paths), - ) - ) - untraced.push(`Input is not traced to tests: ${fixture}`); - } - - // A changed shared module that no test reaches has no coverage in a - // selection, even when other changes select tests. - for (const library of changedModules.filter(isScriptLibrary)) { - const reached = throughHelpers([library]); + const hasTestConsumer = (path) => { + const reached = throughHelpers([path], quotesPath); const paths = [...reached].filter(isNamedTarget); - if ( - !tests.some( - (test) => - imports.get(test).some((target) => reached.has(target)) || - names(test, paths), - ) - ) + return tests.some( + (test) => + imports.get(test).some((target) => reached.has(target)) || + names(test, paths, quotesPath), + ); + }; + + // Check each input independently: another change can select tests without + // exercising this fixture or library. + for (const fixture of files.filter(isTraceableFixture)) + if (!hasTestConsumer(fixture)) + untraced.push(`Input is not traced to tests: ${fixture}`); + for (const library of changedModules.filter(isScriptLibrary)) + if (!hasTestConsumer(library)) untraced.push(`No test reaches shared script module: ${library}`); - } // A shared script module that another script, configuration file, hook or // workflow imports or names as a standalone file name can shape every run. diff --git a/tests/evaluation/ciTestSelection.test.ts b/tests/evaluation/ciTestSelection.test.ts index 612f70741c3..84472d1561f 100644 --- a/tests/evaluation/ciTestSelection.test.ts +++ b/tests/evaluation/ciTestSelection.test.ts @@ -606,6 +606,31 @@ it("traces shared script modules to their tests and falls back for other consume }); }); +it("requires the complete fixture path on direct and transitive consumer edges", () => { + const fixture = "tests/fixtures/card.json"; + const helper = "tests/fixtures/loadCard.ts"; + const direct = "tests/boundary/filesystem/card.test.ts"; + const indirect = "tests/boundary/filesystem/helper.test.ts"; + const reference = (path: string) => `readFileSync(${JSON.stringify(path)});`; + const sources = { + [fixture]: "{}", + [direct]: reference(`${fixture}.txt`), + [helper]: reference(`${fixture} (v2).json`), + [indirect]: 'import "../../fixtures/loadCard.js";', + }; + expect(plan([fixture], sources)).toMatchObject({ + mode: "full", + reason: `Input is not traced to tests: ${fixture}`, + }); + for (const consumer of [direct, helper]) { + expect( + selectedFiles( + plan([fixture], { ...sources, [consumer]: reference(fixture) }), + ), + ).toEqual([direct, indirect]); + } +}); + it("traces JavaScript fixture modules transitively through JS and TS helpers", () => { const sources = { "tests/fixtures/fakeHopperReplies.mjs":