Skip to content

Commit ed56f79

Browse files
committed
chore: make global install tests work during smoke publish
1 parent 7467ff6 commit ed56f79

File tree

2 files changed

+57
-53
lines changed

2 files changed

+57
-53
lines changed

smoke-tests/test/fixtures/setup.js

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ const WINDOWS = process.platform === 'win32'
2020
const GLOBAL_BIN = WINDOWS ? '' : 'bin'
2121
const GLOBAL_NODE_MODULES = join(WINDOWS ? '' : 'lib', 'node_modules')
2222

23+
const getOpts = (...a) => {
24+
const [opts, args] = a.reduce((acc, arg) => {
25+
acc[typeof arg === 'object' ? 0 : 1].push(arg)
26+
return acc
27+
}, [[], []])
28+
return [Object.assign({}, ...opts), args]
29+
}
30+
2331
const normalizePath = path => path.replace(/[A-Z]:/, '').replace(/\\/g, '/')
2432

2533
const testdirHelper = (obj) => {
@@ -169,14 +177,10 @@ module.exports = async (t, { testdir = {}, debug, registry: _registry = {} } = {
169177
return stdout
170178
}
171179

172-
const baseNpm = async (baseOpts, ...args) => {
173-
const hasMoreOpts = args[args.length - 1] && typeof args[args.length - 1] === 'object'
174-
const { cwd, cmd, argv = [], proxy = true, ...opts } = {
175-
...baseOpts,
176-
...hasMoreOpts ? args.pop() : {},
177-
}
180+
const baseNpm = async (...a) => {
181+
const [{ cwd, cmd, argv = [], proxy = true, ...opts }, args] = getOpts(...a)
178182

179-
const isGlobal = args.some(a => ['-g', '--global', '--global=true'].includes(a))
183+
const isGlobal = args.some(arg => ['-g', '--global', '--global=true'].includes(arg))
180184

181185
const defaultFlags = [
182186
proxy ? `--registry=${HTTP_PROXY}` : null,
@@ -206,29 +210,31 @@ module.exports = async (t, { testdir = {}, debug, registry: _registry = {} } = {
206210
})
207211
}
208212

209-
const npmLocal = (...args) => baseNpm({
210-
cwd: CLI_ROOT,
211-
cmd: process.execPath,
212-
argv: ['.'],
213-
proxy: false,
214-
}, ...args)
213+
const npmLocal = async (...args) => {
214+
const [{ force = false }] = getOpts(...args)
215+
if (SMOKE_PUBLISH_NPM && !force) {
216+
throw new Error('npmLocal cannot be called during smoke-publish')
217+
}
218+
return baseNpm({
219+
cwd: CLI_ROOT,
220+
cmd: process.execPath,
221+
argv: ['.'],
222+
proxy: false,
223+
}, ...args)
224+
}
215225

216-
const npmPath = (...args) => baseNpm({
226+
const npmPath = async (...args) => baseNpm({
217227
cwd: paths.project,
218228
cmd: 'npm',
219229
shell: true,
220230
}, ...args)
221231

222-
const npm = (...args) => baseNpm({
232+
const npm = async (...args) => baseNpm({
223233
cwd: paths.project,
224234
cmd: process.execPath,
225235
argv: [NPM_PATH],
226236
}, ...args)
227237

228-
const npmLocalError = async () => {
229-
throw new Error('npmLocal cannot be called during smoke-publish')
230-
}
231-
232238
// helpers for reading/writing files and their source
233239
const readFile = async (f) => {
234240
const file = await fs.readFile(join(paths.project, f), 'utf-8')
@@ -237,22 +243,15 @@ module.exports = async (t, { testdir = {}, debug, registry: _registry = {} } = {
237243

238244
return {
239245
npmPath,
240-
npmLocal: SMOKE_PUBLISH_NPM ? npmLocalError : npmLocal,
246+
npmLocal,
241247
npm: SMOKE_PUBLISH_NPM ? npmPath : npm,
242248
spawn: baseSpawn,
243249
readFile,
244250
getPath,
245251
paths,
246252
registry,
247-
npmLocalTarball: async () => {
248-
if (SMOKE_PUBLISH_TARBALL) {
249-
return SMOKE_PUBLISH_TARBALL
250-
}
251-
if (SMOKE_PUBLISH_NPM) {
252-
return await npmLocalError()
253-
}
254-
return await npmLocal('pack', `--pack-destination=${root}`).then(r => join(root, r))
255-
},
253+
npmLocalTarball: async () => SMOKE_PUBLISH_TARBALL ??
254+
npmLocal('pack', `--pack-destination=${root}`).then(r => join(root, r)),
256255
}
257256
}
258257

smoke-tests/test/npm-replace-global.js

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,18 @@ t.test('pack and replace global self', async t => {
9595
})
9696

9797
t.test('publish and replace global self', async t => {
98+
let publishedPackument = null
99+
const pkg = require('../../package.json')
100+
const { name, version } = pkg
101+
98102
const {
99103
npm,
100104
npmPath,
101105
registry,
102106
npmLocal,
103107
npmLocalTarball,
104108
getPaths,
105-
paths: { globalBin, globalNodeModules },
109+
paths: { globalBin, globalNodeModules, cache },
106110
} = await setupNpmGlobal(t, {
107111
testdir: {
108112
home: {
@@ -111,30 +115,39 @@ t.test('publish and replace global self', async t => {
111115
},
112116
})
113117

114-
const tarball = await npmLocalTarball()
118+
const npmPackage = async ({ manifest, ...opts } = {}) => {
119+
await registry.package({
120+
manifest: registry.manifest({ name, ...manifest }),
121+
...opts,
122+
})
123+
}
115124

116-
let publishedPackument = null
117-
const pkg = require('../../package.json')
118-
const { name, version } = pkg
125+
const npmInstall = async (useNpm) => {
126+
await npmPackage({
127+
manifest: { packuments: [publishedPackument] },
128+
tarballs: { [version]: tarball },
129+
times: 2,
130+
})
131+
await fs.rm(cache, { recursive: true, force: true })
132+
await useNpm('install', 'npm@latest', '--global')
133+
return getPaths()
134+
}
135+
136+
const tarball = await npmLocalTarball()
119137

138+
if (setup.SMOKE_PUBLISH) {
139+
await npmPackage()
140+
}
120141
registry.nock.put('/npm', body => {
121142
if (body._id === 'npm' && body.versions[version]) {
122143
publishedPackument = body.versions[version]
123144
return true
124145
}
125146
return false
126147
}).reply(201, {})
127-
await npmLocal('publish', { proxy: true })
128-
129-
await registry.package({
130-
manifest: registry.manifest({ name, packuments: [publishedPackument] }),
131-
tarballs: { [version]: tarball },
132-
times: 2,
133-
})
134-
135-
await npm('install', 'npm', '--global', '--prefer-online')
148+
await npmLocal('publish', { proxy: true, force: true })
136149

137-
const paths = await getPaths()
150+
const paths = await npmInstall(npm)
138151
t.equal(paths.npmRoot, join(globalNodeModules, 'npm'), 'npm root is in the testdir')
139152
t.equal(paths.pathNpm, join(globalBin, 'npm'), 'npm bin is in the testdir')
140153
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 => {
147160
'bin has npm and npx'
148161
)
149162

150-
await registry.package({
151-
manifest: registry.manifest({ name, packuments: [publishedPackument] }),
152-
tarballs: { [version]: tarball },
153-
times: 2,
154-
})
155-
156-
await npmPath('install', 'npm', '--global', '--prefer-online')
157-
158-
t.strictSame(await getPaths(), paths)
163+
t.strictSame(await npmInstall(npmPath), paths)
159164
})

0 commit comments

Comments
 (0)