Skip to content

Commit 075fe50

Browse files
gfyoungwraithgar
authored andcommitted
fix: restore exit code on "npm outdated"
closes: #2556 xref: #1750 The xref'ed PR apparently dropped this behavior without any explanation. PR-URL: #3799 Credit: @gfyoung Close: #3799 Reviewed-by: @wraithgar
1 parent 56d6cfd commit 075fe50

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

lib/outdated.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ class Outdated extends ArboristWorkspaceCmd {
8787
// sorts list alphabetically
8888
const outdated = this.list.sort((a, b) => a.name.localeCompare(b.name, 'en'))
8989

90+
if (outdated.length > 0)
91+
process.exitCode = 1
92+
9093
// return if no outdated packages
9194
if (outdated.length === 0 && !this.npm.config.get('json'))
9295
return

smoke-tests/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,13 @@ t.test('npm diff', async t => {
174174

175175
t.test('npm outdated', async t => {
176176
const cmd = `${npmBin} outdated`
177-
const cmdRes = await exec(cmd)
177+
const cmdRes = await exec(cmd).catch(err => {
178+
t.equal(err.code, 1, 'should exit with error code')
179+
return err
180+
})
178181

179-
t.matchSnapshot(cmdRes,
182+
t.not(cmdRes.stderr, '', 'should have stderr output')
183+
t.matchSnapshot(String(cmdRes.stdout),
180184
'should have expected outdated output')
181185
})
182186

test/lib/outdated.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ const outdated = (dir, opts) => {
102102

103103
t.beforeEach(() => logs = '')
104104

105+
const { exitCode } = process
106+
107+
t.afterEach(() => process.exitCode = exitCode)
108+
105109
const redactCwd = (path) => {
106110
const normalizePath = p => p
107111
.replace(/\\+/g, '/')
@@ -175,6 +179,7 @@ t.test('should display outdated deps', t => {
175179
outdated(null, {
176180
config: { global: true },
177181
}).exec([], () => {
182+
t.equal(process.exitCode, 1)
178183
t.matchSnapshot(logs)
179184
t.end()
180185
})
@@ -187,6 +192,7 @@ t.test('should display outdated deps', t => {
187192
},
188193
color: true,
189194
}).exec([], () => {
195+
t.equal(process.exitCode, 1)
190196
t.matchSnapshot(logs)
191197
t.end()
192198
})
@@ -200,6 +206,7 @@ t.test('should display outdated deps', t => {
200206
},
201207
color: true,
202208
}).exec([], () => {
209+
t.equal(process.exitCode, 1)
203210
t.matchSnapshot(logs)
204211
t.end()
205212
})
@@ -213,6 +220,7 @@ t.test('should display outdated deps', t => {
213220
},
214221
color: true,
215222
}).exec([], () => {
223+
t.equal(process.exitCode, 1)
216224
t.matchSnapshot(logs)
217225
t.end()
218226
})
@@ -226,6 +234,7 @@ t.test('should display outdated deps', t => {
226234
},
227235
color: true,
228236
}).exec([], () => {
237+
t.equal(process.exitCode, 1)
229238
t.matchSnapshot(logs)
230239
t.end()
231240
})
@@ -238,6 +247,7 @@ t.test('should display outdated deps', t => {
238247
long: true,
239248
},
240249
}).exec([], () => {
250+
t.equal(process.exitCode, 1)
241251
t.matchSnapshot(logs)
242252
t.end()
243253
})
@@ -250,6 +260,7 @@ t.test('should display outdated deps', t => {
250260
json: true,
251261
},
252262
}).exec([], () => {
263+
t.equal(process.exitCode, 1)
253264
t.matchSnapshot(logs)
254265
t.end()
255266
})
@@ -263,6 +274,7 @@ t.test('should display outdated deps', t => {
263274
long: true,
264275
},
265276
}).exec([], () => {
277+
t.equal(process.exitCode, 1)
266278
t.matchSnapshot(logs)
267279
t.end()
268280
})
@@ -275,6 +287,7 @@ t.test('should display outdated deps', t => {
275287
parseable: true,
276288
},
277289
}).exec([], () => {
290+
t.equal(process.exitCode, 1)
278291
t.matchSnapshot(logs)
279292
t.end()
280293
})
@@ -288,6 +301,7 @@ t.test('should display outdated deps', t => {
288301
long: true,
289302
},
290303
}).exec([], () => {
304+
t.equal(process.exitCode, 1)
291305
t.matchSnapshot(logs)
292306
t.end()
293307
})
@@ -299,6 +313,7 @@ t.test('should display outdated deps', t => {
299313
all: true,
300314
},
301315
}).exec([], () => {
316+
t.equal(process.exitCode, 1)
302317
t.matchSnapshot(logs)
303318
t.end()
304319
})
@@ -310,6 +325,7 @@ t.test('should display outdated deps', t => {
310325
global: false,
311326
},
312327
}).exec(['cat'], () => {
328+
t.equal(process.exitCode, 1)
313329
t.matchSnapshot(logs)
314330
t.end()
315331
})
@@ -540,6 +556,7 @@ t.test('workspaces', async t => {
540556
rej(err)
541557

542558
t.matchSnapshot(logs, 'should display ws outdated deps human output')
559+
t.equal(process.exitCode, 1)
543560
res()
544561
})
545562
})
@@ -554,6 +571,7 @@ t.test('workspaces', async t => {
554571
rej(err)
555572

556573
t.matchSnapshot(logs, 'should display ws outdated deps json output')
574+
t.equal(process.exitCode, 1)
557575
res()
558576
})
559577
})
@@ -568,6 +586,7 @@ t.test('workspaces', async t => {
568586
rej(err)
569587

570588
t.matchSnapshot(logs, 'should display ws outdated deps parseable output')
589+
t.equal(process.exitCode, 1)
571590
res()
572591
})
573592
})
@@ -582,6 +601,7 @@ t.test('workspaces', async t => {
582601
rej(err)
583602

584603
t.matchSnapshot(logs, 'should display all dependencies')
604+
t.equal(process.exitCode, 1)
585605
res()
586606
})
587607
})
@@ -594,6 +614,7 @@ t.test('workspaces', async t => {
594614
rej(err)
595615

596616
t.matchSnapshot(logs, 'should highlight ws in dependend by section')
617+
t.equal(process.exitCode, 1)
597618
res()
598619
})
599620
})
@@ -604,6 +625,7 @@ t.test('workspaces', async t => {
604625
rej(err)
605626

606627
t.matchSnapshot(logs, 'should display results filtered by ws')
628+
t.equal(process.exitCode, 1)
607629
res()
608630
})
609631
})
@@ -618,6 +640,7 @@ t.test('workspaces', async t => {
618640
rej(err)
619641

620642
t.matchSnapshot(logs, 'should display json results filtered by ws')
643+
t.equal(process.exitCode, 1)
621644
res()
622645
})
623646
})
@@ -632,6 +655,7 @@ t.test('workspaces', async t => {
632655
rej(err)
633656

634657
t.matchSnapshot(logs, 'should display parseable results filtered by ws')
658+
t.equal(process.exitCode, 1)
635659
res()
636660
})
637661
})
@@ -647,6 +671,7 @@ t.test('workspaces', async t => {
647671

648672
t.matchSnapshot(logs,
649673
'should display nested deps when filtering by ws and using --all')
674+
t.equal(process.exitCode, 1)
650675
res()
651676
})
652677
})
@@ -669,6 +694,7 @@ t.test('workspaces', async t => {
669694

670695
t.matchSnapshot(logs,
671696
'should display missing deps when filtering by ws')
697+
t.equal(process.exitCode, 1)
672698
res()
673699
})
674700
})

0 commit comments

Comments
 (0)