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

chore: convert tests to use mock-npm options #6484

Merged
merged 1 commit into from May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion tap-snapshots/test/lib/docs.js.test.cjs
Expand Up @@ -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 <command> --key=value

More configuration info: npm help config
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/mock-npm.js
Expand Up @@ -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
Expand Down Expand Up @@ -269,6 +269,7 @@ const setupMockNpm = async (t, {

return {
npm,
mockedGlobals,
...mockNpm,
...dirs,
...mockCommand,
Expand Down
142 changes: 52 additions & 90 deletions test/lib/commands/adduser.js
Expand Up @@ -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')
Expand All @@ -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': [
Expand All @@ -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',
},
Expand All @@ -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({
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions 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')
Expand Down Expand Up @@ -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')
})
Expand Down
32 changes: 17 additions & 15 deletions 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,
}
Expand All @@ -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')
Expand Down
5 changes: 3 additions & 2 deletions test/lib/commands/get.js
Expand Up @@ -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(),
Expand Down