diff --git a/scripts/lib/coverage-scope.ts b/scripts/lib/coverage-scope.ts new file mode 100644 index 00000000..2d810587 --- /dev/null +++ b/scripts/lib/coverage-scope.ts @@ -0,0 +1,41 @@ +import path from "node:path"; + +/** + * Vitest matches `coverage.include` and `coverage.exclude` against each + * module's absolute path with picomatch `contains: true`, so a relative + * pattern may match anywhere in that path. `src/**` also matched + * `/vite.config.ts` when the checkout sat in a folder whose name + * ends in `src` (`~/work/app-src/`), and risk coverage then failed on rows + * outside the repository inventory. A pattern that starts with the root + * matches only inside it. + * + * The root is escaped with the expression Vitest's glob library (tinyglobby + * 0.2) uses for a cwd, so a root with glob characters still matches itself + * literally and the untested-file glob resolves the pattern back to + * `src/...` under that cwd. A Windows root is turned into forward slashes + * first; for a valid Windows path that escape equals tinyglobby's Windows one. + */ +const UNESCAPED_GLOB_SYMBOLS = + /(? { + if ( + pattern.length === 0 || + pattern.startsWith("/") || + pattern.startsWith("!") || + pattern.includes("\\") || + pattern.split("/").includes("..") + ) { + throw new TypeError( + `coverage pattern must be a forward-slash path inside the root: ${pattern}`, + ); + } + return `${escapedRoot.replace(/\/$/u, "")}/${pattern}`; + }); +} diff --git a/scripts/lib/v8-coverage-counter-semantics.ts b/scripts/lib/v8-coverage-counter-semantics.ts index 1859f130..334a9170 100644 --- a/scripts/lib/v8-coverage-counter-semantics.ts +++ b/scripts/lib/v8-coverage-counter-semantics.ts @@ -10,6 +10,8 @@ import { tmpdir } from "node:os"; import path from "node:path"; import { promisify } from "node:util"; +import { rootAnchoredGlobs } from "./coverage-scope.ts"; + const execFileAsync = promisify(execFile); const coverageMetrics = [ "lines", @@ -31,11 +33,21 @@ const expectedModules = Object.freeze( [runtimeModule, ...counterlessModules].sort(), ); const childOutputLimit = 1_800; +/** + * The fixture is copied into a folder whose name ends in `src` and has glob + * characters. Vitest matches coverage patterns anywhere in an absolute path, + * so an unanchored `src/**` also counts the fixture's root-level + * `outside-src.ts`, which the row-set check then reports as additional. The + * repository's own coverage patterns are anchored the same way + * (vitest.config.ts, scripts/lib/coverage-scope.ts). + */ +const fixtureCheckoutName = "checkout (copy)-src"; type ChildOutput = Readonly<{ stdout: string; stderr: string }>; type ChildInput = Readonly<{ repositoryRoot: string; ownedRoot: string; + fixtureRoot: string; configPath: string; }>; type CheckerOptions = Readonly<{ @@ -195,7 +207,7 @@ async function defaultRunVitest(input: ChildInput): Promise { "--no-color", ], { - cwd: input.ownedRoot, + cwd: input.fixtureRoot, encoding: "utf8", timeout: 30_000, maxBuffer: 256 * 1024, @@ -232,15 +244,16 @@ export async function checkV8CoverageCounterSemantics( repositoryRoot, "tests/fixtures/v8-coverage-counter-semantics", ); - const configPath = path.join(ownedRoot, "vitest.config.mjs"); + const fixtureRoot = path.join(ownedRoot, fixtureCheckoutName); + const configPath = path.join(fixtureRoot, "vitest.config.mjs"); const reportsDirectory = path.join(ownedRoot, "coverage"); try { - await cp(fixtureSource, ownedRoot, { recursive: true }); + await cp(fixtureSource, fixtureRoot, { recursive: true }); await writeFile( configPath, `export default ${JSON.stringify( { - root: ownedRoot, + root: fixtureRoot, test: { globals: true, include: ["counter-semantics.fixture.ts"], @@ -249,7 +262,7 @@ export async function checkV8CoverageCounterSemantics( provider: "v8", reportsDirectory, reporter: ["json-summary"], - include: ["src/**/*.ts"], + include: rootAnchoredGlobs(fixtureRoot, ["src/**/*.ts"]), }, }, }, @@ -259,7 +272,7 @@ export async function checkV8CoverageCounterSemantics( "utf8", ); try { - await runVitest({ repositoryRoot, ownedRoot, configPath }); + await runVitest({ repositoryRoot, ownedRoot, fixtureRoot, configPath }); } catch (error) { const record = isRecord(error) ? error : {}; throw new Error( @@ -287,7 +300,7 @@ export async function checkV8CoverageCounterSemantics( cause: error, }); } - return assertV8CoverageCounterSemantics(summary, ownedRoot); + return assertV8CoverageCounterSemantics(summary, fixtureRoot); } finally { await rm(ownedRoot, { recursive: true, force: true }); } diff --git a/tests/fixtures/v8-coverage-counter-semantics/counter-semantics.fixture.ts b/tests/fixtures/v8-coverage-counter-semantics/counter-semantics.fixture.ts index 9b572dc9..4cd2bb66 100644 --- a/tests/fixtures/v8-coverage-counter-semantics/counter-semantics.fixture.ts +++ b/tests/fixtures/v8-coverage-counter-semantics/counter-semantics.fixture.ts @@ -8,6 +8,7 @@ import { runtimeValue as namedRuntimeValue } from "./src/reexport-named.ts"; import { runtimeValue } from "./src/runtime.ts"; import { runtimeValue as starRuntimeValue } from "./src/reexport-star.ts"; import type {} from "./src/type-only.ts"; +import { outsideValue } from "./outside-src.ts"; test("loads every counter semantics module", () => { expect([ @@ -15,5 +16,6 @@ test("loads every counter semantics module", () => { importedRuntimeValue, namedRuntimeValue, starRuntimeValue, - ]).toEqual([1, 1, 1, 1]); + outsideValue, + ]).toEqual([1, 1, 1, 1, 1]); }); diff --git a/tests/fixtures/v8-coverage-counter-semantics/outside-src.ts b/tests/fixtures/v8-coverage-counter-semantics/outside-src.ts new file mode 100644 index 00000000..e526dfbb --- /dev/null +++ b/tests/fixtures/v8-coverage-counter-semantics/outside-src.ts @@ -0,0 +1,4 @@ +// Outside the fixture's `src/`. The checker copies this fixture into a folder +// whose name ends in `src`; an unanchored `src/**` coverage pattern counts +// this module too and the row-set check reports it as additional. +export const outsideValue = 1; diff --git a/tests/unit/coverage-scope.test.ts b/tests/unit/coverage-scope.test.ts new file mode 100644 index 00000000..d274d78c --- /dev/null +++ b/tests/unit/coverage-scope.test.ts @@ -0,0 +1,45 @@ +import path from "node:path"; + +import { describe, expect, it } from "vitest"; + +import { rootAnchoredGlobs } from "../../scripts/lib/coverage-scope.ts"; +import vitestConfig from "../../vitest.config.ts"; + +describe("root-anchored coverage patterns", () => { + it("prefixes each pattern with the root", () => { + expect( + rootAnchoredGlobs("/work/app-src", ["src/**/*.{ts,tsx}", "**/*.d.ts"]), + ).toEqual(["/work/app-src/src/**/*.{ts,tsx}", "/work/app-src/**/*.d.ts"]); + }); + + it("escapes glob characters in the root as Vitest's glob library does", () => { + expect( + rootAnchoredGlobs("/work/app (copy) [2] {x} *?|/re+(a)/@(b)/!(c)", ["src/**"]), + ).toEqual([ + "/work/app \\(copy\\) \\[2\\] \\{x\\} \\*\\?\\|/re\\+\\(a\\)/\\@\\(b\\)/\\!\\(c\\)/src/**", + ]); + }); + + it.each(["", "/src/**", "!src/**", "../src/**", "src/../**", "src\\**"])( + "refuses a pattern that is not a forward-slash path inside the root: %j", + (pattern) => { + expect(() => rootAnchoredGlobs("/work/app", [pattern])).toThrow( + /inside the root/u, + ); + }, + ); + + it("anchors every repository coverage pattern at the repository root", () => { + const coverage = vitestConfig.test?.coverage; + if (coverage === undefined || coverage.provider !== "v8") { + throw new TypeError("vitest.config.ts has no V8 coverage options"); + } + const prefix = rootAnchoredGlobs(process.cwd(), ["x"])[0]?.slice(0, -1) ?? ""; + const patterns = [...(coverage.include ?? []), ...(coverage.exclude ?? [])]; + + expect(patterns.length).toBeGreaterThan(0); + for (const pattern of patterns) expect(pattern.startsWith(prefix)).toBe(true); + expect(coverage.include).toEqual([`${prefix}src/**/*.{ts,tsx}`]); + expect(path.isAbsolute(prefix)).toBe(true); + }); +}); diff --git a/tests/unit/v8-coverage-counter-semantics.test.ts b/tests/unit/v8-coverage-counter-semantics.test.ts index 07c97b69..34ed249a 100644 --- a/tests/unit/v8-coverage-counter-semantics.test.ts +++ b/tests/unit/v8-coverage-counter-semantics.test.ts @@ -15,6 +15,7 @@ import { assertV8CoverageCounterSemantics, checkV8CoverageCounterSemantics, } from "../../scripts/lib/v8-coverage-counter-semantics.ts"; +import { rootAnchoredGlobs } from "../../scripts/lib/coverage-scope.ts"; import { childProcessTimeout } from "../helpers/child-process-timeout.ts"; const roots: string[] = []; @@ -184,6 +185,36 @@ describe("V8 coverage counter semantics", () => { await expect(access(ownedRoot)).rejects.toMatchObject({ code: "ENOENT" }); }); + it("copies the fixture under a src-suffixed folder with root-anchored patterns", async () => { + const ownedRoot = await mkdtemp(path.join(tmpdir(), "v8-counter-scope-")); + roots.push(ownedRoot); + let child: { fixtureRoot: string; config: string } | undefined; + + await expect( + checkV8CoverageCounterSemantics({ + repositoryRoot: process.cwd(), + createOwnedRoot: async () => ownedRoot, + runVitest: async (input) => { + child = { + fixtureRoot: input.fixtureRoot, + config: await readFile(input.configPath, "utf8"), + }; + await access(path.join(input.fixtureRoot, "outside-src.ts")); + return { stdout: "", stderr: "" }; + }, + }), + ).rejects.toThrow(/coverage summary is missing/u); + + expect(child?.fixtureRoot.startsWith(`${ownedRoot}${path.sep}`)).toBe(true); + expect(path.basename(child?.fixtureRoot ?? "")).toMatch(/[()].*src$/u); + const config = JSON.parse( + (child?.config ?? "").replace(/^export default /u, "").replace(/;\s*$/u, ""), + ) as { test: { coverage: { include: string[] } } }; + expect(config.test.coverage.include).toEqual( + rootAnchoredGlobs(child?.fixtureRoot ?? "", ["src/**/*.ts"]), + ); + }); + it("keeps the child fixture outside main Vitest discovery", async () => { const outputRoot = await mkdtemp(path.join(tmpdir(), "v8-counter-list-")); roots.push(outputRoot); diff --git a/vitest.config.ts b/vitest.config.ts index 7f09b61e..3e60577f 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,7 +1,13 @@ +import path from "node:path"; +import { fileURLToPath } from "node:url"; + import { configDefaults, defineConfig } from "vitest/config"; +import { rootAnchoredGlobs } from "./scripts/lib/coverage-scope.ts"; import { isNpmScopeEnvironmentName } from "./scripts/lib/package-script-graph.ts"; +const repositoryRoot = path.dirname(fileURLToPath(import.meta.url)); + // Tests must not inherit a caller's production React/runtime selection. // Package scripts also launch Vitest through scripts/run-vitest.ts, which owns // NODE_ENV before the Vitest process starts. Keeping the config deterministic @@ -57,8 +63,11 @@ export default defineConfig({ provider: "v8", reportsDirectory: "artifacts/tests/coverage", reporter: ["text", "json-summary", "lcov"], - include: ["src/**/*.{ts,tsx}"], - exclude: ["**/*.d.ts", "**/*.stories.*"], + // Vitest matches these against absolute paths anywhere in the path; + // anchoring them at the root keeps a checkout under `…src/` from + // pulling root files into the coverage universe. + include: rootAnchoredGlobs(repositoryRoot, ["src/**/*.{ts,tsx}"]), + exclude: rootAnchoredGlobs(repositoryRoot, ["**/*.d.ts", "**/*.stories.*"]), }, }, });