Debian Codegraph @

workspaces/arborist/test/inventory.js

raw
1 // this test depends on debug stuff, so force it on, even if the test env
2 // does not enable it.
3 process.env.ARBORIST_DEBUG = '1'
4 const Inventory = require('../lib/inventory.js')
5 const localeCompare = require('@isaacs/string-locale-compare')('en')
6
7 const t = require('tap')
8
9 t.test('basic operations', t => {
10 const i = new Inventory()
11 t.equal(i.primaryKey, 'location')
12 t.same(i.indexes, ['name', 'license', 'funding', 'realpath', 'packageName'])
13
14 // uk spelling
15 i.add({ location: 'x', name: 'x', package: { licence: 'MIT', funding: 'foo' } })
16 // old-style multi-license array
17 i.add({ location: 'y', name: 'x', package: { licenses: [{ type: 'ISC' }], funding: { url: 'foo' } } })
18 i.add({ location: 'z', name: 'z', package: { license: { type: 'MIT' }, funding: 'bar' } })
19 i.add({ location: 'a', name: 'a', package: {} })
20
21 t.same(i.filter(node => /[xy]/.test(node.name)), [
22 i.get('x'),
23 i.get('y'),
24 ], 'filter returns an iterable of all matching nodes')
25
26 t.same([...i.query('license')].sort((a, b) => localeCompare(String(a), String(b))),
27 ['ISC', 'MIT', undefined])
28 t.same([...i.query('license', 'MIT')], [
29 { location: 'x', name: 'x', package: { licence: 'MIT', funding: 'foo' } },
30 { location: 'z', name: 'z', package: { license: { type: 'MIT' }, funding: 'bar' } },
31 ], 'can query by license')
32 t.same(i.query('license', 'blerg'), new Set(),
33 'empty query returns empty set')
34 t.same([...i.query('name', 'x')], [
35 { location: 'x', name: 'x', package: { licence: 'MIT', funding: 'foo' } },
36 { location: 'y', name: 'x', package: { licenses: [{ type: 'ISC' }], funding: { url: 'foo' } } },
37 ], 'can query by name')
38 t.same([...i.query('funding')].sort((a, b) => localeCompare(String(a), String(b))),
39 ['bar', 'foo', undefined])
40 t.same([...i.query('funding', 'foo')], [
41 { location: 'x', name: 'x', package: { licence: 'MIT', funding: 'foo' } },
42 { location: 'y', name: 'x', package: { licenses: [{ type: 'ISC' }], funding: { url: 'foo' } } },
43 ], 'can query by funding url')
44
45 const x = i.get('x')
46 t.same(x, { location: 'x', name: 'x', package: { licence: 'MIT', funding: 'foo' } }, 'get by location')
47 i.add(x)
48 t.same(i.get('x'), x, 'adding a second time has no effect')
49 t.equal(i.has(x), true, 'has a node')
50 i.add({ location: 'x', name: 'a', package: { licences: [{ type: 'ABC' }] } })
51 t.same(i.get('x'), { location: 'x', name: 'a', package: { licences: [{ type: 'ABC' }] } },
52 'new node at same location overwrites')
53 t.equal(i.has(x), false, 'node has been overwritten')
54
55 const a = i.get('a')
56 t.same([...i.query('license', undefined)], [a], 'can query by missing license')
57
58 t.throws(() => i.set('a', 'b'), {
59 message: 'direct set() not supported, use inventory.add(node)',
60 })
61 const y = i.get('y')
62 i.delete({ location: 'y' })
63 t.equal(i.get('y'), y, 'deleting other node with same key has no effect')
64 i.delete(y)
65 t.equal(i.has(y), false, 'no longer has the y node')
66 t.equal(i.get('y'), undefined, 'get returns undefined')
67
68 // no name or license!
69 const z = { location: 'z' }
70 i.add(z)
71 t.equal(i.get('z'), z, 'can add and retrieve node without secondary keys')
72 // oops! mutated out of band, that's weird, but shouldn't throw anyway
73 // though, of course, this makes the secondary indexes incomplete.
74 // don't do this. but probably not worth making the package immutable.
75 z.package = { name: 'z' }
76 t.doesNotThrow(() => i.delete(z), 'doesnt try to delete from nonexistent sets')
77 t.equal(i.get('z'), undefined, 'node is not here')
78 t.equal(i.has(z), false, 'i does not have z any more')
79
80 t.doesNotThrow(() =>
81 i.add({
82 location: 'f',
83 name: 'f',
84 package: {
85 license: 'MIT',
86 funding: null,
87 },
88 }), 'doesnt throw on falsy funding info')
89
90 t.doesNotThrow(() =>
91 i.add({
92 location: 'l',
93 name: 'l',
94 package: {
95 license: null,
96 },
97 }), 'doesnt throw on falsy license info')
98
99 // inherited properties and getters are allowed
100 const n = Object.assign(Object.create({
101 location: 'n',
102 get packageName () {
103 return this.package.name
104 },
105 get package () {
106 return this._pkg
107 },
108 }), { _pkg: { name: 'n' } })
109 i.add(n)
110 t.equal(i.get('n'), n, 'found by inherited location')
111 t.same([...i.query('packageName', 'n')], [n], 'found by packageName query')
112
113 t.end()
114 })
115
116 t.test('do not allow external nodes to be added to inventory', t => {
117 const i = new Inventory()
118 const root = { location: '', path: 'rootpath' }
119 i.add(root)
120 t.throws(() => i.add({ root: { path: 'otherroot' }, location: 'adsf', path: 'nodepath' }), {
121 message: 'adding external node to inventory',
122 root: 'rootpath',
123 node: 'nodepath',
124 nodeRoot: 'otherroot',
125 })
126 t.end()
127 })
128
129 t.test('adding external nodes is no-op outside debug mode', t => {
130 const Inventory = t.mock('../lib/inventory.js', {
131 '../lib/debug.js': () => {},
132 })
133 const i = new Inventory()
134 const root = { location: '', path: 'rootpath' }
135 i.add(root)
136 const other = { root: { path: 'otherroot' }, location: 'adsf', path: 'nodepath' }
137 i.add(other)
138 t.equal(i.has(other), false, 'did not add external node to inventory')
139 t.end()
140 })