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

gather-dep-set.js « test « arborist « workspaces - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9fdb87628f9ea7909bf9797f0d593df4ffe2b511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const t = require('tap')
const localeCompare = require('@isaacs/string-locale-compare')('en')
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) => localeCompare(a.name, b.name))
  .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')