forked from pnpm/pnpm
/
misc.ts
451 lines (334 loc) · 11.9 KB
/
misc.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import { promises as fs, writeFileSync } from 'fs'
import path from 'path'
import { WANTED_LOCKFILE } from '@pnpm/constants'
import { Lockfile } from '@pnpm/lockfile-types'
import prepare, { prepareEmpty, preparePackages } from '@pnpm/prepare'
import { fromDir as readPackageJsonFromDir } from '@pnpm/read-package-json'
import readProjectManifest from '@pnpm/read-project-manifest'
import writeProjectManifest from '@pnpm/write-project-manifest'
import dirIsCaseSensitive from 'dir-is-case-sensitive'
import readYamlFile from 'read-yaml-file'
import rimraf from '@zkochan/rimraf'
import isWindows from 'is-windows'
import loadJsonFile from 'load-json-file'
import exists from 'path-exists'
import crossSpawn from 'cross-spawn'
import {
execPnpm,
execPnpmSync,
} from '../utils'
const skipOnWindows = isWindows() ? test.skip : test
test('bin files are found by lifecycle scripts', () => {
prepare({
dependencies: {
'hello-world-js-bin': '*',
},
scripts: {
postinstall: 'hello-world-js-bin',
},
})
const result = execPnpmSync(['install'])
expect(result.status).toBe(0)
expect(result.stdout.toString()).toContain('Hello world!')
})
skipOnWindows('install --lockfile-only', async () => {
const project = prepare()
await execPnpm(['install', 'rimraf@2.5.1', '--lockfile-only'])
await project.hasNot('rimraf')
const lockfile = await project.readLockfile()
expect(lockfile.packages).toHaveProperty(['/rimraf/2.5.1'])
})
test('install --no-lockfile', async () => {
const project = prepare()
await execPnpm(['install', 'is-positive', '--no-lockfile'])
await project.has('is-positive')
expect(await project.readLockfile()).toBeFalsy()
})
test('write to stderr when --use-stderr is used', async () => {
const project = prepare()
const result = execPnpmSync(['add', 'is-positive', '--use-stderr'])
await project.has('is-positive')
expect(result.stdout.toString()).toBe('')
expect(result.stderr.toString()).not.toBe('')
})
test('install with package-lock=false in .npmrc', async () => {
const project = prepare()
writeFileSync('.npmrc', 'package-lock=false', 'utf8')
await execPnpm(['add', 'is-positive'])
await project.has('is-positive')
expect(await project.readLockfile()).toBeFalsy()
})
test('install from any location via the --prefix flag', async () => {
const project = prepare({
dependencies: {
rimraf: '2.6.2',
},
})
process.chdir('..')
await execPnpm(['install', '--prefix', 'project'])
await project.has('rimraf')
await project.isExecutable('.bin/rimraf')
})
test('install with external lockfile directory', async () => {
const project = prepare()
await execPnpm(['install', 'is-positive', '--lockfile-directory', path.resolve('..')])
await project.has('is-positive')
const lockfile = await readYamlFile<Lockfile>(path.resolve('..', WANTED_LOCKFILE))
expect(Object.keys(lockfile.importers)).toStrictEqual(['project'])
})
test('install --save-exact', async () => {
const project = prepare()
await execPnpm(['install', 'is-positive@3.1.0', '--save-exact', '--save-dev'])
await project.has('is-positive')
const pkg = await readPackageJsonFromDir(process.cwd())
expect(pkg.devDependencies).toStrictEqual({ 'is-positive': '3.1.0' })
})
test('install to a project that uses package.yaml', async () => {
const project = prepareEmpty()
await writeProjectManifest(path.resolve('package.yaml'), { name: 'foo', version: '1.0.0' })
await execPnpm(['install', 'is-positive@3.1.0', '--save-exact', '--save-dev'])
await project.has('is-positive')
const { manifest } = await readProjectManifest(process.cwd())
expect(manifest?.devDependencies).toStrictEqual({ 'is-positive': '3.1.0' })
})
test('install save new dep with the specified spec', async () => {
const project = prepare()
await execPnpm(['install', 'is-positive@~3.1.0'])
await project.has('is-positive')
const pkg = await readPackageJsonFromDir(process.cwd())
expect(pkg.dependencies).toStrictEqual({ 'is-positive': '~3.1.0' })
})
// Covers https://github.com/pnpm/pnpm/issues/1685
test("don't fail on case insensitive filesystems when package has 2 files with same name", async () => {
const project = prepare()
await execPnpm(['install', 'with-same-file-in-different-cases'])
await project.has('with-same-file-in-different-cases')
const { files: integrityFile } = await loadJsonFile<{ files: object }>(await project.getPkgIndexFilePath('with-same-file-in-different-cases', '1.0.0'))
const packageFiles = Object.keys(integrityFile).sort()
expect(packageFiles).toStrictEqual(['Foo.js', 'foo.js', 'package.json'])
const files = await fs.readdir('node_modules/with-same-file-in-different-cases')
const storeDir = await project.getStorePath()
if (await dirIsCaseSensitive(storeDir)) {
expect([...files]).toStrictEqual(['Foo.js', 'foo.js', 'package.json'])
} else {
expect([...files]).toStrictEqual(['Foo.js', 'package.json'])
}
})
test('top-level packages should find the plugins they use', async () => {
prepare({
scripts: {
test: 'pkg-that-uses-plugins',
},
})
await execPnpm(['install', 'pkg-that-uses-plugins', 'plugin-example'])
const result = crossSpawn.sync('npm', ['test'])
expect(result.stdout.toString()).toContain('My plugin is plugin-example')
expect(result.status).toBe(0)
})
test('not top-level packages should find the plugins they use', async () => {
// standard depends on eslint and eslint plugins
prepare({
scripts: {
test: 'standard',
},
})
await execPnpm(['install', 'standard@8.6.0'])
const result = crossSpawn.sync('npm', ['test'])
expect(result.status).toBe(0)
})
test('run js bin file', async () => {
prepare({
scripts: {
test: 'hello-world-js-bin',
},
})
await execPnpm(['install', 'hello-world-js-bin'])
const result = crossSpawn.sync('npm', ['test'])
expect(result.stdout.toString()).toContain('Hello world!')
expect(result.status).toBe(0)
})
test('create a package.json if there is none', async () => {
prepareEmpty()
await execPnpm(['install', 'dep-of-pkg-with-1-dep@100.1.0'])
expect((await import(path.resolve('package.json'))).default).toStrictEqual({
dependencies: {
'dep-of-pkg-with-1-dep': '100.1.0',
},
})
})
test('`pnpm add` should fail if no package name was provided', () => {
prepare()
const { status, stdout } = execPnpmSync(['add'])
expect(status).toBe(1)
expect(stdout.toString()).toContain('`pnpm add` requires the package name')
})
test('`pnpm -r add` should fail if no package name was provided', () => {
preparePackages([
{
name: 'project',
version: '1.0.0',
},
])
writeFileSync('pnpm-workspace.yaml', '', 'utf8')
const { status, stdout } = execPnpmSync(['-r', 'add'])
expect(status).toBe(1)
expect(stdout.toString()).toContain('`pnpm add` requires the package name')
})
test('install should fail if the used pnpm version does not satisfy the pnpm version specified in engines', async () => {
prepare({
name: 'project',
version: '1.0.0',
engines: {
pnpm: '99999',
},
})
const { status, stdout } = execPnpmSync(['install'])
expect(status).toBe(1)
expect(stdout.toString()).toContain('Your pnpm version is incompatible with')
})
test('engine-strict=false: install should not fail if the used Node version does not satisfy the Node version specified in engines', async () => {
prepare({
name: 'project',
version: '1.0.0',
engines: {
node: '99999',
},
})
const { status, stdout } = execPnpmSync(['install'])
expect(status).toBe(0)
expect(stdout.toString()).toContain('Unsupported engine')
})
test('engine-strict=true: install should fail if the used Node version does not satisfy the Node version specified in engines', async () => {
prepare({
name: 'project',
version: '1.0.0',
engines: {
node: '99999',
},
})
const { status, stdout } = execPnpmSync(['install', '--engine-strict'])
expect(status).toBe(1)
expect(stdout.toString()).toContain('Your Node version is incompatible with')
})
test('recursive install should fail if the used pnpm version does not satisfy the pnpm version specified in engines of any of the workspace projects', async () => {
preparePackages([
{
name: 'project-1',
version: '1.0.0',
dependencies: {
'is-positive': '1.0.0',
},
engines: {
pnpm: '99999',
},
},
{
name: 'project-2',
version: '1.0.0',
dependencies: {
'is-negative': '1.0.0',
},
},
])
await fs.writeFile('pnpm-workspace.yaml', '', 'utf8')
process.chdir('project-1')
const { status, stdout } = execPnpmSync(['recursive', 'install'])
expect(status).toBe(1)
expect(stdout.toString()).toContain('Your pnpm version is incompatible with')
})
test('engine-strict=true: recursive install should fail if the used Node version does not satisfy the Node version specified in engines of any of the workspace projects', async () => {
preparePackages([
{
name: 'project-1',
version: '1.0.0',
dependencies: {
'is-positive': '1.0.0',
},
engines: {
node: '99999',
},
},
{
name: 'project-2',
version: '1.0.0',
dependencies: {
'is-negative': '1.0.0',
},
},
])
await fs.writeFile('pnpm-workspace.yaml', '', 'utf8')
process.chdir('project-1')
const { status, stdout } = execPnpmSync(['recursive', 'install', '--engine-strict'])
expect(status).toBe(1)
expect(stdout.toString()).toContain('Your Node version is incompatible with')
})
test('engine-strict=false: recursive install should not fail if the used Node version does not satisfy the Node version specified in engines of any of the workspace projects', async () => {
preparePackages([
{
name: 'project-1',
version: '1.0.0',
dependencies: {
'is-positive': '1.0.0',
},
engines: {
node: '99999',
},
},
{
name: 'project-2',
version: '1.0.0',
dependencies: {
'is-negative': '1.0.0',
},
},
])
await fs.writeFile('pnpm-workspace.yaml', '', 'utf8')
process.chdir('project-1')
const { status, stdout } = execPnpmSync(['recursive', 'install'])
expect(status).toBe(0)
expect(stdout.toString()).toContain('Unsupported engine')
})
test('using a custom virtual-store-dir location', async () => {
prepare({
dependencies: { rimraf: '2.5.1' },
})
await execPnpm(['install', '--virtual-store-dir=.pnpm'])
expect(await exists('.pnpm/rimraf@2.5.1/node_modules/rimraf/package.json')).toBeTruthy()
expect(await exists('.pnpm/lock.yaml')).toBeTruthy()
expect(await exists('.pnpm/node_modules/once/package.json')).toBeTruthy()
await rimraf('node_modules')
await rimraf('.pnpm')
await execPnpm(['install', '--virtual-store-dir=.pnpm', '--frozen-lockfile'])
expect(await exists('.pnpm/rimraf@2.5.1/node_modules/rimraf/package.json')).toBeTruthy()
expect(await exists('.pnpm/lock.yaml')).toBeTruthy()
expect(await exists('.pnpm/node_modules/once/package.json')).toBeTruthy()
})
// This is an integration test only because it is hard to mock is-ci
test('installing in a CI environment', async () => {
const project = prepare({
dependencies: { rimraf: '2.5.1' },
})
await execPnpm(['install'], { env: { CI: 'true' } })
await project.writePackageJson({
dependencies: { rimraf: '1' },
})
let err!: Error
try {
await execPnpm(['install'], { env: { CI: 'true' } })
} catch (_err: any) { // eslint-disable-line
err = _err
}
expect(err).toBeTruthy()
await execPnpm(['install', '--no-frozen-lockfile'], { env: { CI: 'true' } })
await rimraf('node_modules')
await project.writePackageJson({
dependencies: { rimraf: '2' },
})
await execPnpm(['install', '--no-prefer-frozen-lockfile'], { env: { CI: 'true' } })
})
test('installation fails with a timeout error', async () => {
prepare()
await expect(
execPnpm(['add', 'typescript@2.4.2', '--fetch-timeout=1', '--fetch-retries=0'])
).rejects.toThrow()
})