@@ -246,15 +246,16 @@ describe("Unit - Package Manager", () => {
246246 spyOn ( ProjectConfig , "localConfig" ) . and . callFake ( ( ) => mockProjectConfig ) ;
247247 spyOn ( ProjectConfig , "setConfig" ) ;
248248 spyOn ( TestPackageManager , "addPackage" ) . and . callThrough ( ) ;
249- spyOn ( Util , "execSync " ) ;
249+ spyOn ( Util , "spawnSync " ) ;
250250 spyOn ( Util , "log" ) ;
251251 spyOn ( TestPackageManager , "removePackage" ) ;
252252 spyOn ( TestPackageManager , "getPackageJSON" ) . and . callFake ( ( ) => mockDeps ) ;
253253
254254 await TestPackageManager . ensureIgniteUISource ( true , mockTemplateMgr , true ) ;
255255 expect ( TestPackageManager . addPackage ) . toHaveBeenCalledWith ( `@infragistics/ignite-ui-full@"~20.1"` , true ) ;
256- expect ( Util . execSync ) . toHaveBeenCalledWith (
257- `npm install @infragistics/ignite-ui-full@"~20.1" --quiet --save` ,
256+ expect ( Util . spawnSync ) . toHaveBeenCalledWith (
257+ `npm` ,
258+ [ 'install' , `@infragistics/ignite-ui-full@"~20.1"` , '--quiet' , '--save' ] ,
258259 jasmine . any ( Object )
259260 ) ;
260261 expect ( TestPackageManager . removePackage ) . toHaveBeenCalledWith ( "ignite-ui" , true ) ;
@@ -264,8 +265,9 @@ describe("Unit - Package Manager", () => {
264265 mockTemplateMgr . generateConfig = mockProjectConfig ;
265266 await TestPackageManager . ensureIgniteUISource ( true , mockTemplateMgr , true ) ;
266267 expect ( TestPackageManager . addPackage ) . toHaveBeenCalledWith ( `@infragistics/ignite-ui-full@"^17.1"` , true ) ;
267- expect ( Util . execSync ) . toHaveBeenCalledWith (
268- `npm install @infragistics/ignite-ui-full@"^17.1" --quiet --save` ,
268+ expect ( Util . spawnSync ) . toHaveBeenCalledWith (
269+ `npm` ,
270+ [ 'install' , `@infragistics/ignite-ui-full@"^17.1"` , '--quiet' , '--save' ] ,
269271 jasmine . any ( Object )
270272 ) ;
271273
@@ -274,8 +276,9 @@ describe("Unit - Package Manager", () => {
274276 mockTemplateMgr . generateConfig = mockProjectConfig ;
275277 await TestPackageManager . ensureIgniteUISource ( true , mockTemplateMgr , true ) ;
276278 expect ( TestPackageManager . addPackage ) . toHaveBeenCalledWith ( `@infragistics/ignite-ui-full@">=0.1.0 <0.2.0"` , true ) ;
277- expect ( Util . execSync ) . toHaveBeenCalledWith (
278- `npm install @infragistics/ignite-ui-full@">=0.1.0 <0.2.0" --quiet --save` ,
279+ expect ( Util . spawnSync ) . toHaveBeenCalledWith (
280+ `npm` ,
281+ [ 'install' , `@infragistics/ignite-ui-full@">=0.1.0 <0.2.0"` , '--quiet' , '--save' ] ,
279282 jasmine . any ( Object )
280283 ) ;
281284 } ) ;
@@ -376,7 +379,7 @@ describe("Unit - Package Manager", () => {
376379 } ) ;
377380 it ( "Should run addPackage properly with error code" , async ( ) => {
378381 spyOn ( Util , "log" ) ;
379- spyOn ( Util , "execSync " ) . and . callFake ( ( ) => {
382+ spyOn ( Util , "spawnSync " ) . and . callFake ( ( ) => {
380383 const err = new Error ( "Error" ) ;
381384 err [ "status" ] = 1 ;
382385 throw err ;
@@ -385,17 +388,24 @@ describe("Unit - Package Manager", () => {
385388 expect ( Util . log ) . toHaveBeenCalledTimes ( 2 ) ;
386389 expect ( Util . log ) . toHaveBeenCalledWith ( `Error installing package example-package with npm` ) ;
387390 expect ( Util . log ) . toHaveBeenCalledWith ( `Error` ) ;
388- expect ( Util . execSync ) . toHaveBeenCalledWith (
389- `npm install example-package --quiet --save` , { stdio : "pipe" , encoding : "utf8" } ) ;
391+ expect ( Util . spawnSync ) . toHaveBeenCalledWith (
392+ `npm` , [ ' install' , ' example-package' , ' --quiet' , ' --save' ] , { stdio : "pipe" , encoding : "utf8" } ) ;
390393 } ) ;
391394 it ( "Should run addPackage properly without error code" , async ( ) => {
392395 spyOn ( Util , "log" ) ;
393- spyOn ( Util , "execSync" ) . and . returnValue ( "" ) ;
396+ spyOn ( Util , "spawnSync" ) . and . returnValue ( {
397+ status : 0 ,
398+ pid : 0 ,
399+ output : [ ] ,
400+ stdout : "" ,
401+ stderr : "" ,
402+ signal : null
403+ } ) ;
394404 PackageManager . addPackage ( "example-package" , true ) ;
395405 expect ( Util . log ) . toHaveBeenCalledTimes ( 1 ) ;
396406 expect ( Util . log ) . toHaveBeenCalledWith ( `Package example-package installed successfully` ) ;
397- expect ( Util . execSync ) . toHaveBeenCalledWith (
398- `npm install example-package --quiet --save` , { stdio : "pipe" , encoding : "utf8" } ) ;
407+ expect ( Util . spawnSync ) . toHaveBeenCalledWith (
408+ `npm` , [ ' install' , ' example-package' , ' --quiet' , ' --save' ] , { stdio : "pipe" , encoding : "utf8" } ) ;
399409 } ) ;
400410
401411 it ( "queuePackage should start package install" , async ( ) => {
@@ -412,12 +422,16 @@ describe("Unit - Package Manager", () => {
412422 } ;
413423 // should ignore already installed
414424 spyOn ( App . container , "get" ) . and . returnValue ( mockFs ) ;
415- const execSpy = spyOn ( child_process , "exec" ) ;
425+ const spawnSpy = spyOn ( child_process , "spawn" ) . and . returnValue ( {
426+ stdout : { on : jasmine . createSpy ( ) } ,
427+ stderr : { on : jasmine . createSpy ( ) } ,
428+ on : jasmine . createSpy ( )
429+ } as any ) ;
416430 PackageManager . queuePackage ( "test-pack" ) ;
417431 expect ( Util . log ) . toHaveBeenCalledTimes ( 0 ) ;
418- expect ( child_process . exec ) . toHaveBeenCalledTimes ( 1 ) ;
419- expect ( child_process . exec ) . toHaveBeenCalledWith (
420- `npm install test-pack --quiet --no-save` , { } , jasmine . any ( Function ) ) ;
432+ expect ( child_process . spawn ) . toHaveBeenCalledTimes ( 1 ) ;
433+ expect ( ( child_process . spawn as any ) ) . toHaveBeenCalledWith (
434+ `npm` , [ ' install' , ' test-pack' , ' --quiet' , ' --no-save' ] ) ;
421435 } ) ;
422436
423437 it ( "queuePackage should ignore existing package installs" , async ( ) => {
@@ -435,15 +449,19 @@ describe("Unit - Package Manager", () => {
435449 // should ignore already installed
436450 spyOn ( App . container , "get" ) . and . returnValue ( mockFs ) ;
437451 spyOn ( Util , "log" ) ;
438- const execSpy = spyOn ( child_process , "exec" ) ;
452+ const spawnSpy = spyOn ( child_process , "spawn" ) . and . returnValue ( {
453+ stdout : { on : jasmine . createSpy ( ) } ,
454+ stderr : { on : jasmine . createSpy ( ) } ,
455+ on : jasmine . createSpy ( )
456+ } as any ) ;
439457 PackageManager . queuePackage ( "test-pack" ) ;
440458 expect ( Util . log ) . toHaveBeenCalledTimes ( 0 ) ;
441- expect ( child_process . exec ) . toHaveBeenCalledTimes ( 0 ) ;
459+ expect ( child_process . spawn ) . toHaveBeenCalledTimes ( 0 ) ;
442460
443461 // should ignore if already in queue
444462 PackageManager . queuePackage ( "test-pack2" ) ;
445463 PackageManager . queuePackage ( "test-pack2" ) ;
446- expect ( child_process . exec ) . toHaveBeenCalledTimes ( 1 ) ;
464+ expect ( child_process . spawn ) . toHaveBeenCalledTimes ( 1 ) ;
447465 } ) ;
448466
449467 it ( "Should wait for and log queued package installs" , async ( ) => {
@@ -461,17 +479,41 @@ describe("Unit - Package Manager", () => {
461479 // spyOn(require("module"), "_load").and.returnValue(mockRequire);
462480 spyOn ( Util , "log" ) ;
463481 spyOn ( App . container , "get" ) . and . returnValue ( mockFs ) ;
464- const fakeExec = ( _cmd : any , _opts : any , callback : ( error : Error | null , stdout : string , stderr : string ) => void ) => {
465- setTimeout ( ( ) => callback ( null , 'stdout data' , 'stderr data' ) , 20 ) ;
466- } ;
467-
468- ( fakeExec as any ) . __promisify__ = ( ) => {
469- return new Promise ( ( resolve , reject ) => {
470- setTimeout ( ( ) => resolve ( { stdout : 'stdout data' , stderr : 'stderr data' } ) , 20 ) ;
482+
483+ const createMockChild = ( exitCode : number , stdoutData : string , stderrData : string ) => {
484+ const mockChild = {
485+ stdout : { on : jasmine . createSpy ( ) } ,
486+ stderr : { on : jasmine . createSpy ( ) } ,
487+ on : jasmine . createSpy ( )
488+ } ;
489+
490+ // Setup stdout data handler
491+ mockChild . stdout . on . and . callFake ( ( event : string , handler : any ) => {
492+ if ( event === 'data' ) {
493+ setTimeout ( ( ) => handler ( Buffer . from ( stdoutData ) ) , 10 ) ;
494+ }
471495 } ) ;
496+
497+ // Setup stderr data handler
498+ mockChild . stderr . on . and . callFake ( ( event : string , handler : any ) => {
499+ if ( event === 'data' ) {
500+ setTimeout ( ( ) => handler ( Buffer . from ( stderrData ) ) , 10 ) ;
501+ }
502+ } ) ;
503+
504+ // Setup close handler
505+ mockChild . on . and . callFake ( ( event : string , handler : any ) => {
506+ if ( event === 'close' ) {
507+ setTimeout ( ( ) => handler ( exitCode ) , 20 ) ;
508+ }
509+ } ) ;
510+
511+ return mockChild ;
472512 } ;
473513
474- const execSpy = spyOn ( child_process , 'exec' ) . and . callFake ( fakeExec as any ) ;
514+ const spawnSpy = spyOn ( child_process , 'spawn' ) . and . callFake ( ( ) => {
515+ return createMockChild ( 0 , 'stdout data' , 'stderr data' ) as any ;
516+ } ) ;
475517
476518 PackageManager . queuePackage ( "test-pack" ) ;
477519 PackageManager . queuePackage ( "test-pack2" ) ;
@@ -484,17 +526,9 @@ describe("Unit - Package Manager", () => {
484526 resetSpy ( Util . log ) ;
485527
486528 // on error
487- const fakeExecWithError = ( _cmd : any , _opts : any , callback : ( error : Error | null , stdout : string , stderr : string ) => void ) => {
488- setTimeout ( ( ) => callback ( new Error ( 'Execution failed' ) , '' , 'stderr' ) , 20 ) ;
489- } ;
490-
491- ( fakeExecWithError as any ) . __promisify__ = ( ) => {
492- return new Promise ( ( resolve , reject ) => {
493- setTimeout ( ( ) => reject ( new Error ( 'Execution failed' ) ) , 20 ) ;
494- } ) ;
495- } ;
496-
497- execSpy . and . callFake ( fakeExecWithError as any ) ;
529+ spawnSpy . and . callFake ( ( ) => {
530+ return createMockChild ( 1 , '' , 'stderr' ) as any ;
531+ } ) ;
498532
499533 PackageManager . queuePackage ( "test-pack3" ) ;
500534 await PackageManager . flushQueue ( true , true ) ;
0 commit comments