Skip to content

Commit

Permalink
fix(wrangler): fail pages functions build-env if invalid config file
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmen Popoviciu committed Apr 11, 2024
1 parent c9c68d8 commit 094d4a3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
75 changes: 60 additions & 15 deletions packages/wrangler/src/__tests__/pages/pages-build-env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ describe("pages build env", () => {
);
});

it("should fail with no project dir", async () => {
await expect(
runWrangler("pages functions build-env")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"No Pages project location specified"`
);
});

it("should fail with no outfile", async () => {
await expect(
runWrangler("pages functions build-env .")
).rejects.toThrowErrorMatchingInlineSnapshot(`"No outfile specified"`);
});

it("should exit with specific exit code if no config file is found", async () => {
logger.loggerLevel = "debug";
await runWrangler("pages functions build-env . --outfile out.json");
Expand All @@ -57,20 +71,6 @@ describe("pages build env", () => {
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should fail with no project dir", async () => {
await expect(
runWrangler("pages functions build-env")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"No Pages project location specified"`
);
});

it("should fail with no outfile", async () => {
await expect(
runWrangler("pages functions build-env .")
).rejects.toThrowErrorMatchingInlineSnapshot(`"No outfile specified"`);
});

it("should exit with specific code if a non-pages config file is found", async () => {
logger.loggerLevel = "debug";
writeWranglerToml({
Expand Down Expand Up @@ -112,7 +112,7 @@ describe("pages build env", () => {
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should exit correctly with an unparseable config file", async () => {
it("should exit correctly with an unparseable non-pages config file", async () => {
logger.loggerLevel = "debug";

writeFileSync("./wrangler.toml", 'INVALID "FILE');
Expand Down Expand Up @@ -170,6 +170,51 @@ describe("pages build env", () => {
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should throw an error if an invalid pages confg file is found", async () => {
writeWranglerToml({
pages_build_output_dir: "dist",
vars: {
VAR1: "VALUE1",
},
env: {
staging: {
vars: {
VAR1: "PROD_VALUE1",
},
},
},
});

await expect(runWrangler("pages functions build-env . --outfile data.json"))
.rejects.toThrowErrorMatchingInlineSnapshot(`
"Running configuration file validation for Pages:
- Configuration file contains the following environment names that are not supported by Pages projects:
\\"staging\\".
The supported named-environments for Pages are \\"preview\\" and \\"production\\"."
`);
});

it("should exit if an unparseable pages confg file is found", async () => {
writeFileSync(
"./wrangler.toml",
`
pages_build_output_dir = "./dist"
name = "pages-is-awesome"
compatibility_date = "2024-01-01"
something that fails toml parsing
`
);

await runWrangler("pages functions build-env . --outfile data.json");
expect(process.exitCode).toEqual(EXIT_CODE_INVALID_PAGES_CONFIG);
expect(std.out).toMatchInlineSnapshot(`
"Checking for configuration in a wrangler.toml configuration file (BETA)
Found wrangler.toml file. Reading build configuration..."
`);
});

it("should return top-level by default", async () => {
process.env.PAGES_ENVIRONMENT = "";
writeWranglerToml({
Expand Down
16 changes: 13 additions & 3 deletions packages/wrangler/src/pages/build-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,19 @@ export const Handler = async (args: PagesBuildEnvArgs) => {
true
);
} catch (err) {
logger.debug("wrangler.toml file is invalid. Exiting.");
process.exitCode = EXIT_CODE_INVALID_PAGES_CONFIG;
return;
// found `wrangler.toml` but `pages_build_output_dir` is not specififed
if (
err instanceof FatalError &&
err.code === EXIT_CODE_INVALID_PAGES_CONFIG
) {
logger.debug("wrangler.toml file is invalid. Exiting.");
process.exitCode = EXIT_CODE_INVALID_PAGES_CONFIG;
return;
}

// found `wrangler.toml` with `pages_build_output_dir` specified, but
// file contains invalid configuration
throw err;
}

// Ensure JSON variables are not included
Expand Down

0 comments on commit 094d4a3

Please sign in to comment.