|
| 1 | +const importUtils = import("@apphosting/adapter-nextjs/dist/utils.js"); |
| 2 | +import assert from "assert"; |
| 3 | +import fs from "fs"; |
| 4 | +import path from "path"; |
| 5 | +import os from "os"; |
| 6 | +import { OutputBundleOptions } from "../interfaces.js"; |
| 7 | + |
| 8 | +describe("build commands", () => { |
| 9 | + let tmpDir: string; |
| 10 | + let outputBundleOptions: OutputBundleOptions; |
| 11 | + beforeEach(() => { |
| 12 | + tmpDir = generateTmpDir(); |
| 13 | + outputBundleOptions = { |
| 14 | + bundleYamlPath: path.join(tmpDir, ".apphosting/bundle.yaml"), |
| 15 | + outputDirectory: path.join(tmpDir, ".apphosting"), |
| 16 | + outputPublicDirectory: path.join(tmpDir, ".apphosting/public"), |
| 17 | + outputStaticDirectory: path.join(tmpDir, ".apphosting/.next/static"), |
| 18 | + serverFilePath: path.join(tmpDir, ".apphosting/server.js"), |
| 19 | + }; |
| 20 | + }); |
| 21 | + |
| 22 | + it("expects all output bundle files to be generated", async () => { |
| 23 | + const { generateOutputDirectory } = await importUtils; |
| 24 | + const files = { |
| 25 | + ".next/standalone/standalonefile": "", |
| 26 | + ".next/static/staticfile": "", |
| 27 | + ".next/routes-manifest.json": `{ |
| 28 | + "headers":[], |
| 29 | + "rewrites":[], |
| 30 | + "redirects":[] |
| 31 | + }`, |
| 32 | + }; |
| 33 | + generateTestFiles(tmpDir, files); |
| 34 | + await generateOutputDirectory(tmpDir, outputBundleOptions, path.join(tmpDir, ".next")); |
| 35 | + |
| 36 | + const expectedFiles = { |
| 37 | + ".apphosting/.next/static/staticfile": "", |
| 38 | + ".apphosting/standalonefile": "", |
| 39 | + ".apphosting/bundle.yaml": `headers: [] |
| 40 | +redirects: [] |
| 41 | +rewrites: [] |
| 42 | +runCommand: node .apphosting/server.js |
| 43 | +neededDirs: |
| 44 | + - .apphosting |
| 45 | +staticAssets: |
| 46 | + - .apphosting/public |
| 47 | +`, |
| 48 | + }; |
| 49 | + validateTestFiles(tmpDir, expectedFiles); |
| 50 | + }); |
| 51 | + |
| 52 | + it("expects public directory to be copied over", async () => { |
| 53 | + const { generateOutputDirectory } = await importUtils; |
| 54 | + const files = { |
| 55 | + ".next/standalone/standalonefile": "", |
| 56 | + ".next/static/staticfile": "", |
| 57 | + "public/publicfile": "", |
| 58 | + ".next/routes-manifest.json": `{ |
| 59 | + "headers":[], |
| 60 | + "rewrites":[], |
| 61 | + "redirects":[] |
| 62 | + }`, |
| 63 | + }; |
| 64 | + generateTestFiles(tmpDir, files); |
| 65 | + await generateOutputDirectory(tmpDir, outputBundleOptions, path.join(tmpDir, ".next")); |
| 66 | + |
| 67 | + const expectedFiles = { |
| 68 | + ".apphosting/.next/static/staticfile": "", |
| 69 | + ".apphosting/standalonefile": "", |
| 70 | + ".apphosting/public/publicfile": "", |
| 71 | + ".apphosting/bundle.yaml": `headers: [] |
| 72 | +redirects: [] |
| 73 | +rewrites: [] |
| 74 | +runCommand: node .apphosting/server.js |
| 75 | +neededDirs: |
| 76 | + - .apphosting |
| 77 | +staticAssets: |
| 78 | + - .apphosting/public |
| 79 | +`, |
| 80 | + }; |
| 81 | + validateTestFiles(tmpDir, expectedFiles); |
| 82 | + }); |
| 83 | + |
| 84 | + it("expects bundle.yaml headers/rewrites/redirects to be generated", async () => { |
| 85 | + const { generateOutputDirectory } = await importUtils; |
| 86 | + const files = { |
| 87 | + ".next/standalone/standalonefile": "", |
| 88 | + ".next/static/staticfile": "", |
| 89 | + ".next/routes-manifest.json": `{ |
| 90 | + "headers":[{"source":"source", "headers":["header1"]}], |
| 91 | + "rewrites":[{"source":"source", "destination":"destination"}], |
| 92 | + "redirects":[{"source":"source", "destination":"destination"}] |
| 93 | + }`, |
| 94 | + }; |
| 95 | + generateTestFiles(tmpDir, files); |
| 96 | + await generateOutputDirectory(tmpDir, outputBundleOptions, path.join(tmpDir, ".next")); |
| 97 | + |
| 98 | + const expectedFiles = { |
| 99 | + ".apphosting/.next/static/staticfile": "", |
| 100 | + ".apphosting/standalonefile": "", |
| 101 | + ".apphosting/bundle.yaml": `headers: |
| 102 | + - source: source |
| 103 | + headers: |
| 104 | + - header1 |
| 105 | +redirects: |
| 106 | + - source: source |
| 107 | + destination: destination |
| 108 | +rewrites: |
| 109 | + - source: source |
| 110 | + destination: destination |
| 111 | +runCommand: node .apphosting/server.js |
| 112 | +neededDirs: |
| 113 | + - .apphosting |
| 114 | +staticAssets: |
| 115 | + - .apphosting/public |
| 116 | +`, |
| 117 | + }; |
| 118 | + validateTestFiles(tmpDir, expectedFiles); |
| 119 | + }); |
| 120 | + |
| 121 | + it("test populate output bundle options", async () => { |
| 122 | + const { populateOutputBundleOptions } = await importUtils; |
| 123 | + const expectedOutputBundleOptions = { |
| 124 | + bundleYamlPath: "test/.apphosting/bundle.yaml", |
| 125 | + outputDirectory: "test/.apphosting", |
| 126 | + outputPublicDirectory: "test/.apphosting/public", |
| 127 | + outputStaticDirectory: "test/.apphosting/.next/static", |
| 128 | + serverFilePath: "test/.apphosting/server.js", |
| 129 | + }; |
| 130 | + assert.deepEqual(populateOutputBundleOptions("test"), expectedOutputBundleOptions); |
| 131 | + }); |
| 132 | + afterEach(() => { |
| 133 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 134 | + }); |
| 135 | +}); |
| 136 | +function generateTmpDir(): string { |
| 137 | + return fs.mkdtempSync(path.join(os.tmpdir(), "test-files")); |
| 138 | +} |
| 139 | + |
| 140 | +function generateTestFiles(baseDir: string, filesToGenerate: Object): void { |
| 141 | + Object.entries(filesToGenerate).forEach((file) => { |
| 142 | + const fileName = file[0]; |
| 143 | + const contents = file[1]; |
| 144 | + const fileToGenerate = path.join(baseDir, fileName); |
| 145 | + fs.mkdirSync(path.dirname(fileToGenerate), { recursive: true }); |
| 146 | + fs.writeFileSync(fileToGenerate, contents); |
| 147 | + }); |
| 148 | +} |
| 149 | + |
| 150 | +function validateTestFiles(baseDir: string, expectedFiles: Object): void { |
| 151 | + Object.entries(expectedFiles).forEach((file) => { |
| 152 | + const fileName = file[0]; |
| 153 | + const expectedContents = file[1]; |
| 154 | + const fileToRead = path.join(baseDir, fileName); |
| 155 | + const contents = fs.readFileSync(fileToRead).toString(); |
| 156 | + assert.deepEqual(contents, expectedContents); |
| 157 | + }); |
| 158 | +} |
0 commit comments