1- let test = require ( 'tape' )
2- let { join } = require ( 'path' )
3- let mockTmp = require ( 'mock-tmp' )
4- let proxyquire = require ( 'proxyquire' )
5- let inventory = require ( '@architect/inventory' )
6- let { updater } = require ( '@architect/utils' )
1+ const { test, after } = require ( 'node:test' )
2+ const assert = require ( 'node:assert/strict' )
3+ const { join } = require ( 'path' )
4+ const { mkdtempSync, mkdirSync, writeFileSync, rmSync } = require ( 'fs' )
5+ const { tmpdir } = require ( 'os' )
6+ const inventory = require ( '@architect/inventory' )
7+ const { updater } = require ( '@architect/utils' )
8+
9+ let tmpDirs = [ ]
10+
11+ function createTmpDir ( structure ) {
12+ const tmpDir = mkdtempSync ( join ( tmpdir ( ) , 'arc-test-' ) )
13+ tmpDirs . push ( tmpDir )
14+ const { dirname } = require ( 'path' )
15+
16+ function createStructure ( base , obj ) {
17+ for ( const [ key , value ] of Object . entries ( obj ) ) {
18+ const path = join ( base , key )
19+ if ( typeof value === 'object' && value !== null && ! Buffer . isBuffer ( value ) ) {
20+ mkdirSync ( path , { recursive : true } )
21+ createStructure ( path , value )
22+ }
23+ else {
24+ // Ensure parent directory exists for files
25+ const dir = dirname ( path )
26+ mkdirSync ( dir , { recursive : true } )
27+ writeFileSync ( path , value || '' )
28+ }
29+ }
30+ }
31+
32+ createStructure ( tmpDir , structure )
33+ return tmpDir
34+ }
35+
36+ after ( ( ) => {
37+ tmpDirs . forEach ( dir => {
38+ try {
39+ rmSync ( dir , { recursive : true , force : true } )
40+ }
41+ catch {
42+ // Ignore cleanup errors
43+ }
44+ } )
45+ } )
746
847let published
948function publish ( params , callback ) {
1049 published = params
1150 callback ( null , params )
1251}
1352
14- let staticDeployPath = join ( process . cwd ( ) , 'src' , 'static' , 'index.js' )
15- let staticDeployMod = proxyquire ( staticDeployPath , {
16- './publish' : publish ,
17- } )
53+ // Mock the publish module using Module._load interception
54+ const Module = require ( 'module' )
55+ const originalLoad = Module . _load
56+ Module . _load = function ( request , parent ) {
57+ if ( request === './publish' && parent . filename . includes ( 'src/static/index.js' ) ) {
58+ return publish
59+ }
60+ return originalLoad . apply ( this , arguments )
61+ }
62+
63+ const staticDeployMod = require ( join ( process . cwd ( ) , 'src' , 'static' , 'index.js' ) )
1864
1965let defaultParams = ( ) => ( {
2066 bucket : 'a-bucket' ,
@@ -36,12 +82,11 @@ function setup () {
3682}
3783function reset ( ) {
3884 params = defaultParams ( )
39- mockTmp . reset ( )
4085}
4186
42- function staticDeploy ( t , cwd , callback ) {
87+ function staticDeploy ( cwd , callback ) {
4388 inventory ( { cwd } , function ( err , result ) {
44- if ( err ) t . fail ( err )
89+ if ( err ) callback ( err )
4590 else {
4691 params . inventory = result
4792 staticDeployMod ( params , err => {
@@ -54,113 +99,109 @@ function staticDeploy (t, cwd, callback) {
5499
55100/**
56101 * Notes:
57- * - Unfortunately, proxyquire seems to have a nested file folder + `@global` bug, so we can't run this from index
58- * - Instead, we have to run inventory ourselves on each test, which kinda sucks
59102 * - Also, it'd be nice to test the CloudFormation stackname code path
60103 */
61- test ( 'Set up env' , t => {
62- t . plan ( 1 )
63- t . ok ( staticDeployMod , 'Static asset deployment module is present' )
104+ test ( 'Set up env' , ( ) => {
105+ assert . ok ( staticDeployMod , 'Static asset deployment module is present' )
64106} )
65107
66- test ( `Skip static deploy if @static isn't defined` , t => {
67- t . plan ( 1 )
108+ test ( `Skip static deploy if @static isn't defined` , ( t , done ) => {
68109 setup ( )
69110 let arc = '@app\n an-app'
70- let cwd = mockTmp ( { 'app.arc' : arc } )
71- staticDeploy ( t , cwd , err => {
72- if ( err ) t . fail ( err )
73- t . notOk ( published , 'Publish not called' )
111+ let cwd = createTmpDir ( { 'app.arc' : arc } )
112+ staticDeploy ( cwd , err => {
113+ if ( err ) assert . fail ( err )
114+ assert . ok ( ! published , 'Publish not called' )
115+ done ( )
74116 } )
75117} )
76118
77- test ( `Static deploy exits gracefully if @http is defined, but public/ folder is not present` , t => {
78- t . plan ( 1 )
119+ test ( `Static deploy exits gracefully if @http is defined, but public/ folder is not present` , ( t , done ) => {
79120 setup ( )
80121 let arc = '@app\n an-app\n @http'
81- let cwd = mockTmp ( { 'app.arc' : arc } )
82- staticDeploy ( t , cwd , err => {
83- if ( err ) t . fail ( err )
84- t . notOk ( published , 'Publish not called' )
122+ let cwd = createTmpDir ( { 'app.arc' : arc } )
123+ staticDeploy ( cwd , err => {
124+ if ( err ) assert . fail ( err )
125+ assert . ok ( ! published , 'Publish not called' )
126+ done ( )
85127 } )
86128} )
87129
88- test ( `Publish static deploy if @static is defined` , t => {
89- t . plan ( 4 )
130+ test ( `Publish static deploy if @static is defined` , ( t , done ) => {
90131 setup ( )
91132 let arc = '@app\n an-app\n @static'
92- let cwd = mockTmp ( {
133+ let cwd = createTmpDir ( {
93134 'app.arc' : arc ,
94135 'public' : { } ,
95136 } )
96- staticDeploy ( t , cwd , err => {
97- if ( err ) t . fail ( err )
98- t . equal ( published . Bucket , params . bucket , 'Bucket is unchanged' )
99- t . equal ( published . prefix , null , 'Prefix set to null by default' )
100- t . equal ( published . prune , null , 'Prune set to null by default' )
101- t . equal ( published . region , params . region , 'Region is unchaged' )
137+ staticDeploy ( cwd , err => {
138+ if ( err ) assert . fail ( err )
139+ assert . strictEqual ( published . Bucket , params . bucket , 'Bucket is unchanged' )
140+ assert . strictEqual ( published . prefix , null , 'Prefix set to null by default' )
141+ assert . strictEqual ( published . prune , null , 'Prune set to null by default' )
142+ assert . strictEqual ( published . region , params . region , 'Region is unchaged' )
143+ done ( )
102144 } )
103145} )
104146
105- test ( `Publish static deploy if @http is defined and public/ folder is present` , t => {
106- t . plan ( 1 )
147+ test ( `Publish static deploy if @http is defined and public/ folder is present` , ( t , done ) => {
107148 setup ( )
108149 let arc = '@app\n an-app\n @http'
109- let cwd = mockTmp ( { 'app.arc' : arc , 'public' : { } } )
110- staticDeploy ( t , cwd , err => {
111- if ( err ) t . fail ( err )
112- t . ok ( published , 'Publish was called' )
150+ let cwd = createTmpDir ( { 'app.arc' : arc , 'public' : { } } )
151+ staticDeploy ( cwd , err => {
152+ if ( err ) assert . fail ( err )
153+ assert . ok ( published , 'Publish was called' )
154+ done ( )
113155 } )
114156} )
115157
116- test ( `Respect prune param` , t => {
117- t . plan ( 1 )
158+ test ( `Respect prune param` , ( t , done ) => {
118159 setup ( )
119160 let arc = '@app\n an-app\n @static'
120- let cwd = mockTmp ( { 'app.arc' : arc , 'public' : { } } )
161+ let cwd = createTmpDir ( { 'app.arc' : arc , 'public' : { } } )
121162 params . prune = true
122- staticDeploy ( t , cwd , err => {
123- if ( err ) t . fail ( err )
124- t . ok ( published . prune , 'Prune is unchaged' )
163+ staticDeploy ( cwd , err => {
164+ if ( err ) assert . fail ( err )
165+ assert . ok ( published . prune , 'Prune is unchaged' )
166+ done ( )
125167 } )
126168} )
127169
128- test ( `Respect prune setting in project manifest` , t => {
129- t . plan ( 1 )
170+ test ( `Respect prune setting in project manifest` , ( t , done ) => {
130171 setup ( )
131172 let arc = '@app\n an-app\n @static\n prune true'
132- let cwd = mockTmp ( { 'app.arc' : arc , 'public' : { } } )
133- staticDeploy ( t , cwd , err => {
134- if ( err ) t . fail ( err )
135- t . ok ( published . prune , 'Prune is enabled' )
173+ let cwd = createTmpDir ( { 'app.arc' : arc , 'public' : { } } )
174+ staticDeploy ( cwd , err => {
175+ if ( err ) assert . fail ( err )
176+ assert . ok ( published . prune , 'Prune is enabled' )
177+ done ( )
136178 } )
137179} )
138180
139- test ( `Respect prefix param` , t => {
140- t . plan ( 1 )
181+ test ( `Respect prefix param` , ( t , done ) => {
141182 setup ( )
142183 let arc = '@app\n an-app\n @static'
143- let cwd = mockTmp ( { 'app.arc' : arc , 'public' : { } } )
184+ let cwd = createTmpDir ( { 'app.arc' : arc , 'public' : { } } )
144185 params . prefix = 'some-prefix'
145- staticDeploy ( t , cwd , err => {
146- if ( err ) t . fail ( err )
147- t . equal ( published . prefix , 'some-prefix' , 'Prefix is unchanged' )
186+ staticDeploy ( cwd , err => {
187+ if ( err ) assert . fail ( err )
188+ assert . strictEqual ( published . prefix , 'some-prefix' , 'Prefix is unchanged' )
189+ done ( )
148190 } )
149191} )
150192
151- test ( `Respect prefix setting in project manifest` , t => {
152- t . plan ( 1 )
193+ test ( `Respect prefix setting in project manifest` , ( t , done ) => {
153194 setup ( )
154195 let arc = '@app\n an-app\n @static\n prefix some-prefix'
155- let cwd = mockTmp ( { 'app.arc' : arc , 'public' : { } } )
156- staticDeploy ( t , cwd , err => {
157- if ( err ) t . fail ( err )
158- t . equal ( published . prefix , 'some-prefix' , 'Got correct prefix setting' )
196+ let cwd = createTmpDir ( { 'app.arc' : arc , 'public' : { } } )
197+ staticDeploy ( cwd , err => {
198+ if ( err ) assert . fail ( err )
199+ assert . strictEqual ( published . prefix , 'some-prefix' , 'Got correct prefix setting' )
200+ done ( )
159201 } )
160202} )
161203
162- test ( 'Teardown' , t => {
163- t . plan ( 1 )
204+ test ( 'Teardown' , ( ) => {
164205 reset ( )
165- t . pass ( 'Done' )
206+ assert . ok ( true , 'Done' )
166207} )
0 commit comments