diff --git a/lib/outdated.js b/lib/outdated.js index 17b9414611d7c..bb4c346f9a654 100644 --- a/lib/outdated.js +++ b/lib/outdated.js @@ -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) { @@ -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)) })) }) })) diff --git a/test/tap/outdated-json.js b/test/tap/outdated-json.js index e0040d0285f2f..77db52cc72521 100644 --- a/test/tap/outdated-json.js +++ b/test/tap/outdated-json.js @@ -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') @@ -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) @@ -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) { + 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) -}