diff --git a/lib/utils/reify-output.js b/lib/utils/reify-output.js index b4114c1b2fe04..ff4ff4431c148 100644 --- a/lib/utils/reify-output.js +++ b/lib/utils/reify-output.js @@ -25,9 +25,9 @@ const reifyOutput = (npm, arb) => { // stuff in that case! const auditReport = auditError(npm, arb.auditReport) ? null : arb.auditReport - // don't print any info in --silent mode, but we still need to + // don't print any info if loglevel is 'warn' or higher, but we still need to // set the exitCode properly from the audit report, if we have one. - if (log.levels[log.level] > log.levels.error) { + if (log.levels[log.level] >= log.levels.warn) { getAuditReport(npm, auditReport) return } @@ -88,7 +88,7 @@ const reifyOutput = (npm, arb) => { // at the end if there's still stuff, because it's silly for `npm audit` // to tell you to run `npm audit` for details. otherwise, use the summary // report. if we get here, we know it's not quiet or json. -// If the loglevel is set higher than 'error', then we just run the report +// If the loglevel is set to 'warn' or higher, then we just run the report // to get the exitCode set appropriately. const printAuditReport = (npm, report) => { const res = getAuditReport(npm, report) @@ -103,9 +103,9 @@ const getAuditReport = (npm, report) => { return } - // when in silent mode, we print nothing. the JSON output is + // when loglevel is 'warn' or higher, we print nothing. the JSON output is // going to just JSON.stringify() the report object. - const reporter = log.levels[log.level] > log.levels.error ? 'quiet' + const reporter = log.levels[log.level] >= log.levels.warn ? 'quiet' : npm.flatOptions.json ? 'quiet' : npm.command !== 'audit' ? 'install' : 'detail' diff --git a/test/lib/utils/reify-output.js b/test/lib/utils/reify-output.js index 4e9ed7133c18c..92e36c683c50b 100644 --- a/test/lib/utils/reify-output.js +++ b/test/lib/utils/reify-output.js @@ -2,7 +2,7 @@ const t = require('tap') const log = require('../../../lib/utils/log-shim') const _level = log.level -t.beforeEach(() => log.level = 'warn') +t.beforeEach(() => log.level = 'notice') t.teardown(() => log.level = _level) t.cleanSnapshot = str => str.replace(/in [0-9]+m?s/g, 'in {TIME}') @@ -270,6 +270,40 @@ t.test('showing and not showing audit report', async t => { t.end() }) + t.test('no output when loglevel = error', t => { + npm.output = out => { + t.fail('should not get output when loglevel = error', { actual: out }) + } + log.level = 'error' + reifyOutput(npm, { + actualTree: { inventory: { size: 999 }, children: [] }, + auditReport, + diff: { + children: [ + { action: 'ADD', ideal: { location: 'loc' } }, + ], + }, + }) + t.end() + }) + + t.test('no output when loglevel = warn', t => { + npm.output = out => { + t.fail('should not get output when loglevel = warn', { actual: out }) + } + log.level = 'warn' + reifyOutput(npm, { + actualTree: { inventory: { size: 999 }, children: [] }, + auditReport, + diff: { + children: [ + { action: 'ADD', ideal: { location: 'loc' } }, + ], + }, + }) + t.end() + }) + for (const json of [true, false]) { t.test(`json=${json}`, t => { t.teardown(() => {