/
lockfile.ts
1451 lines (1180 loc) · 49.1 KB
/
lockfile.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { promises as fs } from 'fs'
import path from 'path'
import { LOCKFILE_VERSION_V6 as LOCKFILE_VERSION, WANTED_LOCKFILE } from '@pnpm/constants'
import { RootLog } from '@pnpm/core-loggers'
import { PnpmError } from '@pnpm/error'
import { Lockfile, TarballResolution } from '@pnpm/lockfile-file'
import { LockfileV6 } from '@pnpm/lockfile-types'
import { prepareEmpty, preparePackages } from '@pnpm/prepare'
import { readPackageJsonFromDir } from '@pnpm/read-package-json'
import { addDistTag, getIntegrity, REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
import { ProjectManifest } from '@pnpm/types'
import readYamlFile from 'read-yaml-file'
import {
addDependenciesToPackage,
install,
mutateModules,
mutateModulesInSingleProject,
} from '@pnpm/core'
import rimraf from '@zkochan/rimraf'
import loadJsonFile from 'load-json-file'
import nock from 'nock'
import exists from 'path-exists'
import sinon from 'sinon'
import writeYamlFile from 'write-yaml-file'
import { testDefaults } from './utils'
const LOCKFILE_WARN_LOG = {
level: 'warn',
message: `A ${WANTED_LOCKFILE} file exists. The current configuration prohibits to read or write a lockfile`,
name: 'pnpm',
}
test('lockfile has correct format', async () => {
await addDistTag({ package: '@pnpm.e2e/pkg-with-1-dep', version: '100.0.0', distTag: 'latest' })
const project = prepareEmpty()
await addDependenciesToPackage({},
[
'@pnpm.e2e/pkg-with-1-dep',
'@rstacruz/tap-spec@4.1.1',
'kevva/is-negative#1d7e288222b53a0cab90a331f1865220ec29560c',
], await testDefaults({ fastUnpack: false, save: true }))
const modules = await project.readModulesManifest()
expect(modules).toBeTruthy()
expect(modules!.pendingBuilds.length).toBe(0)
const lockfile = await project.readLockfile()
const id = '/@pnpm.e2e/pkg-with-1-dep@100.0.0'
expect(lockfile.lockfileVersion).toBe(LOCKFILE_VERSION)
expect(lockfile.dependencies).toBeTruthy()
expect(lockfile.dependencies['@pnpm.e2e/pkg-with-1-dep'].version).toBe('100.0.0')
expect(lockfile.dependencies).toHaveProperty(['@rstacruz/tap-spec'])
expect(lockfile.dependencies['is-negative'].version).toContain('/') // has not shortened tarball from the non-standard registry
expect(lockfile.packages).toBeTruthy() // has packages field
expect(lockfile.packages).toHaveProperty([id])
expect(lockfile.packages[id].dependencies).toBeTruthy()
expect(lockfile.packages[id].dependencies).toHaveProperty(['@pnpm.e2e/dep-of-pkg-with-1-dep'])
expect(lockfile.packages[id].resolution).toBeTruthy()
expect((lockfile.packages[id].resolution as { integrity: string }).integrity).toBeTruthy()
expect((lockfile.packages[id].resolution as TarballResolution).tarball).toBeFalsy()
const absDepPath = 'github.com/kevva/is-negative/1d7e288222b53a0cab90a331f1865220ec29560c'
expect(lockfile.packages).toHaveProperty([absDepPath])
expect(lockfile.packages[absDepPath].name).toBeTruthy() // github-hosted package has name specified
})
test('lockfile has dev deps even when installing for prod only', async () => {
const project = prepareEmpty()
await install({
devDependencies: {
'is-negative': '2.1.0',
},
}, await testDefaults({ production: true }))
const lockfile = await project.readLockfile()
const id = '/is-negative@2.1.0'
expect(lockfile.devDependencies).toBeTruthy()
expect(lockfile.devDependencies['is-negative'].version).toBe('2.1.0')
expect(lockfile.packages[id]).toBeTruthy()
})
test('lockfile with scoped package', async () => {
prepareEmpty()
await writeYamlFile(WANTED_LOCKFILE, {
dependencies: {
'@types/semver': {
specifier: '^5.3.31',
version: '5.3.31',
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'/@types/semver@5.3.31': {
resolution: {
integrity: 'sha512-WBv5F9HrWTyG800cB9M3veCVkFahqXN7KA7c3VUCYZm/xhNzzIFiXiq+rZmj75j7GvWelN3YNrLX7FjtqBvhMw==',
},
},
},
}, { lineWidth: 1000 })
await install({
dependencies: {
'@types/semver': '^5.3.31',
},
}, await testDefaults({ frozenLockfile: true }))
})
test("lockfile doesn't lock subdependencies that don't satisfy the new specs", async () => {
const project = prepareEmpty()
// dependends on react-onclickoutside@5.9.0
const manifest = await addDependenciesToPackage({}, ['react-datetime@2.8.8'], await testDefaults({
autoInstallPeers: false,
fastUnpack: false,
save: true,
strictPeerDependencies: false,
}))
// dependends on react-onclickoutside@0.3.4
await addDependenciesToPackage(manifest, ['react-datetime@1.3.0'], await testDefaults({
autoInstallPeers: false,
save: true,
strictPeerDependencies: false,
}))
expect(
project.requireModule('.pnpm/react-datetime@1.3.0/node_modules/react-onclickoutside/package.json').version
).toBe('0.3.4') // react-datetime@1.3.0 has react-onclickoutside@0.3.4 in its node_modules
const lockfile = await project.readLockfile()
expect(Object.keys(lockfile.dependencies).length).toBe(1) // resolutions not duplicated
})
test('a lockfile created even when there are no deps in package.json', async () => {
const project = prepareEmpty()
await install({}, await testDefaults())
expect(await project.readLockfile()).toBeTruthy()
expect(await exists('node_modules')).toBeFalsy()
})
test('current lockfile removed when no deps in package.json', async () => {
const project = prepareEmpty()
await writeYamlFile(WANTED_LOCKFILE, {
dependencies: {
'is-negative': {
specifier: '2.1.0',
version: '2.1.0',
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'/is-negative@2.1.0': {
resolution: {
tarball: `http://localhost:${REGISTRY_MOCK_PORT}/is-negative/-/is-negative-2.1.0.tgz`,
},
},
},
}, { lineWidth: 1000 })
await install({}, await testDefaults())
expect(await project.readLockfile()).toBeTruthy()
expect(await exists('node_modules')).toBeFalsy()
})
test('lockfile is fixed when it does not match package.json', async () => {
const project = prepareEmpty()
await writeYamlFile(WANTED_LOCKFILE, {
dependencies: {
'@types/semver': {
specifier: '5.3.31',
version: '5.3.31',
},
'is-negative': {
specifier: '^2.1.0',
version: '2.1.0',
},
'is-positive': {
specifier: '^3.1.0',
version: '3.1.0',
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'/@types/semver@5.3.31': {
resolution: {
integrity: 'sha512-WBv5F9HrWTyG800cB9M3veCVkFahqXN7KA7c3VUCYZm/xhNzzIFiXiq+rZmj75j7GvWelN3YNrLX7FjtqBvhMw==',
},
},
'/is-negative@2.1.0': {
resolution: {
tarball: `http://localhost:${REGISTRY_MOCK_PORT}/is-negative/-/is-negative-2.1.0.tgz`,
},
},
'/is-positive@3.1.0': {
resolution: {
integrity: 'sha512-8ND1j3y9/HP94TOvGzr69/FgbkX2ruOldhLEsTWwcJVfo4oRjwemJmJxt7RJkKYH8tz7vYBP9JcKQY8CLuJ90Q==',
},
},
},
}, { lineWidth: 1000 })
const reporter = sinon.spy()
await install({
devDependencies: {
'is-negative': '^2.1.0',
},
optionalDependencies: {
'is-positive': '^3.1.0',
},
}, await testDefaults({ reporter }))
const progress = sinon.match({
name: 'pnpm:progress',
status: 'resolving',
})
expect(reporter.withArgs(progress).callCount).toBe(0)
const lockfile = await project.readLockfile()
expect(lockfile.devDependencies['is-negative'].version).toBe('2.1.0')
expect(lockfile.optionalDependencies['is-positive'].version).toBe('3.1.0')
expect(lockfile.dependencies).toBeFalsy()
expect(lockfile.packages).not.toHaveProperty(['/@types/semver@5.3.31'])
})
test(`doing named installation when ${WANTED_LOCKFILE} exists already`, async () => {
const project = prepareEmpty()
await writeYamlFile(WANTED_LOCKFILE, {
dependencies: {
'@types/semver': {
specifier: '5.3.31',
version: '5.3.31',
},
'is-negative': {
specifier: '^2.1.0',
version: '2.1.0',
},
'is-positive': {
specifier: '^3.1.0',
version: '3.1.0',
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'/@types/semver@5.3.31': {
resolution: {
integrity: 'sha512-WBv5F9HrWTyG800cB9M3veCVkFahqXN7KA7c3VUCYZm/xhNzzIFiXiq+rZmj75j7GvWelN3YNrLX7FjtqBvhMw==',
},
},
'/is-negative@2.1.0': {
resolution: {
tarball: `http://localhost:${REGISTRY_MOCK_PORT}/is-negative/-/is-negative-2.1.0.tgz`,
},
},
'/is-positive@3.1.0': {
resolution: {
integrity: 'sha512-8ND1j3y9/HP94TOvGzr69/FgbkX2ruOldhLEsTWwcJVfo4oRjwemJmJxt7RJkKYH8tz7vYBP9JcKQY8CLuJ90Q==',
},
},
},
}, { lineWidth: 1000 })
const reporter = sinon.spy()
const manifest = await addDependenciesToPackage({
dependencies: {
'@types/semver': '5.3.31',
'is-negative': '^2.1.0',
'is-positive': '^3.1.0',
},
}, ['is-positive'], await testDefaults({ reporter }))
await install(manifest, await testDefaults({ reporter }))
expect(reporter.calledWithMatch(LOCKFILE_WARN_LOG)).toBeFalsy()
await project.has('is-negative')
})
test(`respects ${WANTED_LOCKFILE} for top dependencies`, async () => {
const project = prepareEmpty()
const reporter = sinon.spy()
// const fooProgress = sinon.match({
// name: 'pnpm:progress',
// status: 'resolving',
// manifest: {
// name: 'foo',
// },
// })
const pkgs = ['@pnpm.e2e/foo', '@pnpm.e2e/bar', '@pnpm.e2e/qar']
await Promise.all(pkgs.map(async (pkgName) => addDistTag({ package: pkgName, version: '100.0.0', distTag: 'latest' })))
let manifest = await addDependenciesToPackage({}, ['@pnpm.e2e/foo'], await testDefaults({ save: true, reporter }))
// t.equal(reporter.withArgs(fooProgress).callCount, 1, 'reported foo once')
manifest = await addDependenciesToPackage(manifest, ['@pnpm.e2e/bar'], await testDefaults({ targetDependenciesField: 'optionalDependencies' }))
manifest = await addDependenciesToPackage(manifest, ['@pnpm.e2e/qar'], await testDefaults({ addDependenciesToPackage: 'devDependencies' }))
manifest = await addDependenciesToPackage(manifest, ['@pnpm.e2e/foobar'], await testDefaults({ save: true }))
expect((await readPackageJsonFromDir(path.resolve('node_modules', '@pnpm.e2e/foo'))).version).toBe('100.0.0')
expect((await readPackageJsonFromDir(path.resolve('node_modules', '@pnpm.e2e/bar'))).version).toBe('100.0.0')
expect((await readPackageJsonFromDir(path.resolve('node_modules', '@pnpm.e2e/qar'))).version).toBe('100.0.0')
expect((await readPackageJsonFromDir(path.resolve('node_modules/.pnpm/@pnpm.e2e+foobar@100.0.0/node_modules/@pnpm.e2e/foo'))).version).toBe('100.0.0')
expect((await readPackageJsonFromDir(path.resolve('node_modules/.pnpm/@pnpm.e2e+foobar@100.0.0/node_modules/@pnpm.e2e/bar'))).version).toBe('100.0.0')
await Promise.all(pkgs.map(async (pkgName) => addDistTag({ package: pkgName, version: '100.1.0', distTag: 'latest' })))
await rimraf('node_modules')
await rimraf(path.join('..', '.store'))
reporter.resetHistory()
// shouldn't care about what the registry in npmrc is
// the one in lockfile should be used
await install(manifest, await testDefaults({
rawConfig: {
registry: 'https://registry.npmjs.org',
},
registry: 'https://registry.npmjs.org',
reporter,
}))
// t.equal(reporter.withArgs(fooProgress).callCount, 0, 'not reported foo')
await project.storeHasNot('@pnpm.e2e/foo', '100.1.0')
expect((await readPackageJsonFromDir(path.resolve('node_modules', '@pnpm.e2e/foo'))).version).toBe('100.0.0')
expect((await readPackageJsonFromDir(path.resolve('node_modules', '@pnpm.e2e/bar'))).version).toBe('100.0.0')
expect((await readPackageJsonFromDir(path.resolve('node_modules', '@pnpm.e2e/qar'))).version).toBe('100.0.0')
expect((await readPackageJsonFromDir(path.resolve('node_modules/.pnpm/@pnpm.e2e+foobar@100.0.0/node_modules/@pnpm.e2e/foo'))).version).toBe('100.0.0')
expect((await readPackageJsonFromDir(path.resolve('node_modules/.pnpm/@pnpm.e2e+foobar@100.0.0/node_modules/@pnpm.e2e/bar'))).version).toBe('100.0.0')
})
test(`subdeps are updated on repeat install if outer ${WANTED_LOCKFILE} does not match the inner one`, async () => {
const project = prepareEmpty()
await addDistTag({ package: '@pnpm.e2e/pkg-with-1-dep', version: '100.0.0', distTag: 'latest' })
await addDistTag({ package: '@pnpm.e2e/dep-of-pkg-with-1-dep', version: '100.0.0', distTag: 'latest' })
const manifest = await addDependenciesToPackage({}, ['@pnpm.e2e/pkg-with-1-dep'], await testDefaults())
await project.storeHas('@pnpm.e2e/dep-of-pkg-with-1-dep', '100.0.0')
const lockfile = await project.readLockfile()
expect(lockfile.packages).toHaveProperty(['/@pnpm.e2e/dep-of-pkg-with-1-dep@100.0.0'])
delete lockfile.packages['/@pnpm.e2e/dep-of-pkg-with-1-dep@100.0.0']
lockfile.packages['/@pnpm.e2e/dep-of-pkg-with-1-dep@100.1.0'] = {
resolution: {
integrity: getIntegrity('@pnpm.e2e/dep-of-pkg-with-1-dep', '100.1.0'),
},
}
lockfile.packages['/@pnpm.e2e/pkg-with-1-dep@100.0.0'].dependencies!['@pnpm.e2e/dep-of-pkg-with-1-dep'] = '100.1.0'
await writeYamlFile(WANTED_LOCKFILE, lockfile, { lineWidth: 1000 })
await install(manifest, await testDefaults())
await project.storeHas('@pnpm.e2e/dep-of-pkg-with-1-dep', '100.1.0')
})
test("recreates lockfile if it doesn't match the dependencies in package.json", async () => {
const project = prepareEmpty()
let manifest = await addDependenciesToPackage({}, ['is-negative@1.0.0'], await testDefaults({ pinnedVersion: 'patch', targetDependenciesField: 'dependencies' }))
manifest = await addDependenciesToPackage(manifest, ['is-positive@1.0.0'], await testDefaults({ pinnedVersion: 'patch', targetDependenciesField: 'devDependencies' }))
manifest = await addDependenciesToPackage(manifest, ['map-obj@1.0.0'], await testDefaults({ pinnedVersion: 'patch', targetDependenciesField: 'optionalDependencies' }))
const lockfile1 = await project.readLockfile()
expect(lockfile1.dependencies['is-negative'].version).toBe('1.0.0')
expect(lockfile1.dependencies['is-negative'].specifier).toBe('1.0.0')
manifest.dependencies!['is-negative'] = '^2.1.0'
manifest.devDependencies!['is-positive'] = '^2.0.0'
manifest.optionalDependencies!['map-obj'] = '1.0.1'
await install(manifest, await testDefaults())
const lockfile = await project.readLockfile()
expect(lockfile.dependencies['is-negative'].version).toBe('2.1.0')
expect(lockfile.dependencies['is-negative'].specifier).toBe('^2.1.0')
expect(lockfile.devDependencies['is-positive'].version).toBe('2.0.0')
expect(lockfile.devDependencies['is-positive'].specifier).toBe('^2.0.0')
expect(lockfile.optionalDependencies['map-obj'].version).toBe('1.0.1')
expect(lockfile.optionalDependencies['map-obj'].specifier).toBe('1.0.1')
})
test('repeat install with lockfile should not mutate lockfile when dependency has version specified with v prefix', async () => {
const project = prepareEmpty()
const manifest = await addDependenciesToPackage({}, ['highmaps-release@5.0.11'], await testDefaults())
const lockfile1 = await project.readLockfile()
expect(lockfile1.dependencies['highmaps-release'].version).toBe('5.0.11')
await rimraf('node_modules')
await install(manifest, await testDefaults())
const lockfile2 = await project.readLockfile()
expect(lockfile1).toStrictEqual(lockfile2) // lockfile hasn't been changed
})
test('package is not marked dev if it is also a subdep of a regular dependency', async () => {
const project = prepareEmpty()
await addDistTag({ package: '@pnpm.e2e/pkg-with-1-dep', version: '100.0.0', distTag: 'latest' })
await addDistTag({ package: '@pnpm.e2e/dep-of-pkg-with-1-dep', version: '100.0.0', distTag: 'latest' })
const manifest = await addDependenciesToPackage({}, ['@pnpm.e2e/pkg-with-1-dep'], await testDefaults())
console.log('installed @pnpm.e2e/pkg-with-1-dep')
await addDependenciesToPackage(manifest, ['@pnpm.e2e/dep-of-pkg-with-1-dep'], await testDefaults({ targetDependenciesField: 'devDependencies' }))
console.log('installed optional dependency which is also a dependency of @pnpm.e2e/pkg-with-1-dep')
const lockfile = await project.readLockfile()
expect(lockfile.packages['/@pnpm.e2e/dep-of-pkg-with-1-dep@100.0.0'].dev).toBeFalsy()
})
test('package is not marked optional if it is also a subdep of a regular dependency', async () => {
const project = prepareEmpty()
await addDistTag({ package: '@pnpm.e2e/pkg-with-1-dep', version: '100.0.0', distTag: 'latest' })
await addDistTag({ package: '@pnpm.e2e/dep-of-pkg-with-1-dep', version: '100.0.0', distTag: 'latest' })
const manifest = await addDependenciesToPackage({}, ['@pnpm.e2e/pkg-with-1-dep'], await testDefaults())
await addDependenciesToPackage(manifest, ['@pnpm.e2e/dep-of-pkg-with-1-dep'], await testDefaults({ targetDependenciesField: 'optionalDependencies' }))
const lockfile = await project.readLockfile()
expect(lockfile.packages['/@pnpm.e2e/dep-of-pkg-with-1-dep@100.0.0'].optional).toBeFalsy()
})
test('scoped module from different registry', async () => {
const project = prepareEmpty()
const opts = await testDefaults()
opts.registries!.default = 'https://registry.npmjs.org/'
opts.registries!['@zkochan'] = `http://localhost:${REGISTRY_MOCK_PORT}`
opts.registries!['@foo'] = `http://localhost:${REGISTRY_MOCK_PORT}`
await addDependenciesToPackage({}, ['@zkochan/foo', '@foo/has-dep-from-same-scope', 'is-positive'], opts)
await project.has('@zkochan/foo')
const lockfile = await project.readLockfile()
expect(lockfile).toStrictEqual({
dependencies: {
'@foo/has-dep-from-same-scope': {
specifier: '^1.0.0',
version: '1.0.0',
},
'@zkochan/foo': {
specifier: '^1.0.0',
version: '1.0.0',
},
'is-positive': {
specifier: '^3.1.0',
version: '3.1.0',
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'/@foo/has-dep-from-same-scope@1.0.0': {
dependencies: {
'@foo/no-deps': '1.0.0',
'is-negative': '1.0.0',
},
dev: false,
resolution: {
integrity: getIntegrity('@foo/has-dep-from-same-scope', '1.0.0'),
},
},
'/@foo/no-deps@1.0.0': {
dev: false,
resolution: {
integrity: getIntegrity('@foo/no-deps', '1.0.0'),
},
},
'/@zkochan/foo@1.0.0': {
dev: false,
resolution: {
integrity: 'sha512-IFvrYpq7E6BqKex7A7czIFnFncPiUVdhSzGhAOWpp8RlkXns4y/9ZdynxaA/e0VkihRxQkihE2pTyvxjfe/wBg==',
},
},
'/is-negative@1.0.0': {
dev: false,
engines: {
node: '>=0.10.0',
},
resolution: {
integrity: 'sha512-1aKMsFUc7vYQGzt//8zhkjRWPoYkajY/I5MJEvrc0pDoHXrW7n5ri8DYxhy3rR+Dk0QFl7GjHHsZU1sppQrWtw==',
},
},
'/is-positive@3.1.0': {
dev: false,
engines: {
node: '>=0.10.0',
},
resolution: {
integrity: 'sha512-8ND1j3y9/HP94TOvGzr69/FgbkX2ruOldhLEsTWwcJVfo4oRjwemJmJxt7RJkKYH8tz7vYBP9JcKQY8CLuJ90Q==',
},
},
},
})
})
test('repeat install with no inner lockfile should not rewrite packages in node_modules', async () => {
const project = prepareEmpty()
const manifest = await addDependenciesToPackage({}, ['is-negative@1.0.0'], await testDefaults())
await rimraf('node_modules/.pnpm/lock.yaml')
await install(manifest, await testDefaults())
await project.has('is-negative')
})
test('packages are placed in devDependencies even if they are present as non-dev as well', async () => {
const project = prepareEmpty()
await addDistTag({ package: '@pnpm.e2e/pkg-with-1-dep', version: '100.0.0', distTag: 'latest' })
await addDistTag({ package: '@pnpm.e2e/dep-of-pkg-with-1-dep', version: '100.1.0', distTag: 'latest' })
const reporter = sinon.spy()
await install({
devDependencies: {
'@pnpm.e2e/dep-of-pkg-with-1-dep': '^100.1.0',
'@pnpm.e2e/pkg-with-1-dep': '^100.0.0',
},
}, await testDefaults({ reporter }))
const lockfile = await project.readLockfile()
expect(lockfile.devDependencies).toHaveProperty(['@pnpm.e2e/dep-of-pkg-with-1-dep'])
expect(lockfile.devDependencies).toHaveProperty(['@pnpm.e2e/pkg-with-1-dep'])
expect(reporter.calledWithMatch({
added: {
dependencyType: 'dev',
name: '@pnpm.e2e/dep-of-pkg-with-1-dep',
version: '100.1.0',
},
level: 'debug',
name: 'pnpm:root',
} as RootLog)).toBeTruthy()
expect(reporter.calledWithMatch({
added: {
dependencyType: 'dev',
name: '@pnpm.e2e/pkg-with-1-dep',
version: '100.0.0',
},
level: 'debug',
name: 'pnpm:root',
} as RootLog)).toBeTruthy()
})
// This testcase verifies that pnpm is not failing when trying to preserve dependencies.
// Only when a dependency is a range dependency, should pnpm try to compare versions of deps with semver.satisfies().
test('updating package that has a github-hosted dependency', async () => {
prepareEmpty()
const manifest = await addDependenciesToPackage({}, ['@pnpm.e2e/has-github-dep@1'], await testDefaults())
await addDependenciesToPackage(manifest, ['@pnpm.e2e/has-github-dep@latest'], await testDefaults())
})
test('updating package that has deps with peers', async () => {
prepareEmpty()
const manifest = await addDependenciesToPackage({}, ['@pnpm.e2e/abc-grand-parent-with-c@0'], await testDefaults())
await addDependenciesToPackage(manifest, ['@pnpm.e2e/abc-grand-parent-with-c@1'], await testDefaults())
})
test('pendingBuilds gets updated if install removes packages', async () => {
const project = prepareEmpty()
await install({
dependencies: {
'@pnpm.e2e/pre-and-postinstall-scripts-example': '*',
'@pnpm.e2e/with-postinstall-b': '*',
},
}, await testDefaults({ fastUnpack: false, ignoreScripts: true }))
const modules1 = await project.readModulesManifest()
await install({
dependencies: {
'@pnpm.e2e/pre-and-postinstall-scripts-example': '*',
},
}, await testDefaults({ fastUnpack: false, ignoreScripts: true }))
const modules2 = await project.readModulesManifest()
expect(modules1).toBeTruthy()
expect(modules2).toBeTruthy()
expect(modules1!.pendingBuilds.length > modules2!.pendingBuilds.length).toBeTruthy()
})
test('dev properties are correctly updated on named install', async () => {
const project = prepareEmpty()
const manifest = await addDependenciesToPackage(
{},
['inflight@1.0.6'],
await testDefaults({ targetDependenciesField: 'devDependencies' })
)
await addDependenciesToPackage(manifest, ['foo@npm:inflight@1.0.6'], await testDefaults({}))
const lockfile = await project.readLockfile()
expect(
Object.values(lockfile.packages).filter((dep) => typeof dep.dev !== 'undefined')
).toStrictEqual([])
})
test('optional properties are correctly updated on named install', async () => {
const project = prepareEmpty()
const manifest = await addDependenciesToPackage({}, ['inflight@1.0.6'], await testDefaults({ targetDependenciesField: 'optionalDependencies' }))
await addDependenciesToPackage(manifest, ['foo@npm:inflight@1.0.6'], await testDefaults({}))
const lockfile = await project.readLockfile()
expect(Object.values(lockfile.packages).filter((dep) => typeof dep.optional !== 'undefined')).toStrictEqual([])
})
test('dev property is correctly set for package that is duplicated to both the dependencies and devDependencies group', async () => {
const project = prepareEmpty()
// TODO: use a smaller package for testing
await addDependenciesToPackage({}, ['overlap@2.2.8'], await testDefaults())
const lockfile = await project.readLockfile()
expect(lockfile.packages['/couleurs@5.0.0'].dev === false).toBeTruthy()
})
test('no lockfile', async () => {
const project = prepareEmpty()
const reporter = sinon.spy()
await addDependenciesToPackage({}, ['is-positive'], await testDefaults({ useLockfile: false, reporter }))
expect(reporter.calledWithMatch(LOCKFILE_WARN_LOG)).toBeFalsy()
await project.has('is-positive')
expect(await project.readLockfile()).toBeFalsy()
})
test('lockfile is ignored when lockfile = false', async () => {
const project = prepareEmpty()
await writeYamlFile(WANTED_LOCKFILE, {
dependencies: {
'is-negative': {
specifier: '2.1.0',
version: '2.1.0',
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'/is-negative@2.1.0': {
resolution: {
integrity: 'sha1-uZnX2TX0P1IHsBsA094ghS9Mp10=', // Invalid integrity
tarball: `http://localhost:${REGISTRY_MOCK_PORT}/is-negative/-/is-negative-2.1.0.tgz`,
},
},
},
}, { lineWidth: 1000 })
const reporter = sinon.spy()
await install({
dependencies: {
'is-negative': '2.1.0',
},
}, await testDefaults({ useLockfile: false, reporter }))
expect(reporter.calledWithMatch(LOCKFILE_WARN_LOG)).toBeTruthy()
await project.has('is-negative')
expect(await project.readLockfile()).toBeTruthy()
})
test(`don't update ${WANTED_LOCKFILE} during uninstall when useLockfile: false`, async () => {
const project = prepareEmpty()
let manifest!: ProjectManifest
{
const reporter = sinon.spy()
manifest = await addDependenciesToPackage({}, ['is-positive'], await testDefaults({ reporter }))
expect(reporter.calledWithMatch(LOCKFILE_WARN_LOG)).toBeFalsy()
}
{
const reporter = sinon.spy()
await mutateModulesInSingleProject({
dependencyNames: ['is-positive'],
manifest,
mutation: 'uninstallSome',
rootDir: process.cwd(),
}, await testDefaults({ useLockfile: false, reporter }))
expect(reporter.calledWithMatch(LOCKFILE_WARN_LOG)).toBeTruthy()
}
await project.hasNot('is-positive')
expect(await project.readLockfile()).toBeTruthy()
})
test('fail when installing with useLockfile: false and lockfileOnly: true', async () => {
prepareEmpty()
try {
await install({}, await testDefaults({ useLockfile: false, lockfileOnly: true }))
throw new Error('installation should have failed')
} catch (err: any) { // eslint-disable-line
expect(err.message).toBe(`Cannot generate a ${WANTED_LOCKFILE} because lockfile is set to false`)
}
})
test("don't remove packages during named install when useLockfile: false", async () => {
const project = prepareEmpty()
const manifest = await addDependenciesToPackage({}, ['is-positive'], await testDefaults({ useLockfile: false }))
await addDependenciesToPackage(manifest, ['is-negative'], await testDefaults({ useLockfile: false }))
await project.has('is-positive')
await project.has('is-negative')
})
test('save tarball URL when it is non-standard', async () => {
const project = prepareEmpty()
await addDependenciesToPackage({}, ['esprima-fb@3001.1.0-dev-harmony-fb'], await testDefaults({ fastUnpack: false }))
const lockfile = await project.readLockfile()
expect((lockfile.packages['/esprima-fb@3001.1.0-dev-harmony-fb'].resolution as TarballResolution).tarball).toBe('esprima-fb/-/esprima-fb-3001.0001.0000-dev-harmony-fb.tgz')
})
test('packages installed via tarball URL from the default registry are normalized', async () => {
const project = prepareEmpty()
await addDependenciesToPackage({}, [
`http://localhost:${REGISTRY_MOCK_PORT}/@pnpm.e2e/pkg-with-tarball-dep-from-registry/-/pkg-with-tarball-dep-from-registry-1.0.0.tgz`,
'https://registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz',
], await testDefaults())
const lockfile = await project.readLockfile()
expect(lockfile).toStrictEqual({
dependencies: {
'is-positive': {
specifier: 'https://registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz',
version: '@registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz',
},
'@pnpm.e2e/pkg-with-tarball-dep-from-registry': {
specifier: `http://localhost:${REGISTRY_MOCK_PORT}/@pnpm.e2e/pkg-with-tarball-dep-from-registry/-/pkg-with-tarball-dep-from-registry-1.0.0.tgz`,
version: '1.0.0',
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'/@pnpm.e2e/dep-of-pkg-with-1-dep@100.0.0': {
dev: false,
resolution: {
integrity: getIntegrity('@pnpm.e2e/dep-of-pkg-with-1-dep', '100.0.0'),
},
},
'/@pnpm.e2e/pkg-with-tarball-dep-from-registry@1.0.0': {
dependencies: {
'@pnpm.e2e/dep-of-pkg-with-1-dep': '100.0.0',
},
dev: false,
resolution: {
integrity: getIntegrity('@pnpm.e2e/pkg-with-tarball-dep-from-registry', '1.0.0'),
},
},
'@registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz': {
dev: false,
engines: { node: '>=0.10.0' },
name: 'is-positive',
resolution: {
tarball: 'https://registry.npmjs.org/is-positive/-/is-positive-1.0.0.tgz',
},
version: '1.0.0',
},
},
})
})
test('lockfile file has correct format when lockfile directory does not equal the prefix directory', async () => {
await addDistTag({ package: '@pnpm.e2e/pkg-with-1-dep', version: '100.0.0', distTag: 'latest' })
prepareEmpty()
const storeDir = path.resolve('..', '.store')
const manifest = await addDependenciesToPackage(
{},
[
'@pnpm.e2e/pkg-with-1-dep',
'@zkochan/foo@1.0.0',
'kevva/is-negative#1d7e288222b53a0cab90a331f1865220ec29560c',
],
await testDefaults({ save: true, lockfileDir: path.resolve('..'), storeDir })
)
expect(!await exists('node_modules/.modules.yaml')).toBeTruthy()
process.chdir('..')
const modules = await readYamlFile<any>(path.resolve('node_modules', '.modules.yaml')) // eslint-disable-line @typescript-eslint/no-explicit-any
expect(modules).toBeTruthy()
expect(modules.pendingBuilds.length).toBe(0)
{
const lockfile: LockfileV6 = await readYamlFile(WANTED_LOCKFILE)
const id = '/@pnpm.e2e/pkg-with-1-dep@100.0.0'
expect(lockfile.lockfileVersion).toBe(LOCKFILE_VERSION)
expect(lockfile.importers).toBeTruthy()
expect(lockfile.importers.project).toBeTruthy()
expect(lockfile.importers.project).toBeTruthy()
expect(lockfile.importers.project.dependencies).toBeTruthy()
expect(lockfile.importers.project.dependencies!['@pnpm.e2e/pkg-with-1-dep'].version).toBe('100.0.0')
expect(lockfile.importers.project.dependencies!['@zkochan/foo']).toBeTruthy()
expect(lockfile.importers.project.dependencies!['is-negative'].version).toContain('/')
expect(lockfile.packages![id].dependencies).toHaveProperty(['@pnpm.e2e/dep-of-pkg-with-1-dep'])
expect(lockfile.packages![id].resolution).toHaveProperty(['integrity'])
expect(lockfile.packages![id].resolution).not.toHaveProperty(['tarball'])
const absDepPath = 'github.com/kevva/is-negative/1d7e288222b53a0cab90a331f1865220ec29560c'
expect(lockfile.packages).toHaveProperty([absDepPath])
expect(lockfile.packages![absDepPath].name).toBeTruthy()
}
await fs.mkdir('project-2')
process.chdir('project-2')
await addDependenciesToPackage(manifest, ['is-positive'], await testDefaults({
save: true,
lockfileDir: path.resolve('..'),
storeDir,
pruneLockfileImporters: false,
}))
{
const lockfile = await readYamlFile<LockfileV6>(path.join('..', WANTED_LOCKFILE))
expect(lockfile.importers).toHaveProperty(['project-2'])
// previous entries are not removed
const id = '/@pnpm.e2e/pkg-with-1-dep@100.0.0'
expect(lockfile.importers.project.dependencies!['@pnpm.e2e/pkg-with-1-dep'].version).toBe('100.0.0')
expect(lockfile.importers.project.dependencies).toHaveProperty(['@zkochan/foo'])
expect(lockfile.importers.project.dependencies!['is-negative'].version).toContain('/')
expect(lockfile.packages).toHaveProperty([id])
expect(lockfile.packages![id].dependencies).toBeTruthy()
expect(lockfile.packages![id].dependencies).toHaveProperty(['@pnpm.e2e/dep-of-pkg-with-1-dep'])
expect(lockfile.packages![id].resolution).toHaveProperty(['integrity'])
expect(lockfile.packages![id].resolution).not.toHaveProperty(['tarball'])
const absDepPath = 'github.com/kevva/is-negative/1d7e288222b53a0cab90a331f1865220ec29560c'
expect(lockfile.packages).toHaveProperty([absDepPath])
expect(lockfile.packages![absDepPath].name).toBeTruthy()
}
})
test(`doing named installation when shared ${WANTED_LOCKFILE} exists already`, async () => {
const pkg1 = {
name: 'pkg1',
version: '1.0.0',
dependencies: {
'is-negative': '^2.1.0',
},
}
let pkg2: ProjectManifest = {
name: 'pkg2',
version: '1.0.0',
dependencies: {
'is-positive': '^3.1.0',
},
}
const projects = preparePackages([
pkg1,
pkg2,
])
await writeYamlFile(WANTED_LOCKFILE, {
importers: {
pkg1: {
dependencies: {
'is-negative': {
specifier: '^2.1.0',
version: '2.1.0',
},
},
},
pkg2: {
dependencies: {
'is-positive': {
specifier: '^3.1.0',
version: '3.1.0',
},
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'/is-negative@2.1.0': {
resolution: {
tarball: `http://localhost:${REGISTRY_MOCK_PORT}/is-negative/-/is-negative-2.1.0.tgz`,
},
},
'/is-positive@3.1.0': {
resolution: {
integrity: 'sha512-8ND1j3y9/HP94TOvGzr69/FgbkX2ruOldhLEsTWwcJVfo4oRjwemJmJxt7RJkKYH8tz7vYBP9JcKQY8CLuJ90Q==',
},
},
},
}, { lineWidth: 1000 })
pkg2 = await addDependenciesToPackage(
pkg2,
['is-positive'],
await testDefaults({
dir: path.resolve('pkg2'),
lockfileDir: process.cwd(),
})
)
const currentLockfile = await readYamlFile<LockfileV6>(path.resolve('node_modules/.pnpm/lock.yaml'))
expect(Object.keys(currentLockfile['importers'])).toStrictEqual(['pkg2'])
await mutateModules(
[
{
mutation: 'install',
rootDir: path.resolve('pkg1'),
},
{
mutation: 'install',
rootDir: path.resolve('pkg2'),
},
],
await testDefaults({
allProjects: [
{
buildIndex: 0,
manifest: pkg1,
rootDir: path.resolve('pkg1'),
},
{
buildIndex: 0,
manifest: pkg2,
rootDir: path.resolve('pkg2'),
},
],
})
)
await projects['pkg1'].has('is-negative')
await projects['pkg2'].has('is-positive')
})