Skip to content

Commit

Permalink
Add [Symbol.dispose] hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Jun 14, 2024
1 parent 8e31fae commit 3411340
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ Depending on your testing style, you may want to call `rm` in test teardown, or
leave the fixture on disk for manual testing. If the latter, you can call `rm` immediately before `write`
to reset the fixture every time it runs.

[Explicit Resource Management](https://github.com/tc39/proposal-explicit-resource-management)
may also be used to automatically call `rm` on the project when it goes out of scope.

```ts
import { tempdirProject } from "@typestrong/fs-fixture-builder";
import { test } from "node:test"

test("Something with the filesystem", () => {
using project = tempdirProject();
// project will be cleaned up automatically after the test, even if it fails
})
```

## CLI

The command `capture-fs-fixture` is provided by this package to traverse a directory and generate code to recreate
Expand Down
3 changes: 2 additions & 1 deletion src/fs-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ function projectInternal(cwd: string) {
addSymlink,
write,
rm,
copyFilesFrom
copyFilesFrom,
[Symbol.dispose]: rm,
};
return fixture;
}
12 changes: 11 additions & 1 deletion test/fixtures.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { afterEach, beforeEach, test } from "node:test"
import { tempdirProject, type Project } from "#self"
import { equal } from "assert";
import { lstatSync, readFileSync } from "fs";
import { existsSync, lstatSync, readFileSync } from "fs";
import { join } from "path";

let fixture: Project;
Expand Down Expand Up @@ -38,3 +38,13 @@ test("Supports defining symlinks", () => {
const stat = lstatSync(join(fixture.cwd, "test2.txt"));
equal(stat.isSymbolicLink(), true);
});

test("Supports explicit resource management", () => {
let cwd: string;
{
using fix = tempdirProject();
cwd = fix.cwd;
}

equal(existsSync(cwd), false);
})

0 comments on commit 3411340

Please sign in to comment.