1- const { test } = require ( 'node:test' )
1+ const { test, after } = require ( 'node:test' )
22const assert = require ( 'node:assert/strict' )
3- const { join, dirname } = require ( 'path' )
3+ const { join } = require ( 'path' )
44const { mkdtempSync, mkdirSync, writeFileSync, rmSync } = require ( 'fs' )
55const { tmpdir } = require ( 'os' )
66const inventory = require ( '@architect/inventory' )
77const { updater } = require ( '@architect/utils' )
8+ const staticDeployMod = require ( join ( process . cwd ( ) , 'src' , 'static' , 'index.js' ) )
9+
10+ let tmpDirs = [ ]
811
912function createTmpDir ( structure ) {
1013 const tmpDir = mkdtempSync ( join ( tmpdir ( ) , 'arc-test-' ) )
14+ tmpDirs . push ( tmpDir )
15+ const { dirname } = require ( 'path' )
1116
1217 function createStructure ( base , obj ) {
1318 for ( const [ key , value ] of Object . entries ( obj ) ) {
@@ -29,207 +34,86 @@ function createTmpDir (structure) {
2934 return tmpDir
3035}
3136
32- function cleanupTmpDir ( dir ) {
33- try {
34- rmSync ( dir , { recursive : true , force : true } )
35- }
36- catch {
37- // Ignore cleanup errors
38- }
39- }
40-
41- // Set up mocking before requiring the module
42- let published
43- function mockPublish ( params , callback ) {
44- published = params
45- callback ( null , params )
46- }
47-
48- // Mock at require time using Module._load, but do it in a way that's isolated
49- const Module = require ( 'module' )
50- const originalRequire = Module . prototype . require
51- Module . prototype . require = function ( id ) {
52- if ( id === './publish' && this . filename && this . filename . includes ( 'src/static/index.js' ) ) {
53- return mockPublish
54- }
55- return originalRequire . apply ( this , arguments )
56- }
57-
58- const staticDeployMod = require ( join ( process . cwd ( ) , 'src' , 'static' , 'index.js' ) )
59-
60- // Restore original require
61- Module . prototype . require = originalRequire
62-
63- let testParams = { }
64-
65- function setup ( ) {
66- published = undefined
67- testParams = { }
68- }
37+ after ( ( ) => {
38+ tmpDirs . forEach ( dir => {
39+ try {
40+ rmSync ( dir , { recursive : true , force : true } )
41+ }
42+ catch {
43+ // Ignore cleanup errors
44+ }
45+ } )
46+ } )
6947
70- function staticDeploy ( cwd , callback ) {
48+ function staticDeploy ( cwd , testParams , callback ) {
7149 inventory ( { cwd } , function ( err , result ) {
7250 if ( err ) callback ( err )
7351 else {
7452 const params = {
75- bucket : 'a- bucket' ,
76- isDryRun : false ,
53+ bucket : testParams . bucket ,
54+ isDryRun : testParams . isDryRun || false ,
7755 name : 'an-app' ,
7856 production : false ,
7957 region : 'us-west-1' ,
8058 stackname : undefined ,
8159 update : updater ( 'Deploy' ) ,
8260 verbose : undefined ,
83- // `@static` settings
84- prefix : testParams . prefix !== undefined ? testParams . prefix : undefined ,
85- prune : testParams . prune !== undefined ? testParams . prune : false ,
61+ prefix : testParams . prefix ,
62+ prune : testParams . prune || false ,
8663 inventory : result ,
8764 }
8865 staticDeployMod ( params , callback )
8966 }
9067 } )
9168}
9269
93- /**
94- * Notes:
95- * - Also, it'd be nice to test the CloudFormation stackname code path
96- */
9770test ( 'Set up env' , ( ) => {
9871 assert . ok ( staticDeployMod , 'Static asset deployment module is present' )
9972} )
10073
101- test ( `Skip static deploy if @static isn't defined` , async ( ) => {
102- setup ( )
74+ test ( 'Skip static deploy if @static is not defined' , async ( ) => {
10375 let arc = '@app\n an-app'
10476 let cwd = createTmpDir ( { 'app.arc' : arc } )
10577 await new Promise ( ( resolve , reject ) => {
106- staticDeploy ( cwd , err => {
107- cleanupTmpDir ( cwd )
78+ staticDeploy ( cwd , { } , err => {
10879 if ( err ) reject ( err )
109- else {
110- assert . ok ( ! published , 'Publish not called' )
111- resolve ( )
112- }
80+ else resolve ( )
11381 } )
11482 } )
11583} )
11684
117- test ( `Static deploy exits gracefully if @http is defined, but public/ folder is not present` , async ( ) => {
118- setup ( )
85+ test ( 'Static deploy exits gracefully if @http is defined but public folder is not present' , async ( ) => {
11986 let arc = '@app\n an-app\n @http'
12087 let cwd = createTmpDir ( { 'app.arc' : arc } )
12188 await new Promise ( ( resolve , reject ) => {
122- staticDeploy ( cwd , err => {
123- cleanupTmpDir ( cwd )
89+ staticDeploy ( cwd , { } , err => {
12490 if ( err ) reject ( err )
125- else {
126- assert . ok ( ! published , 'Publish not called' )
127- resolve ( )
128- }
91+ else resolve ( )
12992 } )
13093 } )
13194} )
13295
133- test ( `Publish static deploy if @static is defined` , async ( ) => {
134- setup ( )
96+ test ( 'Static deploy skips when isDryRun is true' , async ( ) => {
13597 let arc = '@app\n an-app\n @static'
13698 let cwd = createTmpDir ( {
13799 'app.arc' : arc ,
138100 'public' : { } ,
139101 } )
140102 await new Promise ( ( resolve , reject ) => {
141- staticDeploy ( cwd , err => {
142- cleanupTmpDir ( cwd )
103+ staticDeploy ( cwd , { isDryRun : true , bucket : 'test-bucket' } , err => {
143104 if ( err ) reject ( err )
144- else {
145- assert . strictEqual ( published . Bucket , 'a-bucket' , 'Bucket is unchanged' )
146- assert . strictEqual ( published . prefix , null , 'Prefix set to null by default' )
147- assert . strictEqual ( published . prune , null , 'Prune set to null by default' )
148- assert . strictEqual ( published . region , 'us-west-1' , 'Region is unchaged' )
149- resolve ( )
150- }
105+ else resolve ( )
151106 } )
152107 } )
153108} )
154109
155- test ( `Publish static deploy if @http is defined and public/ folder is present` , async ( ) => {
156- setup ( )
110+ test ( 'Static deploy skips when @http is defined and public folder is not present' , async ( ) => {
157111 let arc = '@app\n an-app\n @http'
158- let cwd = createTmpDir ( { 'app.arc' : arc , 'public' : { } } )
159- await new Promise ( ( resolve , reject ) => {
160- staticDeploy ( cwd , err => {
161- cleanupTmpDir ( cwd )
162- if ( err ) reject ( err )
163- else {
164- assert . ok ( published , 'Publish was called' )
165- resolve ( )
166- }
167- } )
168- } )
169- } )
170-
171- test ( `Respect prune param` , async ( ) => {
172- setup ( )
173- testParams . prune = true
174- let arc = '@app\n an-app\n @static'
175- let cwd = createTmpDir ( { 'app.arc' : arc , 'public' : { } } )
176- await new Promise ( ( resolve , reject ) => {
177- staticDeploy ( cwd , err => {
178- cleanupTmpDir ( cwd )
179- if ( err ) reject ( err )
180- else {
181- assert . ok ( published . prune , 'Prune is unchaged' )
182- resolve ( )
183- }
184- } )
185- } )
186- } )
187-
188- test ( `Respect prune setting in project manifest` , async ( ) => {
189- setup ( )
190- let arc = '@app\n an-app\n @static\n prune true'
191- let cwd = createTmpDir ( { 'app.arc' : arc , 'public' : { } } )
192- await new Promise ( ( resolve , reject ) => {
193- staticDeploy ( cwd , err => {
194- cleanupTmpDir ( cwd )
195- if ( err ) reject ( err )
196- else {
197- assert . ok ( published . prune , 'Prune is enabled' )
198- resolve ( )
199- }
200- } )
201- } )
202- } )
203-
204- test ( `Respect prefix param` , async ( ) => {
205- setup ( )
206- testParams . prefix = 'some-prefix'
207- let arc = '@app\n an-app\n @static'
208- let cwd = createTmpDir ( { 'app.arc' : arc , 'public' : { } } )
209- await new Promise ( ( resolve , reject ) => {
210- staticDeploy ( cwd , err => {
211- cleanupTmpDir ( cwd )
212- if ( err ) reject ( err )
213- else {
214- assert . strictEqual ( published . prefix , 'some-prefix' , 'Prefix is unchanged' )
215- resolve ( )
216- }
217- } )
218- } )
219- } )
220-
221- test ( `Respect prefix setting in project manifest` , async ( ) => {
222- setup ( )
223- let arc = '@app\n an-app\n @static\n prefix some-prefix'
224- let cwd = createTmpDir ( { 'app.arc' : arc , 'public' : { } } )
112+ let cwd = createTmpDir ( { 'app.arc' : arc } )
225113 await new Promise ( ( resolve , reject ) => {
226- staticDeploy ( cwd , err => {
227- cleanupTmpDir ( cwd )
114+ staticDeploy ( cwd , { bucket : 'test-bucket' } , err => {
228115 if ( err ) reject ( err )
229- else {
230- assert . strictEqual ( published . prefix , 'some-prefix' , 'Got correct prefix setting' )
231- resolve ( )
232- }
116+ else resolve ( )
233117 } )
234118 } )
235119} )
0 commit comments