Debian Codegraph @

workspaces/arborist/test/node.js

raw
1 const util = require('node:util')
2 const t = require('tap')
3 const Node = require('../lib/node.js')
4 const OverrideSet = require('../lib/override-set.js')
5 const Link = require('../lib/link.js')
6 const Shrinkwrap = require('../lib/shrinkwrap.js')
7 const { resolve } = require('node:path')
8 const treeCheck = require('../lib/tree-check.js')
9
10 const { normalizePath, normalizePaths } = require('./fixtures/utils.js')
11
12 t.cleanSnapshot = str =>
13 str.split(process.cwd()).join('{CWD}')
14 .replace(/[A-Z]:/g, '')
15 .replace(/\\\\?/g, '/')
16
17 t.test('basic instantiation', t => {
18 const overrides = new OverrideSet({
19 overrides: { foo: '1' },
20 })
21 const root = new Node({
22 pkg: { name: 'root' },
23 path: '/home/user/projects/root',
24 realpath: '/home/user/projects/root',
25 overrides,
26 })
27
28 t.equal(root.depth, 0, 'root is depth 0')
29 t.equal(root.isTop, true, 'root is top')
30 t.equal(root.isLink, false, 'root is not a link')
31 t.equal(root.overrides, overrides, 'constructor copied provided overrides')
32
33 t.test('dep flags all set true', t => {
34 t.equal(root.dummy, false)
35 t.equal(root.extraneous, true)
36 t.equal(root.dev, true)
37 t.equal(root.devOptional, true)
38 t.equal(root.optional, true)
39 t.equal(root.peer, true)
40 t.end()
41 })
42
43 t.matchSnapshot(root, 'just a lone root node')
44
45 t.test('dummy node', t => {
46 const node = new Node({
47 path: '/not/a/real/path',
48 dummy: true,
49 })
50 t.test('dep flags all set false', t => {
51 t.equal(node.dummy, true)
52 t.equal(node.extraneous, false)
53 t.equal(node.dev, false)
54 t.equal(node.devOptional, false)
55 t.equal(node.optional, false)
56 t.equal(node.peer, false)
57 t.end()
58 })
59 t.end()
60 })
61
62 t.end()
63 })
64
65 t.test('testing with dep tree', async t => {
66 const runTest = rootMetadata => async t => {
67 const root = new Node({
68 pkg: {
69 name: 'root',
70 bundleDependencies: ['bundled'],
71 dependencies: { prod: '1.x', bundled: '', missing: '' },
72 devDependencies: { dev: '', overlap: '' },
73 optionalDependencies: { optional: '', overlap: '', optMissing: '' },
74 },
75 realpath: '/home/user/projects/root',
76 path: '/home/user/projects/root',
77 meta: rootMetadata,
78 children: [{
79 pkg: {
80 name: 'prod',
81 version: '1.2.3',
82 dependencies: { meta: '' },
83 peerDependencies: { peer: '' },
84 },
85 fsChildren: [{
86 realpath: '/home/user/projects/root/node_modules/prod/foo',
87 path: '/home/user/projects/root/node_modules/prod/foo',
88 name: 'foo',
89 pkg: { name: 'foo', version: '1.2.3', dependencies: { meta: '' } },
90 }],
91 resolved: 'prod',
92 integrity: 'prod',
93 }],
94 })
95 t.equal(root.root, root, 'root is its own root node')
96 const prod = root.children.get('prod')
97 t.equal(prod.fsChildren.size, 1, 'prod has one fsChild')
98 const foo = [...prod.fsChildren][0]
99 t.equal(foo.fsParent, prod, 'foo has prod as fsParent')
100 t.equal(prod.root, root, 'prod rooted on root')
101 t.equal(prod.depth, 1, 'prod is depth 1')
102 const meta = new Node({
103 pkg: {
104 name: 'meta',
105 version: '1.2.3',
106 devDependencies: { missing: '' },
107 dependencies: { bundled: '' },
108 _resolved: 'meta',
109 _integrity: 'meta',
110 },
111 path: '/home/user/projects/root/node_modules/prod/node_modules/meta',
112 realpath: '/home/user/projects/root/node_modules/prod/node_modules/meta',
113 parent: prod,
114 })
115 t.equal(meta.isDescendantOf(root), true, 'meta descends from root')
116 t.equal(meta.root, root, 'meta rooted in same tree via parent')
117
118 // retrieve a node using querySelectorAll
119 const queryResult = await root.querySelectorAll('* #meta')
120 t.same(queryResult, [meta], 'should retrieve node using querySelectorAll')
121
122 const bundled = new Node({
123 pkg: {
124 name: 'bundled',
125 version: '1.2.3',
126 dependencies: { meta: '' },
127 },
128 resolved: 'bundled',
129 integrity: 'bundled',
130 path: '/home/user/projects/root/node_modules/bundled',
131 realpath: '/home/user/projects/root/node_modules/bundled',
132 parent: root,
133 })
134 t.equal(meta.isDescendantOf(bundled), false, 'meta does not descend from bundled')
135 t.equal(bundled.root, root, 'bundled root is project root')
136
137 const dev = new Node({
138 pkg: {
139 name: 'dev',
140 version: '1.2.3',
141 },
142 resolved: 'dev',
143 integrity: 'dev',
144 path: '/home/user/projects/root/node_modules/dev',
145 realpath: '/home/user/projects/root/node_modules/dev',
146 parent: root,
147 })
148 t.equal(dev.root, root, 'dev root is project root')
149
150 const opt = new Node({
151 pkg: {
152 name: 'optional',
153 version: '1.2.3',
154 },
155 resolved: 'opt',
156 integrity: 'opt',
157 path: '/home/user/projects/root/node_modules/optional',
158 realpath: '/home/user/projects/root/node_modules/optional',
159 parent: root,
160 })
161 t.equal(opt.root, root, 'opt root is project root')
162
163 const peer = new Node({
164 pkg: {
165 name: 'peer',
166 version: '1.2.3',
167 },
168 resolved: 'peer',
169 integrity: 'peer',
170 path: '/home/user/projects/root/node_modules/peer',
171 realpath: '/home/user/projects/root/node_modules/peer',
172 parent: root,
173 })
174 t.equal(peer.root, root)
175
176 const extraneous = new Node({
177 pkg: {
178 name: 'extraneous',
179 version: '1.2.3',
180 },
181 resolved: 'extraneous',
182 integrity: 'extraneous',
183 path: '/home/user/projects/root/node_modules/extraneous',
184 realpath: '/home/user/projects/root/node_modules/extraneous',
185 parent: root,
186 })
187 t.equal(extraneous.root, root, 'extraneous.root is project root')
188
189 t.equal(prod.top, root, 'root is top of tree')
190 t.equal(prod.root, root, 'root is root of tree')
191 t.equal(root.isRoot, true, 'root is root of tree')
192 t.equal(prod.isRoot, false, 'prod is not root of tree')
193 t.equal(extraneous.extraneous, true, 'extraneous is extraneous')
194 t.matchSnapshot(root, 'initial load with some deps')
195
196 // move dep to top level
197 meta.parent = root
198 t.matchSnapshot(root, 'move meta to top level, update stuff')
199 t.equal(meta.root, root, 'meta.root is root still')
200 t.equal(meta.parent, root, 'meta.parent is root')
201 t.equal(root.inventory.get(meta.location), meta)
202
203 const newMeta = new Node({
204 pkg: {
205 name: 'meta',
206 version: '2.3.4',
207 peerDependencies: { asdf: '' },
208 peerDependenciesMeta: {
209 asdf: { optional: true },
210 },
211 },
212 resolved: 'newMeta',
213 integrity: 'newMeta',
214 name: 'meta',
215 parent: prod,
216 })
217 t.equal(newMeta.root, root)
218 newMeta.root = prod
219 t.equal(newMeta.root, root, 'setting root to non-root crawls up root list')
220 t.equal(meta.parent, root)
221 t.equal(newMeta.parent, prod)
222 t.equal(root.inventory.get(meta.location), meta)
223
224 // test that reparenting a link _doesn't_ update realpath
225 const metaMeta = new Link({
226 pkg: {
227 name: 'metameta',
228 version: '1.2.3',
229 _resolved: 'metameta',
230 _integrity: 'metameta',
231 },
232 path: newMeta.path + '/node_modules/metameta',
233 realpath: meta.realpath,
234 target: meta,
235 })
236 t.equal(metaMeta.root, root, 'link takes root of target if unspecified')
237 metaMeta.parent = newMeta
238 t.equal(metaMeta.root, root)
239 t.equal(meta.root, root)
240 t.equal(meta.parent, root)
241 t.equal(root.children.get('meta'), meta)
242 t.equal(root.inventory.get(meta.location), meta)
243
244 t.matchSnapshot(root, 'add new meta under prod')
245
246 t.equal(meta.parent, root, 'old meta parent is root before assigning')
247 newMeta.parent = root
248
249 t.equal(meta.parent, null, 'old meta parent removed')
250 t.not(root.children.get('meta'), meta,
251 'root.children no longer has old meta')
252 t.matchSnapshot(root, 'move new meta to top level')
253
254 newMeta.parent = root
255 t.matchSnapshot(root, 'move new meta to top level second time (no-op)')
256
257 t.test('replacement tests', t => {
258 const newProd = new Node({
259 name: 'prod',
260 pkg: {
261 name: 'prod',
262 version: '1.2.3',
263 dependencies: { meta: '' },
264 peerDependencies: { peer: '' },
265 },
266 path: '/some/path',
267 resolved: 'prod',
268 integrity: 'prod',
269 })
270 t.equal(newProd.canReplace(prod), true, 'new prod can replace prod')
271 const kidCount = prod.children.size
272 newProd.replace(prod)
273 t.equal(newProd.children.size, kidCount, 'kids moved to newProd')
274 t.equal(prod.children.size, 0, 'kids moved to newProd')
275 t.equal(prod.root, prod, 'prod excised from tree')
276 t.equal(newProd.root, root, 'newProd in the tree')
277 // XXX seems wrong, taking over fsChildren is weird?
278 t.equal(newProd.fsChildren.size, 0, 'fsChildren replaced')
279 t.equal(prod.fsChildren.size, 1, 'fsChildren go along with fsParent')
280 t.equal([...prod.fsChildren][0], foo, 'foo still in old prod fsChildren set')
281 t.equal(foo.fsParent, prod, 'prod is still foos fsParent')
282
283 const notProd = new Node({
284 pkg: {
285 name: 'notprod',
286 version: '1.2.3',
287 },
288 path: '/some/path',
289 })
290 t.equal(notProd.canReplace(newProd), false, 'cannot replace with different name')
291
292 const prodV2 = new Node({
293 name: 'prod',
294 path: '/path/to/prod',
295 pkg: {
296 name: 'prod',
297 version: '2.3.4',
298 },
299 })
300 // also call the other alias for this function, from the other dir
301 t.equal(newProd.canReplaceWith(prodV2), false, 'cannot replace with 2.x')
302
303 const root2 = new Node({
304 pkg: {
305 name: 'root',
306 bundleDependencies: ['bundled'],
307 dependencies: { prod: '1.x', bundled: '', missing: '' },
308 devDependencies: { dev: '', overlap: '' },
309 optionalDependencies: { optional: '', overlap: '', optMissing: '' },
310 },
311 realpath: '/home/user/projects/root',
312 path: '/home/user/projects/root',
313 meta: rootMetadata,
314 })
315 // call the inverse function, for coverage
316 root.replaceWith(root2)
317 t.equal(root2.root, root2, 'replacing root preserves self-rootedness')
318 root.replace(root2)
319
320 const prodLink = new Link({
321 pkg: prod.package,
322 realpath: '/some/other/path/entirely',
323 path: '/not/where/it/really/is',
324 name: 'prod',
325 })
326 t.equal(prodLink.canReplace(newProd), true, 'link can replace node')
327 prodLink.replace(newProd)
328 t.equal(newProd.parent, null, 'newProd removed from tree')
329 t.equal(newProd.root, newProd, 'newProd removed from tree')
330 t.equal(normalizePath(prodLink.path), normalizePath(newProd.path), 'replaced link')
331 t.equal(newProd.children.size, 0, 'newProd kids moved over')
332 t.equal(prodLink.children.size, 0, 'links do not have child nodes')
333 t.equal(prodLink.target.children.size, kidCount, 'link target has children')
334
335 t.equal(newProd.canReplace(prodLink), true, 'node can replace link')
336 const { target } = prodLink
337 newProd.replace(prodLink)
338 t.equal(prodLink.parent, null, 'prodLink removed from tree')
339 t.equal(prodLink.target, null, 'prodLink lost its target')
340 t.equal(prodLink.root, prodLink, 'prodLink removed from tree')
341 t.equal(target.root, target, 'prodLinks old target removed from tree')
342 t.equal(normalizePath(newProd.path), normalizePath(prodLink.path), 'replaced node')
343 t.equal(prodLink.children.size, 0, 'prodLink has no child nodes')
344
345 t.end()
346 })
347
348 t.end()
349 }
350
351 t.test('without meta', await runTest())
352 const meta = new Shrinkwrap({ path: '/home/user/projects/root' })
353 meta.data = {
354 lockfileVersion: 2,
355 packages: {},
356 dependencies: {},
357 }
358 t.test('with meta', await runTest(meta))
359
360 t.end()
361 })
362
363 t.test('adding two children that both have links into them', t => {
364 const root = new Node({
365 path: '/path/to/root',
366 })
367 const o1 = new Link({
368 parent: root,
369 realpath: '/path/m/node_modules/n/o1',
370 name: 'o1',
371 })
372 const t1 = new Node({
373 path: o1.realpath,
374 root,
375 })
376 const o2 = new Link({
377 parent: root,
378 realpath: '/path/m/node_modules/n/o2',
379 name: 'o2',
380 })
381 const t2 = new Node({
382 path: o2.realpath,
383 root,
384 })
385 const m = new Node({
386 path: '/path/m',
387 dummy: true,
388 root,
389 })
390 t.equal(o1.target, t1)
391 t.equal(o2.target, t2)
392 t.equal(t1.linksIn.has(o1), true)
393 t.equal(t2.linksIn.has(o2), true)
394 t.equal(t1.fsParent, m)
395 t.equal(t2.fsParent, m)
396 t.end()
397 })
398
399 t.test('edge cases for branch coverage', t => {
400 const noPkg = new Node({
401 realpath: '/home/user/projects/root',
402 path: '/home/user/projects/root',
403 })
404 t.same(noPkg.package, {}, 'default package is empty object')
405 t.equal(noPkg.name, 'root', 'root default name is . if package empty')
406
407 const noPath = new Node({
408 realpath: '/home/user/projects/root',
409 })
410 t.equal(noPath.name, 'root', 'pathless gets named for realpath')
411
412 t.end()
413 })
414
415 t.test('tracks the loading error encountered', t => {
416 const error = new Error('this is fine')
417 const root = new Node({
418 pkg: { name: 'root' },
419 path: '/home/user/projects/root',
420 realpath: '/home/user/projects/root',
421 error,
422 })
423 t.equal(root.errors[0], error, 'keeps ahold of the error')
424 t.end()
425 })
426
427 t.throws(() => new Node({ pkg: {} }), TypeError(
428 'could not detect node name from path or package'))
429
430 t.test('load from system-root path', t => {
431 const root = new Node({
432 path: resolve('/'),
433 })
434 t.equal(root.name, null, 'ok to have a nameless node in system root')
435 t.end()
436 })
437
438 t.test('load with a virtual filesystem parent', t => {
439 const Node = t.mock('../lib/node.js', {
440 '../lib/debug.js': a => a(),
441 })
442 const Link = t.mock('../lib/link.js', {
443 '../lib/node.js': Node,
444 })
445 const root = new Node({
446 pkg: { name: 'root', dependencies: { a: '', link: '', link2: '', link3: '' } },
447 path: '/home/user/projects/root',
448 realpath: '/home/user/projects/root',
449 })
450 const a = new Node({
451 pkg: { name: 'a', version: '1.2.3' },
452 parent: root,
453 name: 'a',
454 })
455 const link = new Link({
456 pkg: { name: 'link', version: '1.2.3', dependencies: { a: '', kid: '' } },
457 realpath: root.realpath + '/link-target',
458 parent: root,
459 })
460 t.ok(link.target, 'link has a target')
461 const linkKid = new Node({
462 pkg: { name: 'kid', dependencies: { a: '' } },
463 parent: link.target,
464 })
465
466 const link2 = new Link({
467 pkg: { name: 'link2', version: '1.2.3', dependencies: { link: '' } },
468 realpath: a.realpath + '/node_modules/link2-target',
469 parent: root,
470 fsParent: a,
471 })
472
473 t.equal(link2.target.parent, a, 'fsParent=parent sets parent')
474 t.equal(link2.target.fsParent, null, 'fsParent=parent does not set fsParent')
475 t.equal(link2.target.resolveParent, a, 'resolveParent is parent')
476
477 const target3 = new Node({
478 name: 'link3',
479 path: root.realpath + '/packages/link3',
480 realpath: root.realpath + '/packages/link3',
481 pkg: { name: 'link3', version: '1.2.3', dependencies: { link2: '' } },
482 fsParent: new Node({
483 path: root.realpath + '/packages',
484 realpath: root.realpath + '/packages',
485 pkg: { name: 'packages', version: '2.3.4', dependencies: { link: '' } },
486 }),
487 })
488 const packages = target3.fsParent
489
490 const link3 = new Link({
491 pkg: { name: 'link3', version: '1.2.3', dependencies: { link2: '' } },
492 realpath: root.realpath + '/packages/link3',
493 target: target3,
494 parent: root,
495 })
496 t.equal(target3.root, root)
497 t.equal(packages.fsChildren.size, 0)
498
499 packages.root = root
500
501 t.equal(normalizePath(target3.fsParent.path), normalizePath(packages.path))
502 t.equal(packages.fsChildren.size, 1)
503
504 t.equal(packages.location, 'packages')
505 t.equal(target3.location, 'packages/link3')
506 t.equal(normalizePath(target3.realpath), normalizePath(root.path + '/packages/link3'))
507 t.equal(target3.fsParent, packages)
508 t.equal(packages.fsChildren.size, 1)
509 t.equal(normalizePath(link3.realpath), normalizePath(target3.realpath))
510 t.equal(link3.root, target3.root)
511 t.equal(link3.target, target3, 'before fsParent move')
512
513 packages.fsParent = link.target
514 t.equal(packages.location, 'link-target/packages')
515 t.equal(target3.location, 'link-target/packages/link3')
516 t.equal(normalizePath(target3.realpath), normalizePath(root.path + '/link-target/packages/link3'))
517 t.equal(target3.fsParent, packages)
518 t.equal(packages.fsChildren.size, 1)
519 t.equal(normalizePath(link3.realpath), normalizePath(target3.realpath))
520 t.equal(link3.root, target3.root)
521 t.equal(link3.target, target3, 'after fsParent move')
522
523 // do it again so we can verify nothing changed. this should be a no-op.
524 packages.fsParent = link.target
525 t.equal(packages.location, 'link-target/packages')
526 t.equal(target3.location, 'link-target/packages/link3')
527 t.equal(normalizePath(target3.realpath), normalizePath(root.path + '/link-target/packages/link3'))
528 t.equal(target3.fsParent, packages)
529 t.equal(packages.fsChildren.size, 1)
530 t.equal(normalizePath(link3.realpath), normalizePath(target3.realpath))
531 t.equal(link3.root, target3.root)
532 t.equal(link3.target, target3, 'after fsParent move')
533
534 t.equal(normalizePath(packages.path), normalizePath(root.realpath + '/link-target/packages'))
535 t.equal(normalizePath(target3.path), normalizePath(root.realpath + '/link-target/packages/link3'))
536 t.equal(link3.target, target3, 'still targeting the right node 4')
537 t.equal(target3.fsParent, packages, 'link3 target under packages')
538 t.equal(normalizePath(link3.realpath), normalizePath(target3.path), 'link realpath updated')
539
540 // can't set fsParent to a link! set to the target instead.
541 const linkChild = new Node({
542 path: target3.path + '/linkchild',
543 pkg: { name: 'linkchild', version: '1.2.3' },
544 })
545 linkChild.fsParent = link3
546 t.equal(linkChild.fsParent, link3.target)
547 t.equal(linkChild.root, root)
548
549 t.equal(target3.fsParent, packages, 'link3 target under packages')
550
551 // can't set fsParent to the same node
552 t.throws(() => packages.fsParent = packages, {
553 message: 'setting node to its own fsParent',
554 })
555
556 t.throws(() => packages.fsParent = new Node({ path: packages.path }), {
557 message: 'setting fsParent to same path',
558 })
559
560 // can't set fsParent on a new node such that it's outside its path
561 const outsideNode = new Node({ path: '/not/the/root/path', pkg: {} })
562 t.throws(() => outsideNode.fsParent = root, {
563 message: 'setting fsParent improperly',
564 })
565
566 // automatically set fsParent based on path calculations
567 t.equal(link.target.fsParent, root)
568 t.equal(link.target.resolveParent, root, 'resolveParent is fsParent')
569 t.equal(link.target.edgesOut.get('a').error, null)
570 t.equal(linkKid.edgesOut.get('a').error, null)
571 t.equal(linkKid.parent, link.target)
572
573 const target = link.target
574 link.target.root = null
575 t.equal(link.target, null)
576 t.equal(target.edgesOut.get('a').error, 'MISSING')
577 t.equal(linkKid.edgesOut.get('a').error, 'MISSING')
578
579 target.fsParent = link.root
580 t.equal(link.target, target)
581 t.equal(link.target.fsParent, root)
582 t.equal(link.target.edgesOut.get('a').error, null)
583 t.equal(linkKid.edgesOut.get('a').error, null)
584
585 // move it under this other one for some reason
586 link.target.fsParent = link2.target
587 t.equal(link.target.fsParent, link2.target)
588 t.equal(link.target.edgesOut.get('a').error, null)
589 t.equal(linkKid.edgesOut.get('a').error, null)
590
591 // move it into node_modules
592 link.target.parent = link2.target
593 t.equal(link.target.fsParent, null, 'lost fsParent for parent')
594 t.equal(link.target.edgesOut.get('a').error, null)
595 t.equal(linkKid.edgesOut.get('a').error, null)
596 t.equal(normalizePath(link.realpath), normalizePath(link2.realpath + '/node_modules/link-target'))
597
598 const linkLoc = link.location
599 const linkTarget = link.target
600 link.parent = null
601 t.equal(link.root, link, 'removed from parent, removed from root')
602 t.equal(root.inventory.get(linkLoc), undefined, 'removed from root inventory')
603 t.equal(link.inventory.has(link), true, 'link added to own inventory')
604 t.equal(link.target, null, 'target left behind when setting root to null')
605 linkTarget.root = link
606 t.equal(link.target, linkTarget, 'target set once roots match')
607 t.equal(link.inventory.get(''), linkTarget)
608 t.equal(root.edgesOut.get('link').error, 'MISSING')
609
610 packages.fsParent = null
611 t.equal(packages.root, packages, 'removed from fsParent, removed from root')
612
613 // now replace the real node with a link
614 // ensure that everything underneath it is removed from root
615 const aChild = new Node({
616 parent: a,
617 pkg: { name: 'achild', version: '1.2.3' },
618 })
619 t.equal(aChild.parent, a)
620 t.equal(a.children.get('achild'), aChild)
621 const underA = [...root.inventory.values()]
622 .filter(node => node.path.startsWith(a.path) && node !== a)
623 const aLoc = a.location
624 const aLink = new Link({
625 path: a.path,
626 target: new Node({
627 path: '/some/other/a',
628 pkg: a.package,
629 }),
630 })
631 aLink.root = root
632 t.equal(root.inventory.get(aLoc), aLink)
633 t.equal(a.root, a)
634 for (const node of underA) {
635 t.not(node.root, root, `${node.path} still under old root`)
636 }
637
638 // create a new fsChild several steps below the root, then shove
639 // a link in the way of it, removing it.
640 const fsD = new Node({
641 path: root.path + '/a/b/c/d',
642 pkg: { name: 'd', version: '1.2.3' },
643 root,
644 })
645 t.equal(fsD.fsParent, root, 'root should be fsParent')
646 new Link({
647 path: root.path + '/a/b',
648 target: new Node({
649 path: '/some/exotic/location',
650 pkg: { name: 'b', version: '1.2.3' },
651 }),
652 root,
653 })
654 t.not(fsD.root, root, 'fsD removed from root')
655
656 // add a node completely outside the root folder, as a link
657 // target, then add a new node that takes over as its parent,
658 // to exercise the code path where a top node has no fsParent
659 const remoteLink = new Link({
660 parent: root,
661 name: 'x',
662 realpath: '/remote/node_modules/a/node_modules/x',
663 })
664 const remoteTarget = new Node({
665 path: '/remote/node_modules/a/node_modules/x',
666 realpath: '/remote/node_modules/a/node_modules/x',
667 pkg: { name: 'x', version: '1.2.3' },
668 root,
669 })
670 t.equal(remoteLink.target, remoteTarget, 'automatically found target')
671 t.equal(remoteTarget.fsParent, null, 'remote target has no fsParent')
672 t.equal(remoteTarget.parent, null, 'remote target has no parent')
673 root.tops.has(remoteTarget, 'remote target in root.tops')
674 const remoteParent = new Node({
675 path: '/remote/node_modules/a',
676 pkg: { name: 'a', version: '1.2.3' },
677 root,
678 })
679 t.throws(() => remoteParent.target = remoteTarget, {
680 message: 'cannot set target on non-Link Nodes',
681 path: remoteParent.path,
682 })
683 t.equal(remoteParent.children.get('x'), remoteTarget)
684 treeCheck(root)
685
686 t.end()
687 })
688
689 t.test('child of link target has path, like parent', t => {
690 const root = new Node({
691 pkg: { name: 'root', dependencies: { a: '', link: '', link2: '' } },
692 path: '/home/user/projects/root',
693 realpath: '/home/user/projects/root',
694 })
695 new Node({
696 pkg: { name: 'a', version: '1.2.3' },
697 parent: root,
698 name: 'a',
699 })
700 const link = new Link({
701 pkg: { name: 'link', version: '1.2.3', dependencies: { a: '', kid: '' } },
702 realpath: root.realpath + '/link-target',
703 parent: root,
704 fsParent: root,
705 })
706 const linkKid = new Node({
707 pkg: { name: 'kid' },
708 parent: link,
709 })
710 t.equal(linkKid.parent, link.target, 'setting link as parent sets target instead')
711 t.equal(normalizePath(linkKid.path), normalizePath(linkKid.realpath), 'child of link target path is realpath')
712 t.end()
713 })
714
715 t.test('changing root', t => {
716 const meta = new Shrinkwrap({ path: '/home/user/projects/root' })
717 meta.data = { lockfileVersion: 2, dependencies: {}, packages: {} }
718 const root = new Node({
719 pkg: { name: 'root', dependencies: { a: '', link: '', link2: '' } },
720 path: '/home/user/projects/root',
721 realpath: '/home/user/projects/root',
722 meta,
723 })
724 const a = new Node({
725 pkg: { name: 'a', version: '1.2.3' },
726 parent: root,
727 name: 'a',
728 resolved: 'https://example.com/a-1.2.3.tgz',
729 integrity: 'sha512-asdfasdfasdf',
730 })
731 const b = new Node({
732 pkg: { name: 'b', version: '1.2.3' },
733 parent: a,
734 name: 'b',
735 })
736 const meta2 = new Shrinkwrap({ path: '/home/user/projects/root2' })
737 meta2.data = { lockfileVersion: 2, dependencies: {}, packages: {} }
738 const root2 = new Node({
739 pkg: { name: 'root2', dependencies: { a: '', link: '', link2: '' } },
740 path: '/home/user/projects/root2',
741 realpath: '/home/user/projects/root2',
742 meta: meta2,
743 })
744 t.equal(a.root, root, 'root is root of tree from a')
745 t.equal(b.root, root, 'root is root of tree from b')
746 a.parent = root2
747 t.equal(a.root, root2, 'root is set when parent is changed')
748 t.equal(b.root, root2, 'root is set on children when parent is changed')
749 t.end()
750 })
751
752 t.test('attempt to assign parent to self on root node', t => {
753 // turn off debugging for this one so we don't throw
754 const Node = t.mock('../lib/node.js', {
755 '../lib/debug.js': () => {},
756 })
757 const root = new Node({
758 pkg: { name: 'root' },
759 path: '/',
760 realpath: '/',
761 })
762 root.parent = root.fsParent = root
763 t.equal(root.parent, null, 'root node parent should be empty')
764 t.equal(root.fsParent, null, 'root node fsParent should be empty')
765 t.end()
766 })
767
768 t.test('bundled dependencies logic', t => {
769 const root = new Node({
770 pkg: {
771 name: 'root',
772 dependencies: { a: '', b: '', d: '', e: '', f: '' },
773 bundleDependencies: ['a'],
774 },
775 path: '/path/to/root',
776 realpath: '/path/to/root',
777 })
778 const a = new Node({
779 pkg: { name: 'a', version: '1.2.3', dependencies: { b: '', aa: '' } },
780 parent: root,
781 })
782 const aa = new Node({
783 pkg: { name: 'aa', version: '1.2.3' },
784 parent: a,
785 })
786 const b = new Node({
787 pkg: { name: 'b', version: '1.2.3', dependencies: { c: '' } },
788 parent: root,
789 })
790 const c = new Node({
791 pkg: { name: 'c', version: '1.2.3', dependencies: { cc: '' } },
792 parent: root,
793 })
794 new Node({
795 pkg: { name: 'cc', version: '1.2.3', dependencies: { d: '' } },
796 parent: c,
797 })
798 const d = new Node({
799 pkg: { name: 'd', version: '1.2.3' },
800 parent: root,
801 })
802 const e = new Node({
803 pkg: { name: 'e', version: '1.2.3' },
804 parent: root,
805 })
806 const f = new Node({
807 pkg: {
808 name: 'f',
809 version: '1.2.3',
810 dependencies: { fa: '', fb: '' },
811 bundleDependencies: ['fb'],
812 },
813 parent: root,
814 })
815 new Node({
816 pkg: { name: 'fa', version: '1.2.3' },
817 parent: f,
818 })
819 const fb = new Node({
820 pkg: { name: 'fb', version: '1.2.3', dependencies: { e: '', fc: '' } },
821 parent: f,
822 })
823 new Node({
824 pkg: { name: 'fc', version: '1.2.3', dependencies: { fb: '' } },
825 parent: f,
826 })
827
828 t.equal(a.inBundle, true, 'bundled dep is bundled')
829 t.equal(a.inDepBundle, false, 'bundled dep is bundled by root')
830 t.equal(aa.inBundle, true, 'child of bundled dep is bundled')
831 t.equal(aa.inDepBundle, false, 'child of dep bundled by root is not dep bundled')
832 t.equal(b.inBundle, true, 'dep of bundled dep at peer level is bundled')
833 t.equal(c.inBundle, true, 'metadep of bundled dep at peer level is bundled')
834 t.equal(d.inBundle, true, 'deduped metadep of bundled metadep is bundled')
835 t.equal(e.inBundle, false, 'deduped dep of bundled dep of metadep is not bundled')
836 t.equal(fb.inBundle, true, 'bundled dep of dep is bundled')
837 t.equal(fb.inDepBundle, true, 'bundled dep of dep is dep bundled (not by root)')
838 t.end()
839 })
840
841 t.test('move fsChildren when moving to a new fsParent in same root', t => {
842 const root = new Node({
843 path: '/path/to/root',
844 })
845 const p1 = new Node({
846 path: '/path/to/root/p1',
847 root: root,
848 })
849 t.equal(p1.fsParent, root)
850 const p2 = new Node({
851 path: '/path/to/root/p2',
852 root: root,
853 })
854 t.equal(p2.fsParent, root)
855 const c2 = new Node({
856 path: '/path/to/root/p2/c2',
857 root: root,
858 })
859 t.equal(c2.fsParent, p2)
860 p2.fsParent = p1
861 t.equal(normalizePath(c2.path), normalizePath('/path/to/root/p1/p2/c2'))
862 t.end()
863 })
864
865 t.test('check if a node is in a node_modules folder or not', t => {
866 const a = new Node({
867 path: '/path/to/foo/node_modules/a',
868 realpath: '/path/to/foo/node_modules/a',
869 pkg: { name: 'a' },
870 })
871 t.equal(normalizePath(a.inNodeModules()), '/path/to/foo', 'basic obvious case')
872
873 const b = new Node({
874 path: '/path/to/foo/node_modules/a',
875 realpath: '/path/to/foo/node_modules/a',
876 pkg: { name: 'b' },
877 })
878 t.equal(normalizePath(b.inNodeModules()), '/path/to/foo', 'based on path name, not pkg name')
879
880 const c = new Node({
881 path: '/path/to/foo/node_modules/a/b/c',
882 realpath: '/path/to/foo/node_modules/a/b/c',
883 pkg: { name: 'c' },
884 })
885 t.equal(c.inNodeModules(), false, 'not directly in node_modules')
886
887 const d = new Node({
888 path: '/path/to/foo/node_modules/@c/d',
889 realpath: '/path/to/foo/node_modules/@c/d',
890 pkg: { name: '@a/b/c/d/e' },
891 })
892 t.equal(normalizePath(d.inNodeModules()), '/path/to/foo', 'scoped package in node_modules')
893
894 t.end()
895 })
896
897 t.test('update metadata when moving between linked top-of-tree parents', t => {
898 // this is a bit of a weird edge case, but covered for completeness.
899 // When moving the parent of a node, we update the metadata in the root,
900 // AND in the top-of-tree node, if it's not also the root (as that would be
901 // redundant).
902
903 const rootMeta = new Shrinkwrap({ path: '/home/user/projects/root' })
904 rootMeta.data = { lockfileVersion: 2, dependencies: {}, packages: {} }
905 const root = new Node({
906 pkg: { name: 'root' },
907 path: rootMeta.path,
908 realpath: rootMeta.path,
909 meta: rootMeta,
910 })
911
912 const top1Meta = new Shrinkwrap({ path: '/path/to/top1' })
913 top1Meta.data = { lockfileVersion: 2, dependencies: {}, packages: {} }
914 const top1 = new Node({
915 pkg: { name: 'top', version: '1.1.1' },
916 path: top1Meta.path,
917 realpath: top1Meta.path,
918 meta: top1Meta,
919 })
920
921 new Link({
922 name: 'link1',
923 parent: root,
924 realpath: top1.path,
925 target: top1,
926 })
927
928 const top2Meta = new Shrinkwrap({ path: '/path/to/top2' })
929 top2Meta.data = { lockfileVersion: 2, dependencies: {}, packages: {} }
930 const top2 = new Node({
931 pkg: { name: 'top', version: '1.1.1' },
932 path: top2Meta.path,
933 realpath: top2Meta.path,
934 meta: top2Meta,
935 })
936
937 const link2 = new Link({
938 name: 'link2',
939 parent: root,
940 realpath: top2.path,
941 target: top2,
942 })
943
944 const child = new Node({
945 parent: top1,
946 pkg: {
947 name: 'child',
948 version: '1.2.3',
949 dependencies: { child2: '2' },
950 },
951 resolved: 'https://child.com/-/child-1.2.3.tgz',
952 integrity: 'sha512-blortzeyblartzeyfartz',
953 })
954 const child2 = new Node({
955 parent: child,
956 pkg: { name: 'child2', version: '2.3.4' },
957 resolved: 'https://child.com/-/child-2.3.4.tgz',
958 integrity: 'sha512-a childs child is a kidkid',
959 })
960
961 t.matchSnapshot(child.location, 'initial child location, pre-move')
962 t.equal(child.root, root, 'child root is the shared root node')
963 t.equal(child.top, top1, 'child top is top1')
964 t.matchSnapshot(child2.location, 'initial child2 location, pre-move')
965 t.equal(child2.root, root, 'child2 root is the shared root node')
966 t.equal(child2.top, top1, 'child2 top is top1')
967 t.matchSnapshot(root.meta.get(child.location), 'metadata from root')
968 t.matchSnapshot(top1.meta.get(child.location), 'metadata from top1')
969
970 // now move it over
971 const oldLocation = child.location
972 const oldLocation2 = child2.location
973 child.parent = link2
974 t.equal(child.top, top2, 'after move, top points at top2')
975 t.equal(child.parent, top2, 'parent assigned to link target')
976 t.matchSnapshot(child.location, 'new child location')
977 t.equal(child2.top, top2, 'after move, top points at top2')
978 t.equal(child2.parent, child, 'parent assigned to link target')
979 t.matchSnapshot(child2.location, 'new child2 location')
980 t.matchSnapshot(root.meta.get(child.location), 'root metadata updated')
981 t.matchSnapshot(root.meta.get(child2.location), 'root metadata updated')
982 t.matchSnapshot(root.meta.get(oldLocation), 'old location deleted from root')
983 t.matchSnapshot(top1.meta.get(oldLocation), 'old location deleted from top1')
984 t.matchSnapshot(root.meta.get(oldLocation2), 'old location2 deleted from root')
985 t.matchSnapshot(top1.meta.get(oldLocation2), 'old location2 deleted from top1')
986 t.matchSnapshot(top2.meta.get(child.location), 'new top metadata updated')
987 t.matchSnapshot(top2.meta.get(child2.location), 'new top metadata updated')
988
989 return t.end()
990 })
991
992 t.test('setting package refreshes deps', t => {
993 const root = new Node({
994 pkg: {
995 dependencies: {
996 a: '1',
997 },
998 },
999 path: '/path/to/root',
1000 })
1001 const a = new Node({
1002 pkg: {
1003 name: 'a',
1004 version: '1.2.3',
1005 },
1006 parent: root,
1007 })
1008 t.equal(root.edgesOut.get('a').valid, true,
1009 'dep is valid before updating pkg')
1010 root.package = { dependencies: { a: '2' } }
1011 t.equal(root.edgesOut.get('a').valid, false,
1012 'dep is invalid after updating pkg')
1013 a.package = { name: 'a', version: '2.3.4' }
1014 t.equal(root.edgesOut.get('a').valid, true,
1015 'dep is valid again after updating dep pkg')
1016 t.end()
1017 })
1018
1019 t.test('nodes in shrinkwraps', t => {
1020 const root = new Node({
1021 pkg: { dependencies: { a: '' } },
1022 path: '/path/to/root',
1023 children: [
1024 {
1025 name: 'a',
1026 pkg: {
1027 name: 'a',
1028 version: '1.2.3',
1029 dependencies: { b: '' },
1030 _hasShrinkwrap: true,
1031 },
1032 children: [
1033 {
1034 name: 'b',
1035 pkg: {
1036 version: '1.2.3',
1037 name: 'b',
1038 dependencies: { c: '' },
1039 },
1040 children: [{ name: 'c', pkg: { name: 'c', version: '1.2.3' } }],
1041 },
1042 ],
1043 },
1044 ],
1045 })
1046 const a = root.children.get('a')
1047 const b = a.children.get('b')
1048 const c = b.children.get('c')
1049
1050 t.equal(a.hasShrinkwrap, true, 'a has a shrinkwrap')
1051 t.equal(b.inShrinkwrap, true, 'b is in shrinkwrap')
1052 t.equal(c.inShrinkwrap, true, 'c is in shrinkwrap')
1053 t.end()
1054 })
1055
1056 t.test('bin paths', t => {
1057 const root = new Node({
1058 path: '/a/b/c',
1059 pkg: { bin: { c: 'blorp' } },
1060 children: [
1061 { pkg: { name: '@foo/bar', bin: { bar: 'foo' } } },
1062 { pkg: { name: 'foo', bin: { foo: 'bloo' } },
1063 children: [{ pkg: { name: 'bar', bin: { bar: 'noscope' } } }] },
1064 { pkg: { name: 'nobin' } },
1065 ],
1066 })
1067
1068 const link = new Link({
1069 parent: root,
1070 name: 'linkfoo',
1071 pkg: { bin: { d: 'from-link' } },
1072 realpath: root.path + '/d/e/f',
1073 })
1074
1075 const { resolve: r } = require('node:path')
1076
1077 t.strictSame(root.binPaths, [])
1078 t.strictSame(link.binPaths, [
1079 r('/a/b/c/node_modules/.bin/d'),
1080 ...(process.platform !== 'win32' ? [] : [
1081 r('/a/b/c/node_modules/.bin/d.cmd'),
1082 r('/a/b/c/node_modules/.bin/d.ps1'),
1083 ]),
1084 ])
1085 t.strictSame(link.target.binPaths, [])
1086 const scoped = root.children.get('@foo/bar')
1087 t.strictSame(scoped.binPaths, [
1088 r('/a/b/c/node_modules/.bin/bar'),
1089 ...(process.platform !== 'win32' ? [] : [
1090 r('/a/b/c/node_modules/.bin/bar.cmd'),
1091 r('/a/b/c/node_modules/.bin/bar.ps1'),
1092 ]),
1093 ])
1094 const unscoped = root.children.get('foo')
1095 t.strictSame(unscoped.binPaths, [
1096 r('/a/b/c/node_modules/.bin/foo'),
1097 ...(process.platform !== 'win32' ? [] : [
1098 r('/a/b/c/node_modules/.bin/foo.cmd'),
1099 r('/a/b/c/node_modules/.bin/foo.ps1'),
1100 ]),
1101 ])
1102 const nested = unscoped.children.get('bar')
1103 t.strictSame(nested.binPaths, [
1104 r('/a/b/c/node_modules/foo/node_modules/.bin/bar'),
1105 ...(process.platform !== 'win32' ? [] : [
1106 r('/a/b/c/node_modules/foo/node_modules/.bin/bar.cmd'),
1107 r('/a/b/c/node_modules/foo/node_modules/.bin/bar.ps1'),
1108 ]),
1109 ])
1110 const nobin = root.children.get('nobin')
1111 t.strictSame(nobin.binPaths, [])
1112 t.end()
1113 })
1114
1115 t.test('binPaths, but global', t => {
1116 const root = new Node({
1117 global: true,
1118 path: '/usr/local/lib',
1119 children: [
1120 { pkg: { name: '@foo/bar', bin: { bar: 'foo' } } },
1121 { pkg: { name: 'foo', bin: { foo: 'bloo' } },
1122 children: [{ pkg: { name: 'bar', bin: { bar: 'noscope' } } }] },
1123 { pkg: { name: 'nobin' } },
1124 ],
1125 })
1126
1127 const link = new Link({
1128 parent: root,
1129 name: 'linkfoo',
1130 pkg: { bin: { d: 'from-link' } },
1131 realpath: root.path + '/d/e/f',
1132 })
1133
1134 const { resolve: r } = require('node:path')
1135
1136 t.strictSame(root.binPaths, [])
1137 t.strictSame(link.binPaths, process.platform === 'win32'
1138 ? [
1139 r('/usr/local/lib/d'),
1140 r('/usr/local/lib/d.cmd'),
1141 r('/usr/local/lib/d.ps1'),
1142 ]
1143 : [
1144 r('/usr/local/bin/d'),
1145 ]
1146 )
1147 t.strictSame(link.target.binPaths, [])
1148 const scoped = root.children.get('@foo/bar')
1149 t.strictSame(scoped.binPaths, process.platform === 'win32'
1150 ? [
1151 r('/usr/local/lib/bar'),
1152 r('/usr/local/lib/bar.cmd'),
1153 r('/usr/local/lib/bar.ps1'),
1154 ]
1155 : [
1156 r('/usr/local/bin/bar'),
1157 ]
1158 )
1159 const unscoped = root.children.get('foo')
1160 t.strictSame(unscoped.binPaths, process.platform === 'win32'
1161 ? [
1162 r('/usr/local/lib/foo'),
1163 r('/usr/local/lib/foo.cmd'),
1164 r('/usr/local/lib/foo.ps1'),
1165 ]
1166 : [
1167 r('/usr/local/bin/foo'),
1168 ]
1169 )
1170 const nested = unscoped.children.get('bar')
1171 t.strictSame(nested.binPaths, [
1172 r('/usr/local/lib/node_modules/foo/node_modules/.bin/bar'),
1173 ...(process.platform !== 'win32' ? [] : [
1174 r('/usr/local/lib/node_modules/foo/node_modules/.bin/bar.cmd'),
1175 r('/usr/local/lib/node_modules/foo/node_modules/.bin/bar.ps1'),
1176 ]),
1177 ])
1178 const nobin = root.children.get('nobin')
1179 t.strictSame(nobin.binPaths, [])
1180 t.end()
1181 })
1182
1183 t.test('has install script', t => {
1184 const node = new Node({
1185 pkg: {},
1186 path: '/a/b/c',
1187 })
1188 t.equal(node.hasInstallScript, false)
1189 node.package = { scripts: { postinstall: 'hello' } }
1190 t.equal(node.hasInstallScript, true)
1191 node.package = { scripts: { nothing: 'of interest' } }
1192 t.equal(node.hasInstallScript, false)
1193 node.package = { hasInstallScript: true }
1194 t.equal(node.hasInstallScript, true)
1195 t.end()
1196 })
1197
1198 t.test('legacy peer dependencies', t => {
1199 const root = new Node({
1200 pkg: {
1201 name: 'root',
1202 peerDependencies: {
1203 foo: '1.x',
1204 },
1205 },
1206 path: '/home/user/projects/root',
1207 realpath: '/home/user/projects/root',
1208 legacyPeerDeps: true,
1209 })
1210
1211 const foo = new Node({
1212 pkg: {
1213 name: 'foo',
1214 version: '1.2.3',
1215 },
1216 path: '/home/user/projects/root/foo',
1217 realpath: '/home/user/projects/root/foo',
1218 legacyPeerDeps: true,
1219 parent: root,
1220 })
1221
1222 t.equal(root.children.get('foo'), foo, 'should be a children')
1223 t.equal(root.edgesOut.size, 0, 'should have no edges out')
1224 t.end()
1225 })
1226
1227 t.test('set workspaces', t => {
1228 const root = new Node({
1229 pkg: { name: 'workspaces_root' },
1230 path: '/home/user/projects/workspaces_root',
1231 realpath: '/home/user/projects/workspaces_root',
1232 })
1233
1234 const link = new Link({
1235 parent: root,
1236 target: new Node({
1237 path: '/home/user/projects/workspaces_root/foo',
1238 pkg: {
1239 name: 'foo',
1240 version: '1.2.3',
1241 },
1242 }),
1243 })
1244
1245 const unknown = new Link({
1246 parent: root,
1247 target: new Node({
1248 path: '/home/user/projects/workspaces_root/unknown',
1249 pkg: {
1250 name: 'unknown',
1251 version: '1.2.3',
1252 },
1253 }),
1254 })
1255
1256 root.workspaces = new Map([
1257 ['foo', '/home/user/projects/workspaces_root/foo'],
1258 ['bar', '/home/user/projects/workspaces_root/bar'],
1259 ])
1260
1261 t.matchSnapshot(root, 'should setup edges out for each workspace')
1262 t.equal(link.isWorkspace, true, 'link node reports isWorkspace true')
1263 t.equal(link.target.isWorkspace, true, 'target reports isWorkspace true')
1264 t.equal(root.isWorkspace, false, 'root is not a workspace')
1265 t.equal(unknown.isWorkspace, false, 'unknown node is not a workspace')
1266 t.end()
1267 })
1268
1269 t.test('get workspaces', t => {
1270 const root = new Node({
1271 pkg: { name: 'workspaces_root' },
1272 path: '/home/user/projects/workspaces_root',
1273 realpath: '/home/user/projects/workspaces_root',
1274 })
1275
1276 t.equal(root.workspaces, null, 'should default to null when no workspaces defined')
1277
1278 const ws = new Map()
1279 root.workspaces = ws
1280
1281 t.equal(root.workspaces, ws, 'should match set value')
1282 t.end()
1283 })
1284
1285 t.test('replace workspaces', t => {
1286 const root = new Node({
1287 pkg: { name: 'workspaces_root' },
1288 path: '/home/user/projects/workspaces_root',
1289 realpath: '/home/user/projects/workspaces_root',
1290 })
1291
1292 root.workspaces = new Map([
1293 ['foo', '/home/user/projects/workspaces_root/foo'],
1294 ['bar', '/home/user/projects/workspaces_root/bar'],
1295 ])
1296
1297 const ws = new Map()
1298 root.workspaces = ws
1299
1300 t.equal(root.workspaces, ws, 'should remove previously set workspaces')
1301 t.end()
1302 })
1303
1304 t.test('replace workspaces keeping existing edges out', t => {
1305 const root = new Node({
1306 pkg: { name: 'workspaces_root' },
1307 path: '/home/user/projects/workspaces_root',
1308 realpath: '/home/user/projects/workspaces_root',
1309 })
1310
1311 root.workspaces = new Map([
1312 ['foo', '/home/user/projects/workspaces_root/foo'],
1313 ])
1314
1315 const ws = new Map([
1316 ['foo', '/home/user/projects/workspaces_root/foo'],
1317 ['bar', '/home/user/projects/workspaces_root/bar'],
1318 ])
1319 root.workspaces = ws
1320
1321 t.equal(root.workspaces, ws, 'should keep existing edges out')
1322 t.end()
1323 })
1324
1325 t.test('do not rely on legacy _resolved for file: nodes', async t => {
1326 const old = new Node({
1327 pkg: {
1328 _resolved: 'file:/x/y/z/blorg.tgz',
1329 _where: '/why/did/i/think/this/was/a/good/idea',
1330 },
1331 path: '/some/completely/different/path',
1332 })
1333 t.equal(old.resolved, null)
1334
1335 // _resolved without _where means it's probably valid though
1336 const notOld = new Node({
1337 pkg: {
1338 _resolved: 'file:/x/y/z/blorg.tgz',
1339 },
1340 path: '/some/completely/different/path',
1341 })
1342 t.equal(normalizePath(notOld.resolved), 'file:/x/y/z/blorg.tgz')
1343 })
1344
1345 t.test('reparenting keeps children in root inventory', async t => {
1346 const root = new Node({ path: '/some/path' })
1347 const nested = new Node({
1348 fsParent: root,
1349 path: '/some/path/node_modules/parent/node_modules/nested',
1350 })
1351 const fsNested = new Node({
1352 fsParent: nested,
1353 path: '/some/path/node_modules/parent/node_modules/nested/x',
1354 })
1355
1356 const kid = new Node({
1357 name: 'kid',
1358 parent: nested,
1359 })
1360
1361 t.equal(root.inventory.has(kid), true)
1362 t.equal(root.inventory.has(fsNested), true)
1363
1364 // now reparent, and make sure the kids are still accounted for
1365 const parent = new Node({ name: 'parent', parent: root })
1366 t.equal(nested.parent, parent)
1367
1368 t.equal(root.inventory.has(kid), true)
1369 t.equal(root.inventory.has(fsNested), true)
1370 })
1371
1372 t.test('reloading named edges should refresh edgesIn', t => {
1373 // pathological dep nesting scenario:
1374 //
1375 // x@1 -> y@1
1376 // x@2 -> y@2
1377 // y@1 -> x@2
1378 // y@2 -> x@1
1379 //
1380 // ensure we have the correct edge state at all points along this
1381 // infinite journey. (will *prevent* said infinite journey in
1382 // buildIdealTree, but only if we can detect its presence properly
1383 // with correct Node edge behavior along the way.)
1384 //
1385 // Resulting tree looks like:
1386 //
1387 // +-- x1
1388 // +-- y1
1389 // +-- x2
1390 // +-- y2
1391 // +-- x1
1392 // +-- y1 ...and so on forever
1393
1394 const root = new Node({
1395 path: '/some/path',
1396 pkg: { dependencies: { x: '1' } },
1397 })
1398 t.match(root.edgesOut.get('x'), { spec: '1', missing: true })
1399
1400 const x1 = new Node({
1401 pkg: { name: 'x', version: '1.0.0', dependencies: { y: '1' } },
1402 parent: root,
1403 })
1404 t.match(root.edgesOut.get('x'), { spec: '1', invalid: false, to: x1 })
1405 t.match(x1.edgesOut.get('y'), { spec: '1', missing: true })
1406
1407 const y1 = new Node({
1408 pkg: { name: 'y', version: '1.0.0', dependencies: { x: '2' } },
1409 parent: root,
1410 })
1411 t.match(root.edgesOut.get('x'), { spec: '1', invalid: false, to: x1 })
1412 t.match(x1.edgesOut.get('y'), { spec: '1', invalid: false, to: y1 })
1413 t.match(y1.edgesOut.get('x'), { spec: '2', invalid: true, to: x1 })
1414
1415 const y1x2 = new Node({
1416 pkg: { name: 'x', version: '2.0.0', dependencies: { y: '2' } },
1417 parent: y1,
1418 })
1419 t.match(root.edgesOut.get('x'), { spec: '1', invalid: false, to: x1 })
1420 t.match(x1.edgesOut.get('y'), { spec: '1', invalid: false, to: y1 })
1421 t.match(y1.edgesOut.get('x'), { spec: '2', invalid: false, to: y1x2 })
1422 t.match(y1x2.edgesOut.get('y'), { spec: '2', invalid: true, to: y1 })
1423
1424 const y1y2 = new Node({
1425 pkg: { name: 'y', version: '2.0.0', dependencies: { x: '1' } },
1426 parent: y1,
1427 })
1428 t.match(root.edgesOut.get('x'), { spec: '1', invalid: false, to: x1 })
1429 t.match(x1.edgesOut.get('y'), { spec: '1', invalid: false, to: y1 })
1430 t.match(y1.edgesOut.get('x'), { spec: '2', invalid: false, to: y1x2 })
1431 t.match(y1x2.edgesOut.get('y'), { spec: '2', invalid: false, to: y1y2 })
1432 t.match(y1y2.edgesOut.get('x'), { spec: '1', invalid: true, to: y1x2 })
1433
1434 const y1y2x1 = new Node({
1435 pkg: { name: 'x', version: '1.0.0', dependencies: { y: '1' } },
1436 parent: y1y2,
1437 })
1438 t.match(root.edgesOut.get('x'), { spec: '1', invalid: false, to: x1 })
1439 t.match(x1.edgesOut.get('y'), { spec: '1', invalid: false, to: y1 })
1440 t.match(y1.edgesOut.get('x'), { spec: '2', invalid: false, to: y1x2 })
1441 t.match(y1x2.edgesOut.get('y'), { spec: '2', invalid: false, to: y1y2 })
1442 t.match(y1y2.edgesOut.get('x'), { spec: '1', invalid: false, to: y1y2x1 })
1443 t.match(y1y2x1.edgesOut.get('y'), { spec: '1', invalid: true, to: y1y2 })
1444
1445 // this is the point at which can tell for certain it's an infinite cycle
1446 const y1y2y1 = new Node({
1447 pkg: { name: 'y', version: '1.0.0', dependencies: { x: '2' } },
1448 parent: y1y2,
1449 })
1450 t.match(root.edgesOut.get('x'), { spec: '1', invalid: false, to: x1 })
1451 t.match(x1.edgesOut.get('y'), { spec: '1', invalid: false, to: y1 })
1452 t.match(y1.edgesOut.get('x'), { spec: '2', invalid: false, to: y1x2 })
1453 t.match(y1x2.edgesOut.get('y'), { spec: '2', invalid: false, to: y1y2 })
1454 t.match(y1y2.edgesOut.get('x'), { spec: '1', invalid: false, to: y1y2x1 })
1455 t.match(y1y2x1.edgesOut.get('y'), { spec: '1', invalid: false, to: y1y2y1 })
1456 t.match(y1y2y1.edgesOut.get('x'), { spec: '2', invalid: true, to: y1y2x1 })
1457
1458 t.end()
1459 })
1460
1461 t.test('detect that two nodes are the same thing', async t => {
1462 const check = (a, b, expect, message) => {
1463 t.equal(a.matches(b), expect, message)
1464 if (a !== b) {
1465 t.equal(b.matches(a), expect, message)
1466 }
1467 }
1468
1469 {
1470 const a = new Node({ path: '/x' })
1471 check(a, a, true, 'same object is trivially matching')
1472 }
1473
1474 {
1475 const p = new Node({ path: '/foo' })
1476 const a = new Node({ name: 'a', parent: p, integrity: 'sha512-xyz' })
1477 const b = new Node({ name: 'b', parent: p, integrity: 'sha512-xyz' })
1478 check(a, b, false, 'different names mean no match')
1479 }
1480
1481 {
1482 const root = new Node({ path: '/root' })
1483 const target = new Node({ root, path: '/foo', pkg: { name: 'x', version: '1.2.3' } })
1484 const a = new Link({ root, path: '/a/x', target })
1485 const b = new Link({ root, path: '/b/x', target })
1486 check(a, b, true, 'links match if targets match')
1487 }
1488
1489 {
1490 const a = new Node({ path: '/foo', pkg: { name: 'x', version: '1.2.3' } })
1491 const b = new Node({ path: '/foo', pkg: { name: 'x', version: '1.2.3' } })
1492 check(a, b, true, 'root nodes match if paths patch')
1493 }
1494
1495 {
1496 const a = new Node({ path: '/a/x', pkg: { name: 'x', version: '1.2.3' } })
1497 const b = new Node({ path: '/b/x', pkg: { name: 'x', version: '1.2.3' } })
1498 check(a, b, false, 'root nodes do not match if paths differ')
1499 }
1500
1501 {
1502 const root = new Node({ path: '/x' })
1503 const integrity = 'sha512-xyzabc'
1504 const a = new Node({ parent: root, name: 'x', integrity })
1505 const b = new Node({ parent: a, name: 'x', integrity })
1506 t.equal(a.integrity, integrity, 'integrity was set')
1507 t.equal(a.integrity, b.integrity, 'integrities match')
1508 check(a, b, true, 'same integrity means same thing')
1509 }
1510
1511 {
1512 const root = new Node({ path: '/x' })
1513 const inta = 'sha512-xyzabc'
1514 const intb = 'sha512-foobar'
1515 const pkg = { name: 'x', version: '1.2.3' }
1516 const resolved = 'https://registry.npmjs.org/x/-/x-1.2.3.tgz'
1517 const a = new Node({ parent: root, pkg, integrity: inta, resolved })
1518 const b = new Node({ parent: a, pkg, integrity: intb, resolved })
1519 t.equal(a.integrity, inta, 'integrity a was set')
1520 t.equal(b.integrity, intb, 'integrity b was set')
1521 check(a, b, false, 'different integrity means different thing')
1522 }
1523
1524 {
1525 const root = new Node({ path: '/x' })
1526 const resolved = 'https://registry.npmjs.org/x/-/x-1.2.3.tgz'
1527 const pkga = { name: 'x', version: '1.2.3-a' }
1528 const pkgb = { name: 'x', version: '1.2.3-b' }
1529 const a = new Node({ parent: root, pkg: pkga, resolved })
1530 const b = new Node({ parent: a, pkg: pkgb, resolved })
1531 check(a, b, true, 'same resolved means same thing, if no integrity')
1532 }
1533
1534 {
1535 const root = new Node({ path: '/x' })
1536 const pkga = { name: 'x', version: '1.2.3' }
1537 const pkgb = { name: 'x', version: '1.2.3' }
1538 const a = new Node({ parent: root, pkg: pkga })
1539 const b = new Node({ parent: a, pkg: pkgb })
1540 check(a, b, true, 'name/version match, if no resolved/integrity')
1541 }
1542
1543 {
1544 const root = new Node({ path: '/x' })
1545 const pkga = { name: 'x', version: '1.2.3-a' }
1546 const pkgb = { name: 'x', version: '1.2.3-b' }
1547 const a = new Node({ parent: root, pkg: pkga })
1548 const b = new Node({ parent: a, pkg: pkgb })
1549 check(a, b, false, 'name/version mismatch, if no resolved/integrity')
1550 }
1551 })
1552
1553 t.test('node.satisfies(requested)', t => {
1554 const node = new Node({
1555 path: '/some/path/to/foo',
1556 resolved: 'https://registry.npmjs.org/foo/-/foo-1.2.3.tgz',
1557 pkg: {
1558 name: 'foo',
1559 version: '1.2.3',
1560 },
1561 })
1562 t.equal(node.satisfies('foo'), true)
1563 t.equal(node.satisfies('foo@1'), true)
1564 t.equal(node.satisfies('https://registry.npmjs.org/foo/-/foo-1.2.3.tgz'), true)
1565 t.equal(node.satisfies('foo@2'), false)
1566 t.equal(node.satisfies('bar'), false)
1567 t.equal(node.satisfies('https://registry.npmjs.org/foo/-/foo-1.2.5.tgz'), false)
1568 node.resolved = 'git+ssh://[email protected]/org/foo.git#decafbad1100facefaceface'
1569 t.equal(node.satisfies('https://registry.npmjs.org/foo/-/foo-1.2.3.tgz'), false)
1570 t.equal(node.satisfies('org/foo'), true)
1571 t.equal(node.satisfies('github:org/foo'), true)
1572 t.end()
1573 })
1574
1575 t.test('node.pkgid', t => {
1576 const parent = new Node({ path: '/some/path' })
1577 t.equal(parent.pkgid, 'path@')
1578
1579 parent.package = { name: 'parent' }
1580 t.equal(parent.pkgid, 'parent@')
1581
1582 parent.package = { name: 'parent', version: '1.2.3' }
1583 t.equal(parent.pkgid, '[email protected]')
1584
1585 const n = new Node({ path: '/some/path/node_modules/foo', parent })
1586 t.equal(n.pkgid, 'foo@')
1587
1588 n.package = { name: 'foo', version: '1.2.3' }
1589 t.equal(n.pkgid, '[email protected]')
1590
1591 n.package = { name: 'bar', version: '1.2.3' }
1592 t.equal(n.pkgid, 'foo@npm:[email protected]')
1593
1594 t.end()
1595 })
1596
1597 t.test('node.version', t => {
1598 const n = new Node({ path: '/some/path' })
1599 t.equal(n.version, '')
1600 n.package.version = '1.2.3'
1601 t.equal(n.version, '1.2.3')
1602 t.end()
1603 })
1604
1605 t.test('explain yourself', t => {
1606 const n = new Node({ path: '/some/path',
1607 pkg: {
1608 dependencies: { x: '1', y: '2' },
1609 } })
1610 t.strictSame(normalizePaths(n.explain()), { location: '/some/path' })
1611 t.equal(n.explain(), n.explain(), 'caches result')
1612 const x = new Node({ parent: n, pkg: { name: 'x', version: '1.2.3' } })
1613 t.strictSame(x.explain(), {
1614 name: 'x',
1615 version: '1.2.3',
1616 location: 'node_modules/x',
1617 isWorkspace: false,
1618 dependents: [{ name: 'x', type: 'prod', spec: '1', from: n.explain() }],
1619 })
1620
1621 const virtual = new Node({
1622 path: '/virtual-root',
1623 sourceReference: x,
1624 })
1625
1626 t.equal(virtual.explain(), x.explain())
1627 const y = new Node({
1628 parent: n,
1629 pkg: { name: 'y', version: '2.3.4', dependencies: { z: '3' } },
1630 children: [
1631 { pkg: { name: 'z', version: '3.4.5', dependencies: { a: '4' } },
1632 children: [
1633 { pkg: { name: 'a', version: '4.5.6', dependencies: {} } },
1634 ],
1635 },
1636 ],
1637 })
1638
1639 const z = y.children.get('z')
1640 const a = z.children.get('a')
1641
1642 t.strictSame(y.explain(), {
1643 name: 'y',
1644 version: '2.3.4',
1645 location: 'node_modules/y',
1646 isWorkspace: false,
1647 dependents: [
1648 {
1649 type: 'prod',
1650 name: 'y',
1651 spec: '2',
1652 from: n.explain(),
1653 },
1654 ],
1655 })
1656
1657 t.strictSame(z.explain(), {
1658 name: 'z',
1659 version: '3.4.5',
1660 location: 'node_modules/y/node_modules/z',
1661 isWorkspace: false,
1662 dependents: [
1663 {
1664 type: 'prod',
1665 name: 'z',
1666 spec: '3',
1667 from: y.explain(),
1668 },
1669 ],
1670 })
1671
1672 t.strictSame(a.explain(), {
1673 name: 'a',
1674 version: '4.5.6',
1675 location: 'node_modules/y/node_modules/z/node_modules/a',
1676 isWorkspace: false,
1677 dependents: [
1678 {
1679 type: 'prod',
1680 name: 'a',
1681 spec: '4',
1682 from: z.explain(),
1683 },
1684 ],
1685 })
1686
1687 // ignore invalid edgesIn except from root node
1688 y.package = {
1689 ...y.package,
1690 dependencies: {
1691 ...y.package.dependencies,
1692 b: '1.2.3',
1693 },
1694 }
1695 a.package = {
1696 ...a.package,
1697 dependencies: {
1698 ...a.package.dependencies,
1699 b: '1.2.3',
1700 },
1701 }
1702 const b = new Node({
1703 parent: n,
1704 pkg: { name: 'b', version: '9.9.9' },
1705 })
1706 t.strictSame(b.explain(), {
1707 name: 'b',
1708 version: '9.9.9',
1709 location: 'node_modules/b',
1710 isWorkspace: false,
1711 dependents: [],
1712 })
1713 b.package = { ...b.package }
1714 n.package = {
1715 ...n.package,
1716 dependencies: {
1717 ...n.package.dependencies,
1718 b: '1.2.3',
1719 },
1720 }
1721 t.strictSame(b.explain(), {
1722 name: 'b',
1723 version: '9.9.9',
1724 location: 'node_modules/b',
1725 isWorkspace: false,
1726 dependents: [{ type: 'prod', name: 'b', spec: '1.2.3', error: 'INVALID', from: n.explain() }],
1727 })
1728
1729 // explain with a given edge
1730 b.package = { ...b.package }
1731 const otherNode = new Node({
1732 pkg: {
1733 ...n.package,
1734 dependencies: {
1735 ...n.package.dependencies,
1736 b: '9',
1737 },
1738 },
1739 path: '/virtual-root',
1740 children: [{ pkg: { ...b.package } }],
1741 })
1742
1743 // explain a node with respect to a specific hypothetical edge
1744 t.strictSame(normalizePaths(b.explain(otherNode.edgesOut.get('b'))), {
1745 name: 'b',
1746 version: '9.9.9',
1747 location: 'node_modules/b',
1748 isWorkspace: false,
1749 dependents: [
1750 {
1751 type: 'prod',
1752 name: 'b',
1753 spec: '9',
1754 from: { location: '/virtual-root' },
1755 },
1756 ],
1757 })
1758
1759 // don't get tripped up by cycles
1760 const cycle = new Node({
1761 path: '/cy/cle',
1762 pkg: { name: 'cycle-root', dependencies: { c: '1' } },
1763 children: [
1764 { pkg: { name: 'a', version: '1.1.1', dependencies: { b: '1' } } },
1765 { pkg: { name: 'b', version: '1.1.1', dependencies: { a: '1' } } },
1766 { pkg: { name: 'c', version: '1.1.1', dependencies: { a: '1' } } },
1767 ],
1768 })
1769
1770 t.strictSame(normalizePaths(cycle.children.get('b').explain()), {
1771 name: 'b',
1772 version: '1.1.1',
1773 location: 'node_modules/b',
1774 isWorkspace: false,
1775 dependents: [
1776 {
1777 type: 'prod',
1778 name: 'b',
1779 spec: '1',
1780 from: {
1781 name: 'a',
1782 version: '1.1.1',
1783 location: 'node_modules/a',
1784 isWorkspace: false,
1785 dependents: [
1786 {
1787 type: 'prod',
1788 name: 'a',
1789 spec: '1',
1790 from: {
1791 name: 'b',
1792 version: '1.1.1',
1793 // doesn't keep adding "from" links here.
1794 },
1795 },
1796 {
1797 type: 'prod',
1798 name: 'a',
1799 spec: '1',
1800 from: {
1801 name: 'c',
1802 version: '1.1.1',
1803 location: 'node_modules/c',
1804 isWorkspace: false,
1805 dependents: [
1806 {
1807 type: 'prod',
1808 name: 'c',
1809 spec: '1',
1810 from: {
1811 location: '/cy/cle',
1812 },
1813 },
1814 ],
1815 },
1816 },
1817 ],
1818 },
1819 },
1820 ],
1821 })
1822
1823 {
1824 // treat a source reference by explaining the thing it's standing in for
1825 const actual = new Node({
1826 path: '/project',
1827 pkg: { dependencies: { a: '1' } },
1828 children: [
1829 { pkg: { name: 'a', version: '1.2.3', dependencies: { b: '1' } } },
1830 { pkg: { name: 'b', version: '1.2.3', dependencies: { c: '1', d: '1' } } },
1831 ],
1832 }).children.get('b')
1833 const virtual = new Node({
1834 path: '/virtual-root',
1835 pkg: { ...actual.package },
1836 sourceReference: actual,
1837 children: [
1838 { pkg: { name: 'c', version: '1.2.3', dependencies: { d: '1' } } },
1839 { pkg: { name: 'd', version: '1.2.3' } },
1840 ],
1841 })
1842
1843 const edge = virtual.children.get('c').edgesOut.get('d')
1844 t.strictSame(normalizePaths(virtual.children.get('d').explain(edge)), {
1845 name: 'd',
1846 version: '1.2.3',
1847 whileInstalling: {
1848 name: 'b',
1849 version: '1.2.3',
1850 path: '/project/node_modules/b',
1851 },
1852 location: 'node_modules/d',
1853 isWorkspace: false,
1854 dependents: [
1855 {
1856 type: 'prod',
1857 name: 'd',
1858 spec: '1',
1859 from: {
1860 name: 'c',
1861 version: '1.2.3',
1862 whileInstalling: {
1863 name: 'b',
1864 version: '1.2.3',
1865 path: '/project/node_modules/b',
1866 },
1867 location: 'node_modules/c',
1868 isWorkspace: false,
1869 dependents: [
1870 {
1871 type: 'prod',
1872 name: 'c',
1873 spec: '1',
1874 from: {
1875 name: 'b',
1876 version: '1.2.3',
1877 location: 'node_modules/b',
1878 isWorkspace: false,
1879 dependents: [
1880 {
1881 type: 'prod',
1882 name: 'b',
1883 spec: '1',
1884 from: {
1885 name: 'a',
1886 version: '1.2.3',
1887 location: 'node_modules/a',
1888 isWorkspace: false,
1889 dependents: [
1890 {
1891 type: 'prod',
1892 name: 'a',
1893 spec: '1',
1894 from: {
1895 location: '/project',
1896 },
1897 },
1898 ],
1899 },
1900 },
1901 ],
1902 },
1903 },
1904 ],
1905 },
1906 },
1907 ],
1908 })
1909 }
1910
1911 // explain with errors
1912 const badParent = new Node({ path: '/bad/nodes' })
1913 const errNode = new Node({
1914 error: new Error('bad node'),
1915 pkg: { name: 'bad', version: 'node' },
1916 parent: badParent,
1917 })
1918 t.match(errNode.explain(), {
1919 errors: [{ message: 'bad node' }],
1920 name: 'bad',
1921 version: 'node',
1922 package: { name: 'bad', version: 'node' },
1923 })
1924 const noPkgDep = new Node({
1925 pkg: { noname: 'bad', noversion: 'node' },
1926 parent: badParent,
1927 path: '/bad/nodes/node_modules/noname',
1928 })
1929 t.match(noPkgDep.explain(), {
1930 errors: [{ message: 'invalid package: lacks name and/or version' }],
1931 package: { noname: 'bad', noversion: 'node' },
1932 })
1933
1934 // workspaces
1935 const workspacesRoot = new Node({
1936 path: '/some/path',
1937 pkg: {
1938 name: 'project-root',
1939 version: '1.0.0',
1940 workspaces: ['a'],
1941 },
1942 })
1943 const workspacesMap = new Map(
1944 [['a', '/some/path/a']]
1945 )
1946 const ws = new Node({
1947 root: workspacesRoot,
1948 path: '/some/path/a',
1949 pkg: { name: 'a', version: '1.0.0' },
1950 })
1951 new Link({
1952 name: 'a',
1953 parent: workspacesRoot,
1954 target: ws,
1955 })
1956 workspacesRoot.workspaces = workspacesMap
1957 t.strictSame(
1958 normalizePaths(ws.explain()),
1959 {
1960 name: 'a',
1961 version: '1.0.0',
1962 location: 'a',
1963 isWorkspace: true,
1964 dependents: [],
1965 linksIn: [
1966 {
1967 name: 'a',
1968 version: '1.0.0',
1969 location: 'node_modules/a',
1970 isWorkspace: true,
1971 dependents: [
1972 {
1973 type: 'workspace',
1974 name: 'a',
1975 spec: 'file:/some/path/a',
1976 from: { location: '/some/path' },
1977 },
1978 ],
1979 },
1980 ],
1981 },
1982 'should have workspaces properly set up'
1983 )
1984 t.end()
1985 })
1986
1987 t.test('guard against setting package to something improper', t => {
1988 // no pkg
1989 const n = new Node({ path: '/some/path' })
1990 t.strictSame(n.package, {})
1991 // falsey pkg
1992 const o = new Node({ path: '/some/path', pkg: null })
1993 t.strictSame(o.package, {})
1994 // non-object pkg
1995 const p = new Node({ path: '/some/path', pkg: 'hello' })
1996 t.strictSame(p.package, {})
1997
1998 // this will throw if we hit the debug, but it'll be an object regardless
1999 try {
2000 p.package = 'this is not an object'
2001 } catch (er) {
2002 t.match(er, { message: 'setting Node.package to non-object' })
2003 } finally {
2004 t.strictSame(p.package, {})
2005 }
2006 t.end()
2007 })
2008
2009 t.test('clear inventory when changing root', t => {
2010 const r1 = new Node({ path: '/root1' })
2011 const r2 = new Node({ path: '/root1/root2',
2012 children: [
2013 { pkg: { name: 'foo', version: '1.2.3' } },
2014 ] })
2015 const r3 = new Node({ path: '/root1/root3' })
2016 // child3 gets munged together with the foo module in root2,
2017 // because the paths are the same
2018 const child3 = new Node({ path: '/root1/root2/node_modules/foo',
2019 pkg: { name: 'foo', version: '1.2.3' },
2020 root: r3,
2021 })
2022 const child = r2.children.get('foo')
2023 t.equal(r1.inventory.size, 1)
2024 t.equal(r2.inventory.size, 2)
2025 t.equal(r1.inventory.has(child), false)
2026 t.equal(r2.inventory.has(child), true)
2027 r2.root = r1
2028 t.equal(r1.inventory.size, 3)
2029 t.equal(r2.inventory.size, 0)
2030 t.equal(r1.inventory.has(child), true)
2031 t.equal(r2.inventory.has(child), false)
2032 t.equal(r3.inventory.size, 2)
2033 r3.root = r1
2034 t.equal(r1.inventory.size, 4)
2035 t.equal(r3.inventory.size, 0)
2036 t.equal(r1.inventory.has(child3), true)
2037 t.equal(child3.root, r1)
2038 t.end()
2039 })
2040
2041 t.test('create a node that doesnt get added to a root until later', t => {
2042 const root = new Node({ path: '/path/to/root' })
2043 const foo = new Node({ name: 'foo' })
2044 t.equal(foo.root, foo, 'other is self-rooted at first')
2045 t.equal(foo.path, null, 'foo has null path')
2046 foo.parent = root
2047 t.equal(foo.root, root, 'foo is rooted on root after parent assignment')
2048 t.equal(normalizePath(foo.path), normalizePath('/path/to/root/node_modules/foo'), 'foo has updated path')
2049 t.end()
2050 })
2051
2052 t.test('changing path to a node_modules folder sets name if necessary', t => {
2053 const node = new Node({
2054 path: '/some/random/path',
2055 })
2056 t.equal(node.name, 'path')
2057 const _changePath = Symbol.for('_changePath')
2058 node[_changePath]('/path/to/node_modules/foo')
2059 t.equal(node.name, 'foo')
2060 node[_changePath]('/path/to/node_modules/foo/node_modules/bar')
2061 t.equal(node.name, 'bar')
2062 node[_changePath]('/path/to/node_modules/foo/node_modules/@bar/baz/node_modules')
2063 t.equal(node.name, 'bar')
2064 node[_changePath]('/path/to/node_modules/foo/node_modules/@bar/baz')
2065 t.equal(node.name, '@bar/baz')
2066 t.end()
2067 })
2068
2069 t.test('printable Node', t => {
2070 t.cleanSnapshot = str => str
2071 // normalize paths
2072 .split(process.cwd()).join('{CWD}')
2073 .replace(/[A-Z]:/g, '')
2074 .replace(/\\+/g, '/')
2075 // FIXME: once we drop support to node10 we can remove some of this
2076 .replace(/:\n? +/g, ':')
2077 .replace(/\n +/g, '\n')
2078 .replace(/\n\}/g, ' }')
2079 .replace(/\n\]/g, ']')
2080 .replace(/\n\[/g, '[')
2081 .replace(/\n\{\n/g, ' { ')
2082 .replace(/Map\([0-9]\)/g, 'Map')
2083 .replace(/Set\([0-9]\)/g, 'Set')
2084 .replace(/:\n *Map/g, ':Map')
2085 .replace(/:\n *Set/g, ':Set')
2086 .replace(/ArboristNode /g, '')
2087 .replace(/Edge /g, '')
2088 .replace(/ *([[\]{}]) */g, '$1')
2089
2090 t.test('extraneous tree', t => {
2091 const tree = new Node({
2092 name: 'printable-node',
2093 pkg: {
2094 name: 'printable-node',
2095 version: '1.1.1',
2096 dependencies: { prod: '1.x', b: '', missing: '' },
2097 },
2098 path: '/home/user/projects/root',
2099 realpath: '/home/user/projects/root',
2100 children: [{
2101 pkg: {
2102 name: 'prod',
2103 version: '1.2.3',
2104 dependencies: { meta: '', b: '' },
2105 peerDependencies: { peer: '' },
2106 },
2107 fsChildren: [{
2108 realpath: '/home/user/projects/root/node_modules/prod/foo',
2109 path: '/home/user/projects/root/node_modules/prod/foo',
2110 name: 'foo',
2111 pkg: { name: 'foo', version: '1.2.3', dependencies: { meta: '' } },
2112 }, {
2113 realpath: '/home/user/projects/root/node_modules/prod/bar',
2114 path: '/home/user/projects/root/node_modules/prod/bar',
2115 name: 'bar',
2116 pkg: { name: 'bar', version: '1.0.0' },
2117 }],
2118 resolved: 'prod',
2119 integrity: 'prod',
2120 }, {
2121 pkg: {
2122 name: 'b',
2123 version: '1.2.3',
2124 },
2125 resolved: 'b',
2126 integrity: 'b',
2127 }, {
2128 pkg: {
2129 name: 'c',
2130 },
2131 resolved: 'c',
2132 integrity: 'c',
2133 }],
2134 })
2135 tree.error = { code: 'ERR', path: '/' }
2136 t.matchSnapshot(util.inspect(tree, { depth: 6 }),
2137 'should print human readable representation of node tree')
2138 t.end()
2139 })
2140
2141 t.test('variations', t => {
2142 // manually tweaked variations in the tree to reach for
2143 // possible different trees output
2144 const tree = new Node({
2145 name: 'variations',
2146 pkg: {
2147 name: 'variations',
2148 version: '1.0.0',
2149 dependencies: { a: '^1.0.0', b: '^1.0.0' },
2150 },
2151 extraneous: false,
2152 path: '/home/user/projects/root',
2153 realpath: '/home/user/projects/root',
2154 })
2155 // append nodes
2156 const a = new Node({
2157 name: 'a',
2158 pkg: {
2159 name: 'a',
2160 version: '1.1.1',
2161 },
2162 path: '/home/users/projects/root/node_modules/a',
2163 realpath: '/home/users/projects/root/node_modules/a',
2164 parent: tree,
2165 })
2166 a.extraneous = false
2167 a.dev = true
2168 a.optional = true
2169 a.getBundler = () => true
2170 a.errors = [Object.assign(new Error('ERR'), { code: 'ERR' })]
2171 const b = new Link({
2172 name: 'b',
2173 pkg: {
2174 name: 'b',
2175 version: '1.0.0',
2176 },
2177 optional: true,
2178 path: '/home/users/projects/root/c-link',
2179 realpath: '/home/users/projects/root/c',
2180 parent: tree,
2181 })
2182 const c = new Node({
2183 name: 'c',
2184 pkg: { name: 'c', version: '1.0.0' },
2185 path: '/home/user/projects/root/c',
2186 realpath: '/home/user/projects/root/c',
2187 fsParent: tree,
2188 })
2189 b.target = c
2190 b.extraneous = false
2191 b.dev = false
2192 b.optional = false
2193 b.peer = false
2194 b.errors = [Object.assign(new Error('ERR'), {
2195 code: 'ERR',
2196 path: '/home/users/projects/root/node_modules/b',
2197 })]
2198
2199 // another link to c
2200 new Link({
2201 name: 'd',
2202 realpath: '/home/users/projects/root/c',
2203 target: c,
2204 parent: tree,
2205 })
2206
2207 tree.error = a.errors[0]
2208
2209 t.matchSnapshot(util.inspect(tree, { depth: 6 }),
2210 'should match non-extraneous tree representation')
2211
2212 t.end()
2213 })
2214 t.end()
2215 })
2216
2217 t.test('isProjectRoot shows if the node is the root link target', async t => {
2218 const link = new Link({
2219 path: '/link',
2220 realpath: '/actual',
2221 })
2222 const n = new Node({ path: '/actual', root: link })
2223 t.equal(n.isProjectRoot, true)
2224 t.equal(link.isProjectRoot, true)
2225 t.equal(link.isRoot, true)
2226 t.equal(n.isRoot, false)
2227 })
2228
2229 t.test('virtual references to root node has devDep edges', async t => {
2230 const root = new Node({
2231 path: '/some/project/path',
2232 pkg: {
2233 devDependencies: {
2234 a: '1',
2235 },
2236 },
2237 })
2238 const virtualRoot = new Node({
2239 path: '/virtual-root',
2240 sourceReference: root,
2241 })
2242 t.equal(virtualRoot.edgesOut.get('a').type, 'dev')
2243 })
2244
2245 t.test('globalTop set for children of global link root target', async t => {
2246 const root = new Link({
2247 path: '/usr/local/lib',
2248 realpath: '/data/lib',
2249 global: true,
2250 })
2251 root.target = new Node({
2252 path: '/data/lib',
2253 global: true,
2254 root,
2255 })
2256 const gtop = new Node({
2257 parent: root.target,
2258 pkg: { name: 'foo', version: '1.2.3' },
2259 })
2260 t.equal(gtop.globalTop, true)
2261 })
2262
2263 t.test('duplicated dependencies', t => {
2264 // the specific logic here is justifiable at all steps, but gets weird
2265 // in the "specified in all three" case, even though that's the logical
2266 // outcome of the other rules. at least we have a test showing what
2267 // actually happens.
2268
2269 t.test('prefer prod over peer', async t => {
2270 const n = new Node({
2271 path: '/path/to/project',
2272 pkg: {
2273 dependencies: {
2274 foo: '1.x',
2275 },
2276 peerDependencies: {
2277 foo: '>=1',
2278 },
2279 },
2280 })
2281 t.match(n.edgesOut.get('foo'), { type: 'prod', spec: '1.x' })
2282 })
2283
2284 t.test('prefer dev over peer', async t => {
2285 const n = new Node({
2286 path: '/path/to/project',
2287 pkg: {
2288 devDependencies: {
2289 foo: '1.x',
2290 },
2291 peerDependencies: {
2292 foo: '>=1',
2293 },
2294 },
2295 })
2296 t.match(n.edgesOut.get('foo'), { type: 'dev', spec: '1.x' })
2297 })
2298
2299 t.test('prefer prod over dev', async t => {
2300 const n = new Node({
2301 path: '/path/to/project',
2302 pkg: {
2303 devDependencies: {
2304 foo: '1.x',
2305 },
2306 dependencies: {
2307 foo: '>=1',
2308 },
2309 },
2310 })
2311 t.match(n.edgesOut.get('foo'), { type: 'dev', spec: '1.x' })
2312 })
2313
2314 t.test('if in all three, use dev', async t => {
2315 const n = new Node({
2316 path: '/path/to/project',
2317 pkg: {
2318 devDependencies: {
2319 foo: '1.x',
2320 },
2321 dependencies: {
2322 foo: '2',
2323 },
2324 peerDependencies: {
2325 foo: '>=1',
2326 },
2327 },
2328 })
2329 t.match(n.edgesOut.get('foo'), { type: 'dev', spec: '1.x' })
2330 })
2331
2332 t.test('prefer workspace version', async t => {
2333 const root = new Node({
2334 pkg: { name: 'workspaces_root' },
2335 path: '/home/user/projects/workspaces_root',
2336 realpath: '/home/user/projects/workspaces_root',
2337 })
2338
2339 root.workspaces = new Map([
2340 ['foo', '/home/user/projects/workspaces_root/foo'],
2341 ])
2342
2343 root.package = { name: 'bar', version: '1.2.3', dependencies: { foo: '2.3.4' } }
2344 t.equal(root.edgesOut.get('foo').type, 'workspace', 'keeps workspace edge')
2345 })
2346
2347 t.end()
2348 })
2349
2350 t.test('canDedupe()', t => {
2351 /*
2352 root
2353 +-- [email protected]
2354 +-- [email protected]
2355 | +-- [email protected] (removable only if preferDedupe)
2356 | +-- [email protected]
2357 | | +-- a 2.3.99
2358 | | +-- e 2.0.1 (removable, older version)
2359 | +-- d 3.4.5
2360 | | +-- a 3.4.5
2361 | +-- e 2.3.4
2362 +-- bundler 1.2.3
2363 | +-- a 1.2.3 (not removable, in bundle)
2364 +-- c 3.4.5
2365 | +-- a 1.2.3 (removable, matches)
2366 +-- extraneous 1.2.3
2367 +-- wrapper
2368 +-- a 1.2.3 (not removable, in shrinkwrap)
2369 */
2370
2371 const root = new Node({
2372 path: '/path/to/root',
2373 pkg: {
2374 name: 'root',
2375 version: '1.2.3',
2376 dependencies: {
2377 bundler: '',
2378 wrapper: '',
2379 a: '1',
2380 b: '2',
2381 c: '3',
2382 },
2383 },
2384 children: [
2385 {
2386 pkg: {
2387 name: 'bundler',
2388 version: '1.2.3',
2389 bundleDependencies: ['a'],
2390 dependencies: {
2391 a: '1',
2392 },
2393 },
2394 children: [
2395 {
2396 pkg: { name: 'a', version: '1.2.3' },
2397 integrity: 'a123',
2398 },
2399 ],
2400 },
2401 {
2402 pkg: {
2403 name: 'wrapper',
2404 version: '1.2.3',
2405 dependencies: {
2406 a: '1',
2407 },
2408 _hasShrinkwrap: true,
2409 },
2410 hasShrinkwrap: true,
2411 children: [
2412 {
2413 pkg: { name: 'a', version: '1.2.3' },
2414 integrity: 'a123',
2415 },
2416 ],
2417 },
2418 {
2419 pkg: { name: 'a', version: '1.2.3' },
2420 integrity: 'a123',
2421 },
2422 {
2423 pkg: { name: 'c', version: '3.4.5', dependencies: { a: '1' } },
2424 children: [
2425 {
2426 pkg: { name: 'a', version: '1.2.3' },
2427 integrity: 'a123',
2428 },
2429 ],
2430 },
2431 {
2432 pkg: {
2433 name: 'b',
2434 version: '2.3.4',
2435 dependencies: {
2436 a: '1',
2437 c: '2',
2438 d: '3',
2439 },
2440 },
2441 children: [
2442 { pkg: { name: 'a', version: '1.9.99' } },
2443 { pkg: { name: 'e', version: '2.3.4' }, integrity: 'y' },
2444 {
2445 pkg: {
2446 name: 'c',
2447 version: '2.3.4',
2448 dependencies: { a: '2' },
2449 },
2450 children: [
2451 {
2452 pkg: {
2453 name: 'a',
2454 version: '2.3.99',
2455 dependencies: { e: '2' },
2456 },
2457 integrity: 'a2399',
2458 children: [
2459 { pkg: { name: 'e', version: '2.0.1' }, integrity: 'x' },
2460 ],
2461 },
2462 ],
2463 },
2464 {
2465 pkg: {
2466 name: 'd',
2467 version: '3.4.5',
2468 dependencies: {
2469 a: '3',
2470 },
2471 },
2472 children: [{ pkg: { name: 'a', version: '3.4.5' } }],
2473 },
2474 ],
2475 },
2476 { pkg: { name: 'extraneous', version: '1.2.3' } },
2477 ],
2478 })
2479
2480 const canDedupeLocs = [...root.inventory.filter(n => n.canDedupe())]
2481 .map(n => n.location)
2482 t.match(canDedupeLocs, [
2483 'node_modules/c/node_modules/a',
2484 'node_modules/b/node_modules/e',
2485 'node_modules/b/node_modules/c/node_modules/a/node_modules/e',
2486 ], 'preferDedupe=false')
2487
2488 const canDedupeTrueLocs = [...root.inventory.filter(n => n.canDedupe(true))]
2489 .map(n => n.location)
2490 t.match(canDedupeTrueLocs, [
2491 'node_modules/c/node_modules/a',
2492 // this is the one that's only deduped if we preferDedupe
2493 'node_modules/b/node_modules/a',
2494 'node_modules/b/node_modules/e',
2495 'node_modules/b/node_modules/c/node_modules/a/node_modules/e',
2496 ], 'preferDedupe=true')
2497
2498 // canDedupe also handles fsChildren properly
2499 const top = new Node({
2500 fsParent: root,
2501 path: root.path + '/packages/top',
2502 pkg: {
2503 name: 'top',
2504 version: '1.2.3',
2505 dependencies: {
2506 a: '1',
2507 },
2508 },
2509 children: [
2510 { pkg: { name: 'a', version: '1.0.0' } },
2511 ],
2512 })
2513
2514 t.equal(top.children.get('a').canDedupe(), true)
2515
2516 // check fsTop and isDescendantOf
2517 t.equal(top.isDescendantOf(root), true)
2518 t.equal(top.isFsTop, false)
2519 t.equal(top.fsTop, root)
2520 t.equal(top.children.get('a').isFsTop, true)
2521
2522 t.end()
2523 })
2524
2525 t.test('canDedupe returns true when explicitRequest is true regardless of other conditions', t => {
2526 // Create a minimal tree with a valid resolveParent
2527 const root = new Node({
2528 pkg: { name: 'root', version: '1.0.0' },
2529 path: '/root',
2530 realpath: '/root',
2531 })
2532
2533 // Create a duplicate candidate node in the tree
2534 const duplicate = new Node({
2535 pkg: { name: 'dup', version: '1.0.0' },
2536 parent: root,
2537 })
2538
2539 // Create a node with the same name but a higher version so that normally dedupe would not occur
2540 const node = new Node({
2541 pkg: { name: 'dup', version: '2.0.0' },
2542 parent: duplicate,
2543 })
2544
2545 // Manually add an incoming edge so that node.edgesIn is non-empty
2546 node.edgesIn.add({
2547 from: duplicate,
2548 satisfiedBy () {
2549 return true
2550 },
2551 })
2552
2553 const preferDedupe = false
2554 let explicitRequest = false
2555 t.notOk(node.canDedupe(preferDedupe, explicitRequest), 'without explicit request, dedupe is not allowed')
2556
2557 explicitRequest = true
2558 t.ok(node.canDedupe(preferDedupe, explicitRequest), 'explicit request forces dedupe to return true')
2559 t.end()
2560 })
2561
2562 t.test('packageName getter', t => {
2563 const node = new Node({
2564 pkg: { name: 'foo' },
2565 path: '/path/to/bar',
2566 })
2567 t.equal(node.name, 'bar')
2568 t.equal(node.packageName, 'foo')
2569 t.end()
2570 })
2571
2572 t.test('node at / should not have fsParent', t => {
2573 const root = new Node({ path: '/some/path' })
2574 const link = new Link({
2575 parent: root,
2576 name: 'link',
2577 realpath: '/',
2578 })
2579 t.equal(link.target.fsParent, null)
2580 t.end()
2581 })
2582
2583 t.test('node.ancestry iterator', t => {
2584 const root = new Node({
2585 path: '/some/path',
2586 pkg: { name: 'root', version: '1.2.3' },
2587 children: [{
2588 pkg: { name: 'a', version: '1.2.3' },
2589 children: [{
2590 pkg: { name: 'b', version: '1.2.3' },
2591 children: [{
2592 pkg: { name: 'c', version: '1.2.3' },
2593 }],
2594 }],
2595 }],
2596 })
2597 const c = root.children.get('a').children.get('b').children.get('c')
2598 const d = new Node({
2599 root,
2600 path: c.path + '/d',
2601 pkg: { name: 'd', version: '1.2.3' },
2602 children: [{
2603 pkg: { name: 'e', version: '1.2.3' },
2604 children: [{
2605 pkg: { name: 'f', version: '1.2.3' },
2606 children: [{
2607 pkg: { name: 'g', version: '1.2.3' },
2608 }],
2609 }],
2610 }],
2611 })
2612 const g = d.children.get('e').children.get('f').children.get('g')
2613
2614 const ancestry = [...g.ancestry()].map(n => n.packageName)
2615 t.strictSame(ancestry, ['g', 'f', 'e', 'd', 'c', 'b', 'a', 'root'])
2616 t.end()
2617 })
2618
2619 t.test('canReplaceWith is always false when packageName does not match', t => {
2620 const root = new Node({
2621 path: '/some/path',
2622 pkg: {
2623 dependencies: {
2624 foo: '1.2.3',
2625 alias: 'npm:[email protected]',
2626 },
2627 },
2628 children: [
2629 { pkg: { name: 'foo', version: '1.2.3' } },
2630 { name: 'alias', pkg: { name: 'bar', version: '1.2.3' } },
2631 ],
2632 })
2633 const rep = new Node({
2634 path: '/some/path/node_modules/foo',
2635 name: 'foo',
2636 pkg: {
2637 name: 'bar',
2638 version: '1.2.3',
2639 },
2640 })
2641 t.equal(rep.canReplace(root.children.get('foo')), false,
2642 'cannot replace actual node with an alias')
2643 const alias = new Node({
2644 path: '/some/path/node_modules/alias',
2645 name: 'alias',
2646 pkg: {
2647 name: 'bar',
2648 version: '1.2.3',
2649 },
2650 })
2651 t.equal(alias.canReplace(root.children.get('alias')), true,
2652 'can replace alias with a different alias to same thing')
2653 t.end()
2654 })
2655
2656 t.test('canReplace while ignoring certain peer deps', t => {
2657 const tree = new Node({
2658 path: '/some/path',
2659 pkg: { dependencies: { a: '1||2', b: '' } },
2660 children: [
2661 { pkg: { name: 'a', version: '1.0.0', peerDependencies: { b: '1' } } },
2662 { pkg: { name: 'b', version: '1.0.0', peerDependencies: { a: '1' } } },
2663 ],
2664 })
2665 const current = tree.children.get('a')
2666 const rep = new Node({
2667 path: current.path,
2668 pkg: { name: 'a', version: '2.0.0' },
2669 })
2670 t.equal(rep.canReplace(current), false, 'cannot replace because peer dep')
2671 t.equal(rep.canReplace(current, ['b']), true,
2672 'can replace if ignoring the `b` peer')
2673
2674 t.end()
2675 })
2676
2677 t.test('children are unicode-normalizing and case-insensitive', t => {
2678 const cafe1 = Buffer.from([0x63, 0x61, 0x66, 0x65, 0xcc, 0x81]).toString()
2679 const cafe2 = Buffer.from([0x63, 0x61, 0x66, 0xc3, 0xa9]).toString()
2680 const tree = new Node({
2681 path: '/some/path',
2682 children: [
2683 { pkg: { name: 'A', version: '1.0.0' } },
2684 { pkg: { name: 'a', version: '2.0.0' } },
2685 { pkg: { name: cafe1, version: '1.0.0' } },
2686 { pkg: { name: cafe2, version: '2.0.0' } },
2687 ],
2688 })
2689 t.equal(tree.children.size, 2)
2690 t.equal(tree.children.get('A'), tree.children.get('a'))
2691 t.match(tree.children.get('a'), {
2692 version: '2.0.0',
2693 packageName: 'a',
2694 name: 'a',
2695 })
2696 t.equal(tree.children.get(cafe1), tree.children.get(cafe2))
2697 t.match(tree.children.get(cafe1), {
2698 version: '2.0.0',
2699 packageName: cafe2,
2700 name: cafe2,
2701 })
2702 t.end()
2703 })
2704
2705 t.test('children of the global root are considered tops', t => {
2706 const tree = new Node({
2707 path: '/usr/local/lib',
2708 global: true,
2709 children: [
2710 {
2711 pkg: { name: 'foo', version: '1.2.3' },
2712 children: [{ pkg: { name: 'bar', version: '1.2.3' } }],
2713 },
2714 ],
2715 })
2716 const foo = tree.children.get('foo')
2717 const bar = foo.children.get('bar')
2718 t.equal(foo.isTop, true)
2719 t.equal(foo.top, foo)
2720 t.equal(bar.top, foo)
2721 t.end()
2722 })
2723
2724 t.test('overrides', (t) => {
2725 t.test('skips loading when no overrides are provided', (t) => {
2726 const tree = new Node({
2727 loadOverrides: true,
2728 path: '/some/path',
2729 pkg: {
2730 name: 'foo',
2731 },
2732 })
2733 t.equal(tree.overrides, undefined, 'overrides is undefined')
2734 t.end()
2735 })
2736
2737 t.test('skips loading when overrides are empty', (t) => {
2738 const tree = new Node({
2739 loadOverrides: true,
2740 path: '/some/path',
2741 pkg: {
2742 name: 'foo',
2743 overrides: {},
2744 },
2745 })
2746 t.equal(tree.overrides, undefined, 'overrides is undefined')
2747 t.end()
2748 })
2749
2750 t.test('loads overrides', (t) => {
2751 const tree = new Node({
2752 loadOverrides: true,
2753 path: '/some/path',
2754 pkg: {
2755 name: 'foo',
2756 dependencies: {
2757 bar: '^1',
2758 },
2759 overrides: {
2760 bar: { '.': '2.0.0' },
2761 },
2762 },
2763 })
2764 t.ok(tree.overrides, 'overrides is defined')
2765 t.end()
2766 })
2767
2768 t.test('node.overridden is true when an override applies to a specific node', async (t) => {
2769 const tree = new Node({
2770 loadOverrides: true,
2771 path: '/some/path',
2772 pkg: {
2773 name: 'foo',
2774 dependencies: {
2775 bar: '^1',
2776 },
2777 overrides: {
2778 baz: '1.0.0',
2779 },
2780 },
2781 children: [{
2782 name: 'bar',
2783 version: '1.0.0',
2784 pkg: {
2785 dependencies: {
2786 baz: '2.0.0',
2787 },
2788 },
2789 children: [{
2790 name: 'baz',
2791 version: '1.0.0',
2792 pkg: {
2793 version: '1.0.0',
2794 dependencies: {
2795 buzz: '1.0.0',
2796 },
2797 },
2798 children: [{
2799 name: 'buzz',
2800 version: '1.0.0',
2801 pkg: {},
2802 }],
2803 }],
2804 }],
2805 })
2806
2807 const bar = tree.edgesOut.get('bar').to
2808 t.not(bar.overridden, 'bar was not overridden')
2809 const baz = bar.edgesOut.get('baz').to
2810 t.ok(baz.overridden, 'baz was overridden')
2811 const buzz = baz.edgesOut.get('buzz').to
2812 t.not(buzz.overridden, 'buzz was not overridden')
2813 })
2814
2815 t.test('node.overridden is false when an override does not match the node version', async (t) => {
2816 const tree = new Node({
2817 loadOverrides: true,
2818 path: '/some/path',
2819 pkg: {
2820 name: 'foo',
2821 dependencies: {
2822 bar: '^1',
2823 },
2824 overrides: {
2825 baz: '1.0.0', // Override specifies "1.0.0"
2826 },
2827 },
2828 children: [{
2829 name: 'bar',
2830 version: '1.0.0',
2831 pkg: {
2832 dependencies: {
2833 baz: '2.0.0',
2834 },
2835 },
2836 children: [{
2837 name: 'baz',
2838 version: '3.0.0',
2839 pkg: {
2840 version: '3.0.0', // This does NOT match the override!
2841 dependencies: {
2842 buzz: '1.0.0',
2843 },
2844 },
2845 children: [{
2846 name: 'buzz',
2847 version: '1.0.0',
2848 pkg: {},
2849 }],
2850 }],
2851 }],
2852 })
2853
2854 const bar = tree.edgesOut.get('bar').to
2855 t.not(bar.overridden, 'bar was not overridden')
2856
2857 const baz = bar.edgesOut.get('baz').to
2858 t.not(baz.overridden, 'baz was not overridden because version mismatch')
2859
2860 const buzz = baz.edgesOut.get('buzz').to
2861 t.not(buzz.overridden, 'buzz was not overridden')
2862 })
2863
2864 t.test('node.overridden returns false when an incoming edge override equals its source override', t => {
2865 const baseOverride = new OverrideSet({
2866 overrides: {
2867 foo: 'bar',
2868 },
2869 })
2870 baseOverride.name = 'test-package'
2871 baseOverride.value = '1.0.0'
2872
2873 const node = new Node({
2874 pkg: { name: 'test-package', version: '1.0.0' },
2875 path: '/some/path/test-package',
2876 realpath: '/some/path/test-package',
2877 overrides: baseOverride,
2878 })
2879
2880 const equalOverride = new OverrideSet({
2881 overrides: {
2882 foo: 'bar',
2883 },
2884 })
2885 equalOverride.name = 'test-package'
2886 equalOverride.value = '1.0.0'
2887
2888 const fakeEdge = {
2889 overrides: equalOverride,
2890 from: { overrides: baseOverride },
2891 }
2892
2893 node.edgesIn.add(fakeEdge)
2894
2895 t.equal(node.overridden, false, 'node.overridden returns false when edge.override equals edge.from.override')
2896 t.end()
2897 })
2898
2899 t.test('assertRootOverrides throws when a dependency and override conflict', async (t) => {
2900 const conflictingTree = new Node({
2901 loadOverrides: true,
2902 path: '/some/path',
2903 pkg: {
2904 name: 'foo',
2905 dependencies: {
2906 bar: '1.x',
2907 },
2908 overrides: {
2909 bar: '2.x',
2910 },
2911 },
2912 children: [
2913 { pkg: { name: 'bar', version: '1.x' } },
2914 ],
2915 })
2916
2917 t.throws(() => conflictingTree.assertRootOverrides(), { code: 'EOVERRIDE' }, 'throws EOVERRIDE')
2918
2919 const conflictingChild = conflictingTree.children.get('bar')
2920 t.doesNotThrow(() => conflictingChild.assertRootOverrides(), 'child does not throw')
2921
2922 const safeTree = new Node({
2923 loadOverrides: true,
2924 path: '/some/path',
2925 pkg: {
2926 name: 'foo',
2927 dependencies: {
2928 bar: '1.x',
2929 },
2930 overrides: {
2931 baz: '2.x',
2932 },
2933 },
2934 children: [
2935 { pkg: { name: 'bar', version: '1.x' } },
2936 ],
2937 })
2938
2939 t.doesNotThrow(() => safeTree.assertRootOverrides(), 'non conflicting tree does not throw')
2940 })
2941
2942 t.test('overrides propagate to children and edges', (t) => {
2943 const tree = new Node({
2944 loadOverrides: true,
2945 path: '/some/path',
2946 pkg: {
2947 name: 'foo',
2948 dependencies: {
2949 bar: '^1',
2950 },
2951 overrides: {
2952 bar: { '.': '2.0.0' },
2953 },
2954 },
2955 children: [
2956 { pkg: { name: 'bar', version: '1.0.0' } },
2957 ],
2958 })
2959
2960 t.ok(tree.overrides, 'overrides is defined on root')
2961 t.ok(tree.edgesOut.get('bar').overrides, 'overrides is defined on edgeOut')
2962 t.ok(tree.children.get('bar').overrides, 'overrides is defined on child')
2963 t.end()
2964 })
2965
2966 t.test('setting root replaces overrides', async (t) => {
2967 const root = new Node({
2968 path: '/some/path',
2969 loadOverrides: true,
2970 pkg: {
2971 name: 'root',
2972 version: '1.0.0',
2973 dependencies: {
2974 foo: '^1.0.0',
2975 },
2976 overrides: {
2977 bar: '^2.0.0',
2978 },
2979 },
2980 })
2981
2982 const foo = new Node({
2983 path: '/some/path/node_modules/foo',
2984 pkg: {
2985 name: 'foo',
2986 version: '1.0.0',
2987 dependencies: {
2988 bar: '^1.0.0',
2989 },
2990 },
2991 })
2992
2993 const bar = new Node({
2994 path: '/some/path/node_modules/bar',
2995 pkg: {
2996 name: 'bar',
2997 version: '2.0.0',
2998 },
2999 })
3000
3001 t.ok(root.overrides, 'root has overrides')
3002 t.notOk(foo.overrides, 'foo does not have overrides')
3003 t.notOk(bar.overrides, 'bar does not have overrides')
3004 t.notOk(root.edgesOut.get('foo').valid, 'foo edge is not valid')
3005 t.notOk(foo.edgesOut.get('bar').valid, 'bar edge is not valid')
3006
3007 // Attach bar to root. This does not trigger override propagation because
3008 // bar is not connected via a dependency edge.
3009 bar.root = root
3010 t.notOk(bar.overrides, 'bar still does not have overrides until connected by a dependency edge')
3011 t.notOk(foo.edgesOut.get('bar').valid, 'bar edge is not valid yet')
3012
3013 // Now attach foo to root so that it is connected as a dependency.
3014 foo.root = root
3015 t.ok(foo.overrides, 'foo now has overrides')
3016 t.ok(root.edgesOut.get('foo').valid, 'foo edge is now valid')
3017 t.ok(bar.overrides, 'bar now has overrides after foo is attached')
3018 t.ok(foo.edgesOut.get('bar').valid, 'bar edge is now valid')
3019 })
3020
3021 t.test('canReplaceWith requires the same overrides', async (t) => {
3022 const original = new Node({
3023 loadOverrides: true,
3024 path: '/some/path',
3025 pkg: {
3026 name: 'foo',
3027 dependencies: {
3028 bar: '^1',
3029 },
3030 overrides: {
3031 bar: { '.': '2.0.0' },
3032 },
3033 },
3034 children: [
3035 { pkg: { name: 'bar', version: '1.0.0' } },
3036 ],
3037 })
3038
3039 const equivalentReplacement = new Node({
3040 loadOverrides: true,
3041 path: '/some/path',
3042 pkg: {
3043 name: 'foo',
3044 dependencies: {
3045 bar: '^1',
3046 },
3047 overrides: {
3048 bar: { '.': '2.0.0' },
3049 },
3050 },
3051 children: [
3052 { pkg: { name: 'bar', version: '1.0.0' } },
3053 ],
3054 })
3055
3056 t.equal(original.canReplaceWith(equivalentReplacement), true, 'different overrides passes')
3057
3058 const goodReplacement = new Node({
3059 path: '/some/path',
3060 pkg: {
3061 name: 'foo',
3062 dependencies: {
3063 bar: '^1',
3064 },
3065 overrides: {
3066 bar: { '.': '2.0.0' },
3067 },
3068 },
3069 children: [
3070 { pkg: { name: 'bar', version: '1.0.0' } },
3071 ],
3072 })
3073
3074 t.equal(original.canReplaceWith(goodReplacement), false, 'no overrides fails')
3075
3076 goodReplacement.overrides = original.overrides
3077 t.equal(original.canReplaceWith(goodReplacement), true, 'same overrides passes')
3078 })
3079
3080 t.end()
3081 })
3082
3083 t.test('node with no edges in is not a registry dep', async t => {
3084 const node = new Node({ path: '/foo' })
3085 t.equal(node.isRegistryDependency, false)
3086 })
3087
3088 t.test('node with non registry edge in is not a registry dep', async t => {
3089 const root = new Node({ path: '/some/path', pkg: { dependencies: { registry: '', tar: '' } } })
3090 const node = new Node({ pkg: { name: 'node', version: '1.0.0' }, parent: root })
3091
3092 new Node({ pkg: { name: 'registry', dependencies: { node: '^1.0.0' } }, parent: root })
3093 new Node({ pkg: { name: 'tar', dependencies: { node: 'file:node' } }, parent: root })
3094
3095 t.equal(node.isRegistryDependency, false)
3096 })
3097
3098 t.test('node with only registry edges in a registry dep', async t => {
3099 const root = new Node({ path: '/some/path', pkg: { dependencies: { registry: '', tar: '' } } })
3100 const node = new Node({ pkg: { name: 'node', version: '1.0.0' }, parent: root })
3101 new Node({ pkg: { name: 'registry', dependencies: { node: '^1.0.0' } }, parent: root })
3102
3103 t.equal(node.isRegistryDependency, true)
3104 })
3105
3106 t.test('canReplaceWith returns false when overrides differ', t => {
3107 const override1 = new OverrideSet({
3108 overrides: { foo: '1.0.0' },
3109 })
3110 const override2 = new OverrideSet({
3111 overrides: { foo: '2.0.0' },
3112 })
3113
3114 // Create two nodes with a dependency to force creation of an outgoing edge
3115 const node1 = new Node({
3116 pkg: { name: 'foo', dependencies: { bar: '^1' } },
3117 path: '/some/path/foo',
3118 realpath: '/some/path/foo',
3119 overrides: override1,
3120 })
3121 const node2 = new Node({
3122 pkg: { name: 'foo', dependencies: { bar: '^1' } },
3123 path: '/some/path/foo',
3124 realpath: '/some/path/foo',
3125 overrides: override2,
3126 })
3127
3128 t.ok(node1.edgesOut.size > 0, 'node1 has outgoing edges')
3129 t.equal(node1.canReplaceWith(node2, new Set()), false, 'cannot replace when overrides differ')
3130 t.end()
3131 })
3132
3133 t.test('updateOverridesEdgeInRemoved uses findSpecificOverrideSet for multiple edgesIn', t => {
3134 const commonOverrides = new OverrideSet({
3135 overrides: {
3136 foo: '1.0.0',
3137 },
3138 })
3139 const specificOverrides = new OverrideSet({
3140 overrides: {
3141 foo: '1.0.0',
3142 bar: '2.0.0',
3143 },
3144 })
3145 // Create a node with initial overrides set to commonOverrides
3146 const node = new Node({
3147 pkg: { name: 'nodeA' },
3148 path: '/some/path/nodeA',
3149 realpath: '/some/path/nodeA',
3150 overrides: commonOverrides,
3151 })
3152 // Simulate incoming edges with overrides
3153 node.edgesIn.add({
3154 overrides: commonOverrides,
3155 })
3156 node.edgesIn.add({
3157 overrides: specificOverrides,
3158 })
3159 // Call updateOverridesEdgeInRemoved passing an override set equal to node.overrides
3160 const result = node.updateOverridesEdgeInRemoved(commonOverrides)
3161 t.equal(result, true, 'updateOverridesEdgeInRemoved returns true when newOverrideSet differs')
3162 t.notOk(commonOverrides.isEqual(node.overrides), 'node.overrides is updated to a more specific override set')
3163 t.end()
3164 })
3165
3166 t.test('updateOverridesEdgeInAdded conflicts on conflicting override set', t => {
3167 const overrides8 = new OverrideSet({
3168 overrides: {
3169 bat: '1.2.0',
3170 },
3171 })
3172 const overrides9 = new OverrideSet({
3173 overrides: {
3174 '[email protected]': '1.2.0',
3175 },
3176 })
3177
3178 // Create a node with an existing override set
3179 const node = new Node({
3180 pkg: { name: 'conflict-node' },
3181 path: '/some/path/conflict-node',
3182 realpath: '/some/path/conflict-node',
3183 overrides: overrides8,
3184 })
3185
3186 // Call updateOverridesEdgeInAdded with a conflicting override set
3187 const result = node.updateOverridesEdgeInAdded(overrides9)
3188 t.equal(result, undefined, 'returns undefined on conflict')
3189
3190 t.end()
3191 })
3192
3193 t.test('updateOverridesEdgeInRemoved calls recalculateOutEdgesOverrides when new override set exists', t => {
3194 const originalOverrides = new OverrideSet({
3195 overrides: {
3196 foo: '1.0.0',
3197 },
3198 })
3199 const specificOverrides = new OverrideSet({
3200 overrides: {
3201 foo: '1.0.0',
3202 bar: '2.0.0',
3203 },
3204 })
3205
3206 // Create a node with original overrides and simulate an incoming edge
3207 // whose override is more specific, so that the computed newOverrideSet
3208 // differs from the original, triggering recalculateOutEdgesOverrides
3209 const node = new Node({
3210 pkg: { name: 'test-node' },
3211 path: '/some/path/test-node',
3212 realpath: '/some/path/test-node',
3213 overrides: originalOverrides,
3214 })
3215
3216 node.edgesIn.add({
3217 overrides: specificOverrides,
3218 })
3219
3220 // Spy on recalculateOutEdgesOverrides to verify it's called
3221 let recalcCalled = false
3222 node.recalculateOutEdgesOverrides = () => {
3223 recalcCalled = true
3224 }
3225
3226 const result = node.updateOverridesEdgeInRemoved(originalOverrides)
3227 t.equal(result, true, 'returns true when override set changes')
3228 t.ok(recalcCalled, 'recalculateOutEdgesOverrides was called')
3229 t.ok(specificOverrides.isEqual(node.overrides), 'node.overrides updated to the specific override set')
3230 t.end()
3231 })
3232
3233 t.test('should propagate the new override set to the target node', t => {
3234 const tree = new Node({
3235 loadOverrides: true,
3236 path: '/root',
3237 pkg: {
3238 name: 'root',
3239 version: '1.0.0',
3240 dependencies: {
3241 mockDep: '1.x',
3242 },
3243 overrides: {
3244 mockDep: '2.x',
3245 },
3246 },
3247 children: [{
3248 name: 'mockDep',
3249 version: '2.0.0',
3250 pkg: {
3251 dependencies: {
3252 subDep: '1.0.0',
3253 },
3254 },
3255 children: [{
3256 name: 'subDep',
3257 version: '1.0.0',
3258 pkg: {},
3259 }],
3260 }],
3261 })
3262
3263 // Force edge.override to a conflicting object so that it will differ from
3264 // the computed override coming from the parent's override set.
3265 const conflictingOverride = new OverrideSet({
3266 overrides: { mockDep: '1.x' },
3267 })
3268 const edge = tree.edgesOut.get('mockDep')
3269 edge.overrides = conflictingOverride
3270
3271 // Calls updateOverridesEdgeInRemoved and updateOverridesEdgeInAdded
3272 edge.reload()
3273
3274 // Validate that the override's value property has been updated
3275 t.equal(edge.overrides.value, '2.x', 'Edge override propagates the correct override value from the parent')
3276
3277 t.end()
3278 })
3279
3280 t.test('override conflict detection with semantic comparison', t => {
3281 t.test('non-conflicting different override sets should be valid', t => {
3282 // Regression test for issue #8688
3283 // This validates that the improved semantic conflict detection allows
3284 // structurally different override sets that don't actually conflict.
3285
3286 // Create two different override sets (simulating Vaadin's structure)
3287 // These override different packages, so they don't conflict
3288 const overridesComponents = new OverrideSet({
3289 overrides: {
3290 '@vaadin/react-components': '24.9.2',
3291 },
3292 })
3293
3294 const overridesComponentsPro = new OverrideSet({
3295 overrides: {
3296 '@vaadin/react-components-pro': '24.9.2',
3297 },
3298 })
3299
3300 const tree = new Node({
3301 loadOverrides: true,
3302 path: '/root',
3303 pkg: {
3304 name: 'root',
3305 version: '1.0.0',
3306 dependencies: {
3307 mockDep: '1.x',
3308 },
3309 overrides: {
3310 mockDep: '2.x',
3311 },
3312 },
3313 children: [{
3314 name: 'mockDep',
3315 version: '2.0.0',
3316 pkg: {
3317 dependencies: {
3318 subDep: '1.0.0',
3319 },
3320 },
3321 children: [{
3322 name: 'subDep',
3323 version: '1.0.0',
3324 pkg: {},
3325 }],
3326 }],
3327 })
3328
3329 const edge = tree.edgesOut.get('mockDep')
3330
3331 // Manually set an override to the edge
3332 edge.overrides = overridesComponents
3333
3334 // Override satisfiedBy so it returns true, ensuring the conflict branch is reached
3335 edge.satisfiedBy = () => true
3336
3337 // Set different but non-conflicting override on the target node
3338 const mockDep = tree.children.get('mockDep')
3339 mockDep.overrides = overridesComponentsPro
3340
3341 // Force edge to recalculate
3342 edge.reload(true)
3343
3344 // The edge should be valid despite different override contexts
3345 // because they don't have conflicting version requirements
3346 t.equal(edge.error, null, 'Edge should be valid with non-conflicting override contexts')
3347 t.ok(edge.valid, 'Edge.valid should be true')
3348
3349 t.end()
3350 })
3351
3352 t.test('conflicting override sets should be detected as invalid', t => {
3353 // This validates that actual conflicts ARE still caught by the semantic detection
3354
3355 // Create two override sets with conflicting version requirements for the same package
3356 const overridesV1 = new OverrideSet({
3357 overrides: {
3358 lodash: '1.x',
3359 },
3360 })
3361
3362 const overridesV4 = new OverrideSet({
3363 overrides: {
3364 lodash: '4.x',
3365 },
3366 })
3367
3368 const tree = new Node({
3369 loadOverrides: true,
3370 path: '/root',
3371 pkg: {
3372 name: 'root',
3373 version: '1.0.0',
3374 dependencies: {
3375 mockDep: '1.x',
3376 },
3377 overrides: {
3378 mockDep: '2.x',
3379 },
3380 },
3381 children: [{
3382 name: 'mockDep',
3383 version: '2.0.0',
3384 pkg: {
3385 dependencies: {
3386 lodash: '1.0.0',
3387 },
3388 },
3389 children: [{
3390 name: 'lodash',
3391 version: '1.0.0',
3392 pkg: {},
3393 }],
3394 }],
3395 })
3396
3397 const edge = tree.edgesOut.get('mockDep')
3398 const mockDep = tree.children.get('mockDep')
3399
3400 // Manually set conflicting overrides
3401 edge.overrides = overridesV1
3402 mockDep.overrides = overridesV4
3403
3404 // Override satisfiedBy so it returns true, ensuring the conflict branch is reached
3405 edge.satisfiedBy = () => true
3406
3407 // Clear the cached error by calling reload(true)
3408 edge.reload(true)
3409
3410 // Re-set the overrides after reload (since reload may have changed them)
3411 edge.overrides = overridesV1
3412 mockDep.overrides = overridesV4
3413
3414 // The edge should be INVALID due to conflicting override requirements
3415 t.equal(edge.error, 'INVALID', 'Edge should be invalid with conflicting override versions')
3416 t.notOk(edge.valid, 'Edge.valid should be false')
3417
3418 t.end()
3419 })
3420
3421 t.test('reference overrides should not cause false positives', t => {
3422 // This validates that reference overrides ($syntax) don't trigger false positives
3423
3424 const overridesRef1 = new OverrideSet({
3425 overrides: {
3426 lodash: '$some-reference',
3427 },
3428 })
3429
3430 const overridesRef2 = new OverrideSet({
3431 overrides: {
3432 lodash: '$another-reference',
3433 },
3434 })
3435
3436 const tree = new Node({
3437 loadOverrides: true,
3438 path: '/root',
3439 pkg: {
3440 name: 'root',
3441 version: '1.0.0',
3442 dependencies: {
3443 mockDep: '1.x',
3444 },
3445 overrides: {
3446 mockDep: '2.x',
3447 },
3448 },
3449 children: [{
3450 name: 'mockDep',
3451 version: '2.0.0',
3452 pkg: {
3453 dependencies: {
3454 lodash: '4.0.0',
3455 },
3456 },
3457 children: [{
3458 name: 'lodash',
3459 version: '4.0.0',
3460 pkg: {},
3461 }],
3462 }],
3463 })
3464
3465 const edge = tree.edgesOut.get('mockDep')
3466
3467 // Set reference overrides
3468 edge.overrides = overridesRef1
3469
3470 // Override satisfiedBy so it returns true, ensuring the conflict branch is reached
3471 edge.satisfiedBy = () => true
3472
3473 const mockDep = tree.children.get('mockDep')
3474 mockDep.overrides = overridesRef2
3475
3476 // Force edge to recalculate
3477 edge.reload(true)
3478
3479 // Reference overrides should not cause conflicts because we can't determine
3480 // their compatibility at this stage - they might resolve to the same version
3481 t.equal(edge.error, null, 'Edge should be valid with reference overrides')
3482 t.ok(edge.valid, 'Edge.valid should be true with reference overrides')
3483
3484 t.end()
3485 })
3486
3487 t.end()
3488 })
3489
3490 t.test('shouldOmit method', t => {
3491 t.test('dev dependency with dev omit', t => {
3492 const node = new Node({
3493 pkg: { name: 'test' },
3494 path: '/test',
3495 dummy: true,
3496 })
3497 node.dev = true
3498 t.equal(node.shouldOmit(new Set(['dev'])), true, 'should omit dev dependency when dev is omitted')
3499 t.equal(node.shouldOmit(new Set(['optional'])), false, 'should not omit dev dependency when only optional is omitted')
3500 t.end()
3501 })
3502
3503 t.test('optional dependency with optional omit', t => {
3504 const node = new Node({
3505 pkg: { name: 'test' },
3506 path: '/test',
3507 dummy: true,
3508 })
3509 node.optional = true
3510 t.equal(node.shouldOmit(new Set(['optional'])), true, 'should omit optional dependency when optional is omitted')
3511 t.equal(node.shouldOmit(new Set(['dev'])), false, 'should not omit optional dependency when only dev is omitted')
3512 t.end()
3513 })
3514
3515 t.test('peer dependency with peer omit', t => {
3516 const node = new Node({
3517 pkg: { name: 'test' },
3518 path: '/test',
3519 dummy: true,
3520 })
3521 node.peer = true
3522 t.equal(node.shouldOmit(new Set(['peer'])), true, 'should omit peer dependency when peer is omitted')
3523 t.equal(node.shouldOmit(new Set(['dev'])), false, 'should not omit peer dependency when only dev is omitted')
3524 t.end()
3525 })
3526
3527 t.test('devOptional dependency', t => {
3528 const node = new Node({
3529 pkg: { name: 'test' },
3530 path: '/test',
3531 dummy: true,
3532 })
3533 node.devOptional = true
3534 t.equal(node.shouldOmit(new Set(['dev', 'optional'])), true, 'should omit devOptional when both dev and optional are omitted')
3535 t.equal(node.shouldOmit(new Set(['dev'])), false, 'should not omit devOptional when only dev is omitted')
3536 t.equal(node.shouldOmit(new Set(['optional'])), false, 'should not omit devOptional when only optional is omitted')
3537 t.end()
3538 })
3539
3540 t.test('regular dependency', t => {
3541 const node = new Node({
3542 pkg: { name: 'test' },
3543 path: '/test',
3544 dummy: true,
3545 })
3546 t.equal(node.shouldOmit(new Set(['dev', 'optional', 'peer'])), false, 'should never omit regular dependencies')
3547 t.end()
3548 })
3549
3550 t.test('empty omit set', t => {
3551 const node = new Node({
3552 pkg: { name: 'test' },
3553 path: '/test',
3554 dummy: true,
3555 })
3556 node.dev = true
3557 t.equal(node.shouldOmit(new Set()), false, 'should not omit anything when omit set is empty')
3558 t.end()
3559 })
3560
3561 t.end()
3562 })