diff --git a/lib/commands/hook.js b/lib/commands/hook.js index 08bf3ca794142..2ff6ac01ce527 100644 --- a/lib/commands/hook.js +++ b/lib/commands/hook.js @@ -46,7 +46,7 @@ class Hook extends BaseCommand { } else if (opts.parseable) { this.npm.output(Object.keys(hook).join('\t')) this.npm.output(Object.keys(hook).map(k => hook[k]).join('\t')) - } else if (!opts.silent) { + } else if (!this.npm.silent) { this.npm.output(`+ ${this.hookName(hook)} ${ opts.unicode ? ' ➜ ' : ' -> ' } ${hook.endpoint}`) @@ -64,7 +64,7 @@ class Hook extends BaseCommand { }) } else if (!hooks.length) { this.npm.output("You don't have any hooks configured yet.") - } else if (!opts.silent) { + } else if (!this.npm.silent) { if (hooks.length === 1) { this.npm.output('You have one hook configured.') } else { @@ -101,7 +101,7 @@ class Hook extends BaseCommand { } else if (opts.parseable) { this.npm.output(Object.keys(hook).join('\t')) this.npm.output(Object.keys(hook).map(k => hook[k]).join('\t')) - } else if (!opts.silent) { + } else if (!this.npm.silent) { this.npm.output(`- ${this.hookName(hook)} ${ opts.unicode ? ' ✘ ' : ' X ' } ${hook.endpoint}`) @@ -115,7 +115,7 @@ class Hook extends BaseCommand { } else if (opts.parseable) { this.npm.output(Object.keys(hook).join('\t')) this.npm.output(Object.keys(hook).map(k => hook[k]).join('\t')) - } else if (!opts.silent) { + } else if (!this.npm.silent) { this.npm.output(`+ ${this.hookName(hook)} ${ opts.unicode ? ' ➜ ' : ' -> ' } ${hook.endpoint}`) diff --git a/lib/commands/org.js b/lib/commands/org.js index 0e4a39bcd788f..f3d344ca33e3d 100644 --- a/lib/commands/org.js +++ b/lib/commands/org.js @@ -74,7 +74,7 @@ class Org extends BaseCommand { this.npm.output( [memDeets.org.name, memDeets.org.size, memDeets.user, memDeets.role].join('\t') ) - } else if (!opts.silent) { + } else if (!this.npm.silent) { this.npm.output( `Added ${memDeets.user} as ${memDeets.role} to ${memDeets.org.name}. You now have ${ memDeets.org.size @@ -116,7 +116,7 @@ class Org extends BaseCommand { } else if (opts.parseable) { this.npm.output(['user', 'org', 'userCount', 'deleted'].join('\t')) this.npm.output([user, org, userCount, true].join('\t')) - } else if (!opts.silent) { + } else if (!this.npm.silent) { this.npm.output( `Successfully removed ${user} from ${org}. You now have ${userCount} member${ userCount === 1 ? '' : 's' @@ -147,7 +147,7 @@ class Org extends BaseCommand { Object.keys(roster).forEach(user => { this.npm.output([user, roster[user]].join('\t')) }) - } else if (!opts.silent) { + } else if (!this.npm.silent) { const table = new Table({ head: ['user', 'role'] }) Object.keys(roster) .sort() diff --git a/lib/commands/team.js b/lib/commands/team.js index 3cb00e3f8f7cb..1e13a83f52b09 100644 --- a/lib/commands/team.js +++ b/lib/commands/team.js @@ -72,7 +72,7 @@ class Team extends BaseCommand { })) } else if (opts.parseable) { this.npm.output(`${entity}\tcreated`) - } else if (!opts.silent) { + } else if (!this.npm.silent) { this.npm.output(`+@${entity}`) } } @@ -86,7 +86,7 @@ class Team extends BaseCommand { })) } else if (opts.parseable) { this.npm.output(`${entity}\tdeleted`) - } else if (!opts.silent) { + } else if (!this.npm.silent) { this.npm.output(`-@${entity}`) } } @@ -101,7 +101,7 @@ class Team extends BaseCommand { })) } else if (opts.parseable) { this.npm.output(`${user}\t${entity}\tadded`) - } else if (!opts.silent) { + } else if (!this.npm.silent) { this.npm.output(`${user} added to @${entity}`) } } @@ -116,7 +116,7 @@ class Team extends BaseCommand { })) } else if (opts.parseable) { this.npm.output(`${user}\t${entity}\tremoved`) - } else if (!opts.silent) { + } else if (!this.npm.silent) { this.npm.output(`${user} removed from @${entity}`) } } @@ -127,7 +127,7 @@ class Team extends BaseCommand { this.npm.output(JSON.stringify(users, null, 2)) } else if (opts.parseable) { this.npm.output(users.join('\n')) - } else if (!opts.silent) { + } else if (!this.npm.silent) { const plural = users.length === 1 ? '' : 's' const more = users.length === 0 ? '' : ':\n' this.npm.output(`\n@${entity} has ${users.length} user${plural}${more}`) @@ -141,7 +141,7 @@ class Team extends BaseCommand { this.npm.output(JSON.stringify(teams, null, 2)) } else if (opts.parseable) { this.npm.output(teams.join('\n')) - } else if (!opts.silent) { + } else if (!this.npm.silent) { const plural = teams.length === 1 ? '' : 's' const more = teams.length === 0 ? '' : ':\n' this.npm.output(`\n@${entity} has ${teams.length} team${plural}${more}`) diff --git a/test/fixtures/mock-npm.js b/test/fixtures/mock-npm.js index 2c82d4af612fb..4846d9a485841 100644 --- a/test/fixtures/mock-npm.js +++ b/test/fixtures/mock-npm.js @@ -156,6 +156,7 @@ class MockNpm { get: (k) => ({ ...realConfig.defaults, ...config })[k], set: (k, v) => { config[k] = v + // mock how real npm derives silent if (k === 'loglevel') { this.flatOptions.silent = v === 'silent' this.silent = v === 'silent' @@ -168,9 +169,8 @@ class MockNpm { setLoglevel(t, config.loglevel) } - if (config.loglevel === 'silent') { - this.flatOptions.silent = true - this.silent = true + if (config.loglevel) { + this.config.set('loglevel', config.loglevel) } } diff --git a/test/lib/commands/doctor.js b/test/lib/commands/doctor.js index 06528554ab6dd..dee2110ff3c89 100644 --- a/test/lib/commands/doctor.js +++ b/test/lib/commands/doctor.js @@ -4,6 +4,7 @@ const { join } = require('path') const fs = require('fs') const ansiTrim = require('../../../lib/utils/ansi-trim.js') const isWindows = require('../../../lib/utils/is-windows.js') +const { fake: mockNpm } = require('../../fixtures/mock-npm') // getuid and getgid do not exist in windows, so we shim them // to return 0, as that is the value that lstat will assign the @@ -61,24 +62,18 @@ const clearLogs = () => { } } -const npm = { +const npm = mockNpm({ flatOptions: { registry: 'https://registry.npmjs.org/', }, - set loglevel (v) { - this.flatOptions.silent = v === 'silent' - this.silent = v === 'silent' - this._loglevel = v + config: { + loglevel: 'info', }, - get loglevel () { - return this._loglevel - }, - _loglevel: 'info', version: '7.1.0', output: data => { output.push(data) }, -} +}) let latestNpm = npm.version const pacote = { @@ -220,7 +215,7 @@ t.test('node versions', t => { npm.globalDir = dir npm.localBin = dir npm.globalBin = dir - npm.loglevel = 'silent' + npm.config.set('loglevel', 'silent') st.teardown(() => { delete npm.cache @@ -229,7 +224,7 @@ t.test('node versions', t => { delete npm.globalDir delete npm.localBin delete npm.globalBin - npm.loglevel = 'info' + npm.config.set('loglevel', 'info') clearLogs() }) diff --git a/test/lib/commands/hook.js b/test/lib/commands/hook.js index 5d364d8a5fc64..60a59a3fe7a3b 100644 --- a/test/lib/commands/hook.js +++ b/test/lib/commands/hook.js @@ -1,18 +1,20 @@ const t = require('tap') +const { fake: mockNpm } = require('../../fixtures/mock-npm') const output = [] -const npm = { +const npm = mockNpm({ flatOptions: { json: false, parseable: false, - silent: false, - loglevel: 'info', unicode: false, }, + config: { + loglevel: 'info', + }, output: msg => { output.push(msg) }, -} +}) const pkgTypes = { semver: 'package', @@ -179,9 +181,9 @@ t.test('npm hook add - parseable output', async t => { }) t.test('npm hook add - silent output', async t => { - npm.flatOptions.silent = true + npm.config.set('loglevel', 'silent') t.teardown(() => { - npm.flatOptions.silent = false + npm.config.set('loglevel', 'info') hookArgs = null output.length = 0 }) @@ -352,9 +354,9 @@ t.test('npm hook ls - parseable output', async t => { }) t.test('npm hook ls - silent output', async t => { - npm.flatOptions.silent = true + npm.config.set('loglevel', 'silent') t.teardown(() => { - npm.flatOptions.silent = false + npm.config.set('loglevel', 'info') hookArgs = null output.length = 0 }) @@ -413,9 +415,9 @@ t.test('npm hook rm - unicode output', async t => { }) t.test('npm hook rm - silent output', async t => { - npm.flatOptions.silent = true + npm.config.set('loglevel', 'silent') t.teardown(() => { - npm.flatOptions.silent = false + npm.config.set('loglevel', 'info') hookArgs = null output.length = 0 }) @@ -598,9 +600,9 @@ t.test('npm hook update - parseable output', async t => { }) t.test('npm hook update - silent output', async t => { - npm.flatOptions.silent = true + npm.config.set('loglevel', 'silent') t.teardown(() => { - npm.flatOptions.silent = false + npm.config.set('loglevel', 'info') hookArgs = null output.length = 0 }) diff --git a/test/lib/commands/org.js b/test/lib/commands/org.js index 14b116d87510a..3ae951dd5c453 100644 --- a/test/lib/commands/org.js +++ b/test/lib/commands/org.js @@ -1,18 +1,20 @@ const t = require('tap') const ansiTrim = require('../../../lib/utils/ansi-trim.js') +const { fake: mockNpm } = require('../../fixtures/mock-npm') const output = [] -const npm = { +const npm = mockNpm({ flatOptions: { json: false, parseable: false, - silent: false, + }, + config: { loglevel: 'info', }, output: msg => { output.push(msg) }, -} +}) let orgSize = 1 let orgSetArgs = null @@ -230,9 +232,9 @@ t.test('npm org add - parseable output', async t => { }) t.test('npm org add - silent output', async t => { - npm.flatOptions.silent = true + npm.config.set('loglevel', 'silent') t.teardown(() => { - npm.flatOptions.silent = false + npm.config.set('loglevel', 'info') orgSetArgs = null output.length = 0 }) @@ -426,9 +428,9 @@ t.test('npm org rm - parseable output', async t => { }) t.test('npm org rm - silent output', async t => { - npm.flatOptions.silent = true + npm.config.set('loglevel', 'silent') t.teardown(() => { - npm.flatOptions.silent = false + npm.config.set('loglevel', 'info') orgRmArgs = null orgLsArgs = null output.length = 0 @@ -608,14 +610,14 @@ t.test('npm org ls - parseable output', async t => { }) t.test('npm org ls - silent output', async t => { - npm.flatOptions.silent = true + npm.config.set('loglevel', 'silent') orgList = { one: 'developer', two: 'admin', three: 'owner', } t.teardown(() => { - npm.flatOptions.silent = false + npm.config.set('loglevel', 'info') orgList = {} orgLsArgs = null output.length = 0 diff --git a/test/lib/commands/team.js b/test/lib/commands/team.js index 96260835fa2b6..0d5378b530647 100644 --- a/test/lib/commands/team.js +++ b/test/lib/commands/team.js @@ -1,4 +1,5 @@ const t = require('tap') +const { fake: mockNpm } = require('../../fixtures/mock-npm') let result = '' const libnpmteam = { @@ -9,12 +10,15 @@ const libnpmteam = { async lsUsers () {}, async rm () {}, } -const npm = { +const npm = mockNpm({ flatOptions: {}, + config: { + loglevel: 'info', + }, output: (...msg) => { result += msg.join('\n') }, -} +}) const mocks = { libnpmteam, 'cli-columns': a => a.join(' '), @@ -25,6 +29,7 @@ const mocks = { t.afterEach(() => { result = '' npm.flatOptions = {} + npm.config.set('loglevel', 'info') }) const Team = t.mock('../../../lib/commands/team.js', mocks) @@ -73,7 +78,7 @@ t.test('team add ', async t => { }) t.test('--silent', async t => { - npm.flatOptions.silent = true + npm.config.set('loglevel', 'silent') await team.exec(['add', '@npmcli:developers', 'foo']) @@ -115,7 +120,7 @@ t.test('team create ', async t => { }) t.test('--silent', async t => { - npm.flatOptions.silent = true + npm.config.set('loglevel', 'silent') await team.exec(['create', '@npmcli:newteam']) @@ -149,7 +154,7 @@ t.test('team destroy ', async t => { }) t.test('--silent', async t => { - npm.flatOptions.silent = true + npm.config.set('loglevel', 'silent') await team.exec(['destroy', '@npmcli:newteam']) t.same(result, '', 'should not output destroy if silent') }) @@ -198,7 +203,7 @@ t.test('team ls ', async t => { }) t.test('--silent', async t => { - npm.flatOptions.silent = true + npm.config.set('loglevel', 'silent') await team.exec(['ls', '@npmcli']) t.same(result, '', 'should not list teams if silent') }) @@ -278,7 +283,7 @@ t.test('team ls ', async t => { }) t.test('--silent', async t => { - npm.flatOptions.silent = true + npm.config.set('loglevel', 'silent') await team.exec(['ls', '@npmcli:developers']) t.same(result, '', 'should not output users if silent') }) @@ -345,7 +350,7 @@ t.test('team rm ', async t => { }) t.test('--silent', async t => { - npm.flatOptions.silent = true + npm.config.set('loglevel', 'silent') await team.exec(['rm', '@npmcli:newteam', 'foo']) t.same(result, '', 'should not output rm result if silent') })