Skip to content

Commit

Permalink
chore: use mock-registry in cli tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Nov 9, 2022
1 parent f36df46 commit 6fffad7
Show file tree
Hide file tree
Showing 22 changed files with 59 additions and 62 deletions.
1 change: 1 addition & 0 deletions package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
"@npmcli/eslint-config": "^4.0.0",
"@npmcli/fs": "^3.1.0",
"@npmcli/git": "^4.0.1",
"@npmcli/mock-registry": "^1.0.0",
"@npmcli/promise-spawn": "^6.0.1",
"@npmcli/template-oss": "4.8.0",
"licensee": "^9.0.0",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
"@npmcli/eslint-config": "^4.0.0",
"@npmcli/fs": "^3.1.0",
"@npmcli/git": "^4.0.1",
"@npmcli/mock-registry": "^1.0.0",
"@npmcli/promise-spawn": "^6.0.1",
"@npmcli/template-oss": "4.8.0",
"licensee": "^9.0.0",
Expand Down
14 changes: 7 additions & 7 deletions smoke-tests/tap-snapshots/test/index.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ Object {
"version": "1.0.0",
},
"node_modules/abbrev": Object {
"resolved": "http://smoke-test-registry.club/abbrev/-/abbrev-1.0.4.tgz",
"resolved": "{REGISTRY}/abbrev/-/abbrev-1.0.4.tgz",
"version": "1.0.4",
},
"node_modules/promise-all-reject-late": Object {
"dev": true,
"funding": Object {
"url": "https://github.com/sponsors",
},
"resolved": "http://smoke-test-registry.club/promise-all-reject-late/-/promise-all-reject-late-5.0.0.tgz",
"resolved": "{REGISTRY}/promise-all-reject-late/-/promise-all-reject-late-5.0.0.tgz",
"version": "5.0.0",
},
},
Expand Down Expand Up @@ -203,7 +203,7 @@ Object {
"version": "1.0.0",
},
"node_modules/abbrev": Object {
"resolved": "http://smoke-test-registry.club/abbrev/-/abbrev-1.0.4.tgz",
"resolved": "{REGISTRY}/abbrev/-/abbrev-1.0.4.tgz",
"version": "1.0.4",
},
},
Expand Down Expand Up @@ -371,7 +371,7 @@ Object {
"version": "1.0.0",
},
"node_modules/abbrev": Object {
"resolved": "http://smoke-test-registry.club/abbrev/-/abbrev-1.1.1.tgz",
"resolved": "{REGISTRY}/abbrev/-/abbrev-1.1.1.tgz",
"version": "1.1.1",
},
},
Expand Down Expand Up @@ -420,15 +420,15 @@ Object {
"version": "1.0.0",
},
"node_modules/abbrev": Object {
"resolved": "http://smoke-test-registry.club/abbrev/-/abbrev-1.1.1.tgz",
"resolved": "{REGISTRY}/abbrev/-/abbrev-1.1.1.tgz",
"version": "1.1.1",
},
"node_modules/promise-all-reject-late": Object {
"dev": true,
"funding": Object {
"url": "https://github.com/sponsors",
},
"resolved": "http://smoke-test-registry.club/promise-all-reject-late/-/promise-all-reject-late-5.0.0.tgz",
"resolved": "{REGISTRY}/promise-all-reject-late/-/promise-all-reject-late-5.0.0.tgz",
"version": "5.0.0",
},
},
Expand Down Expand Up @@ -471,7 +471,7 @@ [email protected] | Proprietary | deps: none | versions: 2
mocked test package
dist
.tarball: http://smoke-test-registry.club/abbrev/-/abbrev-1.0.4.tgz
.tarball: {REGISTRY}/abbrev/-/abbrev-1.0.4.tgz
.shasum: undefined
dist-tags:
Expand Down
69 changes: 32 additions & 37 deletions smoke-tests/test/fixtures/setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs/promises')
const { existsSync } = require('fs')
const { join, resolve, sep, extname, relative, delimiter } = require('path')
const which = require('which')
const spawn = require('@npmcli/promise-spawn')
Expand All @@ -11,11 +12,11 @@ const httpProxy = require('http-proxy')

const { SMOKE_PUBLISH_NPM, CI, PATH, Path, TAP_CHILD_ID = '0' } = process.env
const PORT = 12345 + (+TAP_CHILD_ID)
const CLI_ROOT = resolve(process.cwd(), '..')

const set = (obj, ...args) => justSet(obj, ...args) && obj
const merge = (...args) => justExtend(true, ...args)
const normalizePath = path => path.replace(/[A-Z]:/, '').replace(/\\/g, '/')
const exists = (f) => fs.access(f, fs.constants.F_OK).catch(() => false)

const testdirHelper = (obj) => {
for (const [key, value] of Object.entries(obj)) {
Expand All @@ -30,29 +31,24 @@ const testdirHelper = (obj) => {
return obj
}

const getNpm = async () => {
const cliRoot = resolve(__dirname, '..', '..', '..')
const getSpawnArgs = async () => {
const cliBin = join('bin', 'npm-cli.js')

if (SMOKE_PUBLISH_NPM) {
return {
command: ['npm'],
cleanPaths: [
cliRoot,
await which('npm').then(p => fs.realpath(p).replace(sep + cliBin)),
],
NPM: await which('npm').then(p => fs.realpath(p).replace(sep + cliBin)),
}
}

return {
command: [process.execPath, join(cliRoot, cliBin)],
cleanPaths: [process.execPath, cliRoot],
command: [process.execPath, join(CLI_ROOT, cliBin)],
NODE: process.execPath,
NPM: join(CLI_ROOT, cliBin),
}
}

module.exports = async (t, { testdir = {}, debug } = {}) => {
const { command, cleanPaths } = await getNpm(t)

// setup fixtures
const root = t.testdir({
cache: {},
Expand All @@ -76,48 +72,49 @@ module.exports = async (t, { testdir = {}, debug } = {}) => {
registry: 'http://smoke-test-registry.club/',
debug,
})
const httpProxyRegistry = `http://localhost:${PORT}/`
const httpProxyRegistry = `http://localhost:${PORT}`
const proxy = httpProxy.createProxyServer({})
const server = http.createServer((req, res) => proxy.web(req, res, { target: registry.origin }))
await new Promise(res => server.listen(PORT, res))
t.teardown(() => server.close())

// update notifier should never be written
t.afterEach(async (t) => {
const updateExists = await exists(join(paths.cache, '_update-notifier-last-checked'))
t.equal(updateExists, false)
t.equal(existsSync(join(paths.cache, '_update-notifier-last-checked')), false)
// this requires that mocks not be shared between sub tests but it helps
// find mistakes quicker instead of waiting for the entire test to end
t.strictSame(registry.nock.pendingMocks(), [], 'no pending mocks after each')
t.strictSame(registry.nock.activeMocks(), [], 'no active mocks after each')
})

const { command, ...spawnPaths } = await getSpawnArgs()
const cleanPaths = Object.entries({ ...spawnPaths, CWD: CLI_ROOT })

const cleanOutput = s => {
// sometimes we print normalized paths in snapshots regardless of
// platform so replace those first then replace platform style paths
for (const cleanPath of cleanPaths) {
s = s
.split(normalizePath(cleanPath)).join('{CWD}')
.split(cleanPath).join('{CWD}')
.split(relative(cleanPath, t.testdirName)).join('{TESTDIR}')
for (const [key, value] of cleanPaths) {
s = s.split(normalizePath(value)).join(`{${key}}`)
}

return s.split(httpProxyRegistry).join('{REGISTRY}')
for (const [key, value] of cleanPaths) {
s = s.split(value).join(`{${key}}`)
}
return s
.split(relative(CLI_ROOT, t.testdirName)).join('{TESTDIR}')
.split(httpProxyRegistry).join('{REGISTRY}')
.split(registry.origin).join('{REGISTRY}')
.replace(/\\+/g, '/')
.replace(/\r\n/g, '\n')
.replace(/ \(in a browser\)/g, '')
.replace(/^npm@.* /gm, 'npm ')
.replace(/^.*debug-[0-9]+.log$/gm, '')
.replace(/in \d+ms$/gm, 'in {TIME}')
}
const log = debug || CI ? (...a) => console.error(cleanOutput(a.join(' '))) : () => {}
t.cleanSnapshot = cleanOutput
const log = !debug && !CI ? () => {} : (...args) => {
console.error(...args.map(a => cleanOutput(a.toString())))
}

const npm = async (...args) => {
const defaultCmd = []
const defaultOpts = [
const defaultFlags = [
`--registry=${httpProxyRegistry}`,
`--cache=${paths.cache}`,
`--prefix=${paths.project}`,
Expand All @@ -126,24 +123,21 @@ module.exports = async (t, { testdir = {}, debug } = {}) => {
'--no-audit',
'--no-update-notifier',
'--loglevel=silly',
'--fetch-timeout=5000',
'--fetch-retries=0',
]

const [cmd, opts] = args.reduce((acc, arg) => {
if (arg.startsWith('--')) {
const [positionals, flags] = args.reduce((acc, arg) => {
if (arg.startsWith('-')) {
acc[1].push(arg)
} else {
acc[0].push(arg)
}
return acc
}, [defaultCmd, defaultOpts])
}, [[], defaultFlags])

const spawnCmd = command[0]
const spawnArgs = [...command.slice(1), ...cmd, ...opts]
const spawnArgs = [...command.slice(1), ...positionals, ...flags]

log('='.repeat(40))
log(`\n${spawnCmd} ${spawnArgs.join(' ')}`)
log(`${spawnCmd} ${spawnArgs.filter(a => !defaultFlags.includes(a)).join(' ')}`)
log('-'.repeat(40))

const { stderr, stdout } = await spawn(spawnCmd, spawnArgs, {
cwd: paths.project,
Expand All @@ -155,8 +149,9 @@ module.exports = async (t, { testdir = {}, debug } = {}) => {
},
})

log('\n' + stderr)
log('\n' + stdout)
log(stderr)
log('-'.repeat(40))
log(stdout)
log('='.repeat(40))

return cleanOutput(stdout)
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/access.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const t = require('tap')

const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')

const token = 'test-auth-token'
const auth = { '//registry.npmjs.org/:_authToken': 'test-auth-token' }
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/adduser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const ini = require('ini')

const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
const mockGlobals = require('../../fixtures/mock-globals.js')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')
const stream = require('stream')

t.test('usage', async t => {
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require('path')
const t = require('tap')

const { load: loadMockNpm } = require('../../fixtures/mock-npm')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')

const gunzip = zlib.gunzipSync
const gzip = zlib.gzipSync
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const t = require('tap')
const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')
const mockGlobals = require('../../fixtures/mock-globals')

const cacache = require('cacache')
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/ci.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const t = require('tap')
const { load: _loadMockNpm } = require('../../fixtures/mock-npm')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')

const path = require('path')
const fs = require('fs')
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/dedupe.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path')
const fs = require('fs')

const { load: loadMockNpm } = require('../../fixtures/mock-npm')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')

t.test('should throw in global mode', async (t) => {
const { npm } = await loadMockNpm(t, {
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/deprecate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const t = require('tap')
const { load: loadMockNpm } = require('../../fixtures/mock-npm')

const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')

const user = 'test-user'
const token = 'test-auth-token'
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const t = require('tap')
const fs = require('fs/promises')
const path = require('path')
const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')

t.test('call with args', async t => {
const { npm } = await loadMockNpm(t, {
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/find-dupes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path')
const fs = require('fs')

const { load: loadMockNpm } = require('../../fixtures/mock-npm')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')

const treeWithDupes = {
'package.json': JSON.stringify({
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const ini = require('ini')

const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
const mockGlobals = require('../../fixtures/mock-globals.js')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')
const stream = require('stream')

t.test('usage', async t => {
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/owner.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const t = require('tap')
const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')

const path = require('path')
const npa = require('npm-package-arg')
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/ping.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const t = require('tap')
const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')

t.test('no details', async t => {
const { npm, logs, joinedOutput } = await loadMockNpm(t)
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/publish.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const t = require('tap')
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')
const pacote = require('pacote')
const Arborist = require('@npmcli/arborist')
const path = require('path')
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/search.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const t = require('tap')
const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')
const libnpmsearchResultFixture =
require('../../fixtures/libnpmsearch-stream-result.js')

Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/star.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const t = require('tap')
const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')

const pkgName = '@npmcli/test-package'
const authToken = 'test-auth-token'
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/unpublish.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const t = require('tap')
const { load: loadMockNpm } = require('../../fixtures/mock-npm')

const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')
const user = 'test-user'
const pkg = 'test-package'
const auth = { '//registry.npmjs.org/:_authToken': 'test-auth-token' }
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/unstar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const t = require('tap')
const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')

const pkgName = '@npmcli/test-package'
const authToken = 'test-auth-token'
Expand Down
2 changes: 1 addition & 1 deletion test/lib/commands/whoami.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const t = require('tap')
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
const MockRegistry = require('../../fixtures/mock-registry.js')
const MockRegistry = require('@npmcli/mock-registry')

const username = 'foo'
const auth = { '//registry.npmjs.org/:_authToken': 'test-auth-token' }
Expand Down

0 comments on commit 6fffad7

Please sign in to comment.