From fcf0b6cfde237720d89e0da22370434b919953dd Mon Sep 17 00:00:00 2001 From: hasundue Date: Thu, 1 Aug 2024 13:24:23 +0900 Subject: [PATCH] fix(fs): remove temp dirs on `dispose` --- src/fs.ts | 7 +++++++ src/fs_test.ts | 1 + 2 files changed, 8 insertions(+) diff --git a/src/fs.ts b/src/fs.ts index 8f93d95..dbf33e1 100644 --- a/src/fs.ts +++ b/src/fs.ts @@ -176,6 +176,9 @@ const spies = new class extends Map { } }(); +/** A map from stub paths to corresponding temporary directories. */ +const temps = new Map(); + export interface StubOptions { readThrough?: boolean; } @@ -206,7 +209,9 @@ export function stub( ): FileSystemStub { path = normalize(path); const base = extname(path) ? dirname(path) : path; + const temp = Deno.makeTempDirSync(); + temps.set(path, temp); const fake = isStubOptions(fakeOrOptions) ? createFsFake(base, temp, fakeOrOptions?.readThrough ?? true) @@ -260,6 +265,8 @@ export function restore() { export function dispose() { restore(); + temps.forEach((it) => Deno.removeSync(it, { recursive: true })); + temps.clear(); spies.clear(); } diff --git a/src/fs_test.ts b/src/fs_test.ts index 6b175e9..1f0893a 100644 --- a/src/fs_test.ts +++ b/src/fs_test.ts @@ -147,6 +147,7 @@ describe("dispose", () => { cwd = Deno.cwd(); Deno.chdir(new URL("../", import.meta.url)); }); + afterEach(() => fs.dispose()); afterAll(() => Deno.chdir(cwd)); it("should restore file system functions", () => {