chore(lint): add .out file reference checker (#27078)

Co-authored-by: David Sherret <dsherret@gmail.com>
This commit is contained in:
Leo Kettmeir 2024-11-26 20:53:20 -08:00 committed by GitHub
parent 4700f12ddc
commit 42b71d82db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
765 changed files with 517 additions and 4766 deletions

View file

@ -3,7 +3,16 @@
// deno-lint-ignore-file no-console
import { buildMode, getPrebuilt, getSources, join, ROOT_PATH } from "./util.js";
import {
buildMode,
dirname,
getPrebuilt,
getSources,
join,
parseJSONC,
ROOT_PATH,
walk,
} from "./util.js";
import { checkCopyright } from "./copyright_checker.js";
import * as ciFile from "../.github/workflows/ci.generate.ts";
@ -25,6 +34,7 @@ if (js) {
promises.push(dlintPreferPrimordials());
promises.push(ensureCiYmlUpToDate());
promises.push(ensureNoNewITests());
promises.push(ensureNoUnusedOutFiles());
if (rs) {
promises.push(checkCopyright());
@ -251,3 +261,49 @@ async function ensureNoNewITests() {
}
}
}
async function ensureNoUnusedOutFiles() {
const specsDir = join(ROOT_PATH, "tests", "specs");
const outFilePaths = new Set(
(await Array.fromAsync(
walk(specsDir, { exts: [".out"] }),
)).map((entry) => entry.path),
);
const testFiles = (await Array.fromAsync(
walk(specsDir, { exts: [".jsonc"] }),
)).filter((entry) => {
return entry.path.endsWith("__test__.jsonc");
});
function checkObject(baseDirPath, obj) {
for (const [key, value] of Object.entries(obj)) {
if (typeof value === "object") {
checkObject(baseDirPath, value);
} else if (key === "output" && typeof value === "string") {
const outFilePath = join(baseDirPath, value);
outFilePaths.delete(outFilePath);
}
}
}
for (const testFile of testFiles) {
try {
const text = await Deno.readTextFile(testFile.path);
const data = parseJSONC(text);
checkObject(dirname(testFile.path), data);
} catch (err) {
throw new Error("Failed reading: " + testFile.path, {
cause: err,
});
}
}
const notFoundPaths = Array.from(outFilePaths);
if (notFoundPaths.length > 0) {
notFoundPaths.sort(); // be deterministic
for (const file of notFoundPaths) {
console.error(`Unreferenced .out file: ${file}`);
}
throw new Error(`${notFoundPaths.length} unreferenced .out files`);
}
}