@@ -17,9 +17,6 @@ import {
1717} from '../src/util' ;
1818import StreamZip from 'node-stream-zip' ;
1919
20- const testDirNoIgnore = 'tests/test-node-func' ;
21- const testDirSimpleIgnore = 'tests/test-func-ignore' ;
22- const testDirNodeIgnore = 'tests/test-func-ignore-node' ;
2320const name = `zip-${ Math . round ( Math . random ( ) * 100000 ) } ` ;
2421
2522describe ( 'Util' , ( ) => {
@@ -403,58 +400,54 @@ describe('Util', () => {
403400 } ) ;
404401} ) ;
405402
406- describe ( 'Zip' , function ( ) {
407- it ( 'raises an error if sourceDir does not exist' , async ( ) => {
408- try {
409- await zipDir ( '/not/a/real/path' , path . posix . join ( os . tmpdir ( ) , name ) ) ;
410- throw new Error ( 'Should have throw error' ) ;
411- } catch ( err ) {
412- expect ( `${ err } ` ) . to . contain ( 'Unable to find' ) ;
413- }
414- } ) ;
415-
416- it ( 'creates a zipfile with correct files without gcloudignore' , async ( ) => {
417- const zf = await zipDir (
418- testDirNoIgnore ,
419- path . posix . join ( os . tmpdir ( ) , name ) ,
420- ) ;
421- const filesInsideZip = await getFilesInZip ( zf ) ;
422- const expectedFiles = getNonIgnoredFiles ( testDirNoIgnore , testDirNoIgnore ) ;
423-
424- expect ( filesInsideZip ) . eql ( expectedFiles ) ;
425- filesInsideZip . forEach ( ( f ) => expect ( expectedFiles ) . to . include ( f ) ) ;
426- } ) ;
427-
428- it ( 'creates a zipfile with correct files with simple gcloudignore' , async ( ) => {
429- const zf = await zipDir (
430- testDirSimpleIgnore ,
431- path . posix . join ( os . tmpdir ( ) , name ) ,
432- ) ;
433- const filesInsideZip = await getFilesInZip ( zf ) ;
434- const expectedFiles = getNonIgnoredFiles (
435- testDirSimpleIgnore ,
436- testDirSimpleIgnore ,
437- new Set ( [ 'ignore.txt' , '.gcloudignore' ] ) ,
438- ) ;
439-
440- expect ( filesInsideZip ) . eql ( expectedFiles ) ;
441- filesInsideZip . forEach ( ( f ) => expect ( expectedFiles ) . to . include ( f ) ) ;
442- } ) ;
443-
444- it ( 'creates a zipfile with correct files with dir gcloudignore' , async ( ) => {
445- const zf = await zipDir (
446- testDirNodeIgnore ,
447- path . posix . join ( os . tmpdir ( ) , name ) ,
448- ) ;
449- const filesInsideZip = await getFilesInZip ( zf ) ;
450- const expectedFiles = getNonIgnoredFiles (
451- testDirNodeIgnore ,
452- testDirNodeIgnore ,
453- new Set ( [ 'bar/bar.txt' , 'bar/baz/baz.txt' ] ) ,
454- ) ;
403+ describe ( '#Zip' , ( ) => {
404+ const cases = [
405+ {
406+ name : 'throws an error if sourceDir does not exist' ,
407+ zipDir : '/not/a/real/path' ,
408+ error : 'Unable to find' ,
409+ } ,
410+ {
411+ name : 'creates a zipfile with correct files without gcloudignore' ,
412+ zipDir : 'tests/test-node-func' ,
413+ expectedFiles : [ '.dotfile' , 'index.js' , 'package.json' ] ,
414+ error : 'Unable to find' ,
415+ } ,
416+ {
417+ name : 'creates a zipfile with correct files with simple gcloudignore' ,
418+ zipDir : 'tests/test-func-ignore' ,
419+ expectedFiles : [ 'index.js' , 'package.json' ] ,
420+ error : 'Unable to find' ,
421+ } ,
422+ {
423+ name : 'creates a zipfile with correct files with simple gcloudignore' ,
424+ zipDir : 'tests/test-func-ignore-node' ,
425+ expectedFiles : [
426+ '.gcloudignore' ,
427+ 'foo/data.txt' ,
428+ 'index.js' ,
429+ 'notIgnored.txt' ,
430+ 'package.json' ,
431+ ] ,
432+ error : 'Unable to find' ,
433+ } ,
434+ ] ;
455435
456- expect ( filesInsideZip ) . eql ( expectedFiles ) ;
457- filesInsideZip . forEach ( ( f ) => expect ( expectedFiles ) . to . include ( f ) ) ;
436+ cases . forEach ( ( tc ) => {
437+ it ( tc . name , async ( ) => {
438+ if ( tc . expectedFiles ) {
439+ const zf = await zipDir ( tc . zipDir , path . posix . join ( os . tmpdir ( ) , name ) ) ;
440+ const filesInsideZip = await getFilesInZip ( zf ) ;
441+ expect ( filesInsideZip ) . to . have . members ( tc . expectedFiles ) ;
442+ } else if ( tc . error ) {
443+ try {
444+ await zipDir ( tc . zipDir , path . posix . join ( os . tmpdir ( ) , name ) ) ;
445+ throw new Error ( `Should have thrown err: ${ tc . error } ` ) ;
446+ } catch ( err ) {
447+ expect ( `${ err } ` ) . to . contain ( tc . error ) ;
448+ }
449+ }
450+ } ) ;
458451 } ) ;
459452} ) ;
460453
@@ -474,30 +467,3 @@ async function getFilesInZip(zipFilePath: string): Promise<string[]> {
474467 }
475468 return filesInsideZip ;
476469}
477-
478- function getNonIgnoredFiles (
479- parentDir : string ,
480- directory : string ,
481- ignore : Set < string > = new Set ( ) ,
482- fileList : string [ ] = [ ] ,
483- ) : string [ ] {
484- const items = fs . readdirSync ( directory ) ;
485- for ( const item of items ) {
486- const stat = fs . statSync ( path . posix . join ( directory , item ) ) ;
487- if ( stat . isDirectory ( ) )
488- fileList = getNonIgnoredFiles (
489- parentDir ,
490- path . posix . join ( directory , item ) ,
491- ignore ,
492- fileList ,
493- ) ;
494- else {
495- const fPath = path . posix . relative (
496- parentDir ,
497- path . posix . join ( directory , item ) ,
498- ) ;
499- if ( ! ignore . has ( fPath ) ) fileList . push ( fPath ) ;
500- }
501- }
502- return fileList ;
503- }
0 commit comments