1
1
const importUtils = import ( "@apphosting/adapter-nextjs/dist/utils.js" ) ;
2
2
import assert from "assert" ;
3
3
import fs from "fs" ;
4
+ import yaml from "yaml" ;
4
5
import path from "path" ;
5
6
import os from "os" ;
6
7
import { OutputBundleOptions } from "../interfaces.js" ;
7
8
8
9
describe ( "build commands" , ( ) => {
9
10
let tmpDir : string ;
10
11
let outputBundleOptions : OutputBundleOptions ;
12
+ let defaultNextVersion : string ;
11
13
beforeEach ( ( ) => {
12
14
tmpDir = generateTmpDir ( ) ;
13
15
outputBundleOptions = {
@@ -18,10 +20,11 @@ describe("build commands", () => {
18
20
outputStaticDirectoryPath : path . join ( tmpDir , ".apphosting/.next/static" ) ,
19
21
serverFilePath : path . join ( tmpDir , ".apphosting/server.js" ) ,
20
22
} ;
23
+ defaultNextVersion = "14.0.3" ;
21
24
} ) ;
22
25
23
26
it ( "expects all output bundle files to be generated" , async ( ) => {
24
- const { generateOutputDirectory, validateOutputDirectory } = await importUtils ;
27
+ const { generateOutputDirectory, validateOutputDirectory, createMetadata } = await importUtils ;
25
28
const files = {
26
29
".next/standalone/server.js" : "" ,
27
30
".next/static/staticfile" : "" ,
@@ -31,8 +34,15 @@ describe("build commands", () => {
31
34
"redirects":[]
32
35
}` ,
33
36
} ;
37
+ const packageVersion = createMetadata ( defaultNextVersion ) . adapterVersion ;
34
38
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
+ ) ;
36
46
await validateOutputDirectory ( outputBundleOptions ) ;
37
47
38
48
const expectedFiles = {
@@ -46,6 +56,12 @@ neededDirs:
46
56
- .apphosting
47
57
staticAssets:
48
58
- .apphosting/public
59
+ env: []
60
+ metadata:
61
+ adapterPackageName: "@apphosting/adapter-nextjs"
62
+ adapterVersion: ${ packageVersion }
63
+ framework: nextjs
64
+ frameworkVersion: ${ defaultNextVersion }
49
65
` ,
50
66
} ;
51
67
validateTestFiles ( tmpDir , expectedFiles ) ;
@@ -76,22 +92,23 @@ staticAssets:
76
92
serverFilePath : path . join ( tmpDir , ".apphosting/apps/next-app/server.js" ) ,
77
93
} ,
78
94
path . join ( tmpDir , ".next" ) ,
95
+ defaultNextVersion ,
79
96
) ;
80
97
81
98
const expectedFiles = {
82
99
".apphosting/apps/next-app/.next/static/staticfile" : "" ,
83
100
".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" ] ,
93
109
} ;
94
110
validateTestFiles ( tmpDir , expectedFiles ) ;
111
+ validatePartialYamlContents ( tmpDir , ".apphosting/bundle.yaml" , expectedPartialYaml ) ;
95
112
} ) ;
96
113
97
114
it ( "expects directories and other files to be copied over" , async ( ) => {
@@ -108,25 +125,31 @@ staticAssets:
108
125
}` ,
109
126
} ;
110
127
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
+ ) ;
112
135
await validateOutputDirectory ( outputBundleOptions ) ;
113
136
114
137
const expectedFiles = {
115
138
".apphosting/.next/static/staticfile" : "" ,
116
139
".apphosting/server.js" : "" ,
117
140
".apphosting/public/publicfile" : "" ,
118
141
".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" ] ,
128
150
} ;
129
151
validateTestFiles ( tmpDir , expectedFiles ) ;
152
+ validatePartialYamlContents ( tmpDir , ".apphosting/bundle.yaml" , expectedPartialYaml ) ;
130
153
} ) ;
131
154
132
155
it ( "expects bundle.yaml headers/rewrites/redirects to be generated" , async ( ) => {
@@ -141,30 +164,26 @@ staticAssets:
141
164
}` ,
142
165
} ;
143
166
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
+ ) ;
145
174
await validateOutputDirectory ( outputBundleOptions ) ;
146
175
147
176
const expectedFiles = {
148
177
".apphosting/.next/static/staticfile" : "" ,
149
178
".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" } ] ,
166
184
} ;
167
185
validateTestFiles ( tmpDir , expectedFiles ) ;
186
+ validatePartialYamlContents ( tmpDir , ".apphosting/bundle.yaml" , expectedPartialYaml ) ;
168
187
} ) ;
169
188
it ( "test failed validateOutputDirectory" , async ( ) => {
170
189
const { generateOutputDirectory, validateOutputDirectory } = await importUtils ;
@@ -178,7 +197,13 @@ staticAssets:
178
197
}` ,
179
198
} ;
180
199
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
+ ) ;
182
207
assert . rejects ( async ( ) => await validateOutputDirectory ( outputBundleOptions ) ) ;
183
208
} ) ;
184
209
it ( "test populate output bundle options" , async ( ) => {
@@ -220,3 +245,16 @@ function validateTestFiles(baseDir: string, expectedFiles: Object): void {
220
245
assert . deepEqual ( contents , expectedContents ) ;
221
246
} ) ;
222
247
}
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