fix(test): avoid walking lint suppression scans

This commit is contained in:
Vincent Koc
2026-05-16 09:53:20 +08:00
parent 5f85e6b692
commit 4d2acd9481

View File

@@ -1,6 +1,7 @@
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
const repoRoot = path.resolve(import.meta.dirname, "../..");
const CODE_EXTENSIONS = new Set([".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"]);
@@ -13,7 +14,48 @@ type SuppressionEntry = {
rule: string;
};
function isProductionCodeFile(relativePath: string): boolean {
const basename = path.posix.basename(relativePath);
if (!CODE_EXTENSIONS.has(path.extname(relativePath))) {
return false;
}
if (basename.startsWith("__rootdir_boundary_canary__.")) {
return false;
}
return !(
relativePath.includes("/test/") ||
relativePath.endsWith(".test.ts") ||
relativePath.endsWith(".test.tsx") ||
relativePath.endsWith(".spec.ts") ||
relativePath.endsWith(".spec.tsx")
);
}
function listGitCodeFiles(root: string): string[] | null {
const result = spawnSync("git", ["ls-files", "--", root], {
cwd: repoRoot,
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
});
if (result.status !== 0) {
return null;
}
return result.stdout
.split("\n")
.map((line) => line.trim().replaceAll("\\", "/"))
.filter((line) => line.length > 0 && isProductionCodeFile(line));
}
function walkCodeFiles(dir: string, files: string[] = []): string[] {
const relativeRoot = path.relative(repoRoot, dir).replaceAll(path.sep, "/");
if (relativeRoot && !relativeRoot.startsWith("..") && !path.isAbsolute(relativeRoot)) {
const gitFiles = listGitCodeFiles(relativeRoot);
if (gitFiles) {
files.push(...gitFiles);
return files;
}
}
if (!fs.existsSync(dir)) {
return files;
}
@@ -26,20 +68,8 @@ function walkCodeFiles(dir: string, files: string[] = []): string[] {
walkCodeFiles(fullPath, files);
continue;
}
if (!CODE_EXTENSIONS.has(path.extname(entry.name))) {
continue;
}
if (entry.name.startsWith("__rootdir_boundary_canary__.")) {
continue;
}
const relativePath = path.relative(repoRoot, fullPath).replaceAll(path.sep, "/");
if (
relativePath.includes("/test/") ||
relativePath.endsWith(".test.ts") ||
relativePath.endsWith(".test.tsx") ||
relativePath.endsWith(".spec.ts") ||
relativePath.endsWith(".spec.tsx")
) {
if (!isProductionCodeFile(relativePath)) {
continue;
}
files.push(relativePath);
@@ -76,6 +106,19 @@ function summarizeSuppressions(entries: readonly SuppressionEntry[]): string[] {
}
describe("production lint suppressions", () => {
it("lists production files from git without walking source roots", () => {
const readdirSync = vi.spyOn(fs, "readdirSync");
try {
const files = ROOTS.flatMap((root) => walkCodeFiles(path.join(repoRoot, root))).toSorted();
expect(files.length).toBeGreaterThan(0);
expect(files.some((file) => file.endsWith(".test.ts"))).toBe(false);
expect(readdirSync).not.toHaveBeenCalled();
} finally {
readdirSync.mockRestore();
}
});
it("keeps the intentional production suppression tail on an explicit allowlist", () => {
expect(summarizeSuppressions(collectProductionLintSuppressions())).toEqual([
"extensions/browser/src/browser/pw-tools-core.interactions.ts|@typescript-eslint/no-implied-eval|2",