/
npm-replace-global.js
158 lines (135 loc) · 5.3 KB
/
npm-replace-global.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const t = require('tap')
const { join, dirname, basename, extname } = require('path')
const fs = require('fs/promises')
const _which = require('which')
const setup = require('./fixtures/setup.js')
const which = async (cmd, opts) => {
const path = await _which(cmd, { nothrow: true, ...opts })
return path ? join(dirname(path), basename(path, extname(path))) : null
}
const setupNpmGlobal = async (t, opts) => {
const mock = await setup(t, opts)
return {
...mock,
getPaths: async () => {
const binContents = await fs.readdir(mock.paths.globalBin)
.then(r => r.filter(p => p !== '.npmrc' && p !== 'node_modules'))
.catch(() => null)
const nodeModulesContents = await fs.readdir(join(mock.paths.globalNodeModules, 'npm'))
.catch(() => null)
return {
npmRoot: await mock.npmPath('help').then(setup.getNpmRoot),
pathNpm: await which('npm', { path: mock.getPath(), nothrow: true }),
globalNpm: await which('npm', { nothrow: true }),
pathNpx: await which('npx', { path: mock.getPath(), nothrow: true }),
globalNpx: await which('npx', { nothrow: true }),
binContents,
nodeModulesContents,
}
},
}
}
t.test('pack and replace global self', async t => {
const {
npm,
npmLocalTarball,
npmPath,
getPaths,
paths: { globalBin, globalNodeModules },
} = await setupNpmGlobal(t, {
testdir: {
project: {
'package.json': { name: 'npm', version: '999.999.999' },
},
},
})
const tarball = await npmLocalTarball()
await npm('install', tarball, '--global')
t.equal(
await fs.realpath(join(globalBin, 'npm')),
setup.WINDOWS ? join(globalBin, 'npm') : join(globalNodeModules, 'npm/bin/npm-cli.js'),
'npm realpath is in the testdir'
)
t.equal(
await fs.realpath(join(globalBin, 'npx')),
setup.WINDOWS ? join(globalBin, 'npx') : join(globalNodeModules, 'npm/bin/npx-cli.js'),
'npx realpath is in the testdir'
)
const prePaths = await getPaths()
t.equal(prePaths.npmRoot, join(globalNodeModules, 'npm'), 'npm root is in the testdir')
t.equal(prePaths.pathNpm, join(globalBin, 'npm'), 'npm bin is in the testdir')
t.equal(prePaths.pathNpx, join(globalBin, 'npx'), 'npx bin is in the testdir')
t.not(prePaths.pathNpm, prePaths.globalNpm, 'npm bin is not the same as the global one')
t.not(prePaths.pathNpx, prePaths.globalNpx, 'npm bin is not the same as the global one')
t.ok(prePaths.nodeModulesContents.length > 1, 'node modules has npm contents')
t.ok(prePaths.nodeModulesContents.includes('node_modules'), 'npm has its node_modules')
t.strictSame(
prePaths.binContents,
['npm', 'npx'].flatMap(p => setup.WINDOWS ? [p, `${p}.cmd`, `${p}.ps1`] : p),
'bin has npm and npx'
)
await npmPath('pack')
await npmPath('install', 'npm-999.999.999.tgz', '--global')
const postPaths = await getPaths()
t.not(prePaths.npmRoot, postPaths.npmRoot, 'npm roots are different')
t.equal(postPaths.pathNpm, postPaths.globalNpm, 'npm bin is the same as the global one')
t.equal(postPaths.pathNpx, postPaths.globalNpx, 'npx bin is the same as the global one')
t.equal(postPaths.pathNpm, prePaths.globalNpm, 'after install npm bin is same as previous global')
t.equal(postPaths.pathNpx, prePaths.globalNpx, 'after install npx bin is same as previous global')
t.strictSame(postPaths.binContents, [], 'bin is empty')
t.strictSame(postPaths.nodeModulesContents, ['package.json'], 'contents is only package.json')
})
t.test('publish and replace global self', async t => {
let publishedPackument = null
const {
npm,
npmPath,
registry,
npmLocal,
npmLocalTarball,
getPaths,
paths: { globalBin, globalNodeModules, cache },
} = await setupNpmGlobal(t, {
testdir: {
home: {
'.npmrc': `${setup.HTTP_PROXY.slice(5)}:_authToken = test-token`,
},
},
})
const npmInstall = async (useNpm) => {
await registry.package({
manifest: registry.manifest({ name, 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()
const pkg = require('../../package.json')
const { name, version } = pkg
registry.nock.put('/npm', body => {
if (body._id === 'npm' && body.versions[version]) {
publishedPackument = body.versions[version]
return true
}
return false
}).reply(201, {})
if (setup.SMOKE_PUBLISH) {
await registry.package({ manifest: registry.manifest({ name: 'npm' }) })
}
await npmLocal('publish', { proxy: true, force: true })
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')
t.ok(paths.nodeModulesContents.length > 1, 'node modules has npm contents')
t.ok(paths.nodeModulesContents.includes('node_modules'), 'npm has its node_modules')
t.strictSame(
paths.binContents,
['npm', 'npx'].flatMap(p => setup.WINDOWS ? [p, `${p}.cmd`, `${p}.ps1`] : p),
'bin has npm and npx'
)
t.strictSame(await npmInstall(npmPath), paths)
})