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
+3 -1
View File
@@ -1,6 +1,7 @@
import { expect } from "@std/expect";
import * as path from "@std/path";
import { ProdBuildCache, type StaticFile } from "./build_cache.ts";
import { withTmpDir } from "./test_utils.ts";
import type { ResolvedFreshConfig } from "./mod.ts";
async function getContent(readResult: Promise<StaticFile | null>) {
@@ -13,7 +14,8 @@ async function getContent(readResult: Promise<StaticFile | null>) {
Deno.test({
name: "ProdBuildCache - should error if reading outside of staticDir",
fn: async () => {
const tmp = await Deno.makeTempDir();
await using _tmp = await withTmpDir();
const tmp = _tmp.dir;
const config: ResolvedFreshConfig = {
root: tmp,
mode: "production",