fix: env files not loaded in build task (#1743)

This commit is contained in:
Marvin Hagemeister
2023-09-06 13:00:07 +02:00
committed by GitHub
parent 227cefbf50
commit 20d5630534
2 changed files with 82 additions and 0 deletions
+2
View File
@@ -538,6 +538,8 @@ const DEV_TS = `#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
import config from "./fresh.config.ts";
import "$std/dotenv/load.ts";
await dev(import.meta.url, "./main.ts", config);
`;
const DEV_TS_PATH = join(resolvedDirectory, "dev.ts");
+80
View File
@@ -12,7 +12,9 @@ import {
retry,
} from "./deps.ts";
import {
assertTextMany,
clickWhenListenerReady,
fetchHtml,
startFreshServer,
waitForText,
} from "./test_utils.ts";
@@ -367,6 +369,84 @@ Deno.test({
sanitizeResources: false,
});
Deno.test({
name: "fresh-init loads env variables",
async fn(t) {
// Preparation
const tmpDirName = await Deno.makeTempDir();
const cliProcess = new Deno.Command(Deno.execPath(), {
args: [
"run",
"-A",
"init.ts",
tmpDirName,
"--twind",
"--vscode",
],
stdin: "null",
stdout: "piped",
stderr: "inherit",
});
await cliProcess.output();
// Add .env file
await Deno.writeTextFile(path.join(tmpDirName, ".env"), "FOO=true\n");
await Deno.writeTextFile(
path.join(tmpDirName, "routes", "env.tsx"),
`export default function Page() { return <h1>{Deno.env.get("FOO")}</h1> }`,
);
await t.step("start up the server", async () => {
const { serverProcess, lines, address } = await startFreshServer({
args: ["run", "-A", "--no-check", "dev.ts"],
cwd: tmpDirName,
});
const doc = await fetchHtml(`${address}/env`);
assertTextMany(doc, "h1", ["true"]);
await lines.cancel();
serverProcess.kill("SIGTERM");
await serverProcess.status;
});
await t.step("build code and start server again", async () => {
await new Deno.Command(Deno.execPath(), {
args: [
"task",
"build",
],
cwd: tmpDirName,
stdin: "null",
stdout: "piped",
stderr: "inherit",
}).output();
const { serverProcess, lines, address, output } = await startFreshServer({
args: ["run", "-A", "--no-check", "main.ts"],
cwd: tmpDirName,
});
assert(
output.find((line) => /Using snapshot found a/.test(line)),
"Snapshot message not printed",
);
const doc = await fetchHtml(`${address}/env`);
assertTextMany(doc, "h1", ["true"]);
await lines.cancel();
serverProcess.kill("SIGTERM");
await serverProcess.status;
});
await retry(() => Deno.remove(tmpDirName, { recursive: true }));
},
sanitizeResources: false,
});
Deno.test("fresh-update", async function fn(t) {
// Preparation
const tmpDirName = await Deno.makeTempDir();