Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Fritz <fritzy@github.com>2021-12-16 21:01:56 +0300
committerNathan Fritz <fritzy@github.com>2021-12-16 21:05:19 +0300
commitd7265045730555c03b3142c004c7438e9577028c (patch)
tree035d81b3124bdaa09c21854934bf2b2b50e52e44 /workspaces/arborist/test/gather-dep-set.js
parentd8aac8448e983692cacb427e03f4688cd1b62e30 (diff)
Bring in all libnpm modules + arborist as workspaces (#4166)
Added libnpm workspaces and arborist
Diffstat (limited to 'workspaces/arborist/test/gather-dep-set.js')
-rw-r--r--workspaces/arborist/test/gather-dep-set.js119
1 files changed, 119 insertions, 0 deletions
diff --git a/workspaces/arborist/test/gather-dep-set.js b/workspaces/arborist/test/gather-dep-set.js
new file mode 100644
index 000000000..877e41900
--- /dev/null
+++ b/workspaces/arborist/test/gather-dep-set.js
@@ -0,0 +1,119 @@
+const t = require('tap')
+const gatherDepSet = require('../lib/gather-dep-set.js')
+
+const Node = require('../lib/node.js')
+
+/*
+tree (a, b, c)
++-- a (x, y)
++-- x (b)
++-- y (i)
++-- b (i, j)
++-- i (j, k)
++-- j (k)
++-- k ()
++-- c (r, s)
++-- r (s, t)
++-- s (t)
++-- t (missing) <-- unmet dependency
+
+logical tree:
+tree
++-- a
+| +-- x
+| | +-- b (deduped)
+| +-- y
+| +-- i (deduped)
++-- b
+| +-- i
+| | +-- j (deduped)
+| | +-- k
+| +-- j
+| +-- k (deduped)
++-- c
+ +-- r
+ | +-- s (deduped)
+ | +-- t (deduped)
+ +-- s
+ +-- t
+ +-- missing (error, missing)
+
+Gather dep set, ignoring edges coming from or going to root.
+
+Gathering from a will include b. Gathering from c will not. Gathering from b
+alone will be an empty set.
+*/
+
+const tree = new Node({
+ path: '/path/to/tree',
+ pkg: {
+ dependencies: {
+ a: '',
+ b: '',
+ c: '',
+ },
+ },
+ children: [
+ // [name, [deps]]
+ ['a', ['x', 'y']],
+ ['x', ['b']],
+ ['y', ['i']],
+ ['b', ['i', 'j']],
+ ['i', ['j', 'k']],
+ ['j', ['k']],
+ ['k', []],
+ ['c', ['r', 's']],
+ ['r', ['s', 't']],
+ ['s', ['t']],
+ ['t', ['missing']],
+ ].map(([name, deps]) => ({
+ pkg: {
+ name,
+ version: '1.0.0',
+ dependencies: deps.reduce((d, n) => {
+ d[n] = ''
+ return d
+ }, {}),
+ },
+ })),
+})
+
+const normalizePath = path => path.replace(/[A-Z]:/, '').replace(/\\/g, '/')
+
+const printSet = set => [...set]
+ .sort((a, b) => a.name.localeCompare(b.name, 'en'))
+ .map(n => n.location)
+
+const cwd = normalizePath(process.cwd())
+t.cleanSnapshot = s => s.split(cwd).join('{CWD}')
+
+t.formatSnapshot = obj => obj instanceof Set ? printSet(obj) : obj
+
+const nodeA = tree.children.get('a')
+const nodeB = tree.children.get('b')
+const nodeC = tree.children.get('c')
+const nodeX = tree.children.get('x')
+
+const f = edge => edge.from !== tree && edge.to !== tree
+
+const setA = gatherDepSet(new Set([nodeA]), f)
+t.matchSnapshot(setA, 'set with a')
+t.equal(setA.has(nodeB), true, 'set(a) includes node b')
+t.equal(setA.has(nodeC), false, 'set(a) does not include node c')
+t.equal(setA.has(nodeX), true, 'set(a) includes node x')
+
+const setAX = gatherDepSet(new Set([nodeA, nodeX]), f)
+t.matchSnapshot(setAX, 'set with a and x')
+t.equal(setAX.has(nodeB), true, 'set(ax) includes node b')
+t.equal(setAX.has(nodeC), false, 'set(ax) does not include node c')
+
+const setB = gatherDepSet(new Set([nodeB]), f)
+t.matchSnapshot(setB, 'set with b')
+t.equal(setB.size, 0, 'gathering dep set from b is an empty set')
+
+const setC = gatherDepSet(new Set([nodeC]), f)
+t.matchSnapshot(setC, 'set with c only')
+t.equal(setC.has(nodeC), true, 'set(c) includes node c')
+t.equal(setC.has(nodeA), false, 'set(c) does not includes node a')
+t.equal(setC.has(nodeB), false, 'set(c) does not includes node b')
+t.equal(setC.has(nodeX), false, 'set(c) does not includes node b')