| 1 |
const t = require('tap') |
| 2 |
const Node = require('../lib/node.js') |
| 3 |
const Link = require('../lib/link.js') |
| 4 |
const Edge = require('../lib/edge.js') |
| 5 |
const printable = require('../lib/printable.js') |
| 6 |
const util = require('node:util') |
| 7 |
|
| 8 |
t.cleanSnapshot = str => str |
| 9 |
// normalize paths |
| 10 |
.split(process.cwd()).join('{CWD}') |
| 11 |
.replace(/[A-Z]:/g, '') |
| 12 |
.replace(/\\+/g, '/') |
| 13 |
// FIXME: once we drop support to node10 we can remove some of this |
| 14 |
.replace(/:\n? +/g, ':') |
| 15 |
.replace(/\n +/g, '\n') |
| 16 |
.replace(/\n\}/g, ' }') |
| 17 |
.replace(/\n\]/g, ']') |
| 18 |
.replace(/\n\[/g, '[') |
| 19 |
.replace(/\n\{\n/g, ' { ') |
| 20 |
.replace(/Map\([0-9]\)/g, 'Map') |
| 21 |
.replace(/Set\([0-9]\)/g, 'Set') |
| 22 |
.replace(/:\n *Map/g, ':Map') |
| 23 |
.replace(/:\n *Set/g, ':Set') |
| 24 |
.replace(/ArboristNode /g, '') |
| 25 |
.replace(/Edge /g, '') |
| 26 |
.replace(/ *([[\]{}]) */g, '$1') |
| 27 |
|
| 28 |
t.test('printable Node', t => { |
| 29 |
t.test('extraneous tree', t => { |
| 30 |
const tree = new Node({ |
| 31 |
name: 'printable-node', |
| 32 |
pkg: { |
| 33 |
name: 'printable-node', |
| 34 |
version: '1.1.1', |
| 35 |
dependencies: { prod: '1.x', b: '', missing: '' }, |
| 36 |
}, |
| 37 |
path: '/home/user/projects/root', |
| 38 |
realpath: '/home/user/projects/root', |
| 39 |
children: [{ |
| 40 |
pkg: { |
| 41 |
name: 'prod', |
| 42 |
version: '1.2.3', |
| 43 |
dependencies: { meta: '', b: '' }, |
| 44 |
peerDependencies: { peer: '' }, |
| 45 |
}, |
| 46 |
fsChildren: [{ |
| 47 |
realpath: '/home/user/projects/root/node_modules/prod/foo', |
| 48 |
path: '/home/user/projects/root/node_modules/prod/foo', |
| 49 |
name: 'foo', |
| 50 |
pkg: { name: 'foo', version: '1.2.3', dependencies: { meta: '' } }, |
| 51 |
}, { |
| 52 |
realpath: '/home/user/projects/root/node_modules/prod/bar', |
| 53 |
path: '/home/user/projects/root/node_modules/prod/bar', |
| 54 |
name: 'bar', |
| 55 |
pkg: { name: 'bar', version: '1.0.0' }, |
| 56 |
}], |
| 57 |
resolved: 'prod', |
| 58 |
integrity: 'prod', |
| 59 |
}, { |
| 60 |
pkg: { |
| 61 |
name: 'b', |
| 62 |
version: '1.2.3', |
| 63 |
}, |
| 64 |
resolved: 'b', |
| 65 |
integrity: 'b', |
| 66 |
}, { |
| 67 |
pkg: { |
| 68 |
name: 'c', |
| 69 |
}, |
| 70 |
resolved: 'c', |
| 71 |
integrity: 'c', |
| 72 |
}], |
| 73 |
}) |
| 74 |
tree.error = { code: 'ERR', path: '/' } |
| 75 |
tree.edgesOut.get('prod').peerConflicted = true |
| 76 |
t.matchSnapshot(util.inspect(tree, { depth: 6 }), |
| 77 |
'should print human readable representation of node tree') |
| 78 |
t.end() |
| 79 |
}) |
| 80 |
|
| 81 |
t.test('variations', t => { |
| 82 |
// manually tweaked variations in the tree to reach for |
| 83 |
// possible different trees output |
| 84 |
const tree = new Node({ |
| 85 |
name: 'variations', |
| 86 |
pkg: { |
| 87 |
name: 'variations', |
| 88 |
version: '1.0.0', |
| 89 |
dependencies: { a: '^1.0.0', b: '^1.0.0' }, |
| 90 |
}, |
| 91 |
extraneous: false, |
| 92 |
path: '/home/user/projects/root', |
| 93 |
realpath: '/home/user/projects/root', |
| 94 |
}) |
| 95 |
|
| 96 |
// append nodes |
| 97 |
const a = new Node({ |
| 98 |
name: 'a', |
| 99 |
pkg: { |
| 100 |
name: 'a', |
| 101 |
version: '1.1.1', |
| 102 |
}, |
| 103 |
path: '/home/users/projects/root/node_modules/a', |
| 104 |
realpath: '/home/users/projects/root/node_modules/a', |
| 105 |
parent: tree, |
| 106 |
}) |
| 107 |
a.extraneous = false |
| 108 |
a.dev = true |
| 109 |
a.optional = true |
| 110 |
a.getBundler = () => true |
| 111 |
a.errors = [Object.assign(new Error('ERR'), { code: 'ERR' })] |
| 112 |
const b = new Link({ |
| 113 |
name: 'b', |
| 114 |
pkg: { |
| 115 |
name: 'b', |
| 116 |
version: '1.0.0', |
| 117 |
}, |
| 118 |
optional: true, |
| 119 |
path: '/home/users/projects/root/c-link', |
| 120 |
realpath: '/home/users/projects/root/c', |
| 121 |
parent: tree, |
| 122 |
}) |
| 123 |
const c = new Node({ |
| 124 |
name: 'c', |
| 125 |
pkg: { name: 'c', version: '1.0.0' }, |
| 126 |
path: '/home/user/projects/root/c', |
| 127 |
realpath: '/home/user/projects/root/c', |
| 128 |
fsParent: tree, |
| 129 |
}) |
| 130 |
b.target = c |
| 131 |
b.extraneous = false |
| 132 |
b.dev = false |
| 133 |
b.optional = false |
| 134 |
b.peer = false |
| 135 |
b.errors = [Object.assign(new Error('ERR'), { |
| 136 |
code: 'ERR', |
| 137 |
path: '/home/users/projects/root/node_modules/b', |
| 138 |
})] |
| 139 |
|
| 140 |
// another link to c |
| 141 |
new Link({ |
| 142 |
name: 'd', |
| 143 |
realpath: '/home/users/projects/root/c', |
| 144 |
target: c, |
| 145 |
parent: tree, |
| 146 |
}) |
| 147 |
|
| 148 |
tree.error = a.errors[0] |
| 149 |
|
| 150 |
t.matchSnapshot(util.inspect(tree, { depth: 6 }), |
| 151 |
'should match non-extraneous tree representation') |
| 152 |
t.end() |
| 153 |
}) |
| 154 |
|
| 155 |
t.test('do not recurse forever', t => { |
| 156 |
const flags = { |
| 157 |
extraneous: false, |
| 158 |
dev: false, |
| 159 |
devOptional: false, |
| 160 |
optional: false, |
| 161 |
peer: false, |
| 162 |
} |
| 163 |
const tree = new Node({ |
| 164 |
name: 'recursive', |
| 165 |
pkg: { |
| 166 |
name: 'recursive', |
| 167 |
version: '1.0.0', |
| 168 |
dependencies: { a: '^1.0.0', b: '^1.0.0' }, |
| 169 |
}, |
| 170 |
path: '/home/user/projects/root', |
| 171 |
realpath: '/home/user/projects/root', |
| 172 |
...flags, |
| 173 |
}) |
| 174 |
new Node({ |
| 175 |
name: 'a', |
| 176 |
pkg: { |
| 177 |
name: 'a', |
| 178 |
version: '1.2.3', |
| 179 |
}, |
| 180 |
parent: tree, |
| 181 |
...flags, |
| 182 |
}) |
| 183 |
const target = new Node({ |
| 184 |
fsParent: tree, |
| 185 |
path: tree.path + '/b', |
| 186 |
pkg: { |
| 187 |
name: 'b', |
| 188 |
version: '1.2.3', |
| 189 |
dependencies: { c: '' }, |
| 190 |
}, |
| 191 |
...flags, |
| 192 |
}) |
| 193 |
const c = new Node({ |
| 194 |
parent: target, |
| 195 |
pkg: { |
| 196 |
name: 'c', |
| 197 |
version: '1.2.3', |
| 198 |
dependencies: { b: '9.9.9999', notfound: '' }, |
| 199 |
}, |
| 200 |
...flags, |
| 201 |
}) |
| 202 |
new Link({ |
| 203 |
parent: c, |
| 204 |
target, |
| 205 |
...flags, |
| 206 |
}) |
| 207 |
new Link({ |
| 208 |
parent: tree, |
| 209 |
target, |
| 210 |
...flags, |
| 211 |
}) |
| 212 |
const s = util.inspect(printable(tree), { depth: Infinity }) |
| 213 |
t.matchSnapshot(s) |
| 214 |
t.end() |
| 215 |
}) |
| 216 |
t.end() |
| 217 |
}) |
| 218 |
|
| 219 |
t.test('virtual roots are shown with their sourceReference', t => { |
| 220 |
const node = new Node({ |
| 221 |
path: '/foo/bar/baz', |
| 222 |
pkg: { |
| 223 |
name: 'baz', |
| 224 |
version: '1.2.3', |
| 225 |
}, |
| 226 |
}) |
| 227 |
const virtual = new Node({ sourceReference: node }) |
| 228 |
t.matchSnapshot(printable(virtual)) |
| 229 |
t.end() |
| 230 |
}) |
| 231 |
|
| 232 |
t.test('broken links do not break the printing', t => { |
| 233 |
const tree = new Node({ |
| 234 |
path: '/home/user/projects/root', |
| 235 |
}) |
| 236 |
|
| 237 |
// a link with no target |
| 238 |
const brokenLink = new Link({ |
| 239 |
name: 'devnull', |
| 240 |
realpath: '/home/user/projects/root/no/thing/here', |
| 241 |
parent: tree, |
| 242 |
}) |
| 243 |
brokenLink.target.root = null |
| 244 |
|
| 245 |
t.matchSnapshot(printable(tree)) |
| 246 |
t.end() |
| 247 |
}) |
| 248 |
|
| 249 |
t.test('show workspaces in printable node output', t => { |
| 250 |
const tree = new Node({ |
| 251 |
path: '/home/user/projects/root', |
| 252 |
pkg: { |
| 253 |
workspaces: ['packages/*'], |
| 254 |
}, |
| 255 |
}) |
| 256 |
new Edge({ |
| 257 |
from: tree, |
| 258 |
type: 'workspace', |
| 259 |
name: 'a', |
| 260 |
spec: 'file:/home/user/projects/root/packages/a', |
| 261 |
}) |
| 262 |
new Edge({ |
| 263 |
from: tree, |
| 264 |
type: 'workspace', |
| 265 |
name: 'b', |
| 266 |
spec: 'file:/home/user/projects/root/packages/b', |
| 267 |
}) |
| 268 |
new Link({ |
| 269 |
pkg: { |
| 270 |
name: 'a', |
| 271 |
version: '1.2.3', |
| 272 |
}, |
| 273 |
realpath: '/home/user/projects/root/packages/a', |
| 274 |
parent: tree, |
| 275 |
}) |
| 276 |
new Link({ |
| 277 |
pkg: { |
| 278 |
name: 'b', |
| 279 |
version: '1.2.3', |
| 280 |
}, |
| 281 |
realpath: '/home/user/projects/root/packages/b', |
| 282 |
parent: tree, |
| 283 |
}) |
| 284 |
tree.workspaces = new Map([ |
| 285 |
['a', tree.children.get('a').realpath], |
| 286 |
['b', tree.children.get('b').realpath], |
| 287 |
]) |
| 288 |
|
| 289 |
t.matchSnapshot(printable(tree)) |
| 290 |
t.end() |
| 291 |
}) |
| 292 |
|
| 293 |
t.test('show bundle/shrinkwrap info', t => { |
| 294 |
const tree = new Node({ |
| 295 |
path: '/path/to/root', |
| 296 |
pkg: { |
| 297 |
name: 'root', |
| 298 |
version: '1.2.3', |
| 299 |
dependencies: { |
| 300 |
bundler: '', |
| 301 |
wrapper: '', |
| 302 |
a: '1', |
| 303 |
b: '2', |
| 304 |
c: '3', |
| 305 |
}, |
| 306 |
}, |
| 307 |
children: [ |
| 308 |
{ |
| 309 |
pkg: { |
| 310 |
name: 'bundler', |
| 311 |
version: '1.2.3', |
| 312 |
bundleDependencies: ['a'], |
| 313 |
dependencies: { |
| 314 |
a: '1', |
| 315 |
}, |
| 316 |
}, |
| 317 |
children: [ |
| 318 |
{ |
| 319 |
pkg: { name: 'a', version: '1.2.3' }, |
| 320 |
integrity: 'a123', |
| 321 |
}, |
| 322 |
], |
| 323 |
}, |
| 324 |
{ |
| 325 |
pkg: { |
| 326 |
name: 'wrapper', |
| 327 |
version: '1.2.3', |
| 328 |
dependencies: { |
| 329 |
a: '1', |
| 330 |
}, |
| 331 |
_hasShrinkwrap: true, |
| 332 |
}, |
| 333 |
hasShrinkwrap: true, |
| 334 |
children: [ |
| 335 |
{ |
| 336 |
pkg: { name: 'a', version: '1.2.3' }, |
| 337 |
integrity: 'a123', |
| 338 |
}, |
| 339 |
], |
| 340 |
}, |
| 341 |
{ |
| 342 |
pkg: { name: 'a', version: '1.2.3' }, |
| 343 |
integrity: 'a123', |
| 344 |
}, |
| 345 |
{ |
| 346 |
pkg: { name: 'c', version: '3.4.5', dependencies: { a: '1' } }, |
| 347 |
children: [ |
| 348 |
{ |
| 349 |
pkg: { name: 'a', version: '1.2.3' }, |
| 350 |
integrity: 'a123', |
| 351 |
}, |
| 352 |
], |
| 353 |
}, |
| 354 |
{ |
| 355 |
pkg: { |
| 356 |
name: 'b', |
| 357 |
version: '2.3.4', |
| 358 |
dependencies: { |
| 359 |
a: '1', |
| 360 |
c: '2', |
| 361 |
d: '3', |
| 362 |
}, |
| 363 |
}, |
| 364 |
children: [ |
| 365 |
{ pkg: { name: 'a', version: '1.9.99' } }, |
| 366 |
{ pkg: { name: 'e', version: '2.3.4' }, integrity: 'y' }, |
| 367 |
{ |
| 368 |
pkg: { |
| 369 |
name: 'c', |
| 370 |
version: '2.3.4', |
| 371 |
dependencies: { a: '2' }, |
| 372 |
}, |
| 373 |
children: [ |
| 374 |
{ |
| 375 |
pkg: { |
| 376 |
name: 'a', |
| 377 |
version: '2.3.99', |
| 378 |
dependencies: { e: '2' }, |
| 379 |
}, |
| 380 |
integrity: 'a2399', |
| 381 |
children: [ |
| 382 |
{ pkg: { name: 'e', version: '2.0.1' }, integrity: 'x' }, |
| 383 |
], |
| 384 |
}, |
| 385 |
], |
| 386 |
}, |
| 387 |
{ |
| 388 |
pkg: { |
| 389 |
name: 'd', |
| 390 |
version: '3.4.5', |
| 391 |
dependencies: { |
| 392 |
a: '3', |
| 393 |
}, |
| 394 |
}, |
| 395 |
children: [{ pkg: { name: 'a', version: '3.4.5' } }], |
| 396 |
}, |
| 397 |
], |
| 398 |
}, |
| 399 |
{ pkg: { name: 'extraneous', version: '1.2.3' } }, |
| 400 |
], |
| 401 |
}) |
| 402 |
|
| 403 |
t.matchSnapshot(printable(tree)) |
| 404 |
t.end() |
| 405 |
}) |
| 406 |
|
| 407 |
t.test('show overrides', (t) => { |
| 408 |
const flags = { |
| 409 |
extraneous: false, |
| 410 |
dev: false, |
| 411 |
peer: false, |
| 412 |
optional: false, |
| 413 |
devOptional: false, |
| 414 |
} |
| 415 |
|
| 416 |
const tree = new Node({ |
| 417 |
path: '/some/path', |
| 418 |
loadOverrides: true, |
| 419 |
pkg: { |
| 420 |
name: 'root', |
| 421 |
version: '1.0.0', |
| 422 |
dependencies: { |
| 423 |
foo: '^1.0.0', |
| 424 |
}, |
| 425 |
overrides: { |
| 426 |
bar: '2.0.0', |
| 427 |
}, |
| 428 |
}, |
| 429 |
children: [ |
| 430 |
{ pkg: { name: 'foo', version: '1.0.0', dependencies: { bar: '^1.0.0' } }, ...flags }, |
| 431 |
{ pkg: { name: 'bar', version: '2.0.0' }, ...flags }, |
| 432 |
], |
| 433 |
...flags, |
| 434 |
}) |
| 435 |
|
| 436 |
t.matchSnapshot(util.inspect(printable(tree), { depth: 5 })) |
| 437 |
t.end() |
| 438 |
}) |