From 4d2acd94816a3429a68f9d2abb36d5f82f3eea4a Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sat, 16 May 2026 09:53:20 +0800 Subject: [PATCH] fix(test): avoid walking lint suppression scans --- test/scripts/lint-suppressions.test.ts | 71 +++++++++++++++++++++----- 1 file changed, 57 insertions(+), 14 deletions(-) diff --git a/test/scripts/lint-suppressions.test.ts b/test/scripts/lint-suppressions.test.ts index 998e9ed7d33..02023a33ed0 100644 --- a/test/scripts/lint-suppressions.test.ts +++ b/test/scripts/lint-suppressions.test.ts @@ -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",