feat: add build.outDir option (#1797)

This commit is contained in:
Marvin Hagemeister
2023-09-20 10:40:09 +02:00
committed by GitHub
parent ff6ae7eb7f
commit 3fd8fe89d3
30 changed files with 846 additions and 448 deletions
+88 -22
View File
@@ -1,38 +1,49 @@
import * as path from "$std/path/mod.ts";
import { puppeteer } from "./deps.ts";
import { assert } from "$std/_util/asserts.ts";
import { startFreshServer, waitForText } from "$fresh/tests/test_utils.ts";
import {
getStdOutput,
startFreshServer,
waitForText,
} from "$fresh/tests/test_utils.ts";
import { BuildSnapshotJson } from "$fresh/src/build/mod.ts";
import { assertStringIncludes } from "$std/testing/asserts.ts";
import { assertNotMatch } from "$std/testing/asserts.ts";
function runBuild(fixture: string, subDirPath: string, outDir: string) {
return new Deno.Command(Deno.execPath(), {
args: [
"run",
"-A",
path.join(fixture, subDirPath, "dev.ts"),
"build",
],
env: {
GITHUB_SHA: "__BUILD_ID__",
DENO_DEPLOYMENT_ID: "__BUILD_ID__",
FRESH_TEST_OUTDIR: outDir,
},
stdin: "null",
stdout: "piped",
stderr: "piped",
}).output();
}
async function testBuild(
t: Deno.TestContext,
fixture: string,
subDirPath = "",
options: {
subDirPath?: string;
outDir?: string;
} = {},
) {
const outDir = path.join(fixture, subDirPath, "_fresh");
const subDirPath = options.subDirPath ?? "";
const outDir = options.outDir ?? path.join(fixture, subDirPath, "_fresh");
try {
await t.step("build snapshot", async () => {
const res = await new Deno.Command(Deno.execPath(), {
args: [
"run",
"-A",
path.join(fixture, subDirPath, "dev.ts"),
"build",
],
env: {
GITHUB_SHA: "__BUILD_ID__",
DENO_DEPLOYMENT_ID: "__BUILD_ID__",
},
stdin: "null",
stdout: "piped",
stderr: "inherit",
}).output();
const decoder = new TextDecoder();
const stdout = decoder.decode(res.stdout);
const res = await runBuild(fixture, subDirPath, outDir);
const { stdout } = getStdOutput(res);
assert(
!/Using snapshot found at/.test(stdout),
"Using snapshot message was shown during build",
@@ -161,5 +172,60 @@ Deno.test("build snapshot and restore from it", async (t) => {
Deno.test("build snapshot and restore from it when has sub dirs", async (t) => {
const fixture = path.join(Deno.cwd(), "tests", "fixture_build_sub_dir");
await testBuild(t, fixture, "src");
await testBuild(t, fixture, { subDirPath: "src" });
});
Deno.test(
"build snapshot with custom build.outDir",
async (t) => {
async function assertOutputDir(outDir: string, out: Deno.CommandOutput) {
const { stdout, stderr } = getStdOutput(out);
const msg =
`Missing output directory: ${outDir}\n\nCLI output:\n${stdout}\n${stderr}`;
const dir: string | URL = outDir.startsWith("file://")
? new URL(outDir)
: outDir;
try {
assert((await Deno.stat(dir)).isDirectory, msg);
} catch (err) {
throw new Error(msg, { cause: err });
}
}
await t.step("uses on relative outDir", async () => {
const fixture = path.join(Deno.cwd(), "tests", "fixture_build_out_dir");
const out = await runBuild(fixture, "", "./tmp/asdf");
const outDir = path.join(fixture, "tmp", "asdf");
await assertOutputDir(outDir, out);
});
await t.step("uses absolute outDir", async () => {
const fixture = path.join(
Deno.cwd(),
"tests",
"fixture_build_out_dir_sub",
);
const outDir = path.join(fixture, "tmp");
const out = await runBuild(fixture, "src", outDir);
await assertOutputDir(outDir, out);
});
await t.step("uses file:// outDir", async () => {
const fixture = path.join(
Deno.cwd(),
"tests",
"fixture_build_out_dir_sub2",
);
const outDirPath = path.join(fixture, "tmp");
const outDir = path.toFileUrl(outDirPath).href;
const out = await runBuild(fixture, "src", outDir);
await assertOutputDir(outDir, out);
});
},
);