From 96391dc69aee7eea0d2c474525705445ef123c45 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 25 Sep 2026 23:19:09 +0900 Subject: [PATCH] fix(scripts): report each removal check's own failure and isolate fixtures (14-08) The JUnit report said failures="1" and put the residue list into every failed testcase, and three scripts reused a fixed .tmp path without cleanup on error. A shared renderer counts failures and records each check's cause (command and exit code, or its own findings); every script uses mkdtemp and finally. Co-Authored-By: Claude Opus 5.5 --- scripts/lib/removal-fixture.ts | 73 ++++++ ...st-browser-file-storage-runtime-removal.ts | 210 +++++++++--------- scripts/test-optional-recipe-removal.ts | 146 ++++++------ scripts/test-realtime-runtime-removal.ts | 186 ++++++++-------- scripts/test-sample-removal.ts | 113 +++++----- tests/unit/removal-fixture.test.ts | 28 +++ 6 files changed, 421 insertions(+), 335 deletions(-) diff --git a/scripts/lib/removal-fixture.ts b/scripts/lib/removal-fixture.ts index 9dbd796a..884becff 100644 --- a/scripts/lib/removal-fixture.ts +++ b/scripts/lib/removal-fixture.ts @@ -2,6 +2,7 @@ import { spawnSync } from "node:child_process"; import { cp, mkdir, + mkdtemp, readFile, readdir, rm, @@ -160,6 +161,78 @@ function runFixtureGit(root: string, argv: readonly string[]): void { } } +/** + * 14-08. A fresh fixture directory per run: two runs, or a run after one that + * crashed, no longer share (and overwrite) one fixed path. Callers remove it + * in `finally`. + */ +export async function createRemovalFixtureRoot(name: string): Promise { + const parent = path.resolve(".tmp"); + await mkdir(parent, { recursive: true }); + return mkdtemp(path.join(parent, `${name}-`)); +} + +export type RemovalCheck = Readonly<{ + name: string; + passed: boolean; + /** Why this check failed; each check names its own cause. */ + failure?: string; +}>; + +export function runRemovalFixtureCheck( + name: string, + root: string, + pnpmCli: string, + script: string, + extra: readonly string[] = [], +): RemovalCheck { + const status = spawnSync(process.execPath, [pnpmCli, script, ...extra], { + cwd: root, + stdio: "inherit", + env: process.env, + }).status; + return status === 0 + ? { name, passed: true } + : { + name, + passed: false, + failure: `corepack pnpm ${[script, ...extra].join(" ")} exited ${status ?? "on a signal"}`, + }; +} + +function escapeXml(value: string): string { + return value + .replaceAll("&", "&") + .replaceAll("<", "<") + .replaceAll(">", ">") + .replaceAll('"', """); +} + +/** + * 14-08. The report used to say `failures="1"` whatever failed, and put the + * residue list into every failed testcase, so a type error read as residue. + */ +export function renderRemovalJunit( + suite: string, + checks: readonly RemovalCheck[], +): string { + const failures = checks.filter((check) => !check.passed).length; + const cases = checks + .map((check) => + `${ + check.passed + ? "" + : `` + }`, + ) + .join(""); + return ( + `\n` + + `` + + `${cases}\n` + ); +} + export function runRemovalFixturePnpm( root: string, pnpmCli: string, diff --git a/scripts/test-browser-file-storage-runtime-removal.ts b/scripts/test-browser-file-storage-runtime-removal.ts index 1d23bdc2..a1e98de2 100644 --- a/scripts/test-browser-file-storage-runtime-removal.ts +++ b/scripts/test-browser-file-storage-runtime-removal.ts @@ -8,18 +8,19 @@ import path from "node:path"; import { assertNoRuntimeImports, + createRemovalFixtureRoot, prepareRemovalFixture, pruneRemovalFixtureCiContract, regenerateRemovalFixtureWorkflow, removeRuntimeDependentTests, + renderRemovalJunit, requireRemovalFixtureEnvironment, - runRemovalFixturePnpm, + runRemovalFixtureCheck, + type RemovalCheck, } from "./lib/removal-fixture.ts"; -const fixtureRoot = path.resolve( - ".tmp/browser-file-storage-runtime-removal", -); const pnpmCli = requireRemovalFixtureEnvironment("npm_execpath"); +const fixtureRoot = await createRemovalFixtureRoot("browser-file-storage-runtime-removal"); const runtimePaths = [ "src/application/ports/browser-file-storage", "src/application/ports/browser-transfer", @@ -47,124 +48,117 @@ const removedEvidencePathFragments = [ "browser-capabilities", "browser-file-storage-runtime-removal", ] as const; -function runPnpm(script: string): boolean { - return runRemovalFixturePnpm(fixtureRoot, pnpmCli, script); +function check(name: string, script: string): RemovalCheck { + return runRemovalFixtureCheck(name, fixtureRoot, pnpmCli, script); } -await prepareRemovalFixture(fixtureRoot); +let checks: RemovalCheck[]; +let removedRuntimeTests: number; +try { + await prepareRemovalFixture(fixtureRoot); -const removedRuntimeTests = - await removeRuntimeDependentTests(fixtureRoot, runtimeSourceRoots); -for (const runtimePath of runtimePaths) { - await rm(path.join(fixtureRoot, runtimePath), { - recursive: true, + removedRuntimeTests = + await removeRuntimeDependentTests(fixtureRoot, runtimeSourceRoots); + for (const runtimePath of runtimePaths) { + await rm(path.join(fixtureRoot, runtimePath), { + recursive: true, + force: true, + }); + } + + const catalogPath = path.join( + fixtureRoot, + "config/recipes/frontend-capability-recipes.json", + ); + const catalog = JSON.parse(await readFile(catalogPath, "utf8")) as { + recipes: Array>; + }; + const browserFileRuntimeRecipeIds = new Set([ + "offline-indexeddb", + "service-worker-pwa", + "file-transfer", + ]); + let removedRuntimeEntries = 0; + for (const recipe of catalog.recipes) { + if ( + typeof recipe.id !== "string" || + !browserFileRuntimeRecipeIds.has(recipe.id) || + !Object.hasOwn(recipe, "referenceRuntime") + ) { + continue; + } + delete recipe.referenceRuntime; + removedRuntimeEntries += 1; + } + if (removedRuntimeEntries !== 3) { + throw new Error( + `Expected three reference runtime catalog entries, removed ${removedRuntimeEntries}`, + ); + } + await writeFile(catalogPath, `${JSON.stringify(catalog, null, 2)}\n`); + + await rm(path.join(fixtureRoot, "playwright.capabilities.config.ts"), { force: true, }); -} - -const catalogPath = path.join( - fixtureRoot, - "config/recipes/frontend-capability-recipes.json", -); -const catalog = JSON.parse(await readFile(catalogPath, "utf8")) as { - recipes: Array>; -}; -const browserFileRuntimeRecipeIds = new Set([ - "offline-indexeddb", - "service-worker-pwa", - "file-transfer", -]); -let removedRuntimeEntries = 0; -for (const recipe of catalog.recipes) { - if ( - typeof recipe.id !== "string" || - !browserFileRuntimeRecipeIds.has(recipe.id) || - !Object.hasOwn(recipe, "referenceRuntime") - ) { - continue; - } - delete recipe.referenceRuntime; - removedRuntimeEntries += 1; -} -if (removedRuntimeEntries !== 3) { - throw new Error( - `Expected three reference runtime catalog entries, removed ${removedRuntimeEntries}`, + await rm( + path.join(fixtureRoot, "scripts/check-browser-file-storage-boundaries.ts"), + { force: true }, + ); + await rm( + path.join(fixtureRoot, "scripts/verify-browser-capability-evidence.ts"), + { force: true }, + ); + await rm( + path.join(fixtureRoot, "scripts/test-browser-file-storage-runtime-removal.ts"), + { force: true }, + ); + // The root snapshot locks the full repository inventory. This removal fixture + // validates its smaller registry through check:ci and its regenerated workflow. + await rm( + path.join(fixtureRoot, "tests/unit/ci-workflow-generation.test.ts"), + { force: true }, + ); + await rm( + path.join( + fixtureRoot, + "tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap", + ), + { force: true }, ); -} -await writeFile(catalogPath, `${JSON.stringify(catalog, null, 2)}\n`); -await rm(path.join(fixtureRoot, "playwright.capabilities.config.ts"), { - force: true, -}); -await rm( - path.join(fixtureRoot, "scripts/check-browser-file-storage-boundaries.ts"), - { force: true }, -); -await rm( - path.join(fixtureRoot, "scripts/verify-browser-capability-evidence.ts"), - { force: true }, -); -await rm( - path.join(fixtureRoot, "scripts/test-browser-file-storage-runtime-removal.ts"), - { force: true }, -); -// The root snapshot locks the full repository inventory. This removal fixture -// validates its smaller registry through check:ci and its regenerated workflow. -await rm( - path.join(fixtureRoot, "tests/unit/ci-workflow-generation.test.ts"), - { force: true }, -); -await rm( - path.join( + await pruneRemovalFixtureCiContract({ + root: fixtureRoot, + removedScripts, + removedEvidencePathFragments, + }); + await regenerateRemovalFixtureWorkflow(fixtureRoot); + await assertNoRuntimeImports( fixtureRoot, - "tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap", - ), - { force: true }, -); + runtimeSourceRoots, + "browser file/storage", + ); -await pruneRemovalFixtureCiContract({ - root: fixtureRoot, - removedScripts, - removedEvidencePathFragments, -}); -await regenerateRemovalFixtureWorkflow(fixtureRoot); -await assertNoRuntimeImports( - fixtureRoot, - runtimeSourceRoots, - "browser file/storage", -); - -const checks: Array = [ - ["typecheck", runPnpm("check:types")], - ["lint", runPnpm("lint")], - ["architecture", runPnpm("check:architecture")], - ["test", runPnpm("test:all")], - ["build", runPnpm("build")], - ["optional-catalog", runPnpm("check:optional-recipes:source")], - ["ci-contract", runPnpm("check:ci")], -]; -const passed = checks.every(([, result]) => result); + checks = [ + check("typecheck", "check:types"), + check("lint", "lint"), + check("architecture", "check:architecture"), + check("test", "test:all"), + check("build", "build"), + check("optional-catalog", "check:optional-recipes:source"), + check("ci-contract", "check:ci"), + ]; +} finally { + await rm(fixtureRoot, { recursive: true, force: true }); +} +const failed = checks.filter((entry) => !entry.passed); await mkdir("artifacts/tests", { recursive: true }); await writeFile( "artifacts/tests/browser-file-storage-runtime-removal.xml", - `\n` + - `` + - checks - .map( - ([name, result]) => - `${result ? "" : ""}`, - ) - .join("") + - `\n`, + renderRemovalJunit("browser-file-storage-runtime-removal", checks), ); -await rm(fixtureRoot, { recursive: true, force: true }); - -if (!passed) { +if (failed.length > 0) { process.stderr.write( - `Browser file/storage runtime removal failed: ${checks - .filter(([, result]) => !result) - .map(([name]) => name) - .join(", ")}\n`, + `Browser file/storage runtime removal failed: ${failed.map((entry) => entry.name).join(", ")}\n`, ); process.exit(1); } diff --git a/scripts/test-optional-recipe-removal.ts b/scripts/test-optional-recipe-removal.ts index a64021bb..e7bca250 100644 --- a/scripts/test-optional-recipe-removal.ts +++ b/scripts/test-optional-recipe-removal.ts @@ -7,98 +7,98 @@ import { import path from "node:path"; import { + createRemovalFixtureRoot, filesBelow, prepareRemovalFixture, pruneRemovalFixtureCiContract, pruneScriptOrchestration, regenerateRemovalFixtureWorkflow, + renderRemovalJunit, requireRemovalFixtureEnvironment, - runRemovalFixturePnpm, + runRemovalFixtureCheck, + type RemovalCheck, } from "./lib/removal-fixture.ts"; -const fixtureRoot = path.resolve(".tmp/optional-recipe-removal"); const pnpmCli = requireRemovalFixtureEnvironment("npm_execpath"); +const fixtureRoot = await createRemovalFixtureRoot("optional-recipe-removal"); -function runPnpm(script: string): boolean { - return runRemovalFixturePnpm(fixtureRoot, pnpmCli, script); +function check(name: string, script: string): RemovalCheck { + return runRemovalFixtureCheck(name, fixtureRoot, pnpmCli, script); } -await prepareRemovalFixture(fixtureRoot); -for (const rootOnlyTest of [ - "tests/unit/ci-workflow-generation.test.ts", - "tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap", -]) { - await rm(path.join(fixtureRoot, rootOnlyTest), { force: true }); -} -await rm(path.join(fixtureRoot, "recipes"), { recursive: true, force: true }); -await rm(path.join(fixtureRoot, "tests/recipes"), { - recursive: true, - force: true, -}); - -const removedCiScripts = new Set([ - "check:types:recipes", - "test:recipes", - "check:optional-recipes", - "check:optional-recipes:source", - "check:optional-recipe-fixtures", - "test:optional-recipe-removal", -]); -const fixturePackagePath = path.join(fixtureRoot, "package.json"); -const fixturePackage = JSON.parse(await readFile(fixturePackagePath, "utf8")) as { - scripts: Record; -}; -pruneScriptOrchestration(fixturePackage.scripts, "check:types", removedCiScripts); -pruneScriptOrchestration(fixturePackage.scripts, "test:all", removedCiScripts); -await writeFile(fixturePackagePath, `${JSON.stringify(fixturePackage, null, 2)}\n`); -await pruneRemovalFixtureCiContract({ - root: fixtureRoot, - removedScripts: removedCiScripts, - removedEvidencePathFragments: [ - "optional-recipes", - "optional-recipe-fixtures", - "optional-recipe-removal", - ], -}); -await regenerateRemovalFixtureWorkflow(fixtureRoot); - -const checks: Array<[string, boolean]> = [ - ["typecheck", runPnpm("check:types")], - ["architecture", runPnpm("check:architecture")], - ["test", runPnpm("test:all")], - ["build", runPnpm("build")], - ["ci-contract", runPnpm("check:ci")], -]; -const residue: string[] = []; -for (const file of await filesBelow(path.join(fixtureRoot, "dist"))) { - if (!/\.(?:js|css|html|json)$/.test(file)) continue; - const content = await readFile(file, "utf8"); - if (content.includes("frontend-optional-recipe-must-not-reach-production")) { - residue.push(path.relative(fixtureRoot, file)); +let checks: RemovalCheck[]; +try { + await prepareRemovalFixture(fixtureRoot); + for (const rootOnlyTest of [ + "tests/unit/ci-workflow-generation.test.ts", + "tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap", + ]) { + await rm(path.join(fixtureRoot, rootOnlyTest), { force: true }); } + await rm(path.join(fixtureRoot, "recipes"), { recursive: true, force: true }); + await rm(path.join(fixtureRoot, "tests/recipes"), { + recursive: true, + force: true, + }); + + const removedCiScripts = new Set([ + "check:types:recipes", + "test:recipes", + "check:optional-recipes", + "check:optional-recipes:source", + "check:optional-recipe-fixtures", + "test:optional-recipe-removal", + ]); + const fixturePackagePath = path.join(fixtureRoot, "package.json"); + const fixturePackage = JSON.parse(await readFile(fixturePackagePath, "utf8")) as { + scripts: Record; + }; + pruneScriptOrchestration(fixturePackage.scripts, "check:types", removedCiScripts); + pruneScriptOrchestration(fixturePackage.scripts, "test:all", removedCiScripts); + await writeFile(fixturePackagePath, `${JSON.stringify(fixturePackage, null, 2)}\n`); + await pruneRemovalFixtureCiContract({ + root: fixtureRoot, + removedScripts: removedCiScripts, + removedEvidencePathFragments: [ + "optional-recipes", + "optional-recipe-fixtures", + "optional-recipe-removal", + ], + }); + await regenerateRemovalFixtureWorkflow(fixtureRoot); + + checks = [ + check("typecheck", "check:types"), + check("architecture", "check:architecture"), + check("test", "test:all"), + check("build", "build"), + check("ci-contract", "check:ci"), + ]; + const residue: string[] = []; + for (const file of await filesBelow(path.join(fixtureRoot, "dist")).catch(() => [])) { + if (!/\.(?:js|css|html|json)$/.test(file)) continue; + const content = await readFile(file, "utf8"); + if (content.includes("frontend-optional-recipe-must-not-reach-production")) { + residue.push(path.relative(fixtureRoot, file)); + } + } + checks.push( + residue.length === 0 + ? { name: "bundle-residue", passed: true } + : { name: "bundle-residue", passed: false, failure: `residue: ${residue.join(", ")}` }, + ); +} finally { + await rm(fixtureRoot, { recursive: true, force: true }); } -checks.push(["bundle-residue", residue.length === 0]); -const passed = checks.every(([, result]) => result); +const failed = checks.filter((entry) => !entry.passed); await mkdir("artifacts/tests", { recursive: true }); await writeFile( "artifacts/tests/optional-recipe-removal.xml", - `\n` + - `` + - checks - .map( - ([name, result]) => - `${result ? "" : `${residue.join(", ")}`}`, - ) - .join("") + - `\n`, + renderRemovalJunit("optional-recipe-removal", checks), ); -await rm(fixtureRoot, { recursive: true, force: true }); -if (!passed) { +if (failed.length > 0) { process.stderr.write( - `Optional recipe removal failed: ${checks - .filter(([, result]) => !result) - .map(([name]) => name) - .join(", ")}\n`, + `Optional recipe removal failed: ${failed.map((entry) => entry.name).join(", ")}\n`, ); process.exit(1); } diff --git a/scripts/test-realtime-runtime-removal.ts b/scripts/test-realtime-runtime-removal.ts index 309d2190..e8e25405 100644 --- a/scripts/test-realtime-runtime-removal.ts +++ b/scripts/test-realtime-runtime-removal.ts @@ -8,16 +8,19 @@ import path from "node:path"; import { assertNoRuntimeImports, + createRemovalFixtureRoot, prepareRemovalFixture, pruneRemovalFixtureCiContract, regenerateRemovalFixtureWorkflow, removeRuntimeDependentTests, + renderRemovalJunit, requireRemovalFixtureEnvironment, - runRemovalFixturePnpm, + runRemovalFixtureCheck, + type RemovalCheck, } from "./lib/removal-fixture.ts"; -const fixtureRoot = path.resolve(".tmp/realtime-runtime-removal"); const pnpmCli = requireRemovalFixtureEnvironment("npm_execpath"); +const fixtureRoot = await createRemovalFixtureRoot("realtime-runtime-removal"); const runtimePaths = [ "src/application/ports/realtime", "src/application/ports/out/web-push-control.ts", @@ -41,111 +44,104 @@ const removedEvidencePathFragments = [ "realtime-boundaries", "realtime-runtime-removal", ] as const; -function runPnpm(script: string): boolean { - return runRemovalFixturePnpm(fixtureRoot, pnpmCli, script); +function check(name: string, script: string): RemovalCheck { + return runRemovalFixtureCheck(name, fixtureRoot, pnpmCli, script); } -await prepareRemovalFixture(fixtureRoot); +let checks: RemovalCheck[]; +let removedRuntimeTests: number; +try { + await prepareRemovalFixture(fixtureRoot); -const removedRuntimeTests = - await removeRuntimeDependentTests(fixtureRoot, runtimeSourceRoots); -for (const runtimePath of runtimePaths) { - await rm(path.join(fixtureRoot, runtimePath), { - recursive: true, - force: true, - }); -} + removedRuntimeTests = + await removeRuntimeDependentTests(fixtureRoot, runtimeSourceRoots); + for (const runtimePath of runtimePaths) { + await rm(path.join(fixtureRoot, runtimePath), { + recursive: true, + force: true, + }); + } -const outputPortsIndexPath = path.join( - fixtureRoot, - "src/application/ports/out/index.ts", -); -const outputPortsIndex = await readFile(outputPortsIndexPath, "utf8"); -await writeFile( - outputPortsIndexPath, - outputPortsIndex.replace( - 'export type { WebPushControlPort } from "./web-push-control.ts";\n', - "", - ), -); - -const catalogPath = path.join( - fixtureRoot, - "config/recipes/frontend-capability-recipes.json", -); -const catalog = JSON.parse(await readFile(catalogPath, "utf8")) as { - recipes: Array>; -}; -const realtimeRecipe = catalog.recipes.find( - (recipe) => recipe.id === "realtime", -); -if (!realtimeRecipe || !Object.hasOwn(realtimeRecipe, "referenceRuntime")) { - throw new Error("Expected realtime reference runtime catalog entry"); -} -delete realtimeRecipe.referenceRuntime; -await writeFile(catalogPath, `${JSON.stringify(catalog, null, 2)}\n`); - -for (const scriptPath of [ - "scripts/check-realtime-boundaries.ts", - "scripts/check-realtime-boundary-fixtures.ts", - "scripts/lib/realtime-boundaries.ts", - "scripts/test-realtime-runtime-removal.ts", -]) { - await rm(path.join(fixtureRoot, scriptPath), { force: true }); -} -// The root snapshot locks the full repository inventory. This removal fixture -// validates its smaller registry through check:ci and its regenerated workflow. -await rm( - path.join(fixtureRoot, "tests/unit/ci-workflow-generation.test.ts"), - { force: true }, -); -await rm( - path.join( + const outputPortsIndexPath = path.join( fixtureRoot, - "tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap", - ), - { force: true }, -); + "src/application/ports/out/index.ts", + ); + const outputPortsIndex = await readFile(outputPortsIndexPath, "utf8"); + await writeFile( + outputPortsIndexPath, + outputPortsIndex.replace( + 'export type { WebPushControlPort } from "./web-push-control.ts";\n', + "", + ), + ); -await pruneRemovalFixtureCiContract({ - root: fixtureRoot, - removedScripts: new Set(runtimeScripts), - removedEvidencePathFragments, -}); -await regenerateRemovalFixtureWorkflow(fixtureRoot); -await assertNoRuntimeImports(fixtureRoot, runtimeSourceRoots, "realtime"); + const catalogPath = path.join( + fixtureRoot, + "config/recipes/frontend-capability-recipes.json", + ); + const catalog = JSON.parse(await readFile(catalogPath, "utf8")) as { + recipes: Array>; + }; + const realtimeRecipe = catalog.recipes.find( + (recipe) => recipe.id === "realtime", + ); + if (!realtimeRecipe || !Object.hasOwn(realtimeRecipe, "referenceRuntime")) { + throw new Error("Expected realtime reference runtime catalog entry"); + } + delete realtimeRecipe.referenceRuntime; + await writeFile(catalogPath, `${JSON.stringify(catalog, null, 2)}\n`); -const checks: Array = [ - ["typecheck", runPnpm("check:types")], - ["lint", runPnpm("lint")], - ["architecture", runPnpm("check:architecture")], - ["test", runPnpm("test:all")], - ["build", runPnpm("build")], - ["optional-catalog", runPnpm("check:optional-recipes:source")], - ["ci-contract", runPnpm("check:ci")], -]; -const passed = checks.every(([, result]) => result); + for (const scriptPath of [ + "scripts/check-realtime-boundaries.ts", + "scripts/check-realtime-boundary-fixtures.ts", + "scripts/lib/realtime-boundaries.ts", + "scripts/test-realtime-runtime-removal.ts", + ]) { + await rm(path.join(fixtureRoot, scriptPath), { force: true }); + } + // The root snapshot locks the full repository inventory. This removal fixture + // validates its smaller registry through check:ci and its regenerated workflow. + await rm( + path.join(fixtureRoot, "tests/unit/ci-workflow-generation.test.ts"), + { force: true }, + ); + await rm( + path.join( + fixtureRoot, + "tests/unit/__snapshots__/ci-workflow-generation.test.ts.snap", + ), + { force: true }, + ); + + await pruneRemovalFixtureCiContract({ + root: fixtureRoot, + removedScripts: new Set(runtimeScripts), + removedEvidencePathFragments, + }); + await regenerateRemovalFixtureWorkflow(fixtureRoot); + await assertNoRuntimeImports(fixtureRoot, runtimeSourceRoots, "realtime"); + + checks = [ + check("typecheck", "check:types"), + check("lint", "lint"), + check("architecture", "check:architecture"), + check("test", "test:all"), + check("build", "build"), + check("optional-catalog", "check:optional-recipes:source"), + check("ci-contract", "check:ci"), + ]; +} finally { + await rm(fixtureRoot, { recursive: true, force: true }); +} +const failed = checks.filter((entry) => !entry.passed); await mkdir("artifacts/tests", { recursive: true }); await writeFile( "artifacts/tests/realtime-runtime-removal.xml", - `\n` + - `` + - checks - .map( - ([name, result]) => - `${result ? "" : ""}`, - ) - .join("") + - `\n`, + renderRemovalJunit("realtime-runtime-removal", checks), ); -await rm(fixtureRoot, { recursive: true, force: true }); - -if (!passed) { +if (failed.length > 0) { process.stderr.write( - `Realtime runtime removal failed: ${checks - .filter(([, result]) => !result) - .map(([name]) => name) - .join(", ")}\n`, + `Realtime runtime removal failed: ${failed.map((entry) => entry.name).join(", ")}\n`, ); process.exit(1); } diff --git a/scripts/test-sample-removal.ts b/scripts/test-sample-removal.ts index fd5d5f4a..8113f6c1 100644 --- a/scripts/test-sample-removal.ts +++ b/scripts/test-sample-removal.ts @@ -1,7 +1,6 @@ import { access, mkdir, - mkdtemp, readdir, readFile, rm, @@ -13,13 +12,17 @@ import { ROUTE_FEATURE_OWNER } from "../src/features/installed-feature-contracts import { createFileSystemDeclarationSource } from "./generate-product-feature-index.ts"; import { isProductionModulePath } from "./lib/risk-coverage.ts"; import { + createRemovalFixtureRoot, filesBelow, prepareRemovalFixture, pruneRemovalFixtureCiContract, pruneScriptOrchestration, regenerateRemovalFixtureWorkflow, + renderRemovalJunit, requireRemovalFixtureEnvironment, + runRemovalFixtureCheck, runRemovalFixturePnpm, + type RemovalCheck, } from "./lib/removal-fixture.ts"; /** @@ -137,11 +140,7 @@ const featureRouteIds = Object.entries(ROUTE_FEATURE_OWNER) .filter(([, owner]) => owner === featureId) .map(([routeId]) => routeId); -const fixtureParent = path.resolve(".tmp"); -await mkdir(fixtureParent, { recursive: true }); -const fixtureRoot = await mkdtemp( - path.join(fixtureParent, `${featureId}-removal-`), -); +const fixtureRoot = await createRemovalFixtureRoot(`${featureId}-removal`); const pnpmCli = requireRemovalFixtureEnvironment("npm_execpath"); const featureSource = `src/features/${located.directory}`; const featureTests = `tests/features/${located.directory}`; @@ -193,6 +192,17 @@ function runPnpm(script: string, extra: string[] = []): boolean { return runRemovalFixturePnpm(fixtureRoot, pnpmCli, script, extra); } +function check(name: string, script: string, extra: string[] = []): RemovalCheck { + return runRemovalFixtureCheck(name, fixtureRoot, pnpmCli, script, extra); +} + +/** 14-08. A non-command check fails with its own findings, nothing else's. */ +function outcome(name: string, findings: readonly string[], label: string): RemovalCheck { + return findings.length === 0 + ? { name, passed: true } + : { name, passed: false, failure: `${label}: ${findings.join(", ")}` }; +} + try { await prepareRemovalFixture(fixtureRoot); for (const excludedFixtureTest of [ @@ -390,43 +400,32 @@ try { } } - const checks: Array<[string, boolean]> = [ - [ - "common-test-evidence", - ( - await Promise.all( - commonTestPaths.map(async (testPath) => { - try { - await access(path.join(fixtureRoot, testPath)); - return true; - } catch { - return false; - } - }), - ) - ).every(Boolean), - ], - ["typecheck", runPnpm("check:types")], - ["architecture", runPnpm("check:architecture")], - ["registries", runPnpm("check:registries")], - ["unit-integration", runPnpm("test:all")], - ["coverage", runPnpm("test:coverage")], - ["test-evidence-source", runPnpm("check:test-evidence:source")], - ["ci-contract", runPnpm("check:ci")], - [ - "home-smoke", - runPnpm("exec", [ - "vitest", - "run", - "tests/component/router.test.tsx", - "--reporter=default", - ]), - ], - ["build", runPnpm("build")], + const missingCommonTests: string[] = []; + for (const testPath of commonTestPaths) { + await access(path.join(fixtureRoot, testPath)).catch(() => { + missingCommonTests.push(testPath); + }); + } + const checks: RemovalCheck[] = [ + outcome("common-test-evidence", missingCommonTests, "platform tests missing"), + check("typecheck", "check:types"), + check("architecture", "check:architecture"), + check("registries", "check:registries"), + check("unit-integration", "test:all"), + check("coverage", "test:coverage"), + check("test-evidence-source", "check:test-evidence:source"), + check("ci-contract", "check:ci"), + check("home-smoke", "exec", [ + "vitest", + "run", + "tests/component/router.test.tsx", + "--reporter=default", + ]), + check("build", "build"), ]; const builtResidue: string[] = []; const buildSucceeded = - checks.find(([name]) => name === "build")?.[1] === true; + checks.find(({ name }) => name === "build")?.passed === true; if (buildSucceeded) { for (const file of await filesBelow(path.join(fixtureRoot, "dist"))) { if (!/\.(?:js|css|html|json)$/.test(file)) continue; @@ -443,33 +442,29 @@ try { ).href}?removed=${Date.now()}` ) as { ROUTE_REGISTRY: Readonly> }; const routeIds = new Set(Object.keys(routeCatalog.ROUTE_REGISTRY)); - const routeAbsent = featureRouteIds.every((routeId) => !routeIds.has(routeId)); - checks.push(["route-absent", routeAbsent]); - checks.push(["fixture-id-residue", residue.length === 0]); - checks.push(["built-fixture-id-residue", builtResidue.length === 0]); + checks.push( + outcome( + "route-absent", + featureRouteIds.filter((routeId) => routeIds.has(routeId)), + "routes still registered", + ), + outcome("fixture-id-residue", residue, "residue"), + outcome("built-fixture-id-residue", builtResidue, "built residue"), + ); - const passed = checks.every(([, result]) => result); + const failed = checks.filter((entry) => !entry.passed); await mkdir("artifacts/tests", { recursive: true }); await writeFile( isSample ? "artifacts/tests/sample-removal.xml" : `artifacts/tests/feature-removal-${featureId}.xml`, - `\n` + - `` + - checks - .map( - ([name, result]) => - `${result ? "" : `${[...residue, ...builtResidue].join(", ")}`}`, - ) - .join("") + - `\n`, + renderRemovalJunit(`${featureId}-removal`, checks), ); - if (!passed) { - const failures = checks - .filter(([, result]) => !result) - .map(([name]) => name); + if (failed.length > 0) { process.stderr.write( - `${featureId} removal failed: ${failures.join(", ")}; residue: ${[...residue, ...builtResidue].join(", ")}\n`, + `${featureId} removal failed:\n${failed + .map((entry) => ` ${entry.name}: ${entry.failure ?? "failed"}`) + .join("\n")}\n`, ); process.exitCode = 1; } else { diff --git a/tests/unit/removal-fixture.test.ts b/tests/unit/removal-fixture.test.ts index 61bdebd9..001b11e0 100644 --- a/tests/unit/removal-fixture.test.ts +++ b/tests/unit/removal-fixture.test.ts @@ -9,6 +9,7 @@ import { assertNoRuntimeImports, pruneRemovalFixtureCiContract, removeRuntimeDependentTests, + renderRemovalJunit, runtimeImportGraph, } from "../../scripts/lib/removal-fixture.ts"; @@ -155,3 +156,30 @@ describe("release evidence fixture copy", () => { } }); }); + +describe("removal JUnit report (14-08)", () => { + it("counts every failure and gives each check its own cause", () => { + const xml = renderRemovalJunit("reference-feature-removal", [ + { name: "typecheck", passed: false, failure: "corepack pnpm check:types exited 2" }, + { name: "architecture", passed: true }, + { name: "build", passed: false, failure: "corepack pnpm build exited 1" }, + { + name: "fixture-id-residue", + passed: false, + failure: "residue: tests/a.test.ts, tests/&c.ts", + }, + ]); + expect(xml).toContain('tests="4" failures="3"'); + expect(xml).toContain( + '', + ); + expect(xml).toContain(''); + expect(xml).toContain( + '', + ); + expect(xml).toContain( + 'message="residue: tests/a.test.ts, tests/<b>&c.ts"', + ); + expect(xml.match(/residue:/gu)).toHaveLength(1); + }); +});