/
local.ts
291 lines (226 loc) · 8.95 KB
/
local.ts
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import { promises as fs } from 'fs'
import path from 'path'
import { LOCKFILE_VERSION } from '@pnpm/constants'
import { Lockfile } from '@pnpm/lockfile-file'
import { prepareEmpty, preparePackages } from '@pnpm/prepare'
import { addDistTag } from '@pnpm/registry-mock'
import fixtures from '@pnpm/test-fixtures'
import {
addDependenciesToPackage,
install,
mutateModules,
} from '@pnpm/core'
import rimraf from '@zkochan/rimraf'
import normalizePath from 'normalize-path'
import readYamlFile from 'read-yaml-file'
import symlinkDir from 'symlink-dir'
import { testDefaults } from '../utils'
const f = fixtures(__dirname)
test('scoped modules from a directory', async () => {
const project = prepareEmpty()
await addDependenciesToPackage({}, [`file:${f.find('local-scoped-pkg')}`], await testDefaults())
const m = project.requireModule('@scope/local-scoped-pkg')
expect(m()).toBe('@scope/local-scoped-pkg')
})
test('local file', async () => {
const project = prepareEmpty()
f.copy('local-pkg', path.resolve('..', 'local-pkg'))
const manifest = await addDependenciesToPackage({}, ['file:../local-pkg'], await testDefaults())
const expectedSpecs = { 'local-pkg': `link:..${path.sep}local-pkg` }
expect(manifest.dependencies).toStrictEqual(expectedSpecs)
const m = project.requireModule('local-pkg')
expect(m).toBeTruthy()
const lockfile = await project.readLockfile()
expect(lockfile).toStrictEqual({
dependencies: {
'local-pkg': 'link:../local-pkg',
},
lockfileVersion: LOCKFILE_VERSION,
specifiers: expectedSpecs,
})
})
test('local directory with no package.json', async () => {
const project = prepareEmpty()
await fs.mkdir('pkg')
await fs.writeFile('pkg/index.js', 'hello', 'utf8')
const manifest = await addDependenciesToPackage({}, ['file:./pkg'], await testDefaults())
const expectedSpecs = { pkg: 'link:pkg' }
expect(manifest.dependencies).toStrictEqual(expectedSpecs)
await project.has('pkg')
await rimraf('node_modules')
await install(manifest, await testDefaults({ frozenLockfile: true }))
await project.has('pkg')
})
test('local file via link:', async () => {
const project = prepareEmpty()
f.copy('local-pkg', path.resolve('..', 'local-pkg'))
const manifest = await addDependenciesToPackage({}, ['link:../local-pkg'], await testDefaults())
const expectedSpecs = { 'local-pkg': `link:..${path.sep}local-pkg` }
expect(manifest.dependencies).toStrictEqual(expectedSpecs)
const m = project.requireModule('local-pkg')
expect(m).toBeTruthy()
const lockfile = await project.readLockfile()
expect(lockfile).toStrictEqual({
dependencies: {
'local-pkg': 'link:../local-pkg',
},
lockfileVersion: LOCKFILE_VERSION,
specifiers: expectedSpecs,
})
})
test('local file with symlinked node_modules', async () => {
const project = prepareEmpty()
f.copy('local-pkg', path.resolve('..', 'local-pkg'))
await fs.mkdir(path.join('..', 'node_modules'))
await symlinkDir(path.join('..', 'node_modules'), 'node_modules')
const manifest = await addDependenciesToPackage({}, ['file:../local-pkg'], await testDefaults())
const expectedSpecs = { 'local-pkg': `link:..${path.sep}local-pkg` }
expect(manifest.dependencies).toStrictEqual(expectedSpecs)
const m = project.requireModule('local-pkg')
expect(m).toBeTruthy()
const lockfile = await project.readLockfile()
expect(lockfile).toStrictEqual({
dependencies: {
'local-pkg': 'link:../local-pkg',
},
lockfileVersion: LOCKFILE_VERSION,
specifiers: expectedSpecs,
})
})
test('package with a broken symlink', async () => {
const project = prepareEmpty()
await addDependenciesToPackage({}, [f.find('has-broken-symlink/has-broken-symlink.tar.gz')], await testDefaults({ fastUnpack: false }))
const m = project.requireModule('has-broken-symlink')
expect(m).toBeTruthy()
})
test('tarball local package', async () => {
const project = prepareEmpty()
const manifest = await addDependenciesToPackage({}, [f.find('tar-pkg/tar-pkg-1.0.0.tgz')], await testDefaults({ fastUnpack: false }))
const m = project.requireModule('tar-pkg')
expect(m()).toBe('tar-pkg')
const pkgSpec = `file:${normalizePath(f.find('tar-pkg/tar-pkg-1.0.0.tgz'))}`
expect(manifest.dependencies).toStrictEqual({ 'tar-pkg': pkgSpec })
const lockfile = await project.readLockfile()
expect(lockfile.packages[lockfile.dependencies['tar-pkg']]).toStrictEqual({
dev: false,
name: 'tar-pkg',
resolution: {
integrity: 'sha512-HP/5Rgt3pVFLzjmN9qJJ6vZMgCwoCIl/m2bPndYT283CUqnmFiMx0GeeIJ7SyK6TYoJM78SEvFEOQie++caHqw==',
tarball: `file:${normalizePath(path.relative(process.cwd(), f.find('tar-pkg/tar-pkg-1.0.0.tgz')))}`,
},
version: '1.0.0',
})
})
test('tarball local package from project directory', async () => {
const project = prepareEmpty()
f.copy('tar-pkg/tar-pkg-1.0.0.tgz', path.resolve('tar-pkg-1.0.0.tgz'))
const manifest = await install({
dependencies: {
'tar-pkg': 'file:tar-pkg-1.0.0.tgz',
},
}, await testDefaults({ fastUnpack: false }))
const m = project.requireModule('tar-pkg')
expect(m()).toBe('tar-pkg')
const pkgSpec = 'file:tar-pkg-1.0.0.tgz'
expect(manifest.dependencies).toStrictEqual({ 'tar-pkg': pkgSpec })
const lockfile = await project.readLockfile()
expect(lockfile.dependencies['tar-pkg']).toBe(pkgSpec)
expect(lockfile.packages[lockfile.dependencies['tar-pkg']]).toStrictEqual({
dev: false,
name: 'tar-pkg',
resolution: {
integrity: 'sha512-HP/5Rgt3pVFLzjmN9qJJ6vZMgCwoCIl/m2bPndYT283CUqnmFiMx0GeeIJ7SyK6TYoJM78SEvFEOQie++caHqw==',
tarball: pkgSpec,
},
version: '1.0.0',
})
})
test('update tarball local package when its integrity changes', async () => {
const project = prepareEmpty()
f.copy('tar-pkg-with-dep-1/tar-pkg-with-dep-1.0.0.tgz', path.resolve('..', 'tar.tgz'))
const manifest = await addDependenciesToPackage({}, ['../tar.tgz'], await testDefaults())
const lockfile1 = await project.readLockfile()
expect(lockfile1.packages['file:../tar.tgz'].dependencies!['is-positive']).toBe('1.0.0')
f.copy('tar-pkg-with-dep-2/tar-pkg-with-dep-1.0.0.tgz', path.resolve('..', 'tar.tgz'))
await install(manifest, await testDefaults())
const lockfile2 = await project.readLockfile()
expect(lockfile2.packages['file:../tar.tgz'].dependencies!['is-positive']).toBe('2.0.0')
const manifestOfTarballDep = await import(path.resolve('node_modules/tar-pkg-with-dep/package.json'))
expect(manifestOfTarballDep.dependencies['is-positive']).toBe('^2.0.0')
})
// Covers https://github.com/pnpm/pnpm/issues/1878
test('do not update deps when installing in a project that has local tarball dep', async () => {
await addDistTag({ package: 'peer-a', version: '1.0.0', distTag: 'latest' })
const project = prepareEmpty()
f.copy('tar-pkg-with-dep-1/tar-pkg-with-dep-1.0.0.tgz', path.resolve('..', 'tar.tgz'))
const manifest = await addDependenciesToPackage({}, ['../tar.tgz', 'peer-a'], await testDefaults({ lockfileOnly: true }))
const initialLockfile = await project.readLockfile()
await addDistTag({ package: 'peer-a', version: '1.0.1', distTag: 'latest' })
await mutateModules([
{
buildIndex: 0,
manifest,
mutation: 'install',
rootDir: process.cwd(),
},
], await testDefaults())
const latestLockfile = await project.readLockfile()
expect(initialLockfile).toStrictEqual(latestLockfile)
})
// Covers https://github.com/pnpm/pnpm/issues/1882
test('frozen-lockfile: installation fails if the integrity of a tarball dependency changed', async () => {
prepareEmpty()
f.copy('tar-pkg-with-dep-1/tar-pkg-with-dep-1.0.0.tgz', path.resolve('..', 'tar.tgz'))
const manifest = await addDependenciesToPackage({}, ['../tar.tgz'], await testDefaults())
await rimraf('node_modules')
f.copy('tar-pkg-with-dep-2/tar-pkg-with-dep-1.0.0.tgz', path.resolve('..', 'tar.tgz'))
await expect(
install(manifest, await testDefaults({ frozenLockfile: true }))
).rejects.toThrow(/Got unexpected checksum/)
})
test('deep local', async () => {
const manifest1 = {
name: 'project-1',
version: '1.0.0',
dependencies: {
'project-2': 'file:../project-2',
},
dependenciesMeta: {
'project-2': {
injected: true,
},
},
}
preparePackages([
{
location: 'project-1',
package: manifest1,
},
{
location: 'project-2',
package: {
name: 'project-2',
version: '1.0.0',
dependencies: {
'project-3': 'file:./project-3',
},
dependenciesMeta: {
'project-3': {
injected: true,
},
},
},
},
{
location: 'project-2/project-3',
package: {
name: 'project-3',
version: '1.0.0',
},
},
])
process.chdir('../project-1')
await install(manifest1, await testDefaults())
const lockfile = await readYamlFile<Lockfile>('pnpm-lock.yaml')
expect(Object.keys(lockfile.packages ?? {})).toStrictEqual(['file:../project-2', 'file:../project-2/project-3'])
})