test: use disposable tmp dirs (#2983)

Replaces the patterns below with consistent `AsyncDisposable` and `await
using` resource management for automatic cleanup.

- `await withTmpDir(async (dir) => { ... })` with `finally`
  - Makes code a bit flatter, and reduces callback-hell a bit
- `await Deno.makeTempDir()` + `Deno.remove()`
- `await Deno.makeTempDir()`

Helps be more consistent in removing temporary folders. Among others,
ensures temp folders are removed in the tests below:

- `src/build_cache_test.ts`
- `src/dev/builder_test.ts`
- `src/dev/dev_build_cache_test.ts`

## TODO's

The `Deno.makeTempDir()` method is still used by `buildProd` as well,
but this method is used in the top module scope, where it does not work
correctly with `Deno.test` (it runs before the test methods). I removed
this part from the PR and leave this for later to avoid making the PR
too big.

-
https://github.com/csvn/fresh/blob/test%2Fdisposable-tmp-dirs/tests/test_utils.tsx/#L64
-
https://github.com/csvn/fresh/blob/test%2Fdisposable-tmp-dirs/www/main_test.ts/#L7
This commit is contained in:
Christian Svensson
2025-05-21 11:13:02 +02:00
committed by GitHub
parent db142115ec
commit 17257dd1c2
7 changed files with 482 additions and 491 deletions
+135 -139
View File
@@ -8,6 +8,7 @@ import * as path from "@std/path";
import { getStdOutput, withBrowser } from "../../tests/test_utils.tsx";
import { waitForText } from "../../tests/test_utils.tsx";
import { withChildProcessServer } from "../../tests/test_utils.tsx";
import { withTmpDir as withTmpDirBase } from "../../src/test_utils.ts";
import { stub } from "@std/testing/mock";
function stubPrompt(result: string) {
@@ -22,16 +23,11 @@ function stubConfirm(steps: Record<string, boolean> = {}) {
);
}
async function withTmpDir(fn: (dir: string) => void | Promise<void>) {
const hash = crypto.randomUUID().replaceAll(/-/g, "");
const dir = path.join(import.meta.dirname!, "..", "..", `tmp_${hash}`);
await Deno.mkdir(dir, { recursive: true });
try {
await fn(dir);
} finally {
await Deno.remove(dir, { recursive: true });
}
function withTmpDir(): Promise<{ dir: string } & AsyncDisposable> {
return withTmpDirBase({
dir: path.join(import.meta.dirname!, "..", ".."),
prefix: "tmp_",
});
}
async function patchProject(dir: string): Promise<void> {
@@ -64,57 +60,57 @@ async function readProjectFile(dir: string, pathname: string): Promise<string> {
}
Deno.test("init - new project", async () => {
await withTmpDir(async (dir) => {
using _promptStub = stubPrompt("fresh-init");
using _confirmStub = stubConfirm();
await initProject(dir, [], {});
});
await using tmp = await withTmpDir();
using _promptStub = stubPrompt("fresh-init");
using _confirmStub = stubConfirm();
await initProject(tmp.dir, [], {});
});
Deno.test("init - create project dir", async () => {
await withTmpDir(async (dir) => {
using _promptStub = stubPrompt("fresh-init");
using _confirmStub = stubConfirm();
await initProject(dir, [], {});
await using tmp = await withTmpDir();
const dir = tmp.dir;
using _promptStub = stubPrompt("fresh-init");
using _confirmStub = stubConfirm();
await initProject(dir, [], {});
const root = path.join(dir, "fresh-init");
await expectProjectFile(root, "deno.json");
await expectProjectFile(root, "main.ts");
await expectProjectFile(root, "dev.ts");
await expectProjectFile(root, ".gitignore");
await expectProjectFile(root, "static/styles.css");
});
const root = path.join(dir, "fresh-init");
await expectProjectFile(root, "deno.json");
await expectProjectFile(root, "main.ts");
await expectProjectFile(root, "dev.ts");
await expectProjectFile(root, ".gitignore");
await expectProjectFile(root, "static/styles.css");
});
Deno.test("init - with tailwind", async () => {
await withTmpDir(async (dir) => {
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm({
[CONFIRM_TAILWIND_MESSAGE]: true,
});
await initProject(dir, [], {});
const css = await readProjectFile(dir, "static/styles.css");
expect(css).toMatch(/@tailwind/);
const main = await readProjectFile(dir, "main.ts");
const dev = await readProjectFile(dir, "dev.ts");
expect(main).not.toMatch(/tailwind/);
expect(dev).toMatch(/tailwind/);
await using tmp = await withTmpDir();
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm({
[CONFIRM_TAILWIND_MESSAGE]: true,
});
await initProject(dir, [], {});
const css = await readProjectFile(dir, "static/styles.css");
expect(css).toMatch(/@tailwind/);
const main = await readProjectFile(dir, "main.ts");
const dev = await readProjectFile(dir, "dev.ts");
expect(main).not.toMatch(/tailwind/);
expect(dev).toMatch(/tailwind/);
});
Deno.test("init - with vscode", async () => {
await withTmpDir(async (dir) => {
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm({
[CONFIRM_VSCODE_MESSAGE]: true,
});
await initProject(dir, [], {});
await expectProjectFile(dir, ".vscode/settings.json");
await expectProjectFile(dir, ".vscode/extensions.json");
await using tmp = await withTmpDir();
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm({
[CONFIRM_VSCODE_MESSAGE]: true,
});
await initProject(dir, [], {});
await expectProjectFile(dir, ".vscode/settings.json");
await expectProjectFile(dir, ".vscode/extensions.json");
});
Deno.test({
@@ -123,33 +119,10 @@ Deno.test({
// behaviours when the formatter changes.
ignore: Deno.version.deno.includes("+"),
fn: async () => {
await withTmpDir(async (dir) => {
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], {});
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
await patchProject(dir);
const check = await new Deno.Command(Deno.execPath(), {
args: ["task", "check"],
cwd: dir,
stderr: "inherit",
stdout: "inherit",
}).output();
expect(check.code).toEqual(0);
});
},
});
Deno.test("init with tailwind - fmt, lint, and type check project", async () => {
await withTmpDir(async (dir) => {
await using tmp = await withTmpDir();
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm({
[CONFIRM_TAILWIND_MESSAGE]: true,
});
using _confirmStub = stubConfirm();
await initProject(dir, [], {});
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
@@ -163,86 +136,109 @@ Deno.test("init with tailwind - fmt, lint, and type check project", async () =>
stdout: "inherit",
}).output();
expect(check.code).toEqual(0);
},
});
Deno.test("init with tailwind - fmt, lint, and type check project", async () => {
await using tmp = await withTmpDir();
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm({
[CONFIRM_TAILWIND_MESSAGE]: true,
});
await initProject(dir, [], {});
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
await patchProject(dir);
const check = await new Deno.Command(Deno.execPath(), {
args: ["task", "check"],
cwd: dir,
stderr: "inherit",
stdout: "inherit",
}).output();
expect(check.code).toEqual(0);
});
Deno.test("init - can start dev server", async () => {
await withTmpDir(async (dir) => {
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], {});
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
await using tmp = await withTmpDir();
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], {});
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
await patchProject(dir);
await withChildProcessServer(
dir,
"dev",
async (address) => {
await withBrowser(async (page) => {
await page.goto(address);
await page.locator("button").click();
await waitForText(page, "button + p", "2");
});
},
);
});
await patchProject(dir);
await withChildProcessServer(
dir,
"dev",
async (address) => {
await withBrowser(async (page) => {
await page.goto(address);
await page.locator("button").click();
await waitForText(page, "button + p", "2");
});
},
);
});
Deno.test("init - can start built project", async () => {
await withTmpDir(async (dir) => {
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], {});
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
await using tmp = await withTmpDir();
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], {});
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
await patchProject(dir);
await patchProject(dir);
// Build
await new Deno.Command(Deno.execPath(), {
args: ["task", "build"],
stdin: "null",
stdout: "piped",
stderr: "piped",
cwd: dir,
}).output();
// Build
await new Deno.Command(Deno.execPath(), {
args: ["task", "build"],
stdin: "null",
stdout: "piped",
stderr: "piped",
cwd: dir,
}).output();
await withChildProcessServer(
dir,
"start",
async (address) => {
await withBrowser(async (page) => {
await page.goto(address);
await page.locator("button").click();
await waitForText(page, "button + p", "2");
});
},
);
});
await withChildProcessServer(
dir,
"start",
async (address) => {
await withBrowser(async (page) => {
await page.goto(address);
await page.locator("button").click();
await waitForText(page, "button + p", "2");
});
},
);
});
Deno.test("init - errors on missing build cache in prod", async () => {
await withTmpDir(async (dir) => {
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], {});
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
await using tmp = await withTmpDir();
const dir = tmp.dir;
using _promptStub = stubPrompt(".");
using _confirmStub = stubConfirm();
await initProject(dir, [], {});
await expectProjectFile(dir, "main.ts");
await expectProjectFile(dir, "dev.ts");
await patchProject(dir);
await patchProject(dir);
const cp = await new Deno.Command(Deno.execPath(), {
args: ["task", "start"],
stdin: "null",
stdout: "piped",
stderr: "piped",
cwd: dir,
}).output();
const cp = await new Deno.Command(Deno.execPath(), {
args: ["task", "start"],
stdin: "null",
stdout: "piped",
stderr: "piped",
cwd: dir,
}).output();
const { stderr } = getStdOutput(cp);
expect(cp.code).toEqual(1);
const { stderr } = getStdOutput(cp);
expect(cp.code).toEqual(1);
expect(stderr).toMatch(/Found 1 islands, but did not/);
});
expect(stderr).toMatch(/Found 1 islands, but did not/);
});