-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
shrinkwrap.js
1715 lines (1565 loc) · 51.1 KB
/
shrinkwrap.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
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
const Shrinkwrap = require('../lib/shrinkwrap.js')
const Node = require('../lib/node.js')
const Link = require('../lib/link.js')
const calcDepFlags = require('../lib/calc-dep-flags.js')
const fs = require('fs')
const Arborist = require('../lib/arborist/index.js')
const rimraf = require('rimraf')
const t = require('tap')
const normalizePath = path => path.replace(/[A-Z]:/, '').replace(/\\/g, '/')
t.cleanSnapshot = s => s.split(process.cwd()).join('{CWD}')
const { resolve } = require('path')
const fixture = resolve(__dirname, 'fixtures/install-types')
const swonlyFixture = resolve(__dirname, 'fixtures/install-types-sw-only')
const YarnLock = require('../lib/yarn-lock.js')
const yarnFixture = resolve(__dirname, 'fixtures/yarn-stuff')
const emptyFixture = resolve(__dirname, 'fixtures/empty')
const depTypesFixture = resolve(__dirname, 'fixtures/dev-deps')
const badJsonFixture = resolve(__dirname, 'fixtures/testing-peer-deps-bad-sw')
const hiddenLockfileFixture = resolve(__dirname, 'fixtures/hidden-lockfile')
const hidden = 'node_modules/.package-lock.json'
const saxFixture = resolve(__dirname, 'fixtures/sax')
// start out with the file being fresh
const later = Date.now() + 10000
fs.utimesSync(
resolve(hiddenLockfileFixture, hidden),
new Date(later),
new Date(later)
)
t.test('shrinkwrap key order', async t => t.matchSnapshot(Shrinkwrap.keyOrder))
t.test('path defaults to .', async t => {
const sw = new Shrinkwrap()
t.equal(sw.path, process.cwd())
})
t.test('load and change lockfileVersion', async t => {
const vDefault = await Shrinkwrap.load({ path: fixture })
const v1 = await Shrinkwrap.load({ path: fixture, lockfileVersion: 1 })
const v2 = await Shrinkwrap.load({ path: fixture, lockfileVersion: 2 })
const v3 = await Shrinkwrap.load({ path: fixture, lockfileVersion: 3 })
t.strictSame(vDefault, v2, 'default is same as version 2')
const v1Data = await v1.commit()
const v2Data = await v2.commit()
const v3Data = await v3.commit()
t.strictSame(v2Data, { ...v1Data, ...v3Data, lockfileVersion: 2 },
'v2 is superset of v1 and v3')
t.equal(v1Data.packages, undefined, 'v1 data does not have packages')
t.equal(v3Data.dependencies, undefined, 'v3 data does not have dependencies')
})
t.test('load and then reset gets empty lockfile', t =>
Shrinkwrap.load({ path: fixture }).then(sw => {
sw.reset()
t.strictSame(sw.data, {
lockfileVersion: 2,
requires: true,
dependencies: {},
packages: {},
})
t.equal(sw.loadedFromDisk, true)
t.equal(sw.filename, resolve(fixture, 'package-lock.json'))
}))
t.test('starting out with a reset lockfile is an empty lockfile', t =>
Shrinkwrap.reset({ path: fixture }).then(sw => {
t.strictSame(sw.data, {
lockfileVersion: 2,
requires: true,
dependencies: {},
packages: {},
})
t.equal(sw.originalLockfileVersion, 2)
t.equal(sw.loadedFromDisk, true)
t.equal(sw.filename, resolve(fixture, 'package-lock.json'))
}))
t.test('reset in a bad dir gets an empty lockfile with no lockfile version', async t => {
const nullLockDir = t.testdir({
'package-lock.json': JSON.stringify(null),
})
const [swMissingLock, swNullLock] = await Promise.all([
Shrinkwrap.reset({ path: 'path/which/does/not/exist' }),
Shrinkwrap.reset({ path: nullLockDir }),
])
t.strictSame(swMissingLock.data, {
lockfileVersion: 2,
requires: true,
dependencies: {},
packages: {},
})
t.equal(swMissingLock.lockfileVersion, null)
t.equal(swMissingLock.loadedFromDisk, false)
t.strictSame(swNullLock.data, {
lockfileVersion: 2,
requires: true,
dependencies: {},
packages: {},
})
t.equal(swNullLock.lockfileVersion, null)
t.equal(swNullLock.loadedFromDisk, true)
})
t.test('loading in bad dir gets empty lockfile', t =>
Shrinkwrap.load({ path: 'path/which/does/not/exist' }).then(sw => {
t.strictSame(sw.data, {
lockfileVersion: 2,
requires: true,
dependencies: {},
packages: {},
})
t.equal(sw.loadedFromDisk, false)
}))
t.test('failure to parse json gets empty lockfile', t =>
Shrinkwrap.load({ path: badJsonFixture }).then(sw => {
t.strictSame(sw.data, {
lockfileVersion: 2,
requires: true,
dependencies: {},
packages: {},
})
t.equal(sw.loadedFromDisk, false)
}))
t.test('loading in empty dir gets empty lockfile', t =>
Shrinkwrap.load({ path: emptyFixture }).then(sw => {
t.strictSame(sw.data, {
lockfileVersion: 2,
requires: true,
dependencies: {},
packages: {},
})
t.equal(sw.loadedFromDisk, false)
// update with an empty node, set name to node name, not package name
const root = new Node({
path: emptyFixture,
realpath: emptyFixture,
})
root.peer = false
root.dev = false
root.devOptional = false
root.optional = false
root.extraneous = false
sw.add(root)
t.strictSame(sw.commit(), {
name: 'empty',
lockfileVersion: 2,
requires: true,
packages: {},
})
}))
t.test('look up from locks and such', t =>
new Shrinkwrap({ path: fixture }).load().then(m => {
t.strictSame(m.get(''), {
name: 'a',
version: '1.2.3',
dependencies: {
abbrev: '^1.1.1',
'full-git-url': 'git+https://github.com/isaacs/abbrev-js.git',
ghshort: 'github:isaacs/abbrev-js',
old: 'npm:abbrev@^1.0.3',
pinned: 'npm:abbrev@^1.1.1',
reg: 'npm:abbrev@^1.1.1',
remote: 'https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz',
symlink: 'file:./abbrev-link-target',
tarball: 'file:abbrev-1.1.1.tgz',
bundler: '1.2.3',
},
}, 'root metadata')
t.match(m.data, {
lockfileVersion: 2,
requires: true,
dependencies: Object,
packages: Object,
})
t.equal(m.loadedFromDisk, true)
t.matchSnapshot(m.get('node_modules/abbrev'), 'basic package')
t.matchSnapshot(m.get(
'node_modules/abbrev/node_modules/@scope/name/node_modules/@otherscope/othername', 'scoped package'))
t.matchSnapshot(m.get('package/not/found'), 'not found')
t.matchSnapshot(m.get('node_modules/old/node_modules/notfound'), 'fall off the dep tree')
t.test('lockfile', t => {
const p = m.data.packages
m.data.packages = {}
t.matchSnapshot(m.get('node_modules/abbrev'), 'basic pkg, from lock')
t.matchSnapshot(m.data.packages, 'saved fetched metadata back to packages section')
t.matchSnapshot(m.get(
'node_modules/abbrev/node_modules/@scope/name/node_modules/@otherscope/othername', 'scoped package'))
t.matchSnapshot(m.get('package/not/found'), 'not found')
t.matchSnapshot(m.get('node_modules/full-git-url'), 'full git')
t.matchSnapshot(m.get('node_modules/symlink'), 'symlink')
t.matchSnapshot(m.get('node_modules/unhosted-git'), 'unhosted git')
m.data.packages = p
t.end()
})
}))
t.test('load a shrinkwrap with some dev and optional flags', t =>
Shrinkwrap.load({ path: depTypesFixture }).then(m =>
t.matchSnapshot(m.data, 'got expected dependency types')))
t.test('load a legacy shrinkwrap without a package.json', t =>
Shrinkwrap.load({ path: swonlyFixture }).then(m =>
t.matchSnapshot(m.data, 'did our best with what we had')))
t.test('throws when attempting to access data before loading', t => {
t.throws(() =>
new Shrinkwrap().get(), Error('run load() before getting or setting data'))
t.throws(() =>
new Shrinkwrap().delete(), Error('run load() before getting or setting data'))
t.throws(() =>
new Shrinkwrap().add(), Error('run load() before getting or setting data'))
t.throws(() =>
new Shrinkwrap().toJSON(), Error('run load() before getting or setting data'))
t.throws(() =>
new Shrinkwrap().toString(), Error('run load() before getting or setting data'))
t.throws(() =>
new Shrinkwrap().save(), Error('run load() before saving data'))
t.end()
})
t.only('resolveOptions', async t => {
const url = 'https://private.registry.org/deadbeef/registry/-/registry-1.2.3.tgz'
const someOtherRegistry = 'https://someother.registry.org/registry/-/registry-1.2.3.tgz'
const getData = async (resolveOptions) => {
const dir = t.testdir()
const meta = await Shrinkwrap.load({
path: dir,
resolveOptions,
})
const root = new Node({
pkg: {
name: 'root',
dependencies: {
registry: '^1.0.0',
'some-other-registry': '^1.0.0',
'@scope/some-package': '^1.0.0',
tar: url,
},
},
path: dir,
realpath: dir,
meta,
})
const registry = new Node({
pkg: { name: 'registry', version: '1.2.3' },
resolved: url,
integrity: 'sha512-registry',
parent: root,
})
const otherRegistry = new Node({
pkg: { name: 'some-other-registry', version: '1.2.3' },
resolved: someOtherRegistry,
integrity: 'sha512-registry',
parent: root,
})
const scopedOtherRegistry = new Node({
pkg: { name: '@scope/some-package', version: '1.2.3' },
resolved: someOtherRegistry,
integrity: 'sha512-registry',
parent: root,
})
const tar = new Node({
pkg: { name: 'tar', version: '1.2.3' },
resolved: url,
integrity: 'sha512-registry',
parent: root,
})
calcDepFlags(root)
meta.add(root)
return { data: meta.commit(), registry, tar, root, otherRegistry, scopedOtherRegistry }
}
await t.test('omitLockfileRegistryResolved', async t => {
const { data } = await getData({ omitLockfileRegistryResolved: true })
// registry dependencies in v2 packages and v1 dependencies should
// have resolved stripped.
t.strictSame(data.packages['node_modules/registry'].resolved, undefined)
t.strictSame(data.dependencies.registry.resolved, undefined)
// tar should have resolved because it is not a registry dep.
t.strictSame(data.packages['node_modules/tar'].resolved, url)
// v1 url dependencies never have resolved.
t.strictSame(data.dependencies.tar.resolved, undefined)
})
await t.test('omitLockfileRegistryResolved: false', async t => {
const { data } = await getData({ omitLockfileRegistryResolved: false })
t.strictSame(data.packages['node_modules/registry'].resolved, url)
t.strictSame(data.dependencies.registry.resolved, url)
t.strictSame(data.packages['node_modules/tar'].resolved, url)
// v1 url dependencies never have resolved.
t.strictSame(data.dependencies.tar.resolved, undefined)
})
await t.test('recordDefaultRegistry: true', async t => {
const { data } = await getData({
recordDefaultRegistry: true,
registry: 'https://private.registry.org/deadbeef',
'@scope:registry': 'https://someother.registry.org',
})
// unscoped packages that resolve to their configured registry should be
// record the default registry
t.strictSame(data.packages['node_modules/registry'].resolved,
'https://registry.npmjs.org/registry/-/registry-1.2.3.tgz')
t.strictSame(data.dependencies.registry.resolved,
'https://registry.npmjs.org/registry/-/registry-1.2.3.tgz')
// scoped packages that resolve to their configured registry should be
// record the default registry
t.strictSame(data.packages['node_modules/@scope/some-package'].resolved,
'https://registry.npmjs.org/registry/-/registry-1.2.3.tgz')
t.strictSame(data.dependencies['@scope/some-package'].resolved,
'https://registry.npmjs.org/registry/-/registry-1.2.3.tgz')
// packages with resolved urls that don't match the configured registry
// should record undefined so npm resolves their url again.
t.strictSame(data.packages['node_modules/some-other-registry'].resolved, undefined)
t.strictSame(data.dependencies['some-other-registry'].resolved, undefined)
})
t.test('metaFromNode default', async t => {
// test to cover options default.
const { registry } = await getData(undefined)
t.strictSame(Shrinkwrap.metaFromNode(registry, '').resolved, url)
})
})
t.test('construct metadata from node and package data', t => {
const meta = new Shrinkwrap({ path: '/home/user/projects/root' })
// fake load
meta.data = {
lockfileVersion: 2,
requires: true,
dependencies: {},
packages: {},
}
const root = new Node({
pkg: {
name: 'root',
dependencies: { a: '', link: '', link2: '' },
devDependencies: { d: '', e: 'https://foo.com/e.tgz', devit: '' },
optionalDependencies: { optin: '' },
peerDependencies: { peer: '' },
scripts: {
install: 'true',
},
},
path: '/home/user/projects/root',
realpath: '/home/user/projects/root',
meta,
})
const peer = new Node({
pkg: {
name: 'peer',
version: '1.2.3',
dependencies: { peerdep: '' },
},
resolved: 'https://peer.com/peer.tgz',
integrity: 'sha512-peerpeerpeer',
parent: root,
})
const peerdep = new Node({
pkg: {
name: 'peerdep',
version: '1.2.3',
},
resolved: 'https://peer.com/peerdep.tgz',
integrity: 'sha512-peerdeppeerdep',
parent: peer,
})
const e = new Node({
pkg: {
name: 'e',
version: '1.2.3',
dependencies: {
tgz: '',
'tgz-pkg-resolved': '',
},
// expect to get "license": "blurb" in the metadata, not an object
license: {
type: 'blurb',
url: 'http://example.com/',
},
},
resolved: 'https://foo.com/e.tgz',
parent: root,
})
const tgz = new Node({
pkg: {
name: 'tgz',
version: '1.2.3',
funding: { url: 'https://example.com/' },
engines: {
node: '>=10',
},
os: ['any', '!win32'],
cpu: ['x64'],
},
resolved: '/home/user/projects/root/archives/tarball.tgz',
parent: root,
})
const tgzPkgResolved = new Node({
pkg: {
name: 'tgz-pkg-resolved',
version: '1.2.3',
funding: { url: 'https://example.com/' },
_resolved: '/home/user/projects/root/archives/tarball-pkg-resolved.tgz',
_integrity: 'sha512-tarball/package/resolved/integrity',
},
parent: root,
})
const link = new Link({
path: '/home/user/projects/root/node_modules/link',
realpath: '/home/user/projects/root/target',
parent: root,
pkg: {
name: 'link',
version: '1.2.3',
funding: 'https://example.com/payme',
_resolved: 'github:isaacs/foobarbaz#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
},
target: new Node({
realpath: '/home/user/projects/root/target',
path: '/home/user/projects/root/target',
root,
pkg: {
name: 'link',
version: '1.2.3',
funding: 'https://example.com/payme',
_resolved: 'github:isaacs/foobarbaz#aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
},
}),
})
const a = new Node({
resolved: 'https://example.com/a.tgz',
integrity: 'sha512-helloyesthisisdog',
pkg: {
name: 'a',
version: '1.2.3',
},
parent: root,
})
const optin = new Node({
pkg: {
name: 'optin',
version: '1.2.3',
dependencies: { devo: '' },
},
parent: root,
})
const devit = new Node({
pkg: {
name: 'devit',
version: '1.2.3',
dependencies: { devo: '' },
},
parent: root,
})
const devo = new Node({
pkg: {
name: 'devo',
version: '1.2.3',
},
parent: root,
})
const d = new Node({
pkg: {
name: 'd',
version: '1.2.3',
_shasum: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
bundleDependencies: ['bundled'],
dependencies: {
e: '1.2.3',
bundled: 'npm:not-bundled-tho@*',
'git-dep': 'github:foo/bar',
},
},
parent: root,
})
const bundled = new Node({
pkg: {
name: 'not-bundled-tho',
version: '1.2.3',
},
path: d.path + '/node_modules/bundled',
name: 'bundled',
parent: d,
})
const git = new Node({
pkg: {
name: 'git-dep',
version: '1.2.3',
},
parent: d,
resolved: 'git+ssh://git@github.com/foo/bar.git#0000000000000000000000000000000000000000',
})
const nopkg = new Node({
path: root.path + '/node_modules/nopkg',
parent: root,
})
calcDepFlags(root)
t.matchSnapshot(meta.get(''), 'root metadata, no package version')
root.package.version = '1.2.3'
meta.add(root)
t.matchSnapshot(meta.get(''), 'root metadata, with package version')
t.matchSnapshot(meta.get(optin.location), 'meta for optional dep')
t.matchSnapshot(meta.get(devit.location), 'meta for dev dep')
t.matchSnapshot(meta.get(devo.location), 'meta for devOptional dep')
t.matchSnapshot(meta.get(tgz.location), 'metadata for tarball file pkg')
t.matchSnapshot(meta.get(tgzPkgResolved.location),
'metadata for tarball file pkg with _resolved value')
t.matchSnapshot(meta.get(link.path), 'link metadata')
t.matchSnapshot(meta.get(link.target.path), 'link target metadata')
t.matchSnapshot(meta.get(a.location), 'dep a metadata')
t.matchSnapshot(meta.get(d.location), 'dep d metadata')
t.matchSnapshot(meta.get(e.location), 'dep e metadata')
t.matchSnapshot(meta.get(bundled.location), 'bundled pkg metadata')
t.matchSnapshot(meta.get(git.location), 'git dep metadata')
t.matchSnapshot(meta.get(nopkg.location), 'node without a package')
t.matchSnapshot(meta.get(peer.location), 'a peer dep')
t.matchSnapshot(meta.get(peerdep.location), 'a peer meta-dep')
t.matchSnapshot(meta.commit(), 'data calculated from nodes themselves')
t.end()
})
t.test('saving dependency-free shrinkwrap object', t => {
const dir = t.testdir({
'package.json': JSON.stringify({
name: 'badsave',
version: '1.0.0',
description: 'no node_modules/ will fail',
}),
})
t.test('save meta lockfile into node_modules directory', async t => {
const sw = await Shrinkwrap.load({ path: dir, hiddenLockfile: true })
t.equal(
sw.filename,
resolve(`${dir}/node_modules/.package-lock.json`),
'correct filepath on shrinkwrap instance'
)
// save does not throw, but doens't write the file
await sw.save()
t.throws(() => fs.statSync(sw.filename))
})
t.test('if save fails, it does throw, if not a hidden lockfile', async t => {
const sw = await Shrinkwrap.load({ path: dir, hiddenLockfile: false })
sw.filename = dir + '/this/path/does/not/exist.json'
await t.rejects(sw.save(), { code: 'ENOENT' })
t.throws(() => fs.statSync(sw.filename))
})
t.test('save lockfile to root directory', async t => {
const sw = await Shrinkwrap.load({ path: dir })
t.equal(
sw.filename,
resolve(`${dir}/package-lock.json`),
'correct filepath on shrinkwrap instance'
)
await sw.save({ format: false })
fs.statSync(sw.filename)
t.matchSnapshot(fs.readFileSync(sw.filename, 'utf8'), 'no indent json output')
})
t.test('load the unindented file, and generate expected contents', async t => {
const sw = await Shrinkwrap.load({ path: dir })
t.equal(
sw.filename,
resolve(`${dir}/package-lock.json`),
'correct filepath on shrinkwrap instance'
)
t.equal(sw.indent, '')
const json = await sw.toJSON()
t.matchSnapshot(json, 'indented json object output')
const jsonString = await sw.toString()
t.matchSnapshot(jsonString, 'indented json string output')
})
t.test('load the unindented file, and save it back default', async t => {
const sw = await Shrinkwrap.load({ path: dir })
t.equal(
sw.filename,
resolve(`${dir}/package-lock.json`),
'correct filepath on shrinkwrap instance'
)
t.equal(sw.indent, '')
await sw.save()
t.matchSnapshot(fs.readFileSync(sw.filename, 'utf8'), 'indented json output')
})
t.test('load file, and save it with a custom format', async t => {
const sw = await Shrinkwrap.load({ path: dir })
t.equal(
sw.filename,
resolve(`${dir}/package-lock.json`),
'correct filepath on shrinkwrap instance'
)
await sw.save({ format: '\t' })
t.matchSnapshot(fs.readFileSync(sw.filename, 'utf8'), 'custom indented json output')
})
t.end()
})
t.test('write the shrinkwrap back to disk', t => {
const dir = t.testdir({})
t.test('just read and write back', t =>
Shrinkwrap.load({ path: fixture }).then(s => {
s.filename = dir + '/test-shrinkwrap.json'
return s.save().then(() =>
t.strictSame(require(s.filename), s.data, 'saved json matches data'))
}))
t.test('write back with pending changes', t =>
Shrinkwrap.load({ path: fixture }).then(s => {
const dir = t.testdir({})
s.filename = dir + '/test-shrinkwrap-with-changes.json'
const node = new Node({
path: fixture + '/node_modules/newthing',
pkg: {
name: 'newthing',
version: '1.2.3',
},
})
s.add(node)
const preCommit = JSON.parse(JSON.stringify(s.data))
const postCommit = s.commit()
t.notSame(postCommit, preCommit, 'committing changes the data')
// delete and re-add to put us back in the pre-commit state
s.delete(node.location)
s.add(node)
return s.save().then(() => {
t.strictSame(s.data, postCommit, 'committed changes to data')
t.strictSame(require(s.filename), s.data, 'saved json matches data')
})
}))
t.end()
})
t.test('load shrinkwrap if no package-lock.json present', t => {
const dir = t.testdir({
'npm-shrinkwrap.json': JSON.stringify({
lockfileVersion: 1,
name: 'foo',
version: '1.2.3',
}),
})
return Promise.all([
Shrinkwrap.load({ path: dir, shrinkwrapOnly: true }).then(s =>
t.equal(s.type, 'npm-shrinkwrap.json', 'loaded with swonly')),
Shrinkwrap.reset({ path: dir, shrinkwrapOnly: true }).then(s =>
t.equal(s.type, 'npm-shrinkwrap.json', 'loaded fresh')),
Shrinkwrap.load({ path: dir }).then(s =>
t.equal(s.type, 'npm-shrinkwrap.json', 'loaded without swonly')),
Shrinkwrap.reset({ path: dir }).then(s =>
t.equal(s.type, 'npm-shrinkwrap.json', 'loaded fresh without swonly')),
])
})
t.test('load yarn.lock file if present', t =>
Shrinkwrap.load({ path: yarnFixture }).then(s => {
t.type(s.yarnLock, YarnLock, 'loaded a yarn lock file')
t.not(s.yarnLock.entries.size, 0, 'got some entries')
}))
t.test('save yarn lock if loaded', t =>
Shrinkwrap.load({ path: yarnFixture }).then(s => {
s.path = t.testdir()
s.filename = s.path + '/package-lock.json'
return s.save()
.then(() => Shrinkwrap.load({ path: s.path }))
.then(ss => t.strictSame(s.yarnLock, ss.yarnLock))
}))
t.test('ignore yarn lock file parse errors', t => {
const dir = t.testdir({
'yarn.lock': 'this is not a yarn lock file!',
})
return Shrinkwrap.load({ path: dir }).then(s => {
t.type(s.yarnLock, YarnLock, 'got a yarn lock object because a yarn lock exists')
t.equal(s.yarnLock.entries.size, 0, 'did not get any entries out of it')
})
})
t.test('load a resolution from yarn.lock if we dont have our own', async t => {
const path = t.testdir({
'yarn.lock': `
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
mkdirp@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.2.tgz#5ccd93437619ca7050b538573fc918327eba98fb"
integrity sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==
mcdork@^1.0.0:
version "1.0.2"
resolved "https://registry.npmjs.org/mcdork/-/mcdork-1.0.2.tgz#5ccd93437619ca7050b538573fc918327eba98fb"
integrity sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==
otherreg@^1.0.0:
version "1.0.2"
resolved "https://otherreg.registry.npm/othrreg/-/othrreg-1.0.2.tgz#5ccd93437619ca7050b538573fc918327eba98fb"
integrity sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==
brokenempty@^1.2.3:
version "1.2.3"
regalias@xyz:
version "1.0.2"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.2.tgz#5ccd93437619ca7050b538573fc918327eba98fb"
integrity sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==
`,
'package.json': JSON.stringify({
dependencies: {
mkdirp: '^1.0.2',
},
}),
})
const s = await Shrinkwrap.load({ path })
{
const opt = {}
t.equal(s.checkYarnLock('mkdirp@^1.0.2', opt).raw, 'mkdirp@1.0.2')
t.strictSame(opt, {
resolved: 'https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.2.tgz',
integrity: 'sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==',
})
}
{
const opt = {}
t.equal(s.checkYarnLock('mcdork@^1.0.0', opt).raw, 'mcdork@1.0.2')
t.strictSame(opt, {
resolved: 'https://registry.npmjs.org/mcdork/-/mcdork-1.0.2.tgz',
integrity: 'sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==',
})
}
{
const opt = {}
t.equal(s.checkYarnLock('notfound@^1.0.0', opt).raw, 'notfound@^1.0.0')
t.strictSame(opt, {})
}
{
const opt = {}
t.equal(s.checkYarnLock('otherreg@^1.0.0', opt).raw, 'otherreg@https://otherreg.registry.npm/othrreg/-/othrreg-1.0.2.tgz')
t.strictSame(opt, {
resolved: 'https://otherreg.registry.npm/othrreg/-/othrreg-1.0.2.tgz',
integrity: 'sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==',
})
}
{
const opt = {}
t.equal(s.checkYarnLock('brokenempty@^1.2.3', opt).raw, 'brokenempty@^1.2.3')
t.strictSame(opt, {})
}
{
const opt = {}
t.equal(s.checkYarnLock('regalias@xyz', opt).raw, 'regalias@npm:mkdirp@1.0.2')
t.strictSame(opt, {
resolved: 'https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.2.tgz',
integrity: 'sha512-N2REVrJ/X/jGPfit2d7zea2J1pf7EAR5chIUcfHffAZ7gmlam5U65sAm76+o4ntQbSRdTjYf7qZz3chuHlwXEA==',
})
}
t.equal(s.checkYarnLock('mkdirp').raw, 'mkdirp')
{
s.loadedFromDisk = true
const opt = {}
t.equal(s.checkYarnLock('regalias@xyz', opt).raw, 'regalias@xyz')
t.strictSame(opt, {})
s.loadedFromDisk = false
}
{
const { yarnLock } = s
s.yarnLock = null
const opt = {}
t.equal(s.checkYarnLock('regalias@xyz', opt).raw, 'regalias@xyz')
t.strictSame(opt, {})
s.yarnLock = yarnLock
}
})
t.test('handle missing dependencies object without borking', t => {
const s = new Shrinkwrap({ path: '/path/to/root' })
s.data = {
packages: {
'node_modules/foo': {},
'node_modules/foo/node_modules/bar': {},
},
dependencies: {
foo: {
resolved: 'http://foo.com',
integrity: 'foo integrity',
// no dependencies object here!
},
},
}
s.delete('/path/to/root/node_modules/foo/node_modules/bar')
t.matchSnapshot(s.commit())
t.end()
})
t.test('load a hidden lockfile', t => {
// ensure the hidden lockfile is newer than the contents
// otherwise this can fail on a fresh checkout.
fs.utimesSync(resolve(hiddenLockfileFixture, hidden), new Date(), new Date())
return Shrinkwrap.load({
path: hiddenLockfileFixture,
hiddenLockfile: true,
}).then(s => {
t.matchSnapshot(s.data)
// make sure it does not add to the dependencies block when a new
// node is added.
s.data.dependencies = {}
s.add(new Node({
path: hiddenLockfileFixture + '/node_modules/foo',
pkg: {
name: 'foo',
version: '1.2.3',
_integrity: 'sha512-deadbeef',
_resolved: 'https://registry.npmjs.org/foo/-/foo-1.2.3.tgz',
},
}))
t.strictSame(s.data.dependencies, {}, 'did not add to legacy data')
const data = s.commit()
t.equal(data.packages[''], undefined, 'no root entry')
t.equal(data.dependencies, undefined, 'deleted legacy metadata')
})
})
t.test('load a fresh hidden lockfile', t => Shrinkwrap.reset({
path: hiddenLockfileFixture,
hiddenLockfile: true,
}).then(sw => {
t.strictSame(sw.data, {
lockfileVersion: 3,
requires: true,
dependencies: {},
packages: {},
})
t.equal(sw.loadedFromDisk, true)
t.equal(sw.filename, resolve(hiddenLockfileFixture, hidden))
}))
t.test('hidden lockfile only used if up to date', async t => {
const lockdata = require(resolve(hiddenLockfileFixture, hidden))
const path = t.testdir({
node_modules: {
'.package-lock.json': JSON.stringify(lockdata),
abbrev: {
'package.json': JSON.stringify({ name: 'abbrev', version: '1.1.1' }),
},
},
'package.json': JSON.stringify({ dependencies: { abbrev: '1.1.1' } }),
})
// ensure that the lockfile is fresh to start
{
const later = Date.now() + 10000
fs.utimesSync(resolve(path, hidden), new Date(later), new Date(later))
const s = await Shrinkwrap.load({ path, hiddenLockfile: true })
t.equal(s.loadedFromDisk, true, 'loading from fresh lockfile')
}
// make the node_modules dir have a newer mtime by adding an entry
// and setting the hidden lockfile back in time
{
fs.mkdirSync(resolve(path, 'node_modules/xyz'))
const time = new Date('1999-12-31T23:59:59Z')
fs.utimesSync(resolve(path, hidden), time, time)
const s = await Shrinkwrap.load({ path, hiddenLockfile: true })
t.equal(s.loadedFromDisk, false, 'did not load from disk, updated nm')
t.equal(s.loadingError, 'out of date, updated: node_modules')
}
// make the lockfile newer, but that new entry is still a problem
{
const later = Date.now() + 10000
fs.utimesSync(resolve(path, hidden), new Date(later), new Date(later))
const s = await Shrinkwrap.load({ path, hiddenLockfile: true })
t.equal(s.loadedFromDisk, false, 'did not load, new entry')
t.equal(s.loadingError, 'missing from lockfile: node_modules/xyz')
}
// make the lockfile newer, but missing a folder from node_modules
{
rimraf.sync(resolve(path, 'node_modules/abbrev'))
rimraf.sync(resolve(path, 'node_modules/xyz'))
const later = Date.now() + 10000
fs.utimesSync(resolve(path, hidden), new Date(later), new Date(later))
const s = await Shrinkwrap.load({ path, hiddenLockfile: true })
t.equal(s.loadedFromDisk, false, 'did not load, missing entry')
t.equal(s.loadingError, 'missing from node_modules: node_modules/abbrev')
}
})
t.test('hidden lockfile understands symlinks', async t => {
const path = t.testdir({
node_modules: {
'.package-lock.json': JSON.stringify({
name: 'hidden-lockfile-with-symlink',
lockfileVersion: 2,
requires: true,
packages: {
abbrev: {
version: '1.1.1',
},
'node_modules/abbrev': {
resolved: 'abbrev',
link: true,
},
},
}),
abbrev: t.fixture('symlink', '../abbrev'),
// a symlink missing a target is not relevant
// windows cannot handle missing symlink targets, though
...(process.platform === 'win32' ? {} : {
missing: t.fixture('symlink', '../missing'),
}),
},
abbrev: {
'package.json': JSON.stringify({
name: 'abbrev',
version: '1.1.1',
}),
},
'package.json': JSON.stringify({
dependencies: {
abbrev: 'file:abbrev',
},
}),
})
// respect it if not newer, and the target included in shrinkwrap
{
const later = Date.now() + 10000
fs.utimesSync(resolve(path, hidden), new Date(later), new Date(later))
const s = await Shrinkwrap.load({ path, hiddenLockfile: true })
t.equal(s.loadedFromDisk, true, 'loaded from disk')
t.equal(s.filename, resolve(path, hidden))
}
// don't respect if the target is newer than hidden shrinkwrap
{
const later = Date.now() + 20000
fs.utimesSync(resolve(path, 'abbrev/package.json'), new Date(later), new Date(later))
fs.utimesSync(resolve(path, 'abbrev'), new Date(later), new Date(later))
const s = await Shrinkwrap.load({ path, hiddenLockfile: true })
t.equal(s.loadedFromDisk, false, 'not loaded from disk')
t.equal(s.filename, resolve(path, hidden))
}
// don't respect it if the target is not in hidden shrinkwrap
{
fs.mkdirSync(resolve(path, 'missing'))
fs.writeFileSync(resolve(path, 'missing/package.json'), JSON.stringify({
name: 'missing',
version: '1.2.3',
}))
if (process.platform === 'win32') {
fs.symlinkSync(resolve(path, 'missing'), resolve(path, 'node_modules/missing'), 'junction')