From ab544759e4b38467a6956d71f4e6897dbce85a3c Mon Sep 17 00:00:00 2001 From: Sreeram Jayan Date: Wed, 20 Mar 2019 10:06:06 -0500 Subject: [PATCH] fix: Always return JSON for outdated --json Close: https://github.com/npm/cli/pull/176 EDIT: Added test, do not set exitStatus to 1 if we're just printing an empty list as JSON. -- @isaacs --- lib/outdated.js | 4 ++-- test/tap/outdated-json.js | 39 ++++++++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/lib/outdated.js b/lib/outdated.js index 17b9414611d7c..c4dc7c58973c2 100644 --- a/lib/outdated.js +++ b/lib/outdated.js @@ -101,7 +101,7 @@ 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 +129,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..e6b3623fa4195 100644 --- a/test/tap/outdated-json.js +++ b/test/tap/outdated-json.js @@ -42,8 +42,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 +90,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 (err, 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) -}