From 712b82b48434e7ceb12a7480a7dcf6427908534b Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Tue, 6 Jun 2023 12:42:39 -0700 Subject: [PATCH] chore: make global install tests work during smoke publish --- smoke-tests/test/fixtures/setup.js | 57 +++++++++++++------------- smoke-tests/test/npm-replace-global.js | 53 +++++++++++++----------- 2 files changed, 57 insertions(+), 53 deletions(-) diff --git a/smoke-tests/test/fixtures/setup.js b/smoke-tests/test/fixtures/setup.js index fc54667eb7d77..89a036cb39aeb 100644 --- a/smoke-tests/test/fixtures/setup.js +++ b/smoke-tests/test/fixtures/setup.js @@ -20,6 +20,14 @@ const WINDOWS = process.platform === 'win32' const GLOBAL_BIN = WINDOWS ? '' : 'bin' const GLOBAL_NODE_MODULES = join(WINDOWS ? '' : 'lib', 'node_modules') +const getOpts = (...a) => { + const [opts, args] = a.reduce((acc, arg) => { + acc[typeof arg === 'object' ? 0 : 1].push(arg) + return acc + }, [[], []]) + return [Object.assign({}, ...opts), args] +} + const normalizePath = path => path.replace(/[A-Z]:/, '').replace(/\\/g, '/') const testdirHelper = (obj) => { @@ -169,14 +177,10 @@ module.exports = async (t, { testdir = {}, debug, registry: _registry = {} } = { return stdout } - const baseNpm = async (baseOpts, ...args) => { - const hasMoreOpts = args[args.length - 1] && typeof args[args.length - 1] === 'object' - const { cwd, cmd, argv = [], proxy = true, ...opts } = { - ...baseOpts, - ...hasMoreOpts ? args.pop() : {}, - } + const baseNpm = async (...a) => { + const [{ cwd, cmd, argv = [], proxy = true, ...opts }, args] = getOpts(...a) - const isGlobal = args.some(a => ['-g', '--global', '--global=true'].includes(a)) + const isGlobal = args.some(arg => ['-g', '--global', '--global=true'].includes(arg)) const defaultFlags = [ proxy ? `--registry=${HTTP_PROXY}` : null, @@ -206,29 +210,31 @@ module.exports = async (t, { testdir = {}, debug, registry: _registry = {} } = { }) } - const npmLocal = (...args) => baseNpm({ - cwd: CLI_ROOT, - cmd: process.execPath, - argv: ['.'], - proxy: false, - }, ...args) + const npmLocal = async (...args) => { + const [{ force = false }] = getOpts(...args) + if (SMOKE_PUBLISH_NPM && !force) { + throw new Error('npmLocal cannot be called during smoke-publish') + } + return baseNpm({ + cwd: CLI_ROOT, + cmd: process.execPath, + argv: ['.'], + proxy: false, + }, ...args) + } - const npmPath = (...args) => baseNpm({ + const npmPath = async (...args) => baseNpm({ cwd: paths.project, cmd: 'npm', shell: true, }, ...args) - const npm = (...args) => baseNpm({ + const npm = async (...args) => baseNpm({ cwd: paths.project, cmd: process.execPath, argv: [NPM_PATH], }, ...args) - const npmLocalError = async () => { - throw new Error('npmLocal cannot be called during smoke-publish') - } - // helpers for reading/writing files and their source const readFile = async (f) => { const file = await fs.readFile(join(paths.project, f), 'utf-8') @@ -237,22 +243,15 @@ module.exports = async (t, { testdir = {}, debug, registry: _registry = {} } = { return { npmPath, - npmLocal: SMOKE_PUBLISH_NPM ? npmLocalError : npmLocal, + npmLocal, npm: SMOKE_PUBLISH_NPM ? npmPath : npm, spawn: baseSpawn, readFile, getPath, paths, registry, - npmLocalTarball: async () => { - if (SMOKE_PUBLISH_TARBALL) { - return SMOKE_PUBLISH_TARBALL - } - if (SMOKE_PUBLISH_NPM) { - return await npmLocalError() - } - return await npmLocal('pack', `--pack-destination=${root}`).then(r => join(root, r)) - }, + npmLocalTarball: async () => SMOKE_PUBLISH_TARBALL ?? + npmLocal('pack', `--pack-destination=${root}`).then(r => join(root, r)), } } diff --git a/smoke-tests/test/npm-replace-global.js b/smoke-tests/test/npm-replace-global.js index a5303e48fca83..4e6b78b881312 100644 --- a/smoke-tests/test/npm-replace-global.js +++ b/smoke-tests/test/npm-replace-global.js @@ -95,6 +95,10 @@ t.test('pack and replace global self', async t => { }) t.test('publish and replace global self', async t => { + let publishedPackument = null + const pkg = require('../../package.json') + const { name, version } = pkg + const { npm, npmPath, @@ -102,7 +106,7 @@ t.test('publish and replace global self', async t => { npmLocal, npmLocalTarball, getPaths, - paths: { globalBin, globalNodeModules }, + paths: { globalBin, globalNodeModules, cache }, } = await setupNpmGlobal(t, { testdir: { home: { @@ -111,12 +115,29 @@ t.test('publish and replace global self', async t => { }, }) - const tarball = await npmLocalTarball() + const npmPackage = async ({ manifest, ...opts } = {}) => { + await registry.package({ + manifest: registry.manifest({ name, ...manifest }), + ...opts, + }) + } - let publishedPackument = null - const pkg = require('../../package.json') - const { name, version } = pkg + const npmInstall = async (useNpm) => { + await npmPackage({ + manifest: { packuments: [publishedPackument] }, + tarballs: { [version]: tarball }, + times: 2, + }) + await fs.rm(cache, { recursive: true, force: true }) + await useNpm('install', 'npm@latest', '--global') + return getPaths() + } + + const tarball = await npmLocalTarball() + if (setup.SMOKE_PUBLISH) { + await npmPackage() + } registry.nock.put('/npm', body => { if (body._id === 'npm' && body.versions[version]) { publishedPackument = body.versions[version] @@ -124,17 +145,9 @@ t.test('publish and replace global self', async t => { } return false }).reply(201, {}) - await npmLocal('publish', { proxy: true }) - - await registry.package({ - manifest: registry.manifest({ name, packuments: [publishedPackument] }), - tarballs: { [version]: tarball }, - times: 2, - }) - - await npm('install', 'npm', '--global', '--prefer-online') + await npmLocal('publish', { proxy: true, force: true }) - const paths = await getPaths() + const paths = await npmInstall(npm) t.equal(paths.npmRoot, join(globalNodeModules, 'npm'), 'npm root is in the testdir') t.equal(paths.pathNpm, join(globalBin, 'npm'), 'npm bin is in the testdir') t.equal(paths.pathNpx, join(globalBin, 'npx'), 'npx bin is in the testdir') @@ -147,13 +160,5 @@ t.test('publish and replace global self', async t => { 'bin has npm and npx' ) - await registry.package({ - manifest: registry.manifest({ name, packuments: [publishedPackument] }), - tarballs: { [version]: tarball }, - times: 2, - }) - - await npmPath('install', 'npm', '--global', '--prefer-online') - - t.strictSame(await getPaths(), paths) + t.strictSame(await npmInstall(npmPath), paths) })