import { readFileSync } from "node:fs"; import { readFile } from "node:fs/promises"; import { loadCiGateContract } from "./contracts/ci-gates.ts"; import { loadProductCiOverlay } from "./contracts/product-ci-overlay.ts"; import { gatedTestPaths, gatedTestTargets, listTestFiles, parseUngatedTestExclusions, ungatedTestFailures, UNGATED_TESTS_PATH, } from "./lib/ungated-tests.ts"; import { parseUngatedScriptExclusions, ungatedScriptFailures, UNGATED_SCRIPTS_PATH, } from "./lib/ungated-scripts.ts"; /** * 12-04. Every test file and every verification package script is reached by * a gate command of any stage, or config/ci/ungated-tests.json and * config/ci/ungated-scripts.json say why not. It checks how the gates are * wired, not the change, so it runs in the main stage (FE-GATE-029) after a * merge (design-docs repo-structure/03-ci.md F2); check:ci keeps the contract * and workflow checks on every pull request. */ const contract = await loadCiGateContract(process.cwd()); // 1103/12-07. The product's merge gates are entry scripts of CI as well. const { mergeGates: productGates } = await loadProductCiOverlay(process.cwd()); const productGateScripts = productGates.map(({ script }) => script); const packageScripts = (JSON.parse(await readFile("package.json", "utf8")) as { scripts?: Record }).scripts ?? {}; const failures: string[] = []; // 12-04: every test file runs in a gate, or config/ci/ungated-tests.json says why not. try { failures.push( ...ungatedTestFailures({ testFiles: await listTestFiles(process.cwd()), gatedPaths: gatedTestTargets( packageScripts, [...contract.commands.map(({ script }) => script), ...productGateScripts], (file) => readFileSync(file, "utf8"), ), exclusions: parseUngatedTestExclusions( JSON.parse(await readFile(UNGATED_TESTS_PATH, "utf8")), ), }), ); } catch (error) { failures.push(`ungated test check could not run: ${error instanceof Error ? error.message : String(error)}`); } // 12-04: every verification package script is reached by a gate, or // config/ci/ungated-scripts.json says why not. try { const entryScripts = [...contract.commands.map(({ script }) => script), ...productGateScripts]; failures.push( ...ungatedScriptFailures({ scripts: packageScripts, entryScripts, gatedTestPaths: gatedTestPaths(packageScripts, entryScripts, (file) => readFileSync(file, "utf8")), exclusions: parseUngatedScriptExclusions( JSON.parse(await readFile(UNGATED_SCRIPTS_PATH, "utf8")), ), }), ); } catch (error) { failures.push(`ungated script check could not run: ${error instanceof Error ? error.message : String(error)}`); } if (failures.length > 0) { process.stderr.write(`CI gating failed:\n${failures.join("\n")}\n`); process.exit(1); } process.stdout.write("CI gating: every test file and verification script is gated or excluded with a reason PASS\n");