chore(test): separate development feedback from full verification (#599)
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
export interface DevelopmentTestRequest {
|
||||
readonly mode: "local" | "changed" | "focused";
|
||||
readonly paths: readonly string[];
|
||||
readonly base: string;
|
||||
readonly dryRun: boolean;
|
||||
}
|
||||
|
||||
/** Parse explicit test paths and an optional branch-comparison base. */
|
||||
export function parseDevelopmentTestRequest(
|
||||
mode: string,
|
||||
arguments_: readonly string[],
|
||||
): DevelopmentTestRequest;
|
||||
|
||||
/** Plan source feedback or a strict explicit run with its build prerequisite. */
|
||||
export function developmentTestPlan(
|
||||
request: DevelopmentTestRequest,
|
||||
baseCommit?: string,
|
||||
): {
|
||||
readonly needsBuild: boolean;
|
||||
readonly vitestArguments: readonly string[];
|
||||
};
|
||||
@@ -0,0 +1,77 @@
|
||||
const sourceProjects = [
|
||||
"domain",
|
||||
"services",
|
||||
"adapters",
|
||||
"composition",
|
||||
"conformance",
|
||||
"evaluation",
|
||||
];
|
||||
|
||||
const sourceTest = (path) =>
|
||||
path.startsWith("src/") ||
|
||||
["composition", "conformance", "evaluation"].some((project) =>
|
||||
path.startsWith(`tests/${project}/`),
|
||||
);
|
||||
|
||||
/** Parse explicit test paths and an optional branch-comparison base. */
|
||||
export const parseDevelopmentTestRequest = (mode, arguments_) => {
|
||||
if (!["local", "changed", "focused"].includes(mode))
|
||||
throw new Error("Expected local, changed, or focused test mode");
|
||||
const paths = [];
|
||||
let base = "origin/main";
|
||||
let dryRun = false;
|
||||
for (let index = 0; index < arguments_.length; index += 1) {
|
||||
const argument = arguments_[index];
|
||||
if (argument === "--dry-run") dryRun = true;
|
||||
else if (argument === "--base" || argument.startsWith("--base=")) {
|
||||
if (mode !== "changed") throw new Error("--base requires changed mode");
|
||||
base = argument === "--base" ? arguments_[++index] : argument.slice(7);
|
||||
if (!base || base.startsWith("-") || base.includes("\0"))
|
||||
throw new Error("--base requires a Git revision");
|
||||
} else {
|
||||
const path = argument.replaceAll("\\", "/").replace(/^\.\//u, "");
|
||||
if (
|
||||
!/^(?:src|tests)\/.+\.test\.ts$/u.test(path) ||
|
||||
path.split("/").some((part) => part === ".." || part === ".")
|
||||
)
|
||||
throw new Error(`Expected an exact repository test path: ${argument}`);
|
||||
if (mode !== "focused" && !sourceTest(path))
|
||||
throw new Error(
|
||||
`Use test:focused for compiled boundary tests: ${path}`,
|
||||
);
|
||||
paths.push(path);
|
||||
}
|
||||
}
|
||||
if (mode === "focused" && paths.length === 0)
|
||||
throw new Error("test:focused requires at least one exact test path");
|
||||
if (mode === "changed" && paths.length > 0)
|
||||
throw new Error("Use test:focused for explicit test paths");
|
||||
return { mode, paths: [...new Set(paths)], base, dryRun };
|
||||
};
|
||||
|
||||
/** Plan source feedback or a strict explicit run with its build prerequisite. */
|
||||
export const developmentTestPlan = (request, baseCommit) => {
|
||||
const explicit = request.paths.length > 0;
|
||||
if (
|
||||
request.mode === "changed" &&
|
||||
!/^[a-f0-9]{40,64}$/u.test(baseCommit ?? "")
|
||||
)
|
||||
throw new Error("Changed tests require a resolved Git merge base");
|
||||
const projects = request.mode === "focused" ? [] : sourceProjects;
|
||||
return {
|
||||
needsBuild: request.paths.some((path) => !sourceTest(path)),
|
||||
vitestArguments: [
|
||||
"run",
|
||||
"--maxWorkers=1",
|
||||
...projects.flatMap((project) => ["--project", project]),
|
||||
...(explicit
|
||||
? request.paths
|
||||
: [
|
||||
request.mode === "changed"
|
||||
? `--changed=${baseCommit}`
|
||||
: "--changed",
|
||||
"--passWithNoTests",
|
||||
]),
|
||||
],
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,90 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import { stat } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
import {
|
||||
developmentTestPlan,
|
||||
parseDevelopmentTestRequest,
|
||||
} from "./lib/development-tests.mjs";
|
||||
|
||||
let child;
|
||||
let shutdownSignal;
|
||||
const signalExitCodes = { SIGINT: 130, SIGTERM: 143 };
|
||||
for (const signal of Object.keys(signalExitCodes))
|
||||
process.on(signal, () => {
|
||||
shutdownSignal ??= signal;
|
||||
child?.kill(signal);
|
||||
});
|
||||
|
||||
try {
|
||||
const [mode, ...arguments_] = process.argv.slice(2);
|
||||
const request = parseDevelopmentTestRequest(mode, arguments_);
|
||||
for (const path of request.paths) {
|
||||
if (!(await stat(path)).isFile())
|
||||
throw new Error(`Test path is not a file: ${path}`);
|
||||
}
|
||||
let baseCommit;
|
||||
if (request.mode === "changed") {
|
||||
const base = await run("git", ["merge-base", "HEAD", request.base], true);
|
||||
if (base.code !== 0)
|
||||
throw new Error(
|
||||
`Cannot resolve test base ${request.base}. Fetch origin/main or pass --base REVISION.\n${base.output.trim()}`,
|
||||
);
|
||||
baseCommit = base.output.trim();
|
||||
}
|
||||
const plan = developmentTestPlan(request, baseCommit);
|
||||
if (request.dryRun) {
|
||||
console.log(JSON.stringify({ ...request, baseCommit, ...plan }, null, 2));
|
||||
} else {
|
||||
console.error(
|
||||
request.mode === "focused"
|
||||
? `Focused tests: ${request.paths.join(", ")}`
|
||||
: `Source-test feedback${baseCommit ? ` from merge base ${baseCommit}` : ""}; compiled boundaries and real providers need explicit verification.`,
|
||||
);
|
||||
if (plan.needsBuild) {
|
||||
if (!process.env.npm_execpath)
|
||||
throw new Error("Run compiled tests through npm run test:focused");
|
||||
const built = await run(process.execPath, [
|
||||
process.env.npm_execpath,
|
||||
"run",
|
||||
"build:cached",
|
||||
]);
|
||||
if (built.code !== 0) process.exitCode = built.code;
|
||||
}
|
||||
if (!process.exitCode && !shutdownSignal) {
|
||||
const tested = await run(process.execPath, [
|
||||
resolve("node_modules/vitest/vitest.mjs"),
|
||||
...plan.vitestArguments,
|
||||
]);
|
||||
process.exitCode = tested.code;
|
||||
}
|
||||
}
|
||||
} catch (cause) {
|
||||
console.error(cause instanceof Error ? cause.message : String(cause));
|
||||
process.exitCode = 1;
|
||||
}
|
||||
if (shutdownSignal) process.exitCode = signalExitCodes[shutdownSignal];
|
||||
|
||||
function run(executable, arguments_, capture = false) {
|
||||
if (shutdownSignal)
|
||||
return Promise.resolve({
|
||||
code: signalExitCodes[shutdownSignal],
|
||||
output: "",
|
||||
});
|
||||
return new Promise((resolveResult, reject) => {
|
||||
let output = "";
|
||||
child = spawn(executable, arguments_, {
|
||||
stdio: capture ? ["ignore", "pipe", "pipe"] : "inherit",
|
||||
});
|
||||
if (capture) {
|
||||
child.stdout.on("data", (data) => {
|
||||
output += data.toString();
|
||||
});
|
||||
child.stderr.on("data", (data) => {
|
||||
output += data.toString();
|
||||
});
|
||||
}
|
||||
child.once("error", reject);
|
||||
child.once("close", (code) => resolveResult({ code: code ?? 1, output }));
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user