11const importUtils = import ( "@apphosting/adapter-nextjs/dist/utils.js" ) ;
22import assert from "assert" ;
33import fs from "fs" ;
4+ import yaml from "yaml" ;
45import path from "path" ;
56import os from "os" ;
67import { OutputBundleOptions } from "../interfaces.js" ;
78
89describe ( "build commands" , ( ) => {
910 let tmpDir : string ;
1011 let outputBundleOptions : OutputBundleOptions ;
12+ let defaultNextVersion : string ;
1113 beforeEach ( ( ) => {
1214 tmpDir = generateTmpDir ( ) ;
1315 outputBundleOptions = {
@@ -18,10 +20,11 @@ describe("build commands", () => {
1820 outputStaticDirectoryPath : path . join ( tmpDir , ".apphosting/.next/static" ) ,
1921 serverFilePath : path . join ( tmpDir , ".apphosting/server.js" ) ,
2022 } ;
23+ defaultNextVersion = "14.0.3" ;
2124 } ) ;
2225
2326 it ( "expects all output bundle files to be generated" , async ( ) => {
24- const { generateOutputDirectory, validateOutputDirectory } = await importUtils ;
27+ const { generateOutputDirectory, validateOutputDirectory, createMetadata } = await importUtils ;
2528 const files = {
2629 ".next/standalone/server.js" : "" ,
2730 ".next/static/staticfile" : "" ,
@@ -31,8 +34,15 @@ describe("build commands", () => {
3134 "redirects":[]
3235 }` ,
3336 } ;
37+ const packageVersion = createMetadata ( defaultNextVersion ) . adapterVersion ;
3438 generateTestFiles ( tmpDir , files ) ;
35- await generateOutputDirectory ( tmpDir , tmpDir , outputBundleOptions , path . join ( tmpDir , ".next" ) ) ;
39+ await generateOutputDirectory (
40+ tmpDir ,
41+ tmpDir ,
42+ outputBundleOptions ,
43+ path . join ( tmpDir , ".next" ) ,
44+ defaultNextVersion ,
45+ ) ;
3646 await validateOutputDirectory ( outputBundleOptions ) ;
3747
3848 const expectedFiles = {
@@ -46,6 +56,12 @@ neededDirs:
4656 - .apphosting
4757staticAssets:
4858 - .apphosting/public
59+ env: []
60+ metadata:
61+ adapterPackageName: "@apphosting/adapter-nextjs"
62+ adapterVersion: ${ packageVersion }
63+ framework: nextjs
64+ frameworkVersion: ${ defaultNextVersion }
4965` ,
5066 } ;
5167 validateTestFiles ( tmpDir , expectedFiles ) ;
@@ -76,22 +92,23 @@ staticAssets:
7692 serverFilePath : path . join ( tmpDir , ".apphosting/apps/next-app/server.js" ) ,
7793 } ,
7894 path . join ( tmpDir , ".next" ) ,
95+ defaultNextVersion ,
7996 ) ;
8097
8198 const expectedFiles = {
8299 ".apphosting/apps/next-app/.next/static/staticfile" : "" ,
83100 ".apphosting/apps/next-app/standalonefile" : "" ,
84- ".apphosting/bundle.yaml" : `headers: []
85- redirects: []
86- rewrites: []
87- runCommand: node .apphosting/apps/next-app/server.js
88- neededDirs:
89- - .apphosting
90- staticAssets:
91- - .apphosting/apps/next-app/public
92- ` ,
101+ } ;
102+ const expectedPartialYaml = {
103+ headers : [ ] ,
104+ rewrites : [ ] ,
105+ redirects : [ ] ,
106+ runCommand : "node .apphosting/apps/next-app/server.js" ,
107+ neededDirs : [ ".apphosting" ] ,
108+ staticAssets : [ ".apphosting/apps/next-app/public" ] ,
93109 } ;
94110 validateTestFiles ( tmpDir , expectedFiles ) ;
111+ validatePartialYamlContents ( tmpDir , ".apphosting/bundle.yaml" , expectedPartialYaml ) ;
95112 } ) ;
96113
97114 it ( "expects directories and other files to be copied over" , async ( ) => {
@@ -108,25 +125,31 @@ staticAssets:
108125 }` ,
109126 } ;
110127 generateTestFiles ( tmpDir , files ) ;
111- await generateOutputDirectory ( tmpDir , tmpDir , outputBundleOptions , path . join ( tmpDir , ".next" ) ) ;
128+ await generateOutputDirectory (
129+ tmpDir ,
130+ tmpDir ,
131+ outputBundleOptions ,
132+ path . join ( tmpDir , ".next" ) ,
133+ defaultNextVersion ,
134+ ) ;
112135 await validateOutputDirectory ( outputBundleOptions ) ;
113136
114137 const expectedFiles = {
115138 ".apphosting/.next/static/staticfile" : "" ,
116139 ".apphosting/server.js" : "" ,
117140 ".apphosting/public/publicfile" : "" ,
118141 ".apphosting/extrafile" : "" ,
119- ".apphosting/bundle.yaml" : `headers: []
120- redirects: []
121- rewrites: []
122- runCommand: node .apphosting/server.js
123- neededDirs:
124- - .apphosting
125- staticAssets:
126- - .apphosting/public
127- ` ,
142+ } ;
143+ const expectedPartialYaml = {
144+ headers : [ ] ,
145+ rewrites : [ ] ,
146+ redirects : [ ] ,
147+ runCommand : "node .apphosting/server.js" ,
148+ neededDirs : [ ".apphosting" ] ,
149+ staticAssets : [ ".apphosting/public" ] ,
128150 } ;
129151 validateTestFiles ( tmpDir , expectedFiles ) ;
152+ validatePartialYamlContents ( tmpDir , ".apphosting/bundle.yaml" , expectedPartialYaml ) ;
130153 } ) ;
131154
132155 it ( "expects bundle.yaml headers/rewrites/redirects to be generated" , async ( ) => {
@@ -141,30 +164,26 @@ staticAssets:
141164 }` ,
142165 } ;
143166 generateTestFiles ( tmpDir , files ) ;
144- await generateOutputDirectory ( tmpDir , tmpDir , outputBundleOptions , path . join ( tmpDir , ".next" ) ) ;
167+ await generateOutputDirectory (
168+ tmpDir ,
169+ tmpDir ,
170+ outputBundleOptions ,
171+ path . join ( tmpDir , ".next" ) ,
172+ defaultNextVersion ,
173+ ) ;
145174 await validateOutputDirectory ( outputBundleOptions ) ;
146175
147176 const expectedFiles = {
148177 ".apphosting/.next/static/staticfile" : "" ,
149178 ".apphosting/server.js" : "" ,
150- ".apphosting/bundle.yaml" : `headers:
151- - source: source
152- headers:
153- - header1
154- redirects:
155- - source: source
156- destination: destination
157- rewrites:
158- - source: source
159- destination: destination
160- runCommand: node .apphosting/server.js
161- neededDirs:
162- - .apphosting
163- staticAssets:
164- - .apphosting/public
165- ` ,
179+ } ;
180+ const expectedPartialYaml = {
181+ headers : [ { source : "source" , headers : [ "header1" ] } ] ,
182+ rewrites : [ { source : "source" , destination : "destination" } ] ,
183+ redirects : [ { source : "source" , destination : "destination" } ] ,
166184 } ;
167185 validateTestFiles ( tmpDir , expectedFiles ) ;
186+ validatePartialYamlContents ( tmpDir , ".apphosting/bundle.yaml" , expectedPartialYaml ) ;
168187 } ) ;
169188 it ( "test failed validateOutputDirectory" , async ( ) => {
170189 const { generateOutputDirectory, validateOutputDirectory } = await importUtils ;
@@ -178,7 +197,13 @@ staticAssets:
178197 }` ,
179198 } ;
180199 generateTestFiles ( tmpDir , files ) ;
181- await generateOutputDirectory ( tmpDir , tmpDir , outputBundleOptions , path . join ( tmpDir , ".next" ) ) ;
200+ await generateOutputDirectory (
201+ tmpDir ,
202+ tmpDir ,
203+ outputBundleOptions ,
204+ path . join ( tmpDir , ".next" ) ,
205+ defaultNextVersion ,
206+ ) ;
182207 assert . rejects ( async ( ) => await validateOutputDirectory ( outputBundleOptions ) ) ;
183208 } ) ;
184209 it ( "test populate output bundle options" , async ( ) => {
@@ -220,3 +245,16 @@ function validateTestFiles(baseDir: string, expectedFiles: Object): void {
220245 assert . deepEqual ( contents , expectedContents ) ;
221246 } ) ;
222247}
248+
249+ function validatePartialYamlContents (
250+ baseDir : string ,
251+ yamlFileName : string ,
252+ expectedPartialYaml : any ,
253+ ) : void {
254+ const yamlFilePath = path . join ( baseDir , yamlFileName ) ;
255+ const yamlContents = fs . readFileSync ( yamlFilePath , "utf8" ) ;
256+ const parsedYaml = yaml . parse ( yamlContents ) as { [ key : string ] : any } ;
257+ Object . keys ( expectedPartialYaml ) . forEach ( ( key ) => {
258+ assert . deepEqual ( parsedYaml [ key ] , expectedPartialYaml [ key ] ) ;
259+ } ) ;
260+ }
0 commit comments