@@ -13,6 +13,7 @@ governing permissions and limitations under the License.
1313const TheCommand = require ( '../../../src/commands/app/install.js' )
1414const BaseCommand = require ( '../../../src/BaseCommand.js' )
1515const fs = require ( 'fs-extra' )
16+ const { PACKAGE_LOCK_FILE } = require ( '../../../src/lib/defaults.js' )
1617const unzipper = require ( 'unzipper' )
1718const execa = require ( 'execa' )
1819const installHelper = require ( '../../../src/lib/install-helper' )
@@ -221,16 +222,15 @@ describe('npmInstall', () => {
221222 command = new TheCommand ( )
222223 } )
223224
224- test ( 'success' , async ( ) => {
225+ test ( 'success (defaults) ' , async ( ) => {
225226 execa . mockImplementationOnce ( ( cmd , args , options ) => {
226227 expect ( cmd ) . toEqual ( 'npm' )
227228 expect ( args ) . toEqual ( [ 'install' ] )
228229 expect ( options . stdio ) . toEqual ( 'ignore' )
229230 return Promise . resolve ( { stdout : '' } )
230231 } )
231232
232- const isVerbose = false
233- await expect ( command . npmInstall ( isVerbose ) ) . resolves . toEqual ( undefined )
233+ await expect ( command . npmInstall ( ) ) . resolves . toEqual ( undefined )
234234 } )
235235
236236 test ( 'success --verbose' , async ( ) => {
@@ -245,6 +245,19 @@ describe('npmInstall', () => {
245245 await expect ( command . npmInstall ( isVerbose ) ) . resolves . toEqual ( undefined )
246246 } )
247247
248+ test ( 'success --verbose --no-allow-scripts' , async ( ) => {
249+ execa . mockImplementationOnce ( ( cmd , args , options ) => {
250+ expect ( cmd ) . toEqual ( 'npm' )
251+ expect ( args ) . toEqual ( [ 'install' , '--ignore-scripts' ] )
252+ expect ( options . stdio ) . toEqual ( 'inherit' )
253+ return Promise . resolve ( { stdout : '' } )
254+ } )
255+
256+ const isVerbose = true
257+ const allowScripts = false
258+ await expect ( command . npmInstall ( isVerbose , allowScripts ) ) . resolves . toEqual ( undefined )
259+ } )
260+
248261 test ( 'failure' , async ( ) => {
249262 const errorMessage = 'npm install error'
250263
@@ -258,6 +271,63 @@ describe('npmInstall', () => {
258271 } )
259272} )
260273
274+ describe ( 'npmCI' , ( ) => {
275+ let command
276+
277+ beforeEach ( ( ) => {
278+ execa . mockReset ( )
279+ command = new TheCommand ( )
280+ } )
281+
282+ test ( 'success (defaults)' , async ( ) => {
283+ execa . mockImplementationOnce ( ( cmd , args , options ) => {
284+ expect ( cmd ) . toEqual ( 'npm' )
285+ expect ( args ) . toEqual ( [ 'ci' ] )
286+ expect ( options . stdio ) . toEqual ( 'ignore' )
287+ return Promise . resolve ( { stdout : '' } )
288+ } )
289+
290+ await expect ( command . npmCI ( ) ) . resolves . toEqual ( undefined )
291+ } )
292+
293+ test ( 'success --verbose' , async ( ) => {
294+ execa . mockImplementationOnce ( ( cmd , args , options ) => {
295+ expect ( cmd ) . toEqual ( 'npm' )
296+ expect ( args ) . toEqual ( [ 'ci' ] )
297+ expect ( options . stdio ) . toEqual ( 'inherit' )
298+ return Promise . resolve ( { stdout : '' } )
299+ } )
300+
301+ const isVerbose = true
302+ await expect ( command . npmCI ( isVerbose ) ) . resolves . toEqual ( undefined )
303+ } )
304+
305+ test ( 'success --verbose --no-allow-scripts' , async ( ) => {
306+ execa . mockImplementationOnce ( ( cmd , args , options ) => {
307+ expect ( cmd ) . toEqual ( 'npm' )
308+ expect ( args ) . toEqual ( [ 'ci' , '--ignore-scripts' ] )
309+ expect ( options . stdio ) . toEqual ( 'inherit' )
310+ return Promise . resolve ( { stdout : '' } )
311+ } )
312+
313+ const isVerbose = true
314+ const allowScripts = false
315+ await expect ( command . npmCI ( isVerbose , allowScripts ) ) . resolves . toEqual ( undefined )
316+ } )
317+
318+ test ( 'failure' , async ( ) => {
319+ const errorMessage = 'npm ci error'
320+
321+ execa . mockImplementationOnce ( ( cmd , args ) => {
322+ expect ( cmd ) . toEqual ( 'npm' )
323+ expect ( args ) . toEqual ( [ 'ci' ] )
324+ throw new Error ( errorMessage )
325+ } )
326+
327+ await expect ( command . npmCI ( ) ) . rejects . toThrow ( errorMessage )
328+ } )
329+ } )
330+
261331describe ( 'runTests' , ( ) => {
262332 let command
263333
@@ -284,7 +354,11 @@ describe('runTests', () => {
284354} )
285355
286356describe ( 'run' , ( ) => {
287- test ( 'no flags' , async ( ) => {
357+ beforeEach ( ( ) => {
358+ fs . existsSync . mockReset ( )
359+ } )
360+
361+ test ( 'no flags, no lockfile' , async ( ) => {
288362 const command = new TheCommand ( )
289363 command . argv = [ 'my-app.zip' ]
290364
@@ -295,6 +369,7 @@ describe('run', () => {
295369 command . validateDeployConfig = jest . fn ( )
296370 command . runTests = jest . fn ( )
297371 command . npmInstall = jest . fn ( )
372+ command . npmCI = jest . fn ( )
298373 command . error = jest . fn ( )
299374 await command . run ( )
300375
@@ -305,6 +380,36 @@ describe('run', () => {
305380 expect ( command . validateDeployConfig ) . toHaveBeenCalledTimes ( 1 )
306381 expect ( command . runTests ) . toHaveBeenCalledTimes ( 1 )
307382 expect ( command . npmInstall ) . toHaveBeenCalledTimes ( 1 )
383+ expect ( command . npmCI ) . toHaveBeenCalledTimes ( 0 )
384+ expect ( command . error ) . toHaveBeenCalledTimes ( 0 )
385+ } )
386+
387+ test ( 'no flags, has lockfile' , async ( ) => {
388+ const command = new TheCommand ( )
389+ command . argv = [ 'my-app.zip' ]
390+
391+ // since we already unit test the methods above, we mock it here
392+ command . validateZipDirectoryStructure = jest . fn ( )
393+ command . unzipFile = jest . fn ( )
394+ command . addCodeDownloadAnnotation = jest . fn ( )
395+ command . validateDeployConfig = jest . fn ( )
396+ command . runTests = jest . fn ( )
397+ command . npmInstall = jest . fn ( )
398+ command . npmCI = jest . fn ( )
399+ command . error = jest . fn ( )
400+
401+ fs . existsSync . mockImplementation ( ( filePath ) => filePath === PACKAGE_LOCK_FILE )
402+
403+ await command . run ( )
404+
405+ expect ( command . validateZipDirectoryStructure ) . toHaveBeenCalledTimes ( 1 )
406+ expect ( command . unzipFile ) . toHaveBeenCalledTimes ( 1 )
407+ expect ( libConfig . coalesce ) . toHaveBeenCalledTimes ( 1 )
408+ expect ( libConfig . validate ) . toHaveBeenCalledTimes ( 1 )
409+ expect ( command . validateDeployConfig ) . toHaveBeenCalledTimes ( 1 )
410+ expect ( command . runTests ) . toHaveBeenCalledTimes ( 1 )
411+ expect ( command . npmInstall ) . toHaveBeenCalledTimes ( 0 )
412+ expect ( command . npmCI ) . toHaveBeenCalledTimes ( 1 )
308413 expect ( command . error ) . toHaveBeenCalledTimes ( 0 )
309414 } )
310415
@@ -321,6 +426,7 @@ describe('run', () => {
321426 command . addCodeDownloadAnnotation = jest . fn ( )
322427 command . validateDeployConfig = jest . fn ( )
323428 command . npmInstall = jest . fn ( )
429+ command . npmCI = jest . fn ( )
324430 command . error = jest . fn ( )
325431 command . runTests = jest . fn ( ( ) => { throw errorObject } )
326432
@@ -351,6 +457,7 @@ describe('run', () => {
351457 command . addCodeDownloadAnnotation = jest . fn ( )
352458 command . validateDeployConfig = jest . fn ( )
353459 command . npmInstall = jest . fn ( )
460+ command . npmCI = jest . fn ( )
354461 command . error = jest . fn ( )
355462 command . runTests = jest . fn ( ( ) => { throw new Error ( errorMessage ) } )
356463
@@ -379,6 +486,7 @@ describe('run', () => {
379486 command . validateDeployConfig = jest . fn ( )
380487 command . runTests = jest . fn ( )
381488 command . npmInstall = jest . fn ( )
489+ command . npmCI = jest . fn ( )
382490 command . error = jest . fn ( )
383491
384492 await command . run ( )
@@ -405,6 +513,7 @@ describe('run', () => {
405513 command . validateDeployConfig = jest . fn ( )
406514 command . runTests = jest . fn ( )
407515 command . npmInstall = jest . fn ( )
516+ command . npmCI = jest . fn ( )
408517 command . error = jest . fn ( )
409518
410519 const err = new Error ( 'fake validation error' )
@@ -452,6 +561,7 @@ describe('run', () => {
452561 command . validateDeployConfig = jest . fn ( )
453562 command . runTests = jest . fn ( )
454563 command . npmInstall = jest . fn ( )
564+ command . npmCI = jest . fn ( )
455565 command . error = jest . fn ( )
456566
457567 await command . run ( )
0 commit comments