diff --git a/tests/boundary/filesystem/ciTestProjects.test.ts b/tests/boundary/filesystem/ciTestProjects.test.ts index d980ac215e..cd92d63d54 100644 --- a/tests/boundary/filesystem/ciTestProjects.test.ts +++ b/tests/boundary/filesystem/ciTestProjects.test.ts @@ -1,7 +1,7 @@ import { execFileSync } from "node:child_process"; import { mkdir, writeFile } from "node:fs/promises"; import { availableParallelism } from "node:os"; -import { join } from "node:path"; +import { dirname, join, resolve } from "node:path"; import { pathToFileURL } from "node:url"; import { describe, expect, it } from "vitest"; @@ -16,51 +16,36 @@ import { } from "../../../scripts/lib/development-tests.mjs"; import { - execFileOutput, - execFileOutputFailure, -} from "../../../src/process/ExecFileOutput.js"; + processTest, + type TestProcesses, +} from "../../support/process/processFixture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { writeRepositoryOxlintConfiguration } from "../../fixtures/oxlintConfiguration.js"; const repository = process.cwd(); const oxlint = join(repository, "node_modules/oxlint/bin/oxlint"); -const oxlintConfig = join(repository, ".oxlintrc.json"); const runOxlint = async ( + processes: TestProcesses, cwd: string, config: string, files: readonly string[], ) => { - let stdout: string; - let exitCode: string | number = 0; - try { - ({ stdout } = await execFileOutput( - process.execPath, - [oxlint, "--threads=1", "--config", config, "--format", "json", ...files], - { - cwd, - timeout: 60_000, - maxBuffer: 8 * 1024 * 1024, - stopSignal: "SIGTERM", - }, - )); - } catch (cause) { - const failure = execFileOutputFailure(cause); - if ( - failure === undefined || - failure.code !== 1 || - failure.signal !== null || - failure.killed || - failure.outputTruncated - ) - throw cause; - stdout = failure.stdout; - exitCode = failure.code; - } + const result = await processes.run( + process.execPath, + [oxlint, "--threads=1", "--config", config, "--format", "json", ...files], + { cwd }, + ); + expect(result.signal, result.stderr).toBeNull(); + expect([0, 1], result.stderr).toContain(result.exitCode); return { - exitCode, - report: JSON.parse(stdout) as { - diagnostics?: Array<{ code?: string; severity: string }>; + exitCode: result.exitCode, + report: JSON.parse(result.stdout) as { + diagnostics?: Array<{ + code?: string; + severity: string; + filename: string; + }>; }, }; }; @@ -116,12 +101,16 @@ describe("canonical test-project ownership", () => { }); describe("Vitest project configuration", () => { - it("uses canonical test-project selectors in Vitest", async () => { - const root = await createTestTempDirectory("rea-vitest-project-selectors-"); - const script = join(root, "project-selectors.mjs"); - await writeFile( - script, - ` + processTest( + "uses canonical test-project selectors in Vitest", + async ({ processes }) => { + const root = await createTestTempDirectory( + "rea-vitest-project-selectors-", + ); + const script = join(root, "project-selectors.mjs"); + await writeFile( + script, + ` const { default: config } = await import(process.argv[2]); console.log(JSON.stringify(config.test.projects.map(({ test }) => ({ name: test.name, @@ -129,40 +118,39 @@ describe("Vitest project configuration", () => { exclude: test.exclude ?? [], })))); `, - ); - const result = await execFileOutput( - process.execPath, - [ - "--import", - "tsx", - script, - pathToFileURL(join(repository, "vitest.config.ts")).href, - ], - { - cwd: repository, - timeout: 10_000, - maxBuffer: 64 * 1024, - stopSignal: "SIGTERM", - }, - ); - const selectors = JSON.parse(result.stdout) as Array<{ - name: string; - include: string[]; - exclude: string[]; - }>; - const canonical = [...SOURCE_TEST_PROJECTS, ...BUILT_TEST_PROJECTS].map( - ({ name, include, exclude }) => ({ - name, - include, - exclude: exclude ?? [], - }), - ); - expect(selectors.sort((a, b) => a.name.localeCompare(b.name))).toEqual( - canonical.sort((a, b) => a.name.localeCompare(b.name)), - ); - }); + ); + const result = await processes.run( + process.execPath, + [ + "--import", + "tsx", + script, + pathToFileURL(join(repository, "vitest.config.ts")).href, + ], + { + cwd: repository, + }, + ); + expect(result.exitCode, result.stderr).toBe(0); + const selectors = JSON.parse(result.stdout) as Array<{ + name: string; + include: string[]; + exclude: string[]; + }>; + const canonical = [...SOURCE_TEST_PROJECTS, ...BUILT_TEST_PROJECTS].map( + ({ name, include, exclude }) => ({ + name, + include, + exclude: exclude ?? [], + }), + ); + expect(selectors.sort((a, b) => a.name.localeCompare(b.name))).toEqual( + canonical.sort((a, b) => a.name.localeCompare(b.name)), + ); + }, + ); - it.each( + processTest.for( [ { flags: [], timeout: 30_000, lines: 68 }, { flags: ["--coverage"], timeout: 60_000, lines: 68 }, @@ -197,7 +185,10 @@ describe("Vitest project configuration", () => { ), )( "resolves worker resources from actual CLI options: $flags (hosted: $hosted)", - async ({ flags, timeout, lines, hosted, workers, scale }) => { + async ( + { flags, timeout, lines, hosted, workers, scale }, + { processes }, + ) => { const root = await createTestTempDirectory("rea-vitest-worker-policy-"); const script = join(root, "worker-policy.mjs"); await writeFile( @@ -219,21 +210,19 @@ describe("Vitest project configuration", () => { })); `, ); - const result = await execFileOutput( + const result = await processes.run( process.execPath, ["--import", "tsx", script, ...flags], { cwd: repository, // Select the runner explicitly; CI itself sets GITHUB_ACTIONS. env: { ...process.env, GITHUB_ACTIONS: hosted ? "true" : "" }, - timeout: 10_000, - maxBuffer: 64 * 1024, - stopSignal: "SIGTERM", }, ); // Deadlines are hang detection owned by the project: the source // deadline scales with coverage and hosted runners, built projects allow // four times it, and the long-running tag ten times. + expect(result.exitCode, result.stderr).toBe(0); expect(JSON.parse(result.stdout)).toEqual({ execArgv: ["--max-old-space-size=512"], deadlines: { @@ -265,85 +254,113 @@ describe("test module isolation", () => { expect(projectForTest(path)).toMatchObject({ name, group }); }); - it("uses one isolation policy for Vitest and binding-aware mock lint", async () => { - const files = execFileSync("git", ["ls-files", "*.test.ts"], { - cwd: repository, - encoding: "utf8", - }) - .trim() - .split("\n") - .filter(Boolean); - const root = await createTestTempDirectory("rea-vitest-project-policy-"); - const script = join(root, "project-isolation.mjs"); - await writeFile( - script, - ` + processTest( + "uses canonical module isolation in the actual Vitest configuration", + async ({ processes }) => { + const files = execFileSync("git", ["ls-files", "*.test.ts"], { + cwd: repository, + encoding: "utf8", + }) + .trim() + .split("\n") + .filter(Boolean); + const root = await createTestTempDirectory("rea-vitest-project-policy-"); + const script = join(root, "project-isolation.mjs"); + await writeFile( + script, + ` import config from ${JSON.stringify(pathToFileURL(join(repository, "vitest.config.ts")).href)}; console.log(JSON.stringify(Object.fromEntries( config.test.projects.map(project => [project.test.name, project.test.isolate]), ))); `, - ); - const result = await execFileOutput( - process.execPath, - ["--import", "tsx", script], - { + ); + const result = await processes.run( + process.execPath, + ["--import", "tsx", script], + { + cwd: repository, + }, + ); + expect(result.exitCode, result.stderr).toBe(0); + const isolated = JSON.parse(result.stdout) as Record; + expect(files.length).toBeGreaterThan(0); + expect( + files.filter((file) => { + const project = projectForTest(file); + return isolated[project.name] !== (project.isolate !== false); + }), + ).toEqual([]); + + // Repository-wide rule execution belongs to the normal lint gate. The + // configured rule is exercised across every project by the probes below. + }, + ); + + processTest( + "enforces configured module-mock isolation across every canonical project", + async ({ processes }) => { + const root = await createTestTempDirectory("rea-vitest-mock-probe-"); + const config = await writeRepositoryOxlintConfiguration(root); + const files = execFileSync("git", ["ls-files", "*.test.ts"], { cwd: repository, - timeout: 10_000, - maxBuffer: 64 * 1024, - stopSignal: "SIGTERM", - }, - ); - const isolated = JSON.parse(result.stdout) as Record; - expect(files.length).toBeGreaterThan(0); - expect( - files.filter((file) => { - const project = projectForTest(file); - return isolated[project.name] !== (project.isolate !== false); - }), - ).toEqual([]); - - const { report } = await runOxlint(repository, oxlintConfig, files); - expect( - report.diagnostics?.filter( - ({ code, severity }) => - code === "rea(no-shared-module-mock)" && severity === "error", - ), - ).toEqual([]); - }); - - it("rejects actual Vitest module-mock aliases only in shared projects", async () => { - const root = await createTestTempDirectory("rea-vitest-mock-probe-"); - const sharedDirectory = join(root, "tests/evaluation"); - const isolatedDirectory = join(root, "tests/process-global"); - await mkdir(sharedDirectory, { recursive: true }); - await mkdir(isolatedDirectory, { recursive: true }); - const config = await writeRepositoryOxlintConfiguration(root); - const probe = async (directory: string, source: string) => { - const path = join(directory, "mock-probe.test.ts"); - await writeFile(path, source); - return runOxlint(root, config, [path]); - }; - const shared = await probe( - sharedDirectory, - 'import { vi as testing } from "vitest"; testing.mock("./dependency.js");', - ); - const isolated = await probe( - isolatedDirectory, - 'import * as testing from "vitest"; testing.vi.doMock("./dependency.js");', - ); - expect(shared.exitCode).toBe(1); - expect( - shared.report.diagnostics?.filter( - ({ code, severity }) => - code === "rea(no-shared-module-mock)" && severity === "error", - ), - ).toHaveLength(1); - expect(isolated.exitCode).toBe(0); - expect( - isolated.report.diagnostics?.filter( - ({ code }) => code === "rea(no-shared-module-mock)", - ), - ).toEqual([]); - }); + encoding: "utf8", + }) + .trim() + .split("\n") + .filter(Boolean); + const probes = [...SOURCE_TEST_PROJECTS, ...BUILT_TEST_PROJECTS].map( + (project) => { + const path = files.find( + (file) => projectForTest(file).name === project.name, + ); + if (path === undefined) + throw new Error(`No tracked test in ${project.name}`); + return { path, isolated: project.isolate !== false }; + }, + ); + for (const form of ["named", "namespace"] as const) { + for (const probe of probes) { + const path = join(root, probe.path); + await mkdir(dirname(path), { recursive: true }); + const receiver = form === "named" ? "testing" : "testing.vi"; + await writeFile( + path, + [ + form === "named" + ? 'import { vi as testing } from "vitest";' + : 'import * as testing from "vitest";', + `${receiver}.mock("./dependency.js");`, + `${receiver}.doMock("./other.js");`, + 'const local = { mock() {} }; local.mock("unrelated");', + ].join("\n"), + ); + } + const { exitCode, report } = await runOxlint( + processes, + root, + config, + probes.map(({ path }) => path), + ); + expect(exitCode).toBe(1); + const errors = + report.diagnostics?.filter( + ({ code, severity }) => + code === "rea(no-shared-module-mock)" && severity === "error", + ) ?? []; + expect(errors).toHaveLength( + 2 * probes.filter(({ isolated }) => !isolated).length, + ); + for (const probe of probes) { + const selected = errors.filter( + ({ filename }) => + resolve(root, filename) === resolve(root, probe.path), + ); + expect(selected, `${form}: ${probe.path}`).toHaveLength( + probe.isolated ? 0 : 2, + ); + } + } + }, + ); });