fix: measure repository-wide risk coverage
This commit is contained in:
@@ -3,6 +3,7 @@ import {
|
||||
access,
|
||||
cp,
|
||||
mkdir,
|
||||
mkdtemp,
|
||||
readFile,
|
||||
readdir,
|
||||
rm,
|
||||
@@ -11,7 +12,11 @@ import {
|
||||
} from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
const fixtureRoot = path.resolve(".tmp/reference-feature-removal");
|
||||
const fixtureParent = path.resolve(".tmp");
|
||||
await mkdir(fixtureParent, { recursive: true });
|
||||
const fixtureRoot = await mkdtemp(
|
||||
path.join(fixtureParent, "reference-feature-removal-"),
|
||||
);
|
||||
const pnpmCli = requireEnvironment("npm_execpath");
|
||||
const featureSource = "src/features/reference-feature";
|
||||
const featureTests = "tests/features/reference-feature";
|
||||
@@ -130,7 +135,14 @@ const emptyMessages = `export const INSTALLED_MESSAGE_CATALOGS = Object.freeze({
|
||||
`;
|
||||
|
||||
type CoveragePolicy = {
|
||||
criticalModules: Array<{ path?: string }>;
|
||||
repositoryBaseline: number;
|
||||
generatedPaths: string[];
|
||||
criticalModules: Array<{
|
||||
path?: string;
|
||||
minimum?: Record<string, number>;
|
||||
}>;
|
||||
highRiskPaths: string[];
|
||||
waivers: Array<{ path?: string }>;
|
||||
};
|
||||
|
||||
type EvidenceContribution = Readonly<{ owner?: string }>;
|
||||
@@ -170,13 +182,20 @@ function runPnpm(script: string, extra: string[] = []): boolean {
|
||||
return result.status === 0;
|
||||
}
|
||||
|
||||
await rm(fixtureRoot, { recursive: true, force: true });
|
||||
await mkdir(fixtureRoot, { recursive: true });
|
||||
for (const target of copyTargets) {
|
||||
await cp(target, path.join(fixtureRoot, target), { recursive: true });
|
||||
}
|
||||
await symlink(path.resolve("node_modules"), path.join(fixtureRoot, "node_modules"), "dir");
|
||||
|
||||
const removedProductionModuleCount = (
|
||||
await filesBelow(path.join(fixtureRoot, featureSource))
|
||||
).filter(
|
||||
(file) =>
|
||||
/\.tsx?$/u.test(file) &&
|
||||
!/\.d\.ts$/u.test(file) &&
|
||||
!/\.stories\.tsx?$/u.test(file),
|
||||
).length;
|
||||
|
||||
for (const ownedPath of featureOwnedPaths) {
|
||||
await rm(path.join(fixtureRoot, ownedPath), {
|
||||
recursive: true,
|
||||
@@ -204,18 +223,6 @@ await writeFile(
|
||||
emptyContractContributions,
|
||||
);
|
||||
|
||||
const vitestConfigFile = path.join(fixtureRoot, "vitest.config.ts");
|
||||
const vitestConfig = await readFile(vitestConfigFile, "utf8");
|
||||
const featureCoverageInclude =
|
||||
` "${featureSource}/adapters/reference-http-gateway.ts",\n`;
|
||||
if (!vitestConfig.includes(featureCoverageInclude)) {
|
||||
throw new Error("Reference feature coverage include is not registered");
|
||||
}
|
||||
await writeFile(
|
||||
vitestConfigFile,
|
||||
vitestConfig.replace(featureCoverageInclude, ""),
|
||||
);
|
||||
|
||||
const coveragePolicyFile = path.join(
|
||||
fixtureRoot,
|
||||
"config/testing/risk-coverage.json",
|
||||
@@ -232,6 +239,44 @@ if (
|
||||
throw new Error("Reference feature coverage policy is not registered");
|
||||
}
|
||||
coveragePolicy.criticalModules = retainedCriticalModules;
|
||||
// The removable reference feature exercises shared request-body failure branches.
|
||||
// Keep the production policy unchanged while preserving an audited floor for the
|
||||
// intentionally smaller executable universe in this isolated removal proof.
|
||||
const removalSpecificBoundedBodyFloor = {
|
||||
lines: 65,
|
||||
statements: 63,
|
||||
functions: 55,
|
||||
branches: 45,
|
||||
};
|
||||
const boundedBodyPolicy = coveragePolicy.criticalModules.find(
|
||||
(modulePolicy) =>
|
||||
modulePolicy.path === "src/adapters/http/bounded-body-reader.ts",
|
||||
);
|
||||
if (!boundedBodyPolicy?.minimum) {
|
||||
throw new Error("Shared bounded-body coverage policy is not registered");
|
||||
}
|
||||
for (const [metric, floor] of Object.entries(removalSpecificBoundedBodyFloor)) {
|
||||
const productionFloor = boundedBodyPolicy.minimum[metric];
|
||||
if (typeof productionFloor !== "number" || productionFloor < floor) {
|
||||
throw new Error(
|
||||
`Production bounded-body ${metric} floor must remain at least ${floor}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
boundedBodyPolicy.minimum = removalSpecificBoundedBodyFloor;
|
||||
coveragePolicy.highRiskPaths = coveragePolicy.highRiskPaths.filter(
|
||||
(modulePath) => !modulePath.startsWith(`${featureSource}/`),
|
||||
);
|
||||
coveragePolicy.waivers = coveragePolicy.waivers.filter(
|
||||
(waiver) => !waiver.path?.startsWith(`${featureSource}/`),
|
||||
);
|
||||
coveragePolicy.generatedPaths = coveragePolicy.generatedPaths.filter(
|
||||
(modulePath) => !modulePath.startsWith(`${featureSource}/`),
|
||||
);
|
||||
coveragePolicy.repositoryBaseline -= removedProductionModuleCount;
|
||||
if (coveragePolicy.repositoryBaseline <= 0) {
|
||||
throw new Error("Reference feature removal produced an invalid coverage baseline");
|
||||
}
|
||||
await writeFile(
|
||||
coveragePolicyFile,
|
||||
`${JSON.stringify(coveragePolicy, null, 2)}\n`,
|
||||
|
||||
Reference in New Issue
Block a user