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
81 lines
3.0 KiB
TypeScript
81 lines
3.0 KiB
TypeScript
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>) {
|
|
const res = await readResult;
|
|
if (res === null) return null;
|
|
if (res.readable instanceof Uint8Array) throw new Error("not implemented");
|
|
return new Response(res.readable).text();
|
|
}
|
|
|
|
Deno.test({
|
|
name: "ProdBuildCache - should error if reading outside of staticDir",
|
|
fn: async () => {
|
|
await using _tmp = await withTmpDir();
|
|
const tmp = _tmp.dir;
|
|
const config: ResolvedFreshConfig = {
|
|
root: tmp,
|
|
mode: "production",
|
|
basePath: "/",
|
|
staticDir: path.join(tmp, "static"),
|
|
build: {
|
|
outDir: path.join(tmp, "dist"),
|
|
},
|
|
};
|
|
await Deno.mkdir(path.join(tmp, "static", ".well-known"), {
|
|
recursive: true,
|
|
});
|
|
await Deno.mkdir(path.join(tmp, "dist", "static"), {
|
|
recursive: true,
|
|
});
|
|
await Promise.all([
|
|
Deno.writeTextFile(
|
|
path.join(tmp, "dist", "secret-styles.css"),
|
|
"SECRET!",
|
|
),
|
|
Deno.writeTextFile(path.join(tmp, "SECRETS.txt"), "SECRET!"),
|
|
Deno.writeTextFile(path.join(tmp, "dist", "static", "styles.css"), "OK"),
|
|
Deno.writeTextFile(
|
|
path.join(tmp, "static", ".well-known", "foo.txt"),
|
|
"OK",
|
|
),
|
|
]);
|
|
const buildCache = new ProdBuildCache(
|
|
config,
|
|
new Map(),
|
|
new Map([
|
|
["../secret-styles.css", { generated: true, hash: "SECRET!" }],
|
|
["../SECRETS.txt", { generated: false, hash: "SECRET!" }],
|
|
["./../secret-styles.css", { generated: true, hash: "SECRET!" }],
|
|
["./../SECRETS.txt", { generated: false, hash: "SECRET!" }],
|
|
["styles.css", { generated: true, hash: "OK" }],
|
|
[".well-known/foo.txt", { generated: false, hash: "OK" }],
|
|
["./styles.css", { generated: true, hash: "OK" }],
|
|
["./.well-known/foo.txt", { generated: false, hash: "OK" }],
|
|
]),
|
|
true,
|
|
);
|
|
|
|
const secret1 = getContent(buildCache.readFile("../styles.css"));
|
|
const secret2 = getContent(buildCache.readFile("../SECRETS.txt"));
|
|
const secret3 = getContent(buildCache.readFile("./../styles.css"));
|
|
const secret4 = getContent(buildCache.readFile("./../SECRETS.txt"));
|
|
const public1 = getContent(buildCache.readFile("styles.css"));
|
|
const public2 = getContent(buildCache.readFile(".well-known/foo.txt"));
|
|
const public3 = getContent(buildCache.readFile("./styles.css"));
|
|
const public4 = getContent(buildCache.readFile("./.well-known/foo.txt"));
|
|
|
|
await expect(secret1).resolves.toBe(null);
|
|
await expect(secret2).resolves.toBe(null);
|
|
await expect(secret3).resolves.toBe(null);
|
|
await expect(secret4).resolves.toBe(null);
|
|
await expect(public1).resolves.toBe("OK");
|
|
await expect(public2).resolves.toBe("OK");
|
|
await expect(public3).resolves.toBe("OK");
|
|
await expect(public4).resolves.toBe("OK");
|
|
},
|
|
});
|