Skip to content

Commit

Permalink
chore: make global install tests work during smoke publish
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Jun 6, 2023
1 parent 7467ff6 commit 712b82b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 53 deletions.
57 changes: 28 additions & 29 deletions smoke-tests/test/fixtures/setup.js
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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')
Expand All @@ -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)),
}
}

Expand Down
53 changes: 29 additions & 24 deletions smoke-tests/test/npm-replace-global.js
Expand Up @@ -95,14 +95,18 @@ 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,
registry,
npmLocal,
npmLocalTarball,
getPaths,
paths: { globalBin, globalNodeModules },
paths: { globalBin, globalNodeModules, cache },
} = await setupNpmGlobal(t, {
testdir: {
home: {
Expand All @@ -111,30 +115,39 @@ 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]
return true
}
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')
Expand All @@ -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)
})

0 comments on commit 712b82b

Please sign in to comment.