Skip to content

Commit

Permalink
fix(fs): remove temp dirs on dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Aug 1, 2024
1 parent 63c1558 commit fcf0b6c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ const spies = new class extends Map<string | URL, FileSystemSpy> {
}
}();

/** A map from stub paths to corresponding temporary directories. */
const temps = new Map<string, string>();

export interface StubOptions {
readThrough?: boolean;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -260,6 +265,8 @@ export function restore() {

export function dispose() {
restore();
temps.forEach((it) => Deno.removeSync(it, { recursive: true }));
temps.clear();
spies.clear();
}

Expand Down
1 change: 1 addition & 0 deletions src/fs_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit fcf0b6c

Please sign in to comment.