@@ -187,31 +187,154 @@ t.test('print appropriate message for many packages', (t) => {
187
187
} )
188
188
} )
189
189
190
- t . test ( 'no output when silent' , t => {
191
- npm . output = out => {
192
- t . fail ( 'should not get output when silent' , { actual : out } )
193
- }
194
- t . teardown ( ( ) => log . level = 'warn' )
195
- log . level = 'silent'
196
- reifyOutput ( npm , {
197
- actualTree : { inventory : { size : 999 } , children : [ ] } ,
198
- auditReport : {
199
- toJSON : ( ) => {
200
- throw new Error ( 'this should not get called' )
201
- } ,
202
- vulnerabilities : { } ,
203
- metadata : {
204
- vulnerabilities : {
205
- total : 99 ,
206
- } ,
190
+ t . test ( 'showing and not showing audit report' , async t => {
191
+ const auditReport = {
192
+ toJSON : ( ) => auditReport ,
193
+ auditReportVersion : 2 ,
194
+ vulnerabilities : {
195
+ minimist : {
196
+ name : 'minimist' ,
197
+ severity : 'low' ,
198
+ via : [
199
+ {
200
+ id : 1179 ,
201
+ url : 'https://npmjs.com/advisories/1179' ,
202
+ title : 'Prototype Pollution' ,
203
+ severity : 'low' ,
204
+ vulnerable_versions : '<0.2.1 || >=1.0.0 <1.2.3' ,
205
+ } ,
206
+ ] ,
207
+ effects : [ ] ,
208
+ range : '<0.2.1 || >=1.0.0 <1.2.3' ,
209
+ nodes : [
210
+ 'node_modules/minimist' ,
211
+ ] ,
212
+ fixAvailable : true ,
207
213
} ,
208
214
} ,
209
- diff : {
210
- children : [
211
- { action : 'ADD' , ideal : { location : 'loc' } } ,
212
- ] ,
215
+ metadata : {
216
+ vulnerabilities : {
217
+ info : 0 ,
218
+ low : 1 ,
219
+ moderate : 0 ,
220
+ high : 0 ,
221
+ critical : 0 ,
222
+ total : 1 ,
223
+ } ,
224
+ dependencies : {
225
+ prod : 1 ,
226
+ dev : 0 ,
227
+ optional : 0 ,
228
+ peer : 0 ,
229
+ peerOptional : 0 ,
230
+ total : 1 ,
231
+ } ,
213
232
} ,
233
+ }
234
+
235
+ t . test ( 'no output when silent' , t => {
236
+ npm . output = out => {
237
+ t . fail ( 'should not get output when silent' , { actual : out } )
238
+ }
239
+ t . teardown ( ( ) => log . level = 'warn' )
240
+ log . level = 'silent'
241
+ reifyOutput ( npm , {
242
+ actualTree : { inventory : { size : 999 } , children : [ ] } ,
243
+ auditReport,
244
+ diff : {
245
+ children : [
246
+ { action : 'ADD' , ideal : { location : 'loc' } } ,
247
+ ] ,
248
+ } ,
249
+ } )
250
+ t . end ( )
214
251
} )
252
+
253
+ t . test ( 'output when not silent' , t => {
254
+ const OUT = [ ]
255
+ npm . output = out => {
256
+ OUT . push ( out )
257
+ }
258
+ reifyOutput ( npm , {
259
+ actualTree : { inventory : new Map ( ) , children : [ ] } ,
260
+ auditReport,
261
+ diff : {
262
+ children : [
263
+ { action : 'ADD' , ideal : { location : 'loc' } } ,
264
+ ] ,
265
+ } ,
266
+ } )
267
+ t . match ( OUT . join ( '\n' ) , / R u n ` n p m a u d i t ` f o r d e t a i l s \. $ / , 'got audit report' )
268
+ t . end ( )
269
+ } )
270
+
271
+ for ( const json of [ true , false ] ) {
272
+ t . test ( `json=${ json } ` , t => {
273
+ t . teardown ( ( ) => {
274
+ delete npm . flatOptions . json
275
+ } )
276
+ npm . flatOptions . json = json
277
+ t . test ( 'set exit code when cmd is audit' , t => {
278
+ npm . output = ( ) => { }
279
+ const { exitCode } = process
280
+ const { command } = npm
281
+ npm . flatOptions . auditLevel = 'low'
282
+ t . teardown ( ( ) => {
283
+ delete npm . flatOptions . auditLevel
284
+ npm . command = command
285
+ // only set exitCode back if we're passing tests
286
+ if ( t . passing ( ) )
287
+ process . exitCode = exitCode
288
+ } )
289
+
290
+ process . exitCode = 0
291
+ npm . command = 'audit'
292
+ reifyOutput ( npm , {
293
+ actualTree : { inventory : new Map ( ) , children : [ ] } ,
294
+ auditReport,
295
+ diff : {
296
+ children : [
297
+ { action : 'ADD' , ideal : { location : 'loc' } } ,
298
+ ] ,
299
+ } ,
300
+ } )
301
+
302
+ t . equal ( process . exitCode , 1 , 'set exit code' )
303
+ t . end ( )
304
+ } )
305
+
306
+ t . test ( 'do not set exit code when cmd is install' , t => {
307
+ npm . output = ( ) => { }
308
+ const { exitCode } = process
309
+ const { command } = npm
310
+ npm . flatOptions . auditLevel = 'low'
311
+ t . teardown ( ( ) => {
312
+ delete npm . flatOptions . auditLevel
313
+ npm . command = command
314
+ // only set exitCode back if we're passing tests
315
+ if ( t . passing ( ) )
316
+ process . exitCode = exitCode
317
+ } )
318
+
319
+ process . exitCode = 0
320
+ npm . command = 'install'
321
+ reifyOutput ( npm , {
322
+ actualTree : { inventory : new Map ( ) , children : [ ] } ,
323
+ auditReport,
324
+ diff : {
325
+ children : [
326
+ { action : 'ADD' , ideal : { location : 'loc' } } ,
327
+ ] ,
328
+ } ,
329
+ } )
330
+
331
+ t . equal ( process . exitCode , 0 , 'did not set exit code' )
332
+ t . end ( )
333
+ } )
334
+ t . end ( )
335
+ } )
336
+ }
337
+
215
338
t . end ( )
216
339
} )
217
340
0 commit comments