Debian Codegraph @

workspaces/arborist/test/optional-set.js

raw
1 const t = require('tap')
2 const optionalSet = require('../lib/optional-set.js')
3 const calcDepFlags = require('../lib/calc-dep-flags.js')
4
5 const Node = require('../lib/node.js')
6
7 /*
8 tree (PROD a, PROD c, OPT i)
9 +-- a (OPT o)
10 +-- b (PROD c)
11 +-- c (OPT b)
12 +-- o (PROD m, OPT i)
13 +-- m (OPT n)
14 +-- n ()
15 +-- OPT i (PROD j)
16 +-- j ()
17
18 Gathering the optional set from:
19 j: [j, i],
20 a: [],
21 o: [o, m, n],
22 m: [o, m, n],
23 b: [b],
24 */
25
26 const tree = new Node({
27 path: '/path/to/tree',
28 pkg: {
29 dependencies: {
30 a: '',
31 c: '',
32 },
33 optionalDependencies: {
34 i: '',
35 },
36 },
37 children: [
38 // [name, deps, optDeps]
39 ['a', [], ['o']],
40 ['b', ['c'], []],
41 ['c', [], ['b']],
42 ['o', ['m'], ['i']],
43 ['m', [], ['n']],
44 ['n', [], []],
45 ['i', ['j'], []],
46 ['j', [], []],
47 ].map(([name, deps, optDeps]) => ({
48 pkg: {
49 name,
50 version: '1.0.0',
51 dependencies: deps.reduce((d, n) => {
52 d[n] = ''
53 return d
54 }, {}),
55 optionalDependencies: optDeps.reduce((d, n) => {
56 d[n] = ''
57 return d
58 }, {}),
59 },
60 })),
61 })
62
63 calcDepFlags(tree)
64
65 const nodeJ = tree.children.get('j')
66 const nodeI = tree.children.get('i')
67 const nodeO = tree.children.get('o')
68 const nodeM = tree.children.get('m')
69 const nodeN = tree.children.get('n')
70 const nodeB = tree.children.get('b')
71
72 const setJ = optionalSet(nodeJ)
73 t.equal(setJ.has(nodeJ), true, 'gathering from j includes j')
74 t.equal(setJ.has(nodeI), true, 'gathering from j includes i')
75 t.equal(setJ.size, 2, 'two nodes in j set')
76
77 const setO = optionalSet(nodeO)
78 t.equal(setO.size, 3, 'three nodes in o set')
79 t.equal(setO.has(nodeO), true, 'set o includes o')
80 t.equal(setO.has(nodeM), true, 'set o includes m')
81 t.equal(setO.has(nodeN), true, 'set o includes n')
82
83 const setM = optionalSet(nodeM)
84 t.equal(setM.size, 3, 'three nodes in m set')
85 t.equal(setM.has(nodeO), true, 'set m includes o')
86 t.equal(setM.has(nodeM), true, 'set m includes m')
87 t.equal(setM.has(nodeN), true, 'set m includes n')
88
89 const setB = optionalSet(nodeB)
90 t.equal(setB.size, 1, 'gathering from b is only b')
91 t.equal(setB.has(nodeB), true, 'set b includes b')
92
93 // tree (OPT opt-p, OPT opt-q)
94 // +-- OPT opt-p (PROD shared-dep)
95 // +-- OPT opt-q (PROD shared-dep)
96 // +-- shared-dep ()
97 const sharedTree = new Node({
98 path: '/path/to/shared-tree',
99 pkg: {
100 optionalDependencies: {
101 'opt-p': '',
102 'opt-q': '',
103 },
104 },
105 children: [
106 { pkg: { name: 'opt-p', version: '1.0.0', dependencies: { 'shared-dep': '' } } },
107 { pkg: { name: 'opt-q', version: '1.0.0', dependencies: { 'shared-dep': '' } } },
108 { pkg: { name: 'shared-dep', version: '1.0.0' } },
109 ],
110 })
111
112 calcDepFlags(sharedTree)
113
114 const nodeOptP = sharedTree.children.get('opt-p')
115 const nodeOptQ = sharedTree.children.get('opt-q')
116 const nodeSharedDep = sharedTree.children.get('shared-dep')
117
118 // Simulate opt-p failing platform check and being marked inert first
119 const setOptP = optionalSet(nodeOptP)
120 // shared-dep is excluded because opt-q (not yet inert) also depends on it
121 t.equal(setOptP.has(nodeOptP), true, 'set opt-p includes opt-p')
122 t.equal(setOptP.has(nodeSharedDep), false, 'set opt-p excludes shared-dep (opt-q is not inert)')
123 for (const n of setOptP) {
124 n.inert = true
125 }
126
127 // Simulate opt-q failing platform check second (opt-p is already inert)
128 const setOptQ = optionalSet(nodeOptQ)
129 // shared-dep now has no active external dependents and is included
130 t.equal(setOptQ.has(nodeOptQ), true, 'set opt-q includes opt-q')
131 t.equal(setOptQ.has(nodeSharedDep), true, 'set opt-q includes shared-dep (opt-p is inert)')
132 t.equal(setOptQ.size, 2, 'set opt-q has two nodes: opt-q and shared-dep')