diff --git a/tap-snapshots/test/lib/docs.js.test.cjs b/tap-snapshots/test/lib/docs.js.test.cjs index f9fb62271bc66..29d6b57fcb65a 100644 --- a/tap-snapshots/test/lib/docs.js.test.cjs +++ b/tap-snapshots/test/lib/docs.js.test.cjs @@ -24,7 +24,7 @@ All commands: Specify configs in the ini-formatted file: - /some/config/file/.npmrc + {USERCONFIG} or on the command line via: npm --key=value More configuration info: npm help config diff --git a/test/fixtures/mock-npm.js b/test/fixtures/mock-npm.js index 4b683e67edb5b..650123cbac366 100644 --- a/test/fixtures/mock-npm.js +++ b/test/fixtures/mock-npm.js @@ -212,7 +212,7 @@ const setupMockNpm = async (t, { return acc }, { argv: [...rawArgv], env: {}, config: {} }) - mockGlobals(t, { + const mockedGlobals = mockGlobals(t, { 'process.env.HOME': dirs.home, // global prefix cannot be (easily) set via argv so this is the easiest way // to set it that also closely mimics the behavior a user would see since it @@ -269,6 +269,7 @@ const setupMockNpm = async (t, { return { npm, + mockedGlobals, ...mockNpm, ...dirs, ...mockCommand, diff --git a/test/lib/commands/adduser.js b/test/lib/commands/adduser.js index 2478c69a032f2..e12af12f46388 100644 --- a/test/lib/commands/adduser.js +++ b/test/lib/commands/adduser.js @@ -8,6 +8,34 @@ const mockGlobals = require('@npmcli/mock-globals') const MockRegistry = require('@npmcli/mock-registry') const stream = require('stream') +const mockAddUser = async (t, { stdin: stdinLines, registry: registryUrl, ...options } = {}) => { + let stdin + if (stdinLines) { + stdin = new stream.PassThrough() + for (const l of stdinLines) { + stdin.write(l + '\n') + } + mockGlobals(t, { + 'process.stdin': stdin, + 'process.stdout': new stream.PassThrough(), // to quiet readline + }, { replace: true }) + } + const mock = await loadMockNpm(t, { + ...options, + command: 'adduser', + }) + const registry = new MockRegistry({ + tap: t, + registry: registryUrl ?? mock.npm.config.get('registry'), + }) + return { + registry, + stdin, + rc: () => ini.parse(fs.readFileSync(path.join(mock.home, '.npmrc'), 'utf8')), + ...mock, + } +} + t.test('usage', async t => { const { npm } = await loadMockNpm(t) const adduser = await npm.cmd('adduser') @@ -16,15 +44,8 @@ t.test('usage', async t => { t.test('legacy', async t => { t.test('simple adduser', async t => { - const stdin = new stream.PassThrough() - stdin.write('test-user\n') - stdin.write('test-password\n') - stdin.write('test-email@npmjs.org\n') - mockGlobals(t, { - 'process.stdin': stdin, - 'process.stdout': new stream.PassThrough(), // to quiet readline - }, { replace: true }) - const { npm, home } = await loadMockNpm(t, { + const { npm, rc, registry, adduser } = await mockAddUser(t, { + stdin: ['test-user', 'test-password', 'test-email@npmjs.org'], config: { 'auth-type': 'legacy' }, homeDir: { '.npmrc': [ @@ -34,71 +55,48 @@ t.test('legacy', async t => { ].join('\n'), }, }) - const registry = new MockRegistry({ - tap: t, - registry: npm.config.get('registry'), - }) registry.couchadduser({ username: 'test-user', password: 'test-password', email: 'test-email@npmjs.org', token: 'npm_test-token', }) - await npm.exec('adduser', []) + await adduser.exec([]) t.same(npm.config.get('email'), 'test-email-old@npmjs.org') t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') - const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8')) - t.same(rc, { + t.same(rc(), { '//registry.npmjs.org/:_authToken': 'npm_test-token', email: 'test-email-old@npmjs.org', }, 'should only have token and un-nerfed old email') }) t.test('scoped adduser', async t => { - const stdin = new stream.PassThrough() - stdin.write('test-user\n') - stdin.write('test-password\n') - stdin.write('test-email@npmjs.org\n') - mockGlobals(t, { - 'process.stdin': stdin, - 'process.stdout': new stream.PassThrough(), // to quiet readline - }, { replace: true }) - const { npm, home } = await loadMockNpm(t, { + const { npm, rc, registry, adduser } = await mockAddUser(t, { + stdin: ['test-user', 'test-password', 'test-email@npmjs.org'], config: { 'auth-type': 'legacy', scope: '@myscope', }, }) - const registry = new MockRegistry({ - tap: t, - registry: npm.config.get('registry'), - }) registry.couchadduser({ username: 'test-user', password: 'test-password', email: 'test-email@npmjs.org', token: 'npm_test-token', }) - await npm.exec('adduser', []) + await adduser.exec([]) t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') t.same(npm.config.get('@myscope:registry'), 'https://registry.npmjs.org/') - const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8')) - t.same(rc, { + t.same(rc(), { '//registry.npmjs.org/:_authToken': 'npm_test-token', '@myscope:registry': 'https://registry.npmjs.org/', }, 'should only have token and scope:registry') }) t.test('scoped adduser with valid scoped registry config', async t => { - const stdin = new stream.PassThrough() - stdin.write('test-user\n') - stdin.write('test-password\n') - stdin.write('test-email@npmjs.org\n') - mockGlobals(t, { - 'process.stdin': stdin, - 'process.stdout': new stream.PassThrough(), // to quiet readline - }, { replace: true }) - const { npm, home } = await loadMockNpm(t, { + const { npm, rc, registry, adduser } = await mockAddUser(t, { + stdin: ['test-user', 'test-password', 'test-email@npmjs.org'], + registry: 'https://diff-registry.npmjs.org', homeDir: { '.npmrc': '@myscope:registry=https://diff-registry.npmjs.org', }, @@ -107,106 +105,70 @@ t.test('legacy', async t => { scope: '@myscope', }, }) - const registry = new MockRegistry({ - tap: t, - registry: 'https://diff-registry.npmjs.org', - }) registry.couchadduser({ username: 'test-user', password: 'test-password', email: 'test-email@npmjs.org', token: 'npm_test-token', }) - await npm.exec('adduser', []) + await adduser.exec([]) t.same(npm.config.get('//diff-registry.npmjs.org/:_authToken'), 'npm_test-token') t.same(npm.config.get('@myscope:registry'), 'https://diff-registry.npmjs.org') - const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8')) - t.same(rc, { + t.same(rc(), { '@myscope:registry': 'https://diff-registry.npmjs.org', '//diff-registry.npmjs.org/:_authToken': 'npm_test-token', }, 'should only have token and scope:registry') }) t.test('save config failure', async t => { - const stdin = new stream.PassThrough() - stdin.write('test-user\n') - stdin.write('test-password\n') - stdin.write('test-email@npmjs.org\n') - mockGlobals(t, { - 'process.stdin': stdin, - 'process.stdout': new stream.PassThrough(), // to quiet readline - }, { replace: true }) - const { npm } = await loadMockNpm(t, { + const { registry, adduser } = await mockAddUser(t, { + stdin: ['test-user', 'test-password', 'test-email@npmjs.org'], config: { 'auth-type': 'legacy' }, homeDir: { '.npmrc': {}, }, }) - const registry = new MockRegistry({ - tap: t, - registry: npm.config.get('registry'), - }) registry.couchadduser({ username: 'test-user', password: 'test-password', email: 'test-email@npmjs.org', token: 'npm_test-token', }) - await t.rejects(npm.exec('adduser', [])) + await t.rejects(adduser.exec([])) }) t.end() }) t.test('web', t => { t.test('basic adduser', async t => { - const { npm, home } = await loadMockNpm(t, { + const { npm, rc, registry, adduser } = await mockAddUser(t, { config: { 'auth-type': 'web' }, }) - const registry = new MockRegistry({ - tap: t, - registry: npm.config.get('registry'), - }) registry.webadduser({ token: 'npm_test-token' }) - await npm.exec('adduser', []) + await adduser.exec([]) t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') - const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8')) - t.same(rc, { + t.same(rc(), { '//registry.npmjs.org/:_authToken': 'npm_test-token', }) }) t.test('server error', async t => { - const { npm } = await loadMockNpm(t, { + const { adduser, registry } = await mockAddUser(t, { config: { 'auth-type': 'web' }, }) - const registry = new MockRegistry({ - tap: t, - registry: npm.config.get('registry'), - }) registry.nock.post(registry.fullPath('/-/v1/login')) .reply(503, {}) await t.rejects( - npm.exec('adduser', []), + adduser.exec([]), { message: /503/ } ) }) t.test('fallback', async t => { - const stdin = new stream.PassThrough() - stdin.write('test-user\n') - stdin.write('test-password\n') - stdin.write('test-email@npmjs.org\n') - mockGlobals(t, { - 'process.stdin': stdin, - 'process.stdout': new stream.PassThrough(), // to quiet readline - }, { replace: true }) - const { npm } = await loadMockNpm(t, { + const { npm, registry, adduser } = await mockAddUser(t, { + stdin: ['test-user', 'test-password', 'test-email@npmjs.org'], config: { 'auth-type': 'web' }, }) - const registry = new MockRegistry({ - tap: t, - registry: npm.config.get('registry'), - }) registry.nock.post(registry.fullPath('/-/v1/login')) .reply(404, {}) registry.couchadduser({ @@ -215,7 +177,7 @@ t.test('web', t => { email: 'test-email@npmjs.org', token: 'npm_test-token', }) - await npm.exec('adduser', []) + await adduser.exec([]) t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') }) t.end() diff --git a/test/lib/commands/cache.js b/test/lib/commands/cache.js index 3a7438831009f..c3adef42d424c 100644 --- a/test/lib/commands/cache.js +++ b/test/lib/commands/cache.js @@ -1,7 +1,6 @@ const t = require('tap') const { load: loadMockNpm } = require('../../fixtures/mock-npm.js') const MockRegistry = require('@npmcli/mock-registry') -const mockGlobals = require('@npmcli/mock-globals') const cacache = require('cacache') const fs = require('fs') @@ -267,8 +266,9 @@ t.test('cache verify', async t => { }) t.test('cache verify as part of home', async t => { - const { npm, joinedOutput, prefix } = await loadMockNpm(t) - mockGlobals(t, { 'process.env.HOME': path.dirname(prefix) }) + const { npm, joinedOutput } = await loadMockNpm(t, { + globals: ({ prefix }) => ({ 'process.env.HOME': path.dirname(prefix) }), + }) await npm.exec('cache', ['verify']) t.match(joinedOutput(), 'Cache verified and compressed (~', 'contains ~ shorthand') }) diff --git a/test/lib/commands/completion.js b/test/lib/commands/completion.js index 3cd73edc5c5f9..93ea6210a89a5 100644 --- a/test/lib/commands/completion.js +++ b/test/lib/commands/completion.js @@ -1,29 +1,26 @@ const t = require('tap') const fs = require('fs') const path = require('path') +const { load: loadMockNpm } = require('../../fixtures/mock-npm') const completionScript = fs .readFileSync(path.resolve(__dirname, '../../../lib/utils/completion.sh'), { encoding: 'utf8' }) .replace(/^#!.*?\n/, '') -const { load: loadMockNpm } = require('../../fixtures/mock-npm') -const mockGlobals = require('@npmcli/mock-globals') - const loadMockCompletion = async (t, o = {}) => { const { globals = {}, windows, ...options } = o - let resetGlobals = {} - resetGlobals = mockGlobals(t, { - 'process.platform': windows ? 'win32' : 'posix', - 'process.env.term': 'notcygwin', - 'process.env.msystem': 'nogmingw', - ...globals, - }).reset const res = await loadMockNpm(t, { ...options, + globals: (dirs) => ({ + 'process.platform': windows ? 'win32' : 'posix', + 'process.env.term': 'notcygwin', + 'process.env.msystem': 'nogmingw', + ...(typeof globals === 'function' ? globals(dirs) : globals), + }), }) const completion = await res.npm.cmd('completion') return { - resetGlobals, + resetGlobals: res.mockedGlobals.reset, completion, ...res, } @@ -40,21 +37,26 @@ const loadMockCompletionComp = async (t, word, line) => t.test('completion', async t => { t.test('completion completion', async t => { - const { outputs, completion, prefix } = await loadMockCompletion(t, { + const { outputs, completion } = await loadMockCompletion(t, { prefixDir: { '.bashrc': 'aaa', '.zshrc': 'aaa', }, + globals: ({ prefix }) => ({ + 'process.env.HOME': prefix, + }), }) - mockGlobals(t, { 'process.env.HOME': prefix }) await completion.completion({ w: 2 }) t.matchSnapshot(outputs, 'both shells') }) t.test('completion completion no known shells', async t => { - const { outputs, completion, prefix } = await loadMockCompletion(t) - mockGlobals(t, { 'process.env.HOME': prefix }) + const { outputs, completion } = await loadMockCompletion(t, { + globals: ({ prefix }) => ({ + 'process.env.HOME': prefix, + }), + }) await completion.completion({ w: 2 }) t.matchSnapshot(outputs, 'no responses') diff --git a/test/lib/commands/get.js b/test/lib/commands/get.js index 597cccc3ff0ba..dec634f835172 100644 --- a/test/lib/commands/get.js +++ b/test/lib/commands/get.js @@ -2,10 +2,11 @@ const t = require('tap') const { load: loadMockNpm } = require('../../fixtures/mock-npm') t.test('should retrieve values from config', async t => { - const { joinedOutput, npm } = await loadMockNpm(t) const name = 'editor' const value = 'vigor' - npm.config.set(name, value) + const { joinedOutput, npm } = await loadMockNpm(t, { + config: { [name]: value }, + }) await npm.exec('get', [name]) t.equal( joinedOutput(), diff --git a/test/lib/commands/login.js b/test/lib/commands/login.js index 38e574937da64..3c6e33f503ea6 100644 --- a/test/lib/commands/login.js +++ b/test/lib/commands/login.js @@ -8,6 +8,34 @@ const mockGlobals = require('@npmcli/mock-globals') const MockRegistry = require('@npmcli/mock-registry') const stream = require('stream') +const mockLogin = async (t, { stdin: stdinLines, registry: registryUrl, ...options } = {}) => { + let stdin + if (stdinLines) { + stdin = new stream.PassThrough() + for (const l of stdinLines) { + stdin.write(l + '\n') + } + mockGlobals(t, { + 'process.stdin': stdin, + 'process.stdout': new stream.PassThrough(), // to quiet readline + }, { replace: true }) + } + const mock = await loadMockNpm(t, { + ...options, + command: 'login', + }) + const registry = new MockRegistry({ + tap: t, + registry: registryUrl ?? mock.npm.config.get('registry'), + }) + return { + registry, + stdin, + rc: () => ini.parse(fs.readFileSync(path.join(mock.home, '.npmrc'), 'utf8')), + ...mock, + } +} + t.test('usage', async t => { const { npm } = await loadMockNpm(t) const login = await npm.cmd('login') @@ -16,14 +44,8 @@ t.test('usage', async t => { t.test('legacy', t => { t.test('basic login', async t => { - const stdin = new stream.PassThrough() - stdin.write('test-user\n') - stdin.write('test-password\n') - mockGlobals(t, { - 'process.stdin': stdin, - 'process.stdout': new stream.PassThrough(), // to quiet readline - }, { replace: true }) - const { npm, home } = await loadMockNpm(t, { + const { npm, registry, login, rc } = await mockLogin(t, { + stdin: ['test-user', 'test-password'], config: { 'auth-type': 'legacy' }, homeDir: { '.npmrc': [ @@ -33,66 +55,45 @@ t.test('legacy', t => { ].join('\n'), }, }) - const registry = new MockRegistry({ - tap: t, - registry: npm.config.get('registry'), - }) registry.couchlogin({ username: 'test-user', password: 'test-password', token: 'npm_test-token', }) - await npm.exec('login', []) + await login.exec([]) t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') - const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8')) - t.same(rc, { + t.same(rc(), { '//registry.npmjs.org/:_authToken': 'npm_test-token', email: 'test-email-old@npmjs.org', }, 'should only have token and un-nerfed old email') }) t.test('scoped login default registry', async t => { - const stdin = new stream.PassThrough() - stdin.write('test-user\n') - stdin.write('test-password\n') - mockGlobals(t, { - 'process.stdin': stdin, - 'process.stdout': new stream.PassThrough(), // to quiet readline - }, { replace: true }) - const { npm, home } = await loadMockNpm(t, { + const { npm, registry, login, rc } = await mockLogin(t, { + stdin: ['test-user', 'test-password'], config: { 'auth-type': 'legacy', scope: '@npmcli', }, }) - const registry = new MockRegistry({ - tap: t, - registry: npm.config.get('registry'), - }) registry.couchlogin({ username: 'test-user', password: 'test-password', token: 'npm_test-token', }) - await npm.exec('login', []) + await login.exec([]) t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') t.same(npm.config.get('@npmcli:registry'), 'https://registry.npmjs.org/') - const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8')) - t.same(rc, { + t.same(rc(), { '//registry.npmjs.org/:_authToken': 'npm_test-token', '@npmcli:registry': 'https://registry.npmjs.org/', }, 'should only have token and scope:registry') }) t.test('scoped login scoped registry', async t => { - const stdin = new stream.PassThrough() - stdin.write('test-user\n') - stdin.write('test-password\n') - mockGlobals(t, { - 'process.stdin': stdin, - 'process.stdout': new stream.PassThrough(), // to quiet readline - }, { replace: true }) - const { npm, home } = await loadMockNpm(t, { + const { npm, registry, login, rc } = await mockLogin(t, { + stdin: ['test-user', 'test-password'], + registry: 'https://diff-registry.npmjs.org', config: { 'auth-type': 'legacy', scope: '@npmcli', @@ -101,20 +102,15 @@ t.test('legacy', t => { '.npmrc': '@npmcli:registry=https://diff-registry.npmjs.org', }, }) - const registry = new MockRegistry({ - tap: t, - registry: 'https://diff-registry.npmjs.org', - }) registry.couchlogin({ username: 'test-user', password: 'test-password', token: 'npm_test-token', }) - await npm.exec('login', []) + await login.exec([]) t.same(npm.config.get('//diff-registry.npmjs.org/:_authToken'), 'npm_test-token') t.same(npm.config.get('@npmcli:registry'), 'https://diff-registry.npmjs.org') - const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8')) - t.same(rc, { + t.same(rc(), { '@npmcli:registry': 'https://diff-registry.npmjs.org', '//diff-registry.npmjs.org/:_authToken': 'npm_test-token', }, 'should only have token and scope:registry') @@ -124,51 +120,32 @@ t.test('legacy', t => { t.test('web', t => { t.test('basic login', async t => { - const { npm, home } = await loadMockNpm(t, { + const { npm, registry, login, rc } = await mockLogin(t, { config: { 'auth-type': 'web' }, }) - const registry = new MockRegistry({ - tap: t, - registry: npm.config.get('registry'), - }) registry.weblogin({ token: 'npm_test-token' }) - await npm.exec('login', []) + await login.exec([]) t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') - const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8')) - t.same(rc, { + t.same(rc(), { '//registry.npmjs.org/:_authToken': 'npm_test-token', }) }) t.test('server error', async t => { - const { npm } = await loadMockNpm(t, { + const { registry, login } = await mockLogin(t, { config: { 'auth-type': 'web' }, }) - const registry = new MockRegistry({ - tap: t, - registry: npm.config.get('registry'), - }) registry.nock.post(registry.fullPath('/-/v1/login')) .reply(503, {}) await t.rejects( - npm.exec('login', []), + login.exec([]), { message: /503/ } ) }) t.test('fallback', async t => { - const stdin = new stream.PassThrough() - stdin.write('test-user\n') - stdin.write('test-password\n') - mockGlobals(t, { - 'process.stdin': stdin, - 'process.stdout': new stream.PassThrough(), // to quiet readline - }, { replace: true }) - const { npm } = await loadMockNpm(t, { + const { npm, registry, login } = await mockLogin(t, { + stdin: ['test-user', 'test-password'], config: { 'auth-type': 'web' }, }) - const registry = new MockRegistry({ - tap: t, - registry: npm.config.get('registry'), - }) registry.nock.post(registry.fullPath('/-/v1/login')) .reply(404, {}) registry.couchlogin({ @@ -176,7 +153,7 @@ t.test('web', t => { password: 'test-password', token: 'npm_test-token', }) - await npm.exec('login', []) + await login.exec([]) t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') }) t.end() diff --git a/test/lib/commands/pack.js b/test/lib/commands/pack.js index 3e7c0225c3068..61296cc93a53a 100644 --- a/test/lib/commands/pack.js +++ b/test/lib/commands/pack.js @@ -28,8 +28,8 @@ t.test('follows pack-destination config', async t => { }), 'tar-destination': {}, }, + config: ({ prefix }) => ({ 'pack-destination': path.join(prefix, 'tar-destination') }), }) - npm.config.set('pack-destination', path.join(npm.prefix, 'tar-destination')) await npm.exec('pack', []) const filename = 'test-package-1.0.0.tgz' t.strictSame(outputs, [[filename]]) @@ -59,8 +59,8 @@ t.test('should log output as valid json', async t => { version: '1.0.0', }), }, + config: { json: true }, }) - npm.config.set('json', true) await npm.exec('pack', []) const filename = 'test-package-1.0.0.tgz' t.matchSnapshot(outputs.map(JSON.parse), 'outputs as json') @@ -76,8 +76,8 @@ t.test('should log scoped package output as valid json', async t => { version: '1.0.0', }), }, + config: { json: true }, }) - npm.config.set('json', true) await npm.exec('pack', []) const filename = 'myscope-test-package-1.0.0.tgz' t.matchSnapshot(outputs.map(JSON.parse), 'outputs as json') @@ -93,8 +93,8 @@ t.test('dry run', async t => { version: '1.0.0', }), }, + config: { 'dry-run': true }, }) - npm.config.set('dry-run', true) await npm.exec('pack', []) const filename = 'test-package-1.0.0.tgz' t.strictSame(outputs, [[filename]]) diff --git a/test/lib/docs.js b/test/lib/docs.js index 2b342bf6c06b4..d74cdc5e47988 100644 --- a/test/lib/docs.js +++ b/test/lib/docs.js @@ -1,11 +1,10 @@ const t = require('tap') -const { join, resolve, basename, extname, dirname } = require('path') +const { join, resolve, basename, extname } = require('path') const fs = require('fs/promises') const localeCompare = require('@isaacs/string-locale-compare')('en') const docs = require('@npmcli/docs') const { load: loadMockNpm } = require('../fixtures/mock-npm.js') -const mockGlobals = require('@npmcli/mock-globals') const { definitions } = require('../../lib/utils/config/index.js') const cmdList = require('../../lib/utils/cmd-list.js') const pkg = require('../../package.json') @@ -31,21 +30,21 @@ t.test('config', async t => { }) t.test('basic usage', async t => { - mockGlobals(t, { process: { platform: 'posix' } }) - - t.cleanSnapshot = str => str - .split(dirname(dirname(__dirname))).join('{BASEDIR}') - .split(pkg.version).join('{VERSION}') - // snapshot basic usage without commands since all the command snapshots // are generated in the following test const { npm } = await loadMockNpm(t, { mocks: { '{LIB}/utils/cmd-list.js': { commands: [] }, }, + config: { userconfig: '/some/config/file/.npmrc' }, + globals: { process: { platform: 'posix' } }, }) - npm.config.set('userconfig', '/some/config/file/.npmrc') + t.cleanSnapshot = str => str + .replace(npm.npmRoot, '{BASEDIR}') + .replace(npm.config.get('userconfig'), '{USERCONFIG}') + .split(pkg.version).join('{VERSION}') + t.matchSnapshot(await npm.usage) }) diff --git a/test/lib/npm.js b/test/lib/npm.js index 9f9fbe001a310..2dc3899e0dbe0 100644 --- a/test/lib/npm.js +++ b/test/lib/npm.js @@ -659,8 +659,7 @@ t.test('implicit workspace accept', async t => { t.test('usage', async t => { t.test('with browser', async t => { - mockGlobals(t, { process: { platform: 'posix' } }) - const { npm } = await loadMockNpm(t) + const { npm } = await loadMockNpm(t, { globals: { process: { platform: 'posix' } } }) const usage = await npm.usage npm.config.set('viewer', 'browser') const browserUsage = await npm.usage @@ -669,8 +668,7 @@ t.test('usage', async t => { }) t.test('windows always uses browser', async t => { - mockGlobals(t, { process: { platform: 'win32' } }) - const { npm } = await loadMockNpm(t) + const { npm } = await loadMockNpm(t, { globals: { process: { platform: 'win32' } } }) const usage = await npm.usage npm.config.set('viewer', 'browser') const browserUsage = await npm.usage