| 1 |
const t = require('tap') |
| 2 |
const localeCompare = require('@isaacs/string-locale-compare')('en') |
| 3 |
const gatherDepSet = require('../lib/gather-dep-set.js') |
| 4 |
|
| 5 |
const Node = require('../lib/node.js') |
| 6 |
|
| 7 |
/* |
| 8 |
tree (a, b, c) |
| 9 |
+-- a (x, y) |
| 10 |
+-- x (b) |
| 11 |
+-- y (i) |
| 12 |
+-- b (i, j) |
| 13 |
+-- i (j, k) |
| 14 |
+-- j (k) |
| 15 |
+-- k () |
| 16 |
+-- c (r, s) |
| 17 |
+-- r (s, t) |
| 18 |
+-- s (t) |
| 19 |
+-- t (missing) <-- unmet dependency |
| 20 |
|
| 21 |
logical tree: |
| 22 |
tree |
| 23 |
+-- a |
| 24 |
| +-- x |
| 25 |
| | +-- b (deduped) |
| 26 |
| +-- y |
| 27 |
| +-- i (deduped) |
| 28 |
+-- b |
| 29 |
| +-- i |
| 30 |
| | +-- j (deduped) |
| 31 |
| | +-- k |
| 32 |
| +-- j |
| 33 |
| +-- k (deduped) |
| 34 |
+-- c |
| 35 |
+-- r |
| 36 |
| +-- s (deduped) |
| 37 |
| +-- t (deduped) |
| 38 |
+-- s |
| 39 |
+-- t |
| 40 |
+-- missing (error, missing) |
| 41 |
|
| 42 |
Gather dep set, ignoring edges coming from or going to root. |
| 43 |
|
| 44 |
Gathering from a will include b. Gathering from c will not. Gathering from b |
| 45 |
alone will be an empty set. |
| 46 |
*/ |
| 47 |
|
| 48 |
const tree = new Node({ |
| 49 |
path: '/path/to/tree', |
| 50 |
pkg: { |
| 51 |
dependencies: { |
| 52 |
a: '', |
| 53 |
b: '', |
| 54 |
c: '', |
| 55 |
}, |
| 56 |
}, |
| 57 |
children: [ |
| 58 |
// [name, [deps]] |
| 59 |
['a', ['x', 'y']], |
| 60 |
['x', ['b']], |
| 61 |
['y', ['i']], |
| 62 |
['b', ['i', 'j']], |
| 63 |
['i', ['j', 'k']], |
| 64 |
['j', ['k']], |
| 65 |
['k', []], |
| 66 |
['c', ['r', 's']], |
| 67 |
['r', ['s', 't']], |
| 68 |
['s', ['t']], |
| 69 |
['t', ['missing']], |
| 70 |
].map(([name, deps]) => ({ |
| 71 |
pkg: { |
| 72 |
name, |
| 73 |
version: '1.0.0', |
| 74 |
dependencies: deps.reduce((d, n) => { |
| 75 |
d[n] = '' |
| 76 |
return d |
| 77 |
}, {}), |
| 78 |
}, |
| 79 |
})), |
| 80 |
}) |
| 81 |
|
| 82 |
const normalizePath = path => path.replace(/[A-Z]:/, '').replace(/\\/g, '/') |
| 83 |
|
| 84 |
const printSet = set => [...set] |
| 85 |
.sort((a, b) => localeCompare(a.name, b.name)) |
| 86 |
.map(n => n.location) |
| 87 |
|
| 88 |
const cwd = normalizePath(process.cwd()) |
| 89 |
t.cleanSnapshot = s => s.split(cwd).join('{CWD}') |
| 90 |
|
| 91 |
t.formatSnapshot = obj => obj instanceof Set ? printSet(obj) : obj |
| 92 |
|
| 93 |
const nodeA = tree.children.get('a') |
| 94 |
const nodeB = tree.children.get('b') |
| 95 |
const nodeC = tree.children.get('c') |
| 96 |
const nodeX = tree.children.get('x') |
| 97 |
|
| 98 |
const f = edge => edge.from !== tree && edge.to !== tree |
| 99 |
|
| 100 |
const setA = gatherDepSet(new Set([nodeA]), f) |
| 101 |
t.matchSnapshot(setA, 'set with a') |
| 102 |
t.equal(setA.has(nodeB), true, 'set(a) includes node b') |
| 103 |
t.equal(setA.has(nodeC), false, 'set(a) does not include node c') |
| 104 |
t.equal(setA.has(nodeX), true, 'set(a) includes node x') |
| 105 |
|
| 106 |
const setAX = gatherDepSet(new Set([nodeA, nodeX]), f) |
| 107 |
t.matchSnapshot(setAX, 'set with a and x') |
| 108 |
t.equal(setAX.has(nodeB), true, 'set(ax) includes node b') |
| 109 |
t.equal(setAX.has(nodeC), false, 'set(ax) does not include node c') |
| 110 |
|
| 111 |
const setB = gatherDepSet(new Set([nodeB]), f) |
| 112 |
t.matchSnapshot(setB, 'set with b') |
| 113 |
t.equal(setB.size, 0, 'gathering dep set from b is an empty set') |
| 114 |
|
| 115 |
const setC = gatherDepSet(new Set([nodeC]), f) |
| 116 |
t.matchSnapshot(setC, 'set with c only') |
| 117 |
t.equal(setC.has(nodeC), true, 'set(c) includes node c') |
| 118 |
t.equal(setC.has(nodeA), false, 'set(c) does not includes node a') |
| 119 |
t.equal(setC.has(nodeB), false, 'set(c) does not includes node b') |
| 120 |
t.equal(setC.has(nodeX), false, 'set(c) does not includes node b') |