@@ -4,7 +4,7 @@ const chai = require("chai"),
4
4
sinon = require ( "sinon" ) ,
5
5
fs = require ( 'fs-extra' ) ,
6
6
path = require ( 'path' ) ,
7
- npm = require ( 'npm ' ) ;
7
+ cp = require ( 'child_process ' ) ;
8
8
9
9
const logger = require ( "../../../../bin/helpers/logger" ) . winstonLogger ,
10
10
fileHelpers = require ( "../../../../bin/helpers/fileHelpers" ) ;
@@ -150,7 +150,7 @@ describe("packageInstaller", () => {
150
150
sinon . assert . calledOnce ( pathdirnameStub ) ;
151
151
sinon . assert . calledThrice ( pathjoinStub ) ;
152
152
sinon . assert . calledWith ( fswriteFileSyncStub , null , packageCreated ) ;
153
- chai . assert . equal ( data , "package file created" ) ;
153
+ chai . assert . equal ( data , "Package file created" ) ;
154
154
} )
155
155
. catch ( ( _error ) => {
156
156
console . log ( _error )
@@ -202,7 +202,7 @@ describe("packageInstaller", () => {
202
202
sinon . assert . calledOnce ( pathdirnameStub ) ;
203
203
sinon . assert . calledThrice ( pathjoinStub ) ;
204
204
sinon . assert . calledWith ( fswriteFileSyncStub , null , packageCreated ) ;
205
- chai . assert . equal ( data , "package file created" ) ;
205
+ chai . assert . equal ( data , "Package file created" ) ;
206
206
} )
207
207
. catch ( ( _error ) => {
208
208
console . log ( _error )
@@ -212,80 +212,52 @@ describe("packageInstaller", () => {
212
212
} ) ;
213
213
214
214
context ( "packageInstall" , ( ) => {
215
- let npmInstallStub ;
216
215
const packageInstaller = rewire ( "../../../../bin/helpers/packageInstaller" ) ;
217
- beforeEach ( ( ) => {
218
- npmInstallStub = sandbox . stub ( npm . commands , "install" ) . returns ( null ) ;
219
- } ) ;
220
216
221
- it ( "should reject if error in npm load" , ( ) => {
222
- packageInstaller . __set__ ( {
223
- npm : {
224
- commands : {
225
- install : npmInstallStub
226
- } ,
227
- load : ( _npmLoad , loadCallback ) => {
228
- loadCallback ( "test error" ) ;
229
- }
230
- } ,
217
+ it ( "should call npm install on directory and resolve if spawn is closed successfully" , ( ) => {
218
+ let spawnStub = sandbox . stub ( cp , 'spawn' ) . returns ( {
219
+ on : ( _close , nodeProcessCloseCallback ) => {
220
+ nodeProcessCloseCallback ( 0 ) ;
221
+ }
231
222
} ) ;
232
- let packageInstallrewire = packageInstaller . __get__ ( 'packageInstall' ) ;
233
- let directoryPath = "/random/path" ;
234
- return packageInstallrewire ( directoryPath )
235
- . then ( ( _data ) => {
236
- chai . assert . fail ( "Promise error" ) ;
237
- } )
238
- . catch ( ( error ) => {
239
- chai . assert . equal ( error , "test error" )
240
- } ) ;
241
- } ) ;
242
-
243
- it ( "should call npm install on directory" , ( ) => {
244
223
packageInstaller . __set__ ( {
245
- npm : {
246
- commands : {
247
- install : ( _packageDir , [ ] , installCallback ) => {
248
- installCallback ( null , "npm install done" ) ;
249
- }
250
- } ,
251
- load : ( _npmLoad , loadCallback ) => {
252
- loadCallback ( null ) ;
253
- }
254
- } ,
224
+ nodeProcess : { } ,
225
+ spawn : spawnStub
255
226
} ) ;
256
227
let packageInstallrewire = packageInstaller . __get__ ( 'packageInstall' ) ;
257
228
let directoryPath = "/random/path" ;
258
229
return packageInstallrewire ( directoryPath )
259
- . then ( ( data ) => {
260
- chai . assert . equal ( data , "npm install done" )
261
- } )
262
- . catch ( ( _error ) => {
263
- chai . assert . fail ( "Promise error" ) ;
264
- } ) ;
230
+ . then ( ( data ) => {
231
+ console . log ( data ) ;
232
+ chai . assert . equal ( data , "Packages were installed successfully." )
233
+ spawnStub . restore ( ) ;
234
+ } )
235
+ . catch ( ( _error ) => {
236
+ chai . assert . fail ( `Promise error ${ _error } ` ) ;
237
+ } ) ;
265
238
} ) ;
266
239
267
- it ( "should reject if error in npm install" , ( ) => {
240
+ it ( "should call npm install on directory and reject if spawn is not closed successfully" , ( ) => {
241
+ let spawnStub = sandbox . stub ( cp , 'spawn' ) . returns ( {
242
+ on : ( _close , nodeProcessCloseCallback ) => {
243
+ nodeProcessCloseCallback ( 1 ) ;
244
+ }
245
+ } ) ;
268
246
packageInstaller . __set__ ( {
269
- npm : {
270
- commands : {
271
- install : ( _packageDir , [ ] , installCallback ) => {
272
- installCallback ( "test error" , "npm install failed" ) ;
273
- }
274
- } ,
275
- load : ( _npmLoad , loadCallback ) => {
276
- loadCallback ( null ) ;
277
- }
278
- } ,
247
+ nodeProcess : { } ,
248
+ spawn : spawnStub
279
249
} ) ;
280
250
let packageInstallrewire = packageInstaller . __get__ ( 'packageInstall' ) ;
281
251
let directoryPath = "/random/path" ;
282
252
return packageInstallrewire ( directoryPath )
283
- . then ( ( _data ) => {
284
- chai . assert . fail ( "Promise error" ) ;
285
- } )
286
- . catch ( ( error ) => {
287
- chai . assert . equal ( error , "test error" )
288
- } ) ;
253
+ . then ( ( _data ) => {
254
+ spawnStub . restore ( ) ;
255
+ chai . assert . fail ( "Promise error" ) ;
256
+ } )
257
+ . catch ( ( error ) => {
258
+ spawnStub . restore ( ) ;
259
+ chai . assert . equal ( error , "Packages were not installed successfully." )
260
+ } ) ;
289
261
} ) ;
290
262
} ) ;
291
263
0 commit comments