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", () => {