| 1 |
const t = require('tap') |
| 2 |
|
| 3 |
// turn on debug mode, or this does nothing |
| 4 |
const treeCheck = t.mock('../lib/tree-check.js', { |
| 5 |
'../lib/debug.js': fn => fn(), |
| 6 |
}) |
| 7 |
|
| 8 |
const { resolve } = require('node:path') |
| 9 |
|
| 10 |
const Node = require('../lib/node.js') |
| 11 |
const Link = require('../lib/link.js') |
| 12 |
const Edge = require('../lib/edge.js') |
| 13 |
|
| 14 |
t.test('basic tree that is a-ok', t => { |
| 15 |
const tree = new Node({ |
| 16 |
path: '/some/path', |
| 17 |
pkg: {}, |
| 18 |
children: [ |
| 19 |
{ pkg: { name: 'foo', version: '1.2.3' } }, |
| 20 |
], |
| 21 |
}) |
| 22 |
const link = new Link({ |
| 23 |
parent: tree, |
| 24 |
name: 'glorb', |
| 25 |
target: new Node({ |
| 26 |
fsParent: tree, |
| 27 |
path: '/some/path/glorb', |
| 28 |
pkg: { name: 'glorb', version: '1.2.3' }, |
| 29 |
}), |
| 30 |
}) |
| 31 |
link.parent = tree |
| 32 |
t.equal(treeCheck(tree), tree) |
| 33 |
t.equal(treeCheck(link), link) |
| 34 |
t.equal(treeCheck(link.target), link.target) |
| 35 |
const nonTree = {} |
| 36 |
t.equal(treeCheck(nonTree), nonTree) |
| 37 |
nonTree.root = {} |
| 38 |
t.equal(treeCheck(nonTree), nonTree) |
| 39 |
t.end() |
| 40 |
}) |
| 41 |
|
| 42 |
t.test('break some stuff', t => { |
| 43 |
const tree = new Node({ |
| 44 |
path: '/some/path', |
| 45 |
pkg: {}, |
| 46 |
children: [ |
| 47 |
{ pkg: { name: 'foo', version: '1.2.3' } }, |
| 48 |
{ pkg: { name: 'disowned', version: '1.2.3' } }, |
| 49 |
], |
| 50 |
}) |
| 51 |
new Link({ |
| 52 |
parent: tree, |
| 53 |
name: 'glorb', |
| 54 |
target: new Node({ |
| 55 |
fsParent: tree, |
| 56 |
path: '/some/path/glorb', |
| 57 |
pkg: { name: 'glorb', version: '1.2.3' }, |
| 58 |
}), |
| 59 |
}) |
| 60 |
const disowned = tree.children.get('disowned') |
| 61 |
disowned.parent = null |
| 62 |
|
| 63 |
// oops... |
| 64 |
Map.prototype.set.call(tree.inventory, 'xyz', disowned) |
| 65 |
t.equal(treeCheck(tree, false), tree, 'unreachable allowed') |
| 66 |
t.throws(() => treeCheck(tree), { |
| 67 |
message: 'unreachable in inventory', |
| 68 |
node: resolve('/some/path/node_modules/disowned'), |
| 69 |
realpath: resolve('/some/path/node_modules/disowned'), |
| 70 |
location: '', |
| 71 |
name: 'Error', |
| 72 |
log: Array, |
| 73 |
}) |
| 74 |
|
| 75 |
Map.prototype.delete.call(tree.inventory, 'xyz') |
| 76 |
disowned.parent = tree |
| 77 |
tree.inventory.delete(disowned) |
| 78 |
t.throws(() => treeCheck(tree), { |
| 79 |
message: 'not in inventory', |
| 80 |
node: resolve('/some/path/node_modules/disowned'), |
| 81 |
name: 'Error', |
| 82 |
log: Array, |
| 83 |
}) |
| 84 |
|
| 85 |
disowned.root = null |
| 86 |
tree.children.set('wtf', disowned) |
| 87 |
t.throws(() => treeCheck(tree), { |
| 88 |
message: 'double root', |
| 89 |
node: resolve('/some/path/node_modules/disowned'), |
| 90 |
realpath: resolve('/some/path/node_modules/disowned'), |
| 91 |
tree: tree.path, |
| 92 |
name: 'Error', |
| 93 |
log: Array, |
| 94 |
}) |
| 95 |
|
| 96 |
const otherTree = new Node({ |
| 97 |
name: 'other', |
| 98 |
parent: disowned, |
| 99 |
}) |
| 100 |
tree.children.set('wtf', otherTree) |
| 101 |
t.throws(() => treeCheck(tree), { |
| 102 |
message: 'node from other root in tree', |
| 103 |
node: resolve('/some/path/node_modules/disowned/node_modules/other'), |
| 104 |
realpath: resolve('/some/path/node_modules/disowned/node_modules/other'), |
| 105 |
tree: tree.path, |
| 106 |
name: 'Error', |
| 107 |
log: Array, |
| 108 |
}) |
| 109 |
|
| 110 |
tree.children.delete('wtf') |
| 111 |
Map.prototype.set.call(otherTree.inventory, 'othertree', disowned) |
| 112 |
t.throws(() => treeCheck(otherTree), { |
| 113 |
message: 'non-root has non-zero inventory', |
| 114 |
node: disowned.path, |
| 115 |
tree: otherTree.path, |
| 116 |
inventory: [[disowned.path, disowned.location]], |
| 117 |
name: 'Error', |
| 118 |
log: Array, |
| 119 |
}) |
| 120 |
Map.prototype.delete.call(tree.inventory, 'othertree') |
| 121 |
|
| 122 |
t.end() |
| 123 |
}) |
| 124 |
|
| 125 |
t.test('just return the tree if not in debug mode', t => { |
| 126 |
const treeCheck = t.mock('../lib/tree-check.js', { |
| 127 |
'../lib/debug.js': () => {}, |
| 128 |
}) |
| 129 |
const tree = new Node({ |
| 130 |
path: '/some/path', |
| 131 |
pkg: {}, |
| 132 |
children: [ |
| 133 |
{ pkg: { name: 'foo', version: '1.2.3' } }, |
| 134 |
{ pkg: { name: 'disowned', version: '1.2.3' } }, |
| 135 |
], |
| 136 |
}) |
| 137 |
const disowned = tree.children.get('disowned') |
| 138 |
disowned.parent = null |
| 139 |
// oops... |
| 140 |
Map.prototype.set.call(tree.inventory, 'xyz', disowned) |
| 141 |
t.equal(treeCheck(tree), tree, 'error suppressed outside of debug mode') |
| 142 |
t.end() |
| 143 |
}) |
| 144 |
|
| 145 |
t.test('tree with dev edges on a nested dep node', t => { |
| 146 |
const tree = new Node({ |
| 147 |
path: '/some/path', |
| 148 |
pkg: {}, |
| 149 |
children: [ |
| 150 |
{ pkg: { name: 'foo', version: '1.2.3', devDependencies: { x: '' } } }, |
| 151 |
], |
| 152 |
}) |
| 153 |
// ensure it actually gets added |
| 154 |
new Edge({ |
| 155 |
type: 'dev', |
| 156 |
name: 'x', |
| 157 |
spec: '', |
| 158 |
from: tree.children.get('foo'), |
| 159 |
}) |
| 160 |
t.throws(() => treeCheck(tree), { |
| 161 |
message: 'dev edges on non-top node', |
| 162 |
node: tree.children.get('foo').path, |
| 163 |
tree: tree.path, |
| 164 |
root: tree.root.path, |
| 165 |
via: tree.path, |
| 166 |
viaType: 'children', |
| 167 |
devEdges: [['dev', 'x', '', 'MISSING']], |
| 168 |
log: Array, |
| 169 |
}) |
| 170 |
t.end() |
| 171 |
}) |
| 172 |
|
| 173 |
t.test('node with same path as root', t => { |
| 174 |
const root = new Node({ |
| 175 |
path: '/path/to/root', |
| 176 |
pkg: { |
| 177 |
dependencies: { |
| 178 |
foo: '1', |
| 179 |
}, |
| 180 |
}, |
| 181 |
}) |
| 182 |
const tree = new Node({ |
| 183 |
parent: root, |
| 184 |
pkg: { |
| 185 |
name: 'foo', |
| 186 |
version: '1.2.3', |
| 187 |
}, |
| 188 |
}) |
| 189 |
const child = new Node({ |
| 190 |
parent: tree, |
| 191 |
pkg: { |
| 192 |
name: 'not-root', |
| 193 |
version: '1.2.3', |
| 194 |
}, |
| 195 |
}) |
| 196 |
child.path = root.path |
| 197 |
t.throws(() => treeCheck(tree), { |
| 198 |
message: 'node with same path as root', |
| 199 |
node: child.path, |
| 200 |
tree: tree.path, |
| 201 |
root: root.path, |
| 202 |
via: tree.path, |
| 203 |
viaType: 'children', |
| 204 |
log: Array, |
| 205 |
}) |
| 206 |
child.path = root.path + '/some/where/else' |
| 207 |
t.throws(() => treeCheck(tree), { |
| 208 |
message: 'non-link with mismatched path/realpath', |
| 209 |
node: child.path, |
| 210 |
tree: tree.path, |
| 211 |
root: root.path, |
| 212 |
via: tree.path, |
| 213 |
viaType: 'children', |
| 214 |
log: Array, |
| 215 |
}) |
| 216 |
t.end() |
| 217 |
}) |