Debian Codegraph @

workspaces/arborist/test/deepest-nesting-target.js

raw
1 const t = require('tap')
2 const Node = require('../lib/node.js')
3 const Link = require('../lib/link.js')
4 const deepestNestingTarget = require('../lib/deepest-nesting-target.js')
5
6 // root -> (a, b, file:target) PEER(p)
7 // a -> (x) PEER(p)
8 // b -> PEER(p)
9 // target -> (a, b, file:/outer) PEER(p)
10 // /outer -> (a, b) PEER(p)
11 //
12 // p can be placed under the root (because project root), or any of the
13 // 'x' nodes (because no peer dep), or in the /outer node (no resolve parent)
14 // Any other place is not allowed, because of peer dependencies.
15
16 const tree = new Node({
17 path: '/some/path',
18 pkg: {
19 name: 'root',
20 dependencies: {
21 a: '',
22 b: '',
23 link: 'file:target',
24 } },
25 fsChildren: [
26 {
27 path: '/some/path/target',
28 pkg: {
29 name: 'link',
30 version: '1.2.3',
31 peerDependencies: { p: '' },
32 dependencies: {
33 a: '',
34 b: '',
35 outerlink: 'file:/other/path',
36 },
37 },
38 children: [
39 {
40 pkg: {
41 name: 'a',
42 version: '1.2.3',
43 dependencies: { x: '' },
44 peerDependencies: { p: '' },
45 },
46 children: [{ pkg: { name: 'x', version: '1.2.3' } }],
47 },
48 { pkg: { name: 'b', version: '1.2.3', peerDependencies: { p: '' } } },
49 ],
50 },
51 ],
52 children: [
53 {
54 pkg: {
55 name: 'a',
56 version: '1.2.3',
57 dependencies: { x: '' },
58 peerDependencies: { p: '' },
59 },
60 children: [{ pkg: { name: 'x', version: '1.2.3' } }],
61 },
62 { pkg: { name: 'b', version: '1.2.3', peerDependencies: { p: '' } } },
63 ],
64 })
65
66 const a = tree.children.get('a')
67 const x = a.children.get('x')
68 const b = tree.children.get('b')
69
70 const inner = tree.inventory.get('target')
71 const innerLink = new Link({
72 parent: tree,
73 name: 'link',
74 target: inner,
75 })
76 t.equal(inner, innerLink.target, 'gut check')
77 t.equal(innerLink, tree.children.get('link'), 'gut check')
78 const innerA = inner.children.get('a')
79 const innerX = innerA.children.get('x')
80 const innerB = inner.children.get('b')
81
82 // the target of the outer link dep. it can hold p, but b cannot
83 const outer = new Node({
84 path: '/other/path',
85 pkg: {
86 name: 'other',
87 version: '1.2.3',
88 peerDependencies: { p: '' },
89 dependencies: { a: '', b: '' },
90 },
91 children: [
92 {
93 pkg: {
94 name: 'a',
95 version: '1.2.3',
96 dependencies: { x: '' },
97 peerDependencies: { p: '' },
98 },
99 children: [{ pkg: { name: 'x', version: '1.2.3' } }],
100 },
101 { pkg: { name: 'b', version: '1.2.3', peerDependencies: { p: '' } } },
102 ],
103 })
104 const outerA = outer.children.get('a')
105 const outerX = outerA.children.get('x')
106 const outerB = outer.children.get('b')
107
108 const outerLink = new Link({
109 parent: inner,
110 name: 'outerlink',
111 target: outer,
112 })
113 t.equal(outer, tree.inventory.get('../../other/path'), 'gut check')
114 t.equal(outer, outerLink.target, 'gut check')
115 t.equal(outerLink, inner.children.get('outerlink'), 'gut check')
116
117 t.equal(deepestNestingTarget(tree, 'p'), tree)
118 t.equal(deepestNestingTarget(a, 'p'), tree)
119 t.equal(deepestNestingTarget(b, 'p'), tree)
120 t.equal(deepestNestingTarget(x, 'p'), x)
121 t.equal(deepestNestingTarget(inner, 'p'), tree)
122 t.equal(deepestNestingTarget(innerA, 'p'), tree)
123 t.equal(deepestNestingTarget(innerB, 'p'), tree)
124 t.equal(deepestNestingTarget(innerX, 'p'), innerX)
125 t.equal(deepestNestingTarget(outer, 'p'), outer)
126 t.equal(deepestNestingTarget(outerA, 'p'), outer)
127 t.equal(deepestNestingTarget(outerB, 'p'), outer)
128 t.equal(deepestNestingTarget(outerX, 'p'), outerX)