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
+13 -6
View File
@@ -4,6 +4,7 @@ import { Builder } from "./builder.ts";
import { App } from "../app.ts";
import { RemoteIsland } from "@marvinh-test/fresh-island";
import { BUILD_ID } from "../runtime/build_id.ts";
import { withTmpDir } from "../test_utils.ts";
Deno.test({
name: "Builder - chain onTransformStaticFile",
@@ -29,7 +30,8 @@ Deno.test({
},
);
const tmp = await Deno.makeTempDir();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
await Deno.writeTextFile(path.join(tmp, "foo.css"), "body { color: red; }");
const app = new App({
staticDir: tmp,
@@ -49,7 +51,8 @@ Deno.test({
name: "Builder - handles Windows paths",
fn: async () => {
const builder = new Builder();
const tmp = await Deno.makeTempDir();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
await Deno.mkdir(path.join(tmp, "images"));
await Deno.writeTextFile(
path.join(tmp, "images", "batman.svg"),
@@ -76,7 +79,8 @@ Deno.test({
name: "Builder - hashes CSS urls by default",
fn: async () => {
const builder = new Builder();
const tmp = await Deno.makeTempDir();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
await Deno.writeTextFile(
path.join(tmp, "foo.css"),
"body { background: url('/foo.jpg'); }",
@@ -103,7 +107,8 @@ Deno.test({
name: "Builder - hashes CSS urls by default",
fn: async () => {
const builder = new Builder();
const tmp = await Deno.makeTempDir();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
await Deno.writeTextFile(
path.join(tmp, "foo.css"),
`:root { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' fill='none' stroke='rgb(76, 154.5, 137.5)' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='20 6 9 17 4 12'%3E%3C/polyline%3E%3C/svg%3E"); }`,
@@ -131,7 +136,8 @@ Deno.test({
name: "Builder - can bundle islands from JSR",
fn: async () => {
const builder = new Builder();
const tmp = await Deno.makeTempDir();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const app = new App({
staticDir: tmp,
build: {
@@ -190,7 +196,8 @@ Deno.test({
},
);
const tmp = await Deno.makeTempDir();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
await Deno.writeTextFile(path.join(tmp, "foo.css"), "body { color: red; }");
await Deno.writeTextFile(
path.join(tmp, "bar.css"),