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:
+13
-6
@@ -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"),
|
||||
|
||||
@@ -2,13 +2,14 @@ import { expect } from "@std/expect";
|
||||
import * as path from "@std/path";
|
||||
import { MemoryBuildCache } from "./dev_build_cache.ts";
|
||||
import { FreshFileTransformer } from "./file_transformer.ts";
|
||||
import { createFakeFs } from "../test_utils.ts";
|
||||
import { createFakeFs, withTmpDir } from "../test_utils.ts";
|
||||
import type { ResolvedFreshConfig } from "../mod.ts";
|
||||
|
||||
Deno.test({
|
||||
name: "MemoryBuildCache - 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: "development",
|
||||
|
||||
@@ -3,6 +3,7 @@ import denoJson from "../../deno.json" with { type: "json" };
|
||||
import { WEEK } from "@std/datetime";
|
||||
import { getStdOutput } from "../../tests/test_utils.tsx";
|
||||
import { expect } from "@std/expect";
|
||||
import { withTmpDir } from "../test_utils.ts";
|
||||
import type { CheckFile } from "./update_check.ts";
|
||||
|
||||
const CURRENT_VERSION = denoJson.version;
|
||||
@@ -10,8 +11,8 @@ const CURRENT_VERSION = denoJson.version;
|
||||
const cwd = import.meta.dirname!;
|
||||
|
||||
Deno.test("stores update check file in $HOME/fresh", async () => {
|
||||
const tmpDirName = await Deno.makeTempDir();
|
||||
const filePath = path.join(tmpDirName, "latest.json");
|
||||
await using tmp = await withTmpDir();
|
||||
const filePath = path.join(tmp.dir, "latest.json");
|
||||
|
||||
await new Deno.Command(Deno.execPath(), {
|
||||
args: [
|
||||
@@ -22,7 +23,7 @@ Deno.test("stores update check file in $HOME/fresh", async () => {
|
||||
cwd,
|
||||
env: {
|
||||
CI: "false",
|
||||
TEST_HOME: tmpDirName,
|
||||
TEST_HOME: tmp.dir,
|
||||
},
|
||||
}).output();
|
||||
|
||||
@@ -33,8 +34,6 @@ Deno.test("stores update check file in $HOME/fresh", async () => {
|
||||
last_checked: text.last_checked,
|
||||
last_shown: text.last_shown,
|
||||
});
|
||||
|
||||
await Deno.remove(tmpDirName, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("skips update check on specific environment variables", async (t) => {
|
||||
@@ -42,7 +41,7 @@ Deno.test("skips update check on specific environment variables", async (t) => {
|
||||
|
||||
for (const env of envs) {
|
||||
await t.step(`checking ${env}`, async () => {
|
||||
const tmpDirName = await Deno.makeTempDir();
|
||||
await using tmp = await withTmpDir();
|
||||
const out = await new Deno.Command(Deno.execPath(), {
|
||||
args: [
|
||||
"run",
|
||||
@@ -52,7 +51,7 @@ Deno.test("skips update check on specific environment variables", async (t) => {
|
||||
cwd,
|
||||
env: {
|
||||
[env]: "true",
|
||||
TEST_HOME: tmpDirName,
|
||||
TEST_HOME: tmp.dir,
|
||||
LATEST_VERSION: "1.30.0",
|
||||
},
|
||||
stderr: "piped",
|
||||
@@ -61,15 +60,13 @@ Deno.test("skips update check on specific environment variables", async (t) => {
|
||||
|
||||
const { stdout } = getStdOutput(out);
|
||||
expect(stdout).not.toMatch(/Fresh 1\.30\.0 is available/);
|
||||
|
||||
await Deno.remove(tmpDirName, { recursive: true });
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("shows update message on version mismatch", async () => {
|
||||
const tmpDirName = await Deno.makeTempDir();
|
||||
const filePath = path.join(tmpDirName, "latest.json");
|
||||
await using tmp = await withTmpDir();
|
||||
const filePath = path.join(tmp.dir, "latest.json");
|
||||
|
||||
await Deno.writeTextFile(
|
||||
filePath,
|
||||
@@ -89,7 +86,7 @@ Deno.test("shows update message on version mismatch", async () => {
|
||||
cwd,
|
||||
env: {
|
||||
CI: "false",
|
||||
TEST_HOME: tmpDirName,
|
||||
TEST_HOME: tmp.dir,
|
||||
LATEST_VERSION: "999.999.0",
|
||||
},
|
||||
stderr: "piped",
|
||||
@@ -107,12 +104,10 @@ Deno.test("shows update message on version mismatch", async () => {
|
||||
last_checked: text.last_checked,
|
||||
last_shown: text.last_shown,
|
||||
});
|
||||
|
||||
await Deno.remove(tmpDirName, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("only fetch new version defined by interval", async (t) => {
|
||||
const tmpDirName = await Deno.makeTempDir();
|
||||
await using tmp = await withTmpDir();
|
||||
|
||||
await t.step("fetches latest version initially", async () => {
|
||||
const out = await new Deno.Command(Deno.execPath(), {
|
||||
@@ -125,7 +120,7 @@ Deno.test("only fetch new version defined by interval", async (t) => {
|
||||
env: {
|
||||
CI: "false",
|
||||
UPDATE_INTERVAL: "100000",
|
||||
TEST_HOME: tmpDirName,
|
||||
TEST_HOME: tmp.dir,
|
||||
LATEST_VERSION: "1.30.0",
|
||||
},
|
||||
stderr: "piped",
|
||||
@@ -147,7 +142,7 @@ Deno.test("only fetch new version defined by interval", async (t) => {
|
||||
env: {
|
||||
CI: "false",
|
||||
UPDATE_INTERVAL: "100000",
|
||||
TEST_HOME: tmpDirName,
|
||||
TEST_HOME: tmp.dir,
|
||||
LATEST_VERSION: "1.30.0",
|
||||
},
|
||||
stderr: "piped",
|
||||
@@ -169,7 +164,7 @@ Deno.test("only fetch new version defined by interval", async (t) => {
|
||||
env: {
|
||||
CI: "false",
|
||||
UPDATE_INTERVAL: "1 ",
|
||||
TEST_HOME: tmpDirName,
|
||||
TEST_HOME: tmp.dir,
|
||||
LATEST_VERSION: "1.30.0",
|
||||
},
|
||||
}).output();
|
||||
@@ -177,12 +172,10 @@ Deno.test("only fetch new version defined by interval", async (t) => {
|
||||
const { stdout } = getStdOutput(out);
|
||||
expect(stdout).toMatch(/fetching latest version/);
|
||||
});
|
||||
|
||||
await Deno.remove(tmpDirName, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("updates current version in cache file", async () => {
|
||||
const tmpDirName = await Deno.makeTempDir();
|
||||
await using tmp = await withTmpDir();
|
||||
|
||||
const checkFile: CheckFile = {
|
||||
current_version: "1.2.0",
|
||||
@@ -191,7 +184,7 @@ Deno.test("updates current version in cache file", async () => {
|
||||
};
|
||||
|
||||
await Deno.writeTextFile(
|
||||
path.join(tmpDirName, "latest.json"),
|
||||
path.join(tmp.dir, "latest.json"),
|
||||
JSON.stringify(checkFile, null, 2),
|
||||
);
|
||||
|
||||
@@ -204,7 +197,7 @@ Deno.test("updates current version in cache file", async () => {
|
||||
cwd,
|
||||
env: {
|
||||
CI: "false",
|
||||
TEST_HOME: tmpDirName,
|
||||
TEST_HOME: tmp.dir,
|
||||
LATEST_VERSION: CURRENT_VERSION,
|
||||
},
|
||||
stderr: "piped",
|
||||
@@ -213,12 +206,10 @@ Deno.test("updates current version in cache file", async () => {
|
||||
|
||||
const { stdout } = getStdOutput(out);
|
||||
expect(stdout).not.toMatch(/Fresh .* is available/);
|
||||
|
||||
await Deno.remove(tmpDirName, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("only shows update message when current < latest", async () => {
|
||||
const tmpDirName = await Deno.makeTempDir();
|
||||
await using tmp = await withTmpDir();
|
||||
|
||||
const checkFile: CheckFile = {
|
||||
current_version: "9999.999.0",
|
||||
@@ -227,7 +218,7 @@ Deno.test("only shows update message when current < latest", async () => {
|
||||
};
|
||||
|
||||
await Deno.writeTextFile(
|
||||
path.join(tmpDirName, "latest.json"),
|
||||
path.join(tmp.dir, "latest.json"),
|
||||
JSON.stringify(checkFile, null, 2),
|
||||
);
|
||||
|
||||
@@ -240,7 +231,7 @@ Deno.test("only shows update message when current < latest", async () => {
|
||||
cwd,
|
||||
env: {
|
||||
CI: "false",
|
||||
TEST_HOME: tmpDirName,
|
||||
TEST_HOME: tmp.dir,
|
||||
LATEST_VERSION: CURRENT_VERSION,
|
||||
CURRENT_VERSION: "99999.9999.0",
|
||||
},
|
||||
@@ -250,12 +241,10 @@ Deno.test("only shows update message when current < latest", async () => {
|
||||
|
||||
const { stdout } = getStdOutput(out);
|
||||
expect(stdout).not.toMatch(/Fresh .* is available/);
|
||||
|
||||
await Deno.remove(tmpDirName, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("migrates to last_shown property", async () => {
|
||||
const tmpDirName = await Deno.makeTempDir();
|
||||
await using tmp = await withTmpDir();
|
||||
|
||||
const checkFile: CheckFile = {
|
||||
latest_version: "1.4.0",
|
||||
@@ -264,7 +253,7 @@ Deno.test("migrates to last_shown property", async () => {
|
||||
};
|
||||
|
||||
await Deno.writeTextFile(
|
||||
path.join(tmpDirName, "latest.json"),
|
||||
path.join(tmp.dir, "latest.json"),
|
||||
JSON.stringify(checkFile, null, 2),
|
||||
);
|
||||
|
||||
@@ -277,7 +266,7 @@ Deno.test("migrates to last_shown property", async () => {
|
||||
cwd,
|
||||
env: {
|
||||
CI: "false",
|
||||
TEST_HOME: tmpDirName,
|
||||
TEST_HOME: tmp.dir,
|
||||
CURRENT_VERSION: "1.2.0",
|
||||
LATEST_VERSION: "99999.9999.0",
|
||||
},
|
||||
@@ -290,18 +279,16 @@ Deno.test("migrates to last_shown property", async () => {
|
||||
|
||||
const checkFileAfter = JSON.parse(
|
||||
await Deno.readTextFile(
|
||||
path.join(tmpDirName, "latest.json"),
|
||||
path.join(tmp.dir, "latest.json"),
|
||||
),
|
||||
);
|
||||
|
||||
// Check if last version was written
|
||||
expect(typeof checkFileAfter.last_shown).toEqual("string");
|
||||
|
||||
await Deno.remove(tmpDirName, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("doesn't show update if last_shown + interval >= today", async () => {
|
||||
const tmpDirName = await Deno.makeTempDir();
|
||||
await using tmp = await withTmpDir();
|
||||
|
||||
const todayMinus1Hour = new Date();
|
||||
todayMinus1Hour.setHours(todayMinus1Hour.getHours() - 1);
|
||||
@@ -314,7 +301,7 @@ Deno.test("doesn't show update if last_shown + interval >= today", async () => {
|
||||
};
|
||||
|
||||
await Deno.writeTextFile(
|
||||
path.join(tmpDirName, "latest.json"),
|
||||
path.join(tmp.dir, "latest.json"),
|
||||
JSON.stringify(checkFile, null, 2),
|
||||
);
|
||||
|
||||
@@ -327,7 +314,7 @@ Deno.test("doesn't show update if last_shown + interval >= today", async () => {
|
||||
cwd,
|
||||
env: {
|
||||
CI: "false",
|
||||
TEST_HOME: tmpDirName,
|
||||
TEST_HOME: tmp.dir,
|
||||
CURRENT_VERSION: "1.2.0",
|
||||
LATEST_VERSION: "99999.9999.0",
|
||||
},
|
||||
@@ -337,14 +324,12 @@ Deno.test("doesn't show update if last_shown + interval >= today", async () => {
|
||||
|
||||
const { stdout } = getStdOutput(out);
|
||||
expect(stdout).not.toMatch(/Fresh .* is available/);
|
||||
|
||||
await Deno.remove(tmpDirName, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test(
|
||||
"shows update if last_shown + interval < today",
|
||||
async () => {
|
||||
const tmpDirName = await Deno.makeTempDir();
|
||||
await using tmp = await withTmpDir();
|
||||
|
||||
const yesterday = new Date();
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
@@ -357,7 +342,7 @@ Deno.test(
|
||||
};
|
||||
|
||||
await Deno.writeTextFile(
|
||||
path.join(tmpDirName, "latest.json"),
|
||||
path.join(tmp.dir, "latest.json"),
|
||||
JSON.stringify(checkFile, null, 2),
|
||||
);
|
||||
|
||||
@@ -370,7 +355,7 @@ Deno.test(
|
||||
cwd,
|
||||
env: {
|
||||
CI: "false",
|
||||
TEST_HOME: tmpDirName,
|
||||
TEST_HOME: tmp.dir,
|
||||
CURRENT_VERSION: CURRENT_VERSION,
|
||||
LATEST_VERSION: "99999.9999.0",
|
||||
},
|
||||
@@ -383,12 +368,10 @@ Deno.test(
|
||||
|
||||
const checkFileAfter = JSON.parse(
|
||||
await Deno.readTextFile(
|
||||
path.join(tmpDirName, "latest.json"),
|
||||
path.join(tmp.dir, "latest.json"),
|
||||
),
|
||||
);
|
||||
|
||||
expect(checkFileAfter.last_shown).not.toEqual(yesterday.toISOString());
|
||||
|
||||
await Deno.remove(tmpDirName, { recursive: true });
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user