Debian Codegraph @

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

raw
1
2 const t = require('tap')
3 const { join, dirname, basename, extname } = require('node:path')
4 const fs = require('node:fs/promises')
5 const _which = require('which')
6 const setup = require('./fixtures/setup.js')
7
8 const which = async (cmd, opts) => {
9 const path = await _which(cmd, { nothrow: true, ...opts })
10 return path ? join(dirname(path), basename(path, extname(path))) : null
11 }
12
13 const setupNpmGlobal = async (t, opts) => {
14 const mock = await setup(t, opts)
15
16 return {
17 ...mock,
18 getPaths: async () => {
19 const binContents = await fs.readdir(mock.paths.globalBin)
20 .then(r => r.filter(p => p !== '.npmrc' && p !== 'node_modules'))
21 .catch(() => null)
22
23 const nodeModulesContents = await fs.readdir(join(mock.paths.globalNodeModules, 'npm'))
24 .catch(() => null)
25
26 return {
27 npmRoot: await mock.npmPath('help').then(setup.getNpmRoot),
28 pathNpm: await which('npm', { path: mock.getPath() }),
29 globalNpm: await which('npm'),
30 pathNpx: await which('npx', { path: mock.getPath() }),
31 globalNpx: await which('npx'),
32 binContents,
33 nodeModulesContents,
34 }
35 },
36 }
37 }
38
39 t.test('pack and replace global self', async t => {
40 const {
41 npm,
42 npmLocalTarball,
43 npmPath,
44 getPaths,
45 paths: { globalBin, globalNodeModules },
46 } = await setupNpmGlobal(t, {
47 testdir: {
48 project: {
49 'package.json': { name: 'npm', version: '999.999.999' },
50 },
51 },
52 })
53
54 const tarball = await npmLocalTarball()
55 await npm('install', tarball, '--global')
56
57 t.equal(
58 await fs.realpath(join(globalBin, 'npm')),
59 setup.WINDOWS ? join(globalBin, 'npm') : join(globalNodeModules, 'npm/bin/npm-cli.js'),
60 'npm realpath is in the testdir'
61 )
62 t.equal(
63 await fs.realpath(join(globalBin, 'npx')),
64 setup.WINDOWS ? join(globalBin, 'npx') : join(globalNodeModules, 'npm/bin/npx-cli.js'),
65 'npx realpath is in the testdir'
66 )
67
68 const prePaths = await getPaths()
69 t.equal(prePaths.npmRoot, join(globalNodeModules, 'npm'), 'npm root is in the testdir')
70 t.equal(prePaths.pathNpm, join(globalBin, 'npm'), 'npm bin is in the testdir')
71 t.equal(prePaths.pathNpx, join(globalBin, 'npx'), 'npx bin is in the testdir')
72 t.not(prePaths.pathNpm, prePaths.globalNpm, 'npm bin is not the same as the global one')
73 t.not(prePaths.pathNpx, prePaths.globalNpx, 'npm bin is not the same as the global one')
74 t.ok(prePaths.nodeModulesContents.length > 1, 'node modules has npm contents')
75 t.ok(prePaths.nodeModulesContents.includes('node_modules'), 'npm has its node_modules')
76
77 t.strictSame(
78 prePaths.binContents,
79 ['npm', 'npx'].flatMap(p => setup.WINDOWS ? [p, `${p}.cmd`, `${p}.ps1`] : p),
80 'bin has npm and npx'
81 )
82
83 await npmPath('pack')
84 await npmPath('install', 'npm-999.999.999.tgz', '--global')
85
86 const postPaths = await getPaths()
87 t.not(prePaths.npmRoot, postPaths.npmRoot, 'npm roots are different')
88 t.equal(postPaths.pathNpm, postPaths.globalNpm, 'npm bin is the same as the global one')
89 t.equal(postPaths.pathNpx, postPaths.globalNpx, 'npx bin is the same as the global one')
90 t.equal(postPaths.pathNpm, prePaths.globalNpm, 'after install npm bin is same as previous global')
91 t.equal(postPaths.pathNpx, prePaths.globalNpx, 'after install npx bin is same as previous global')
92 t.strictSame(postPaths.binContents, [], 'bin is empty')
93 t.strictSame(postPaths.nodeModulesContents, ['package.json'], 'contents is only package.json')
94 })
95
96 t.test('publish and replace global self', async t => {
97 const {
98 npm,
99 npmPath,
100 registry,
101 npmLocal,
102 npmLocalTarball,
103 getPaths,
104 paths: { globalBin, globalNodeModules, cache },
105 } = await setupNpmGlobal(t, {
106 strictRegistryNock: false,
107 testdir: {
108 home: {
109 '.npmrc': `//${setup.MOCK_REGISTRY.host}/:_authToken = test-token`,
110 },
111 },
112 })
113
114 let publishedPackument = null
115 const { name, version } = require('../../package.json')
116
117 const npmPackage = async ({ manifest, ...opts } = {}) => {
118 await registry.package({
119 manifest: registry.manifest({ name, ...manifest }),
120 ...opts,
121 })
122 }
123
124 const npmInstall = async (useNpm) => {
125 await npmPackage({
126 manifest: { packuments: [publishedPackument] },
127 tarballs: { [version]: tarball },
128 times: 3,
129 })
130 await fs.rm(cache, { recursive: true, force: true })
131 await useNpm('install', 'npm@latest', '--global')
132 return getPaths()
133 }
134
135 const tarball = await npmLocalTarball()
136
137 if (setup.SMOKE_PUBLISH) {
138 await npmPackage()
139 }
140 registry.nock.put('/npm', body => {
141 if (body._id === 'npm' && body.versions[version]) {
142 publishedPackument = body.versions[version]
143 return true
144 }
145 return false
146 }).reply(201, {})
147 await npmLocal('publish', '--tag=smoke-test', { proxy: true, force: true })
148
149 t.comment(JSON.stringify(publishedPackument, null, 2))
150
151 const paths = await npmInstall(npm)
152 t.equal(paths.npmRoot, join(globalNodeModules, 'npm'), 'npm root is in the testdir')
153 t.equal(paths.pathNpm, join(globalBin, 'npm'), 'npm bin is in the testdir')
154 t.equal(paths.pathNpx, join(globalBin, 'npx'), 'npx bin is in the testdir')
155 t.ok(paths.nodeModulesContents.length > 1, 'node modules has npm contents')
156 t.ok(paths.nodeModulesContents.includes('node_modules'), 'npm has its node_modules')
157
158 t.strictSame(
159 paths.binContents,
160 ['npm', 'npx'].flatMap(p => setup.WINDOWS ? [p, `${p}.cmd`, `${p}.ps1`] : p),
161 'bin has npm and npx'
162 )
163
164 t.strictSame(await npmInstall(npmPath), paths)
165 })
166
167 t.test('fail when updating with lazy require', async t => {
168 const {
169 npm,
170 npmLocalTarball,
171 npmPath,
172 paths,
173 } = await setupNpmGlobal(t, {
174 testdir: {
175 project: {
176 'package.json': {
177 name: 'npm',
178 version: '999.999.999',
179 bin: {
180 npm: './my-new-npm-bin.js',
181 },
182 },
183 'my-new-npm-bin.js': `#!/usr/bin/env node\nconsole.log('This worked!')`,
184 },
185 },
186 })
187
188 const tarball = await npmLocalTarball()
189 await npm('install', tarball, '--global')
190 await npmPath('pack')
191
192 // exit-handler is the last thing called in the code
193 // so an uncached lazy require within the exit handler will always throw
194 await fs.writeFile(
195 join(paths.globalNodeModules, 'npm/lib/cli/exit-handler.js'),
196 `module.exports = class {
197 setNpm(){}
198 registerUncaughtHandlers(){}
199 exit() { require('./LAZY_REQUIRE_CANARY') }
200 }`,
201 'utf-8'
202 )
203
204 await t.rejects(npmPath('install', 'npm-999.999.999.tgz', '--global'), {
205 stderr: `Error: Cannot find module './LAZY_REQUIRE_CANARY'`,
206 }, 'install command fails with lazy require error')
207
208 await t.resolveMatch(npmPath(), { stdout: 'This worked!' }, 'bin placement still works')
209 })