Skip to content

Commit

Permalink
fix: Always return JSON for outdated --json
Browse files Browse the repository at this point in the history
Close: #176

EDIT: Added test, do not set exitStatus to 1 if we're just printing an
empty list as JSON. -- @isaacs
  • Loading branch information
Sreeram Jayan authored and isaacs committed Jun 30, 2019
1 parent d9238af commit 87fef4e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
7 changes: 5 additions & 2 deletions lib/outdated.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ function outdated (args, silent, cb) {
return aa[0].path.localeCompare(bb[0].path) ||
aa[1].localeCompare(bb[1])
})
if (er || silent || list.length === 0) return cb(er, list)
if (er || silent ||
(list.length === 0 && !opts.json)) {
return cb(er, list)
}
if (opts.json) {
output(makeJSON(list, opts))
} else if (opts.parseable) {
Expand Down Expand Up @@ -129,7 +132,7 @@ function outdated (args, silent, cb) {
}
output(table(outTable, tableOpts))
}
process.exitCode = 1
process.exitCode = list.length ? 1 : 0
cb(null, list.map(function (item) { return [item[0].parent.path].concat(item.slice(1, 7)) }))
})
}))
Expand Down
42 changes: 30 additions & 12 deletions test/tap/outdated-json.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
var fs = require('graceful-fs')
var path = require('path')

var mkdirp = require('mkdirp')
var mr = require('npm-registry-mock')
var osenv = require('osenv')
var rimraf = require('rimraf')
var test = require('tap').test

var common = require('../common-tap.js')
Expand Down Expand Up @@ -42,8 +39,6 @@ var expected = {
}

test('setup', function (t) {
cleanup()
mkdirp.sync(pkg)
fs.writeFileSync(
path.join(pkg, 'package.json'),
JSON.stringify(json, null, 2)
Expand Down Expand Up @@ -92,14 +87,37 @@ test('it should log json data', function (t) {
)
})

test('it should log json data even when the list is empty', function (t) {

This comment has been minimized.

Copy link
@dwayne728

dwayne728 Aug 4, 2019

OK

common.npm(
[
'rm',
'request',
'underscore'
],
EXEC_OPTS,
function (er, code, stdout) {
t.ifError(er, 'run without error')
t.is(code, 0, 'successful exit status')
common.npm(
[
'--registry', common.registry,
'--silent',
'--json',
'outdated'
],
EXEC_OPTS,
function (er, code, stdout) {
t.ifError(er, 'run without error')
t.is(code, 0, 'successful exit status')
t.same(JSON.parse(stdout), {}, 'got an empty object printed')
t.end()
}
)
}
)
})

test('cleanup', function (t) {
server.close()
cleanup()
t.end()
})

function cleanup () {
// windows fix for locked files
process.chdir(osenv.tmpdir())
rimraf.sync(pkg)
}

0 comments on commit 87fef4e

Please sign in to comment.