Skip to content

Commit 68eaa28

Browse files
committed
wrap in try-finally and add e2e tests
1 parent 3769262 commit 68eaa28

File tree

2 files changed

+67
-26
lines changed

2 files changed

+67
-26
lines changed

packages/@apphosting/adapter-nextjs/e2e/config-override.spec.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as assert from "assert";
2-
import { posix } from "path";
2+
import { posix, join } from "path";
33
import fsExtra from "fs-extra";
44

5+
56
const host = process.env.HOST;
67
if (!host) {
78
throw new Error("HOST environment variable expected");
@@ -27,9 +28,47 @@ const compiledFilesPath = posix.join(
2728
".next",
2829
);
2930

31+
const standalonePath = posix.join(
32+
process.cwd(),
33+
"e2e",
34+
"runs",
35+
runId,
36+
".next",
37+
"standalone",
38+
);
39+
40+
const appPath = posix.join(
41+
process.cwd(),
42+
"e2e",
43+
"runs",
44+
runId,
45+
);
46+
3047
const requiredServerFilePath = posix.join(compiledFilesPath, "required-server-files.json");
3148

3249
describe("next.config override", () => {
50+
it("Should not overwrite original next config", async function () {
51+
if (
52+
scenario.includes("with-empty-config") ||
53+
scenario.includes("with-images-unoptimized-false") ||
54+
scenario.includes("with-custom-image-loader")
55+
) {
56+
this.skip();
57+
}
58+
const files = await fsExtra.readdir(appPath);
59+
const configRegex = /^next\.config\..*$/g;
60+
const configOriginalRegex = /^next\.config\.(?!original).*$/g;
61+
const configFiles = files
62+
.filter((file) => file.match(configRegex));
63+
assert.strictEqual(configFiles.length, 1);
64+
assert.ok(configFiles[0].match(configOriginalRegex), "found original config file in root");
65+
66+
const standaloneFiles = await fsExtra.readdir(standalonePath);
67+
const standaloneConfigFiles = standaloneFiles
68+
.filter((file) => file.match(configRegex));
69+
assert.strictEqual(standaloneConfigFiles.length, 2);
70+
assert.ok(configFiles.some((file) => file.match(configOriginalRegex)), "no original config found in standalone");
71+
});
3372
it("should have images optimization disabled", async function () {
3473
if (
3574
scenario.includes("with-empty-config") ||

packages/@apphosting/adapter-nextjs/src/bin/build.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,34 @@ if (await exists(nextConfigPath)) {
4141
await validateNextConfigOverride(root, opts.projectDirectory, nextConfig.configFileName);
4242
}
4343

44-
await runBuild();
44+
try {
45+
await runBuild();
4546

4647

47-
const adapterMetadata = getAdapterMetadata();
48-
const nextBuildDirectory = join(opts.projectDirectory, nextConfig.distDir);
49-
const outputBundleOptions = populateOutputBundleOptions(
50-
root,
51-
opts.projectDirectory,
52-
nextBuildDirectory,
53-
);
48+
const adapterMetadata = getAdapterMetadata();
49+
const nextBuildDirectory = join(opts.projectDirectory, nextConfig.distDir);
50+
const outputBundleOptions = populateOutputBundleOptions(
51+
root,
52+
opts.projectDirectory,
53+
nextBuildDirectory,
54+
);
5455

55-
await addRouteOverrides(
56-
outputBundleOptions.outputDirectoryAppPath,
57-
nextConfig.distDir,
58-
adapterMetadata,
59-
);
56+
await addRouteOverrides(
57+
outputBundleOptions.outputDirectoryAppPath,
58+
nextConfig.distDir,
59+
adapterMetadata,
60+
);
6061

61-
const nextjsVersion = process.env.FRAMEWORK_VERSION || "unspecified";
62-
await generateBuildOutput(
63-
root,
64-
opts.projectDirectory,
65-
outputBundleOptions,
66-
nextBuildDirectory,
67-
nextjsVersion,
68-
adapterMetadata,
69-
);
70-
await validateOutputDirectory(outputBundleOptions, nextBuildDirectory);
71-
72-
await restoreNextConfig(root, nextConfig.configFileName);
62+
const nextjsVersion = process.env.FRAMEWORK_VERSION || "unspecified";
63+
await generateBuildOutput(
64+
root,
65+
opts.projectDirectory,
66+
outputBundleOptions,
67+
nextBuildDirectory,
68+
nextjsVersion,
69+
adapterMetadata,
70+
);
71+
await validateOutputDirectory(outputBundleOptions, nextBuildDirectory);
72+
} finally {
73+
await restoreNextConfig(root, nextConfig.configFileName);
74+
}

0 commit comments

Comments
 (0)