Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make 'npm install --loglevel warn' Output Quieter #4335

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/utils/reify-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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'
Expand Down
36 changes: 35 additions & 1 deletion test/lib/utils/reify-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down Expand Up @@ -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(() => {
Expand Down