/
node.js
2983 lines (2738 loc) · 85.2 KB
/
node.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 util = require('util')
const t = require('tap')
const Node = require('../lib/node.js')
const OverrideSet = require('../lib/override-set.js')
const Link = require('../lib/link.js')
const Shrinkwrap = require('../lib/shrinkwrap.js')
const { resolve } = require('path')
const treeCheck = require('../lib/tree-check.js')
const { normalizePath, normalizePaths } = require('./fixtures/utils.js')
t.cleanSnapshot = str =>
str.split(process.cwd()).join('{CWD}')
.replace(/[A-Z]:/g, '')
.replace(/\\\\?/g, '/')
t.test('basic instantiation', t => {
const overrides = new OverrideSet({
overrides: { foo: '1' },
})
const root = new Node({
pkg: { name: 'root' },
path: '/home/user/projects/root',
realpath: '/home/user/projects/root',
overrides,
})
t.equal(root.depth, 0, 'root is depth 0')
t.equal(root.isTop, true, 'root is top')
t.equal(root.isLink, false, 'root is not a link')
t.equal(root.overrides, overrides, 'constructor copied provided overrides')
t.test('dep flags all set true', t => {
t.equal(root.dummy, false)
t.equal(root.extraneous, true)
t.equal(root.dev, true)
t.equal(root.devOptional, true)
t.equal(root.optional, true)
t.equal(root.peer, true)
t.end()
})
t.matchSnapshot(root, 'just a lone root node')
t.test('dummy node', t => {
const node = new Node({
path: '/not/a/real/path',
dummy: true,
})
t.test('dep flags all set false', t => {
t.equal(node.dummy, true)
t.equal(node.extraneous, false)
t.equal(node.dev, false)
t.equal(node.devOptional, false)
t.equal(node.optional, false)
t.equal(node.peer, false)
t.end()
})
t.end()
})
t.end()
})
t.test('testing with dep tree', async t => {
const runTest = rootMetadata => async t => {
const root = new Node({
pkg: {
name: 'root',
bundleDependencies: ['bundled'],
dependencies: { prod: '1.x', bundled: '', missing: '' },
devDependencies: { dev: '', overlap: '' },
optionalDependencies: { optional: '', overlap: '', optMissing: '' },
},
realpath: '/home/user/projects/root',
path: '/home/user/projects/root',
meta: rootMetadata,
children: [{
pkg: {
name: 'prod',
version: '1.2.3',
dependencies: { meta: '' },
peerDependencies: { peer: '' },
},
fsChildren: [{
realpath: '/home/user/projects/root/node_modules/prod/foo',
path: '/home/user/projects/root/node_modules/prod/foo',
name: 'foo',
pkg: { name: 'foo', version: '1.2.3', dependencies: { meta: '' } },
}],
resolved: 'prod',
integrity: 'prod',
}],
})
t.equal(root.root, root, 'root is its own root node')
const prod = root.children.get('prod')
t.equal(prod.fsChildren.size, 1, 'prod has one fsChild')
const foo = [...prod.fsChildren][0]
t.equal(foo.fsParent, prod, 'foo has prod as fsParent')
t.equal(prod.root, root, 'prod rooted on root')
t.equal(prod.depth, 1, 'prod is depth 1')
const meta = new Node({
pkg: {
name: 'meta',
version: '1.2.3',
devDependencies: { missing: '' },
dependencies: { bundled: '' },
_resolved: 'meta',
_integrity: 'meta',
},
path: '/home/user/projects/root/node_modules/prod/node_modules/meta',
realpath: '/home/user/projects/root/node_modules/prod/node_modules/meta',
parent: prod,
})
t.equal(meta.isDescendantOf(root), true, 'meta descends from root')
t.equal(meta.root, root, 'meta rooted in same tree via parent')
// retrieve a node using querySelectorAll
const queryResult = await root.querySelectorAll('* #meta')
t.same(queryResult, [meta], 'should retrieve node using querySelectorAll')
const bundled = new Node({
pkg: {
name: 'bundled',
version: '1.2.3',
dependencies: { meta: '' },
},
resolved: 'bundled',
integrity: 'bundled',
path: '/home/user/projects/root/node_modules/bundled',
realpath: '/home/user/projects/root/node_modules/bundled',
parent: root,
})
t.equal(meta.isDescendantOf(bundled), false, 'meta does not descend from bundled')
t.equal(bundled.root, root, 'bundled root is project root')
const dev = new Node({
pkg: {
name: 'dev',
version: '1.2.3',
},
resolved: 'dev',
integrity: 'dev',
path: '/home/user/projects/root/node_modules/dev',
realpath: '/home/user/projects/root/node_modules/dev',
parent: root,
})
t.equal(dev.root, root, 'dev root is project root')
const opt = new Node({
pkg: {
name: 'optional',
version: '1.2.3',
},
resolved: 'opt',
integrity: 'opt',
path: '/home/user/projects/root/node_modules/optional',
realpath: '/home/user/projects/root/node_modules/optional',
parent: root,
})
t.equal(opt.root, root, 'opt root is project root')
const peer = new Node({
pkg: {
name: 'peer',
version: '1.2.3',
},
resolved: 'peer',
integrity: 'peer',
path: '/home/user/projects/root/node_modules/peer',
realpath: '/home/user/projects/root/node_modules/peer',
parent: root,
})
t.equal(peer.root, root)
const extraneous = new Node({
pkg: {
name: 'extraneous',
version: '1.2.3',
},
resolved: 'extraneous',
integrity: 'extraneous',
path: '/home/user/projects/root/node_modules/extraneous',
realpath: '/home/user/projects/root/node_modules/extraneous',
parent: root,
})
t.equal(extraneous.root, root, 'extraneous.root is project root')
t.equal(prod.top, root, 'root is top of tree')
t.equal(prod.root, root, 'root is root of tree')
t.equal(root.isRoot, true, 'root is root of tree')
t.equal(prod.isRoot, false, 'prod is not root of tree')
t.equal(extraneous.extraneous, true, 'extraneous is extraneous')
t.matchSnapshot(root, 'initial load with some deps')
// move dep to top level
meta.parent = root
t.matchSnapshot(root, 'move meta to top level, update stuff')
t.equal(meta.root, root, 'meta.root is root still')
t.equal(meta.parent, root, 'meta.parent is root')
t.equal(root.inventory.get(meta.location), meta)
const newMeta = new Node({
pkg: {
name: 'meta',
version: '2.3.4',
peerDependencies: { asdf: '' },
peerDependenciesMeta: {
asdf: { optional: true },
},
},
resolved: 'newMeta',
integrity: 'newMeta',
name: 'meta',
parent: prod,
})
t.equal(newMeta.root, root)
newMeta.root = prod
t.equal(newMeta.root, root, 'setting root to non-root crawls up root list')
t.equal(meta.parent, root)
t.equal(newMeta.parent, prod)
t.equal(root.inventory.get(meta.location), meta)
// test that reparenting a link _doesn't_ update realpath
const metaMeta = new Link({
pkg: {
name: 'metameta',
version: '1.2.3',
_resolved: 'metameta',
_integrity: 'metameta',
},
path: newMeta.path + '/node_modules/metameta',
realpath: meta.realpath,
target: meta,
})
t.equal(metaMeta.root, root, 'link takes root of target if unspecified')
metaMeta.parent = newMeta
t.equal(metaMeta.root, root)
t.equal(meta.root, root)
t.equal(meta.parent, root)
t.equal(root.children.get('meta'), meta)
t.equal(root.inventory.get(meta.location), meta)
t.matchSnapshot(root, 'add new meta under prod')
t.equal(meta.parent, root, 'old meta parent is root before assigning')
newMeta.parent = root
t.equal(meta.parent, null, 'old meta parent removed')
t.not(root.children.get('meta'), meta,
'root.children no longer has old meta')
t.matchSnapshot(root, 'move new meta to top level')
newMeta.parent = root
t.matchSnapshot(root, 'move new meta to top level second time (no-op)')
t.test('replacement tests', t => {
const newProd = new Node({
name: 'prod',
pkg: {
name: 'prod',
version: '1.2.3',
dependencies: { meta: '' },
peerDependencies: { peer: '' },
},
path: '/some/path',
resolved: 'prod',
integrity: 'prod',
})
t.equal(newProd.canReplace(prod), true, 'new prod can replace prod')
const kidCount = prod.children.size
newProd.replace(prod)
t.equal(newProd.children.size, kidCount, 'kids moved to newProd')
t.equal(prod.children.size, 0, 'kids moved to newProd')
t.equal(prod.root, prod, 'prod excised from tree')
t.equal(newProd.root, root, 'newProd in the tree')
// XXX seems wrong, taking over fsChildren is weird?
t.equal(newProd.fsChildren.size, 0, 'fsChildren replaced')
t.equal(prod.fsChildren.size, 1, 'fsChildren go along with fsParent')
t.equal([...prod.fsChildren][0], foo, 'foo still in old prod fsChildren set')
t.equal(foo.fsParent, prod, 'prod is still foos fsParent')
const notProd = new Node({
pkg: {
name: 'notprod',
version: '1.2.3',
},
path: '/some/path',
})
t.equal(notProd.canReplace(newProd), false, 'cannot replace with different name')
const prodV2 = new Node({
name: 'prod',
path: '/path/to/prod',
pkg: {
name: 'prod',
version: '2.3.4',
},
})
// also call the other alias for this function, from the other dir
t.equal(newProd.canReplaceWith(prodV2), false, 'cannot replace with 2.x')
const root2 = new Node({
pkg: {
name: 'root',
bundleDependencies: ['bundled'],
dependencies: { prod: '1.x', bundled: '', missing: '' },
devDependencies: { dev: '', overlap: '' },
optionalDependencies: { optional: '', overlap: '', optMissing: '' },
},
realpath: '/home/user/projects/root',
path: '/home/user/projects/root',
meta: rootMetadata,
})
// call the inverse function, for coverage
root.replaceWith(root2)
t.equal(root2.root, root2, 'replacing root preserves self-rootedness')
root.replace(root2)
const prodLink = new Link({
pkg: prod.package,
realpath: '/some/other/path/entirely',
path: '/not/where/it/really/is',
name: 'prod',
})
t.equal(prodLink.canReplace(newProd), true, 'link can replace node')
prodLink.replace(newProd)
t.equal(newProd.parent, null, 'newProd removed from tree')
t.equal(newProd.root, newProd, 'newProd removed from tree')
t.equal(normalizePath(prodLink.path), normalizePath(newProd.path), 'replaced link')
t.equal(newProd.children.size, 0, 'newProd kids moved over')
t.equal(prodLink.children.size, 0, 'links do not have child nodes')
t.equal(prodLink.target.children.size, kidCount, 'link target has children')
t.equal(newProd.canReplace(prodLink), true, 'node can replace link')
const { target } = prodLink
newProd.replace(prodLink)
t.equal(prodLink.parent, null, 'prodLink removed from tree')
t.equal(prodLink.target, null, 'prodLink lost its target')
t.equal(prodLink.root, prodLink, 'prodLink removed from tree')
t.equal(target.root, target, 'prodLinks old target removed from tree')
t.equal(normalizePath(newProd.path), normalizePath(prodLink.path), 'replaced node')
t.equal(prodLink.children.size, 0, 'prodLink has no child nodes')
t.end()
})
t.end()
}
t.test('without meta', await runTest())
const meta = new Shrinkwrap({ path: '/home/user/projects/root' })
meta.data = {
lockfileVersion: 2,
packages: {},
dependencies: {},
}
t.test('with meta', await runTest(meta))
t.end()
})
t.test('adding two children that both have links into them', t => {
const root = new Node({
path: '/path/to/root',
})
const o1 = new Link({
parent: root,
realpath: '/path/m/node_modules/n/o1',
name: 'o1',
})
const t1 = new Node({
path: o1.realpath,
root,
})
const o2 = new Link({
parent: root,
realpath: '/path/m/node_modules/n/o2',
name: 'o2',
})
const t2 = new Node({
path: o2.realpath,
root,
})
const m = new Node({
path: '/path/m',
dummy: true,
root,
})
t.equal(o1.target, t1)
t.equal(o2.target, t2)
t.equal(t1.linksIn.has(o1), true)
t.equal(t2.linksIn.has(o2), true)
t.equal(t1.fsParent, m)
t.equal(t2.fsParent, m)
t.end()
})
t.test('edge cases for branch coverage', t => {
const noPkg = new Node({
realpath: '/home/user/projects/root',
path: '/home/user/projects/root',
})
t.same(noPkg.package, {}, 'default package is empty object')
t.equal(noPkg.name, 'root', 'root default name is . if package empty')
const noPath = new Node({
realpath: '/home/user/projects/root',
})
t.equal(noPath.name, 'root', 'pathless gets named for realpath')
t.end()
})
t.test('tracks the loading error encountered', t => {
const error = new Error('this is fine')
const root = new Node({
pkg: { name: 'root' },
path: '/home/user/projects/root',
realpath: '/home/user/projects/root',
error,
})
t.equal(root.errors[0], error, 'keeps ahold of the error')
t.end()
})
t.throws(() => new Node({ pkg: {} }), TypeError(
'could not detect node name from path or package'))
t.test('load from system-root path', t => {
const root = new Node({
path: resolve('/'),
})
t.equal(root.name, null, 'ok to have a nameless node in system root')
t.end()
})
t.test('load with a virtual filesystem parent', t => {
const Node = t.mock('../lib/node.js', {
'../lib/debug.js': a => a(),
})
const Link = t.mock('../lib/link.js', {
'../lib/node.js': Node,
})
const root = new Node({
pkg: { name: 'root', dependencies: { a: '', link: '', link2: '', link3: '' } },
path: '/home/user/projects/root',
realpath: '/home/user/projects/root',
})
const a = new Node({
pkg: { name: 'a', version: '1.2.3' },
parent: root,
name: 'a',
})
const link = new Link({
pkg: { name: 'link', version: '1.2.3', dependencies: { a: '', kid: '' } },
realpath: root.realpath + '/link-target',
parent: root,
})
t.ok(link.target, 'link has a target')
const linkKid = new Node({
pkg: { name: 'kid', dependencies: { a: '' } },
parent: link.target,
})
const link2 = new Link({
pkg: { name: 'link2', version: '1.2.3', dependencies: { link: '' } },
realpath: a.realpath + '/node_modules/link2-target',
parent: root,
fsParent: a,
})
t.equal(link2.target.parent, a, 'fsParent=parent sets parent')
t.equal(link2.target.fsParent, null, 'fsParent=parent does not set fsParent')
t.equal(link2.target.resolveParent, a, 'resolveParent is parent')
const target3 = new Node({
name: 'link3',
path: root.realpath + '/packages/link3',
realpath: root.realpath + '/packages/link3',
pkg: { name: 'link3', version: '1.2.3', dependencies: { link2: '' } },
fsParent: new Node({
path: root.realpath + '/packages',
realpath: root.realpath + '/packages',
pkg: { name: 'packages', version: '2.3.4', dependencies: { link: '' } },
}),
})
const packages = target3.fsParent
const link3 = new Link({
pkg: { name: 'link3', version: '1.2.3', dependencies: { link2: '' } },
realpath: root.realpath + '/packages/link3',
target: target3,
parent: root,
})
t.equal(target3.root, root)
t.equal(packages.fsChildren.size, 0)
packages.root = root
t.equal(normalizePath(target3.fsParent.path), normalizePath(packages.path))
t.equal(packages.fsChildren.size, 1)
t.equal(packages.location, 'packages')
t.equal(target3.location, 'packages/link3')
t.equal(normalizePath(target3.realpath), normalizePath(root.path + '/packages/link3'))
t.equal(target3.fsParent, packages)
t.equal(packages.fsChildren.size, 1)
t.equal(normalizePath(link3.realpath), normalizePath(target3.realpath))
t.equal(link3.root, target3.root)
t.equal(link3.target, target3, 'before fsParent move')
packages.fsParent = link.target
t.equal(packages.location, 'link-target/packages')
t.equal(target3.location, 'link-target/packages/link3')
t.equal(normalizePath(target3.realpath), normalizePath(root.path + '/link-target/packages/link3'))
t.equal(target3.fsParent, packages)
t.equal(packages.fsChildren.size, 1)
t.equal(normalizePath(link3.realpath), normalizePath(target3.realpath))
t.equal(link3.root, target3.root)
t.equal(link3.target, target3, 'after fsParent move')
// do it again so we can verify nothing changed. this should be a no-op.
packages.fsParent = link.target
t.equal(packages.location, 'link-target/packages')
t.equal(target3.location, 'link-target/packages/link3')
t.equal(normalizePath(target3.realpath), normalizePath(root.path + '/link-target/packages/link3'))
t.equal(target3.fsParent, packages)
t.equal(packages.fsChildren.size, 1)
t.equal(normalizePath(link3.realpath), normalizePath(target3.realpath))
t.equal(link3.root, target3.root)
t.equal(link3.target, target3, 'after fsParent move')
t.equal(normalizePath(packages.path), normalizePath(root.realpath + '/link-target/packages'))
t.equal(normalizePath(target3.path), normalizePath(root.realpath + '/link-target/packages/link3'))
t.equal(link3.target, target3, 'still targetting the right node 4')
t.equal(target3.fsParent, packages, 'link3 target under packages')
t.equal(normalizePath(link3.realpath), normalizePath(target3.path), 'link realpath updated')
// can't set fsParent to a link! set to the target instead.
const linkChild = new Node({
path: target3.path + '/linkchild',
pkg: { name: 'linkchild', version: '1.2.3' },
})
linkChild.fsParent = link3
t.equal(linkChild.fsParent, link3.target)
t.equal(linkChild.root, root)
t.equal(target3.fsParent, packages, 'link3 target under packages')
// can't set fsParent to the same node
t.throws(() => packages.fsParent = packages, {
message: 'setting node to its own fsParent',
})
t.throws(() => packages.fsParent = new Node({ path: packages.path }), {
message: 'setting fsParent to same path',
})
// can't set fsParent on a new node such that it's outside its path
const outsideNode = new Node({ path: '/not/the/root/path', pkg: {} })
t.throws(() => outsideNode.fsParent = root, {
message: 'setting fsParent improperly',
})
// automatically set fsParent based on path calculations
t.equal(link.target.fsParent, root)
t.equal(link.target.resolveParent, root, 'resolveParent is fsParent')
t.equal(link.target.edgesOut.get('a').error, null)
t.equal(linkKid.edgesOut.get('a').error, null)
t.equal(linkKid.parent, link.target)
const target = link.target
link.target.root = null
t.equal(link.target, null)
t.equal(target.edgesOut.get('a').error, 'MISSING')
t.equal(linkKid.edgesOut.get('a').error, 'MISSING')
target.fsParent = link.root
t.equal(link.target, target)
t.equal(link.target.fsParent, root)
t.equal(link.target.edgesOut.get('a').error, null)
t.equal(linkKid.edgesOut.get('a').error, null)
// move it under this other one for some reason
link.target.fsParent = link2.target
t.equal(link.target.fsParent, link2.target)
t.equal(link.target.edgesOut.get('a').error, null)
t.equal(linkKid.edgesOut.get('a').error, null)
// move it into node_modules
link.target.parent = link2.target
t.equal(link.target.fsParent, null, 'lost fsParent for parent')
t.equal(link.target.edgesOut.get('a').error, null)
t.equal(linkKid.edgesOut.get('a').error, null)
t.equal(normalizePath(link.realpath), normalizePath(link2.realpath + '/node_modules/link-target'))
const linkLoc = link.location
const linkTarget = link.target
link.parent = null
t.equal(link.root, link, 'removed from parent, removed from root')
t.equal(root.inventory.get(linkLoc), undefined, 'removed from root inventory')
t.equal(link.inventory.has(link), true, 'link added to own inventory')
t.equal(link.target, null, 'target left behind when setting root to null')
linkTarget.root = link
t.equal(link.target, linkTarget, 'target set once roots match')
t.equal(link.inventory.get(''), linkTarget)
t.equal(root.edgesOut.get('link').error, 'MISSING')
packages.fsParent = null
t.equal(packages.root, packages, 'removed from fsParent, removed from root')
// now replace the real node with a link
// ensure that everything underneath it is removed from root
const aChild = new Node({
parent: a,
pkg: { name: 'achild', version: '1.2.3' },
})
t.equal(aChild.parent, a)
t.equal(a.children.get('achild'), aChild)
const underA = [...root.inventory.values()]
.filter(node => node.path.startsWith(a.path) && node !== a)
const aLoc = a.location
const aLink = new Link({
path: a.path,
target: new Node({
path: '/some/other/a',
pkg: a.package,
}),
})
aLink.root = root
t.equal(root.inventory.get(aLoc), aLink)
t.equal(a.root, a)
for (const node of underA) {
t.not(node.root, root, `${node.path} still under old root`)
}
// create a new fsChild several steps below the root, then shove
// a link in the way of it, removing it.
const fsD = new Node({
path: root.path + '/a/b/c/d',
pkg: { name: 'd', version: '1.2.3' },
root,
})
t.equal(fsD.fsParent, root, 'root should be fsParent')
new Link({
path: root.path + '/a/b',
target: new Node({
path: '/some/exotic/location',
pkg: { name: 'b', version: '1.2.3' },
}),
root,
})
t.not(fsD.root, root, 'fsD removed from root')
// add a node completely outside the root folder, as a link
// target, then add a new node that takes over as its parent,
// to exercise the code path where a top node has no fsParent
const remoteLink = new Link({
parent: root,
name: 'x',
realpath: '/remote/node_modules/a/node_modules/x',
})
const remoteTarget = new Node({
path: '/remote/node_modules/a/node_modules/x',
realpath: '/remote/node_modules/a/node_modules/x',
pkg: { name: 'x', version: '1.2.3' },
root,
})
t.equal(remoteLink.target, remoteTarget, 'automatically found target')
t.equal(remoteTarget.fsParent, null, 'remote target has no fsParent')
t.equal(remoteTarget.parent, null, 'remote target has no parent')
root.tops.has(remoteTarget, 'remote target in root.tops')
const remoteParent = new Node({
path: '/remote/node_modules/a',
pkg: { name: 'a', version: '1.2.3' },
root,
})
t.throws(() => remoteParent.target = remoteTarget, {
message: 'cannot set target on non-Link Nodes',
path: remoteParent.path,
})
t.equal(remoteParent.children.get('x'), remoteTarget)
treeCheck(root)
t.end()
})
t.test('child of link target has path, like parent', t => {
const root = new Node({
pkg: { name: 'root', dependencies: { a: '', link: '', link2: '' } },
path: '/home/user/projects/root',
realpath: '/home/user/projects/root',
})
new Node({
pkg: { name: 'a', version: '1.2.3' },
parent: root,
name: 'a',
})
const link = new Link({
pkg: { name: 'link', version: '1.2.3', dependencies: { a: '', kid: '' } },
realpath: root.realpath + '/link-target',
parent: root,
fsParent: root,
})
const linkKid = new Node({
pkg: { name: 'kid' },
parent: link,
})
t.equal(linkKid.parent, link.target, 'setting link as parent sets target instead')
t.equal(normalizePath(linkKid.path), normalizePath(linkKid.realpath), 'child of link target path is realpath')
t.end()
})
t.test('changing root', t => {
const meta = new Shrinkwrap({ path: '/home/user/projects/root' })
meta.data = { lockfileVersion: 2, dependencies: {}, packages: {} }
const root = new Node({
pkg: { name: 'root', dependencies: { a: '', link: '', link2: '' } },
path: '/home/user/projects/root',
realpath: '/home/user/projects/root',
meta,
})
const a = new Node({
pkg: { name: 'a', version: '1.2.3' },
parent: root,
name: 'a',
resolved: 'https://example.com/a-1.2.3.tgz',
integrity: 'sha512-asdfasdfasdf',
})
const b = new Node({
pkg: { name: 'b', version: '1.2.3' },
parent: a,
name: 'b',
})
const meta2 = new Shrinkwrap({ path: '/home/user/projects/root2' })
meta2.data = { lockfileVersion: 2, dependencies: {}, packages: {} }
const root2 = new Node({
pkg: { name: 'root2', dependencies: { a: '', link: '', link2: '' } },
path: '/home/user/projects/root2',
realpath: '/home/user/projects/root2',
meta: meta2,
})
t.equal(a.root, root, 'root is root of tree from a')
t.equal(b.root, root, 'root is root of tree from b')
a.parent = root2
t.equal(a.root, root2, 'root is set when parent is changed')
t.equal(b.root, root2, 'root is set on children when parent is changed')
t.end()
})
t.test('attempt to assign parent to self on root node', t => {
// turn off debugging for this one so we don't throw
const Node = t.mock('../lib/node.js', {
'../lib/debug.js': () => {},
})
const root = new Node({
pkg: { name: 'root' },
path: '/',
realpath: '/',
})
root.parent = root.fsParent = root
t.equal(root.parent, null, 'root node parent should be empty')
t.equal(root.fsParent, null, 'root node fsParent should be empty')
t.end()
})
t.test('bundled dependencies logic', t => {
const root = new Node({
pkg: {
name: 'root',
dependencies: { a: '', b: '', d: '', e: '', f: '' },
bundleDependencies: ['a'],
},
path: '/path/to/root',
realpath: '/path/to/root',
})
const a = new Node({
pkg: { name: 'a', version: '1.2.3', dependencies: { b: '', aa: '' } },
parent: root,
})
const aa = new Node({
pkg: { name: 'aa', version: '1.2.3' },
parent: a,
})
const b = new Node({
pkg: { name: 'b', version: '1.2.3', dependencies: { c: '' } },
parent: root,
})
const c = new Node({
pkg: { name: 'c', version: '1.2.3', dependencies: { cc: '' } },
parent: root,
})
new Node({
pkg: { name: 'cc', version: '1.2.3', dependencies: { d: '' } },
parent: c,
})
const d = new Node({
pkg: { name: 'd', version: '1.2.3' },
parent: root,
})
const e = new Node({
pkg: { name: 'e', version: '1.2.3' },
parent: root,
})
const f = new Node({
pkg: {
name: 'f',
version: '1.2.3',
dependencies: { fa: '', fb: '' },
bundleDependencies: ['fb'],
},
parent: root,
})
new Node({
pkg: { name: 'fa', version: '1.2.3' },
parent: f,
})
const fb = new Node({
pkg: { name: 'fb', version: '1.2.3', dependencies: { e: '', fc: '' } },
parent: f,
})
new Node({
pkg: { name: 'fc', version: '1.2.3', dependencies: { fb: '' } },
parent: f,
})
t.equal(a.inBundle, true, 'bundled dep is bundled')
t.equal(a.inDepBundle, false, 'bundled dep is bundled by root')
t.equal(aa.inBundle, true, 'child of bundled dep is bundled')
t.equal(aa.inDepBundle, false, 'child of dep bundled by root is not dep bundled')
t.equal(b.inBundle, true, 'dep of bundled dep at peer level is bundled')
t.equal(c.inBundle, true, 'metadep of bundled dep at peer level is bundled')
t.equal(d.inBundle, true, 'deduped metadep of bundled metadep is bundled')
t.equal(e.inBundle, false, 'deduped dep of bundled dep of metadep is not bundled')
t.equal(fb.inBundle, true, 'bundled dep of dep is bundled')
t.equal(fb.inDepBundle, true, 'bundled dep of dep is dep bundled (not by root)')
t.end()
})
t.test('move fsChildren when moving to a new fsParent in same root', t => {
const root = new Node({
path: '/path/to/root',
})
const p1 = new Node({
path: '/path/to/root/p1',
root: root,
})
t.equal(p1.fsParent, root)
const p2 = new Node({
path: '/path/to/root/p2',
root: root,
})
t.equal(p2.fsParent, root)
const c2 = new Node({
path: '/path/to/root/p2/c2',
root: root,
})
t.equal(c2.fsParent, p2)
p2.fsParent = p1
t.equal(normalizePath(c2.path), normalizePath('/path/to/root/p1/p2/c2'))
t.end()
})
t.test('check if a node is in a node_modules folder or not', t => {
const a = new Node({
path: '/path/to/foo/node_modules/a',
realpath: '/path/to/foo/node_modules/a',
pkg: { name: 'a' },
})
t.equal(normalizePath(a.inNodeModules()), '/path/to/foo', 'basic obvious case')
const b = new Node({
path: '/path/to/foo/node_modules/a',
realpath: '/path/to/foo/node_modules/a',
pkg: { name: 'b' },
})
t.equal(normalizePath(b.inNodeModules()), '/path/to/foo', 'based on path name, not pkg name')
const c = new Node({
path: '/path/to/foo/node_modules/a/b/c',
realpath: '/path/to/foo/node_modules/a/b/c',
pkg: { name: 'c' },
})
t.equal(c.inNodeModules(), false, 'not directly in node_modules')
const d = new Node({
path: '/path/to/foo/node_modules/@c/d',
realpath: '/path/to/foo/node_modules/@c/d',
pkg: { name: '@a/b/c/d/e' },
})
t.equal(normalizePath(d.inNodeModules()), '/path/to/foo', 'scoped package in node_modules')
t.end()
})
t.test('update metadata when moving between linked top-of-tree parents', t => {
// this is a bit of a weird edge case, but covered for completeness.
// When moving the parent of a node, we update the metadata in the root,
// AND in the top-of-tree node, if it's not also the root (as that would be
// redundant).
const rootMeta = new Shrinkwrap({ path: '/home/user/projects/root' })
rootMeta.data = { lockfileVersion: 2, dependencies: {}, packages: {} }
const root = new Node({
pkg: { name: 'root' },
path: rootMeta.path,
realpath: rootMeta.path,
meta: rootMeta,
})
const top1Meta = new Shrinkwrap({ path: '/path/to/top1' })
top1Meta.data = { lockfileVersion: 2, dependencies: {}, packages: {} }
const top1 = new Node({
pkg: { name: 'top', version: '1.1.1' },
path: top1Meta.path,
realpath: top1Meta.path,
meta: top1Meta,
})
new Link({
name: 'link1',
parent: root,
realpath: top1.path,
target: top1,
})
const top2Meta = new Shrinkwrap({ path: '/path/to/top2' })
top2Meta.data = { lockfileVersion: 2, dependencies: {}, packages: {} }
const top2 = new Node({
pkg: { name: 'top', version: '1.1.1' },
path: top2Meta.path,
realpath: top2Meta.path,
meta: top2Meta,
})
const link2 = new Link({
name: 'link2',
parent: root,
realpath: top2.path,
target: top2,
})
const child = new Node({
parent: top1,
pkg: {
name: 'child',
version: '1.2.3',
dependencies: { child2: '2' },
},
resolved: 'https://child.com/-/child-1.2.3.tgz',
integrity: 'sha512-blortzeyblartzeyfartz',
})
const child2 = new Node({
parent: child,
pkg: { name: 'child2', version: '2.3.4' },
resolved: 'https://child.com/-/child-2.3.4.tgz',
integrity: 'sha512-a childs child is a kidkid',
})
t.matchSnapshot(child.location, 'initial child location, pre-move')
t.equal(child.root, root, 'child root is the shared root node')
t.equal(child.top, top1, 'child top is top1')
t.matchSnapshot(child2.location, 'initial child2 location, pre-move')
t.equal(child2.root, root, 'child2 root is the shared root node')
t.equal(child2.top, top1, 'child2 top is top1')
t.matchSnapshot(root.meta.get(child.location), 'metadata from root')
t.matchSnapshot(top1.meta.get(child.location), 'metadata from top1')
// now move it over
const oldLocation = child.location
const oldLocation2 = child2.location
child.parent = link2
t.equal(child.top, top2, 'after move, top points at top2')
t.equal(child.parent, top2, 'parent assigned to link target')
t.matchSnapshot(child.location, 'new child location')
t.equal(child2.top, top2, 'after move, top points at top2')
t.equal(child2.parent, child, 'parent assigned to link target')
t.matchSnapshot(child2.location, 'new child2 location')
t.matchSnapshot(root.meta.get(child.location), 'root metadata updated')
t.matchSnapshot(root.meta.get(child2.location), 'root metadata updated')
t.matchSnapshot(root.meta.get(oldLocation), 'old location deleted from root')
t.matchSnapshot(top1.meta.get(oldLocation), 'old location deleted from top1')
t.matchSnapshot(root.meta.get(oldLocation2), 'old location2 deleted from root')
t.matchSnapshot(top1.meta.get(oldLocation2), 'old location2 deleted from top1')
t.matchSnapshot(top2.meta.get(child.location), 'new top metadata updated')
t.matchSnapshot(top2.meta.get(child2.location), 'new top metadata updated')
return t.end()
})
t.test('setting package refreshes deps', t => {
const root = new Node({
pkg: {
dependencies: {
a: '1',
},
},
path: '/path/to/root',
})