refactor: add incremental capability migration safeguards (#819)
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
/** Compare actual Vitest discovery with repository-owned test paths. */
|
||||
export function compareTestDiscovery(
|
||||
expectedPaths: readonly string[],
|
||||
discovery: unknown,
|
||||
root: string,
|
||||
): {
|
||||
readonly missing: readonly string[];
|
||||
readonly unexpected: readonly string[];
|
||||
readonly duplicates: readonly {
|
||||
readonly file: string;
|
||||
readonly projects: readonly string[];
|
||||
}[];
|
||||
readonly discovered: number;
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
import { relative, resolve } from "node:path";
|
||||
|
||||
/** Compare actual Vitest discovery with repository-owned test paths. */
|
||||
export const compareTestDiscovery = (expectedPaths, discovery, root) => {
|
||||
if (!Array.isArray(discovery))
|
||||
throw new TypeError("Vitest file discovery must be an array");
|
||||
const projectsByFile = new Map();
|
||||
for (const entry of discovery) {
|
||||
if (
|
||||
entry === null ||
|
||||
typeof entry !== "object" ||
|
||||
typeof entry.file !== "string" ||
|
||||
entry.file.length === 0 ||
|
||||
typeof entry.projectName !== "string" ||
|
||||
entry.projectName.length === 0
|
||||
)
|
||||
throw new TypeError(
|
||||
"Vitest discovery requires file and projectName strings",
|
||||
);
|
||||
const path = relative(root, resolve(root, entry.file)).replaceAll(
|
||||
"\\",
|
||||
"/",
|
||||
);
|
||||
const projects = projectsByFile.get(path) ?? [];
|
||||
projects.push(entry.projectName);
|
||||
projectsByFile.set(path, projects);
|
||||
}
|
||||
const expected = new Set(expectedPaths);
|
||||
const sorted = (values) => [...values].sort();
|
||||
return {
|
||||
missing: sorted(expected).filter((path) => !projectsByFile.has(path)),
|
||||
unexpected: sorted(projectsByFile.keys()).filter(
|
||||
(path) => !expected.has(path),
|
||||
),
|
||||
duplicates: sorted(projectsByFile.keys()).flatMap((file) => {
|
||||
const projects = projectsByFile.get(file);
|
||||
return projects.length > 1 ? [{ file, projects: sorted(projects) }] : [];
|
||||
}),
|
||||
discovered: projectsByFile.size,
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,65 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdtemp, readFile, rm, stat } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { compareTestDiscovery } from "./lib/test-discovery.mjs";
|
||||
|
||||
const execute = promisify(execFile);
|
||||
const root = resolve(import.meta.dirname, "..");
|
||||
|
||||
const temporary = await mkdtemp(join(tmpdir(), "rea-test-discovery-"));
|
||||
try {
|
||||
const output = join(temporary, "discovery.json");
|
||||
await execute(
|
||||
process.execPath,
|
||||
[
|
||||
join(root, "node_modules/vitest/vitest.mjs"),
|
||||
"list",
|
||||
"--filesOnly",
|
||||
`--json=${output}`,
|
||||
],
|
||||
{ cwd: root, maxBuffer: 8 * 1024 * 1024 },
|
||||
);
|
||||
const files = await execute(
|
||||
"git",
|
||||
[
|
||||
"ls-files",
|
||||
"-z",
|
||||
"--cached",
|
||||
"--others",
|
||||
"--exclude-standard",
|
||||
"--",
|
||||
"src",
|
||||
"tests",
|
||||
],
|
||||
{ cwd: root, maxBuffer: 8 * 1024 * 1024 },
|
||||
);
|
||||
const expected = [];
|
||||
for (const path of files.stdout
|
||||
.split("\0")
|
||||
.filter((path) => path.endsWith(".test.ts"))) {
|
||||
try {
|
||||
if ((await stat(join(root, path))).isFile()) expected.push(path);
|
||||
} catch (cause) {
|
||||
if (cause.code !== "ENOENT") throw cause;
|
||||
}
|
||||
}
|
||||
const report = compareTestDiscovery(
|
||||
expected,
|
||||
JSON.parse(await readFile(output, "utf8")),
|
||||
root,
|
||||
);
|
||||
if (
|
||||
report.missing.length ||
|
||||
report.unexpected.length ||
|
||||
report.duplicates.length
|
||||
) {
|
||||
console.error(JSON.stringify(report, null, 2));
|
||||
process.exitCode = 1;
|
||||
} else
|
||||
console.log(`Discovered all ${report.discovered} test files exactly once.`);
|
||||
} finally {
|
||||
await rm(temporary, { recursive: true, force: true });
|
||||
}
|
||||
Reference in New Issue
Block a user