From 4acae5b28134dee7ff1dc5e1f7dbe014db6ed611 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 23 Apr 2026 18:21:20 +0100 Subject: [PATCH] test: dedupe skill scanner stat mocks --- src/security/skill-scanner.test.ts | 35 +++++++++++++----------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/security/skill-scanner.test.ts b/src/security/skill-scanner.test.ts index edf86238333..9f3add19adc 100644 --- a/src/security/skill-scanner.test.ts +++ b/src/security/skill-scanner.test.ts @@ -61,6 +61,19 @@ function writeFixtureFiles(root: string, files: Record { + const pathArg = args[0]; + if (typeof pathArg === "string" && pathArg === filePath) { + const err = new Error("EACCES: permission denied") as NodeJS.ErrnoException; + err.code = "EACCES"; + throw err; + } + return await realStat(...args); + }); +} + function expectRulePresence(findings: { ruleId: string }[], ruleId: string, expected: boolean) { expect(findings.some((finding) => finding.ruleId === ruleId)).toBe(expected); } @@ -762,16 +775,7 @@ describe("scanDirectoryWithSummary", () => { const filePath = path.join(root, "forbidden.js"); fsSync.writeFileSync(filePath, `export const x = 1;`); - const realStat = fs.stat; - const spy = vi.spyOn(fs, "stat").mockImplementation(async (...args) => { - const pathArg = args[0]; - if (typeof pathArg === "string" && pathArg === filePath) { - const err = new Error("EACCES: permission denied") as NodeJS.ErrnoException; - err.code = "EACCES"; - throw err; - } - return await realStat(...args); - }); + const spy = mockStatPermissionDeniedFor(filePath); try { await expect(scanDirectory(root, { includeFiles: ["forbidden.js"] })).rejects.toMatchObject({ @@ -787,16 +791,7 @@ describe("scanDirectoryWithSummary", () => { const filePath = path.join(root, "noperm.js"); fsSync.writeFileSync(filePath, `export const x = 1;`); - const realStat = fs.stat; - const spy = vi.spyOn(fs, "stat").mockImplementation(async (...args) => { - const pathArg = args[0]; - if (typeof pathArg === "string" && pathArg === filePath) { - const err = new Error("EACCES: permission denied") as NodeJS.ErrnoException; - err.code = "EACCES"; - throw err; - } - return await realStat(...args); - }); + const spy = mockStatPermissionDeniedFor(filePath); try { await expect(scanDirectory(root)).rejects.toMatchObject({ code: "EACCES" });