| 1 |
const localeCompare = require('@isaacs/string-locale-compare')('en') |
| 2 |
const Diff = require('../lib/diff.js') |
| 3 |
const t = require('tap') |
| 4 |
const Node = require('../lib/node.js') |
| 5 |
const Link = require('../lib/link.js') |
| 6 |
const Arborist = require('../lib/arborist/index.js') |
| 7 |
|
| 8 |
// don't print the full node objects because we don't need to track that |
| 9 |
// for this test and it makes the snapshot unnecessarily noisy. |
| 10 |
|
| 11 |
const normalizePath = path => path.replace(/^\w:/, '').replace(/\\/g, '/') |
| 12 |
const normalizedCWD = normalizePath(process.cwd()) |
| 13 |
const formatNode = node => |
| 14 |
node && Object.assign(Object.create(node.constructor.prototype), { |
| 15 |
name: node.name, |
| 16 |
path: normalizePath(node.path).split(normalizedCWD).join('{CWD}'), |
| 17 |
integrity: node.integrity, |
| 18 |
}) |
| 19 |
|
| 20 |
const { format } = require('tcompare') |
| 21 |
|
| 22 |
const path = diff => normalizePath((diff.actual || diff.ideal).path) |
| 23 |
.split(normalizedCWD).join('{CWD}') |
| 24 |
|
| 25 |
const formatDiff = obj => |
| 26 |
Object.assign(Object.create(obj.constructor.prototype), { |
| 27 |
action: obj.action, |
| 28 |
actual: formatNode(obj.actual), |
| 29 |
ideal: formatNode(obj.ideal), |
| 30 |
leaves: obj.leaves.map(d => path(d)), |
| 31 |
unchanged: obj.unchanged.map(d => normalizePath(d.path).split(normalizedCWD).join('{CWD}')), |
| 32 |
removed: obj.removed.map(d => normalizePath(d.path).split(normalizedCWD).join('{CWD}')), |
| 33 |
children: [...obj.children] |
| 34 |
.map(formatDiff) |
| 35 |
.sort((a, b) => localeCompare(path(a), path(b))), |
| 36 |
}) |
| 37 |
|
| 38 |
t.formatSnapshot = obj => format(formatDiff(obj), { sort: false }) |
| 39 |
|
| 40 |
const actual = new Node({ |
| 41 |
name: 'a', |
| 42 |
path: '/path/to/root', |
| 43 |
realpath: '/path/to/root', |
| 44 |
integrity: 'sha512-aaa', |
| 45 |
fsChildren: [ |
| 46 |
{ |
| 47 |
name: 'foo', |
| 48 |
path: '/path/to/root/foo', |
| 49 |
children: [ |
| 50 |
{ name: 'bar', integrity: 'sha512-bar' }, |
| 51 |
{ name: 'baz', integrity: 'sha512-baz' }, |
| 52 |
], |
| 53 |
}, |
| 54 |
], |
| 55 |
children: [ |
| 56 |
{ |
| 57 |
name: 'b', |
| 58 |
integrity: 'sha512-bbb', |
| 59 |
children: [ |
| 60 |
{ name: 'c', integrity: 'sha512-ccc' }, |
| 61 |
{ name: 'd', |
| 62 |
integrity: 'sha512-ddd', |
| 63 |
children: [{ name: 'e', integrity: 'sha512-eee' }], |
| 64 |
}, |
| 65 |
{ name: 'f', integrity: 'sha512-fff' }, |
| 66 |
], |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: 'x', |
| 70 |
integrity: 'sha512-xxx', |
| 71 |
children: [ |
| 72 |
{ name: 'y', |
| 73 |
integrity: 'sha512-yyy', |
| 74 |
children: [ |
| 75 |
{ name: 'z', integrity: null, resolved: 'foobarbaz' }, |
| 76 |
] }, |
| 77 |
], |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: 'p', |
| 81 |
integrity: 'sha512-ppp', |
| 82 |
children: [{ name: 'q', integrity: 'sha512-qqq' }], |
| 83 |
}, |
| 84 |
{ |
| 85 |
name: 'bundler', |
| 86 |
integrity: 'sha512-bundler', |
| 87 |
pkg: { |
| 88 |
bundleDependencies: ['bundled-a'], |
| 89 |
}, |
| 90 |
children: [ |
| 91 |
{ |
| 92 |
name: 'bundled-a', |
| 93 |
pkg: { |
| 94 |
dependencies: { metabundled: '' }, |
| 95 |
}, |
| 96 |
}, |
| 97 |
{ |
| 98 |
name: 'metabundled', |
| 99 |
pkg: { |
| 100 |
name: 'metabundled', |
| 101 |
version: '1.2.3', |
| 102 |
}, |
| 103 |
}, |
| 104 |
{ |
| 105 |
name: 'not-bundled', |
| 106 |
integrity: 'sha512-not-bundled', |
| 107 |
}, |
| 108 |
], |
| 109 |
}, |
| 110 |
{ |
| 111 |
name: 'should-have-bins', |
| 112 |
integrity: 'sha512-should-have-bins', |
| 113 |
pkg: { |
| 114 |
bin: { |
| 115 |
'should-exist': 'should-exist.js', |
| 116 |
}, |
| 117 |
}, |
| 118 |
}, |
| 119 |
], |
| 120 |
}) |
| 121 |
|
| 122 |
const ideal = new Node({ |
| 123 |
name: 'a', |
| 124 |
path: '/path/to/root', |
| 125 |
realpath: '/path/to/root', |
| 126 |
integrity: 'sha512-aaa', |
| 127 |
fsChildren: [ |
| 128 |
{ |
| 129 |
name: 'foo', |
| 130 |
path: '/path/to/root/foo', |
| 131 |
children: [ |
| 132 |
{ name: 'bar', integrity: 'sha512-bar' }, |
| 133 |
{ name: 'baz', integrity: 'sha512-BAZ' }, |
| 134 |
{ name: 'boo', integrity: 'sha512-BOO' }, |
| 135 |
], |
| 136 |
}, |
| 137 |
], |
| 138 |
children: [ |
| 139 |
{ |
| 140 |
name: 'b', |
| 141 |
integrity: 'sha512-BBB', |
| 142 |
children: [ |
| 143 |
{ name: 'c', integrity: 'sha512-CCC' }, |
| 144 |
{ name: 'd', |
| 145 |
integrity: 'sha512-ddd', |
| 146 |
children: [{ name: 'e', integrity: 'sha512-EEE' }], |
| 147 |
}, |
| 148 |
{ name: 'f', integrity: 'sha512-fff' }, |
| 149 |
], |
| 150 |
}, |
| 151 |
{ |
| 152 |
name: 'x', |
| 153 |
integrity: 'sha512-xxx', |
| 154 |
children: [ |
| 155 |
{ name: 'y', |
| 156 |
integrity: 'sha512-YYY', |
| 157 |
children: [ |
| 158 |
{ name: 'z', integrity: null, resolved: 'foobarbaz' }, |
| 159 |
] }, |
| 160 |
], |
| 161 |
}, |
| 162 |
{ |
| 163 |
name: 'i', |
| 164 |
integrity: 'sha512-III', |
| 165 |
children: [{ name: 'j', integrity: 'sha512-JJJ' }], |
| 166 |
}, |
| 167 |
{ |
| 168 |
name: 'bundler', |
| 169 |
integrity: 'sha512-bundler', |
| 170 |
pkg: { |
| 171 |
bundleDependencies: ['bundled-a'], |
| 172 |
}, |
| 173 |
children: [ |
| 174 |
{ |
| 175 |
name: 'not-bundled', |
| 176 |
integrity: 'sha512-NOT-BUNDLED', |
| 177 |
}, |
| 178 |
], |
| 179 |
}, |
| 180 |
{ |
| 181 |
name: 'should-have-bins', |
| 182 |
integrity: 'sha512-should-have-bins', |
| 183 |
pkg: { |
| 184 |
bin: { |
| 185 |
'should-exist': 'should-exist.js', |
| 186 |
}, |
| 187 |
}, |
| 188 |
}, |
| 189 |
], |
| 190 |
}) |
| 191 |
|
| 192 |
const d = Diff.calculate({ actual, ideal }) |
| 193 |
t.matchSnapshot(d, 'diff two trees') |
| 194 |
t.equal(d.parent, null, 'root has no parent') |
| 195 |
t.equal([...d.children][0].parent, d, 'parent of root child is root') |
| 196 |
t.equal(d.action, null, 'root has no action') |
| 197 |
|
| 198 |
t.test('when a root is a link, traverse the target children', t => { |
| 199 |
t.plan(2) |
| 200 |
for (const global of [true, false]) { |
| 201 |
t.test(`global=${global}`, async t => { |
| 202 |
const actual = new Link({ |
| 203 |
global, |
| 204 |
path: '/some/linked/path', |
| 205 |
realpath: '/some/real/path', |
| 206 |
}) |
| 207 |
|
| 208 |
actual.target = new Node({ |
| 209 |
global, |
| 210 |
path: '/some/real/path', |
| 211 |
pkg: {}, |
| 212 |
children: [{ |
| 213 |
name: 'child', |
| 214 |
pkg: { |
| 215 |
name: 'child', |
| 216 |
version: '1.2.3', |
| 217 |
}, |
| 218 |
}], |
| 219 |
}) |
| 220 |
|
| 221 |
const ideal = new Node({ |
| 222 |
path: '/some/real/path', |
| 223 |
realpath: '/some/real/path', |
| 224 |
children: [], |
| 225 |
}) |
| 226 |
|
| 227 |
const diff = Diff.calculate({ actual, ideal }) |
| 228 |
t.matchSnapshot(diff, 'correctly removes the child node') |
| 229 |
t.equal(diff.removed.length, 1, 'identifies the need to remove the child') |
| 230 |
}) |
| 231 |
} |
| 232 |
}) |
| 233 |
|
| 234 |
t.test('filtered diff', async t => { |
| 235 |
const ideal = new Node({ |
| 236 |
path: '/project/path', |
| 237 |
pkg: { |
| 238 |
name: 'root', |
| 239 |
dependencies: { a: 'file:a', c: '' }, |
| 240 |
}, |
| 241 |
}) |
| 242 |
|
| 243 |
new Link({ |
| 244 |
parent: ideal, |
| 245 |
pkg: {}, |
| 246 |
realpath: '/project/path/a', |
| 247 |
}) |
| 248 |
const a = new Node({ |
| 249 |
fsParent: ideal, |
| 250 |
path: '/project/path/a', |
| 251 |
pkg: { |
| 252 |
name: 'a', |
| 253 |
dependencies: { |
| 254 |
b: '', |
| 255 |
}, |
| 256 |
}, |
| 257 |
}) |
| 258 |
new Node({ |
| 259 |
parent: ideal, |
| 260 |
pkg: { |
| 261 |
name: 'b', |
| 262 |
version: '1.2.3', |
| 263 |
}, |
| 264 |
}) |
| 265 |
new Node({ |
| 266 |
parent: ideal, |
| 267 |
pkg: { |
| 268 |
name: 'c', |
| 269 |
version: '1.2.3', |
| 270 |
}, |
| 271 |
}) |
| 272 |
|
| 273 |
const actual = new Node({ |
| 274 |
path: '/project/path', |
| 275 |
pkg: { |
| 276 |
name: 'root', |
| 277 |
dependencies: { a: 'file:a', c: '' }, |
| 278 |
}, |
| 279 |
}) |
| 280 |
|
| 281 |
const cExcludedABPresent = Diff.calculate({ actual, ideal, filterNodes: [a] }) |
| 282 |
t.matchSnapshot(cExcludedABPresent, 'c excluded, a and b present') |
| 283 |
|
| 284 |
// make sure that *removing* something that *would* be depended on |
| 285 |
// by the actual node in the filter set is also removed properly. |
| 286 |
new Link({ |
| 287 |
parent: actual, |
| 288 |
pkg: {}, |
| 289 |
realpath: '/project/path/a', |
| 290 |
}) |
| 291 |
new Node({ |
| 292 |
fsParent: actual, |
| 293 |
path: '/project/path/a', |
| 294 |
pkg: { |
| 295 |
name: 'a', |
| 296 |
dependencies: { |
| 297 |
b: '', |
| 298 |
d: '', |
| 299 |
}, |
| 300 |
}, |
| 301 |
}) |
| 302 |
new Node({ |
| 303 |
parent: actual, |
| 304 |
pkg: { |
| 305 |
name: 'b', |
| 306 |
version: '1.2.3', |
| 307 |
}, |
| 308 |
}) |
| 309 |
new Node({ |
| 310 |
parent: actual, |
| 311 |
pkg: { |
| 312 |
name: 'd', |
| 313 |
version: '1.2.3', |
| 314 |
}, |
| 315 |
}) |
| 316 |
|
| 317 |
const removeD = Diff.calculate({ actual, ideal, filterNodes: [a] }) |
| 318 |
t.matchSnapshot(removeD, 'd is removed') |
| 319 |
|
| 320 |
// removing a dependency, like we would with `npm rm -g foo` |
| 321 |
actual.package = { |
| 322 |
...actual.package, |
| 323 |
dependencies: { |
| 324 |
...actual.package.dependencies, |
| 325 |
e: '*', |
| 326 |
}, |
| 327 |
} |
| 328 |
const e = new Node({ |
| 329 |
parent: actual, |
| 330 |
pkg: { |
| 331 |
name: 'e', |
| 332 |
version: '1.2.3', |
| 333 |
}, |
| 334 |
}) |
| 335 |
const eRemoved = Diff.calculate({ actual, ideal, filterNodes: [e] }) |
| 336 |
t.matchSnapshot(eRemoved, 'e is removed') |
| 337 |
|
| 338 |
// can't filter based on something that isn't there |
| 339 |
t.throws(() => Diff.calculate({ |
| 340 |
actual, |
| 341 |
ideal, |
| 342 |
filterNodes: [ |
| 343 |
new Node({ |
| 344 |
path: '/project/path/node_modules/x', |
| 345 |
pkg: { name: 'x' }, |
| 346 |
}), |
| 347 |
], |
| 348 |
}), { |
| 349 |
message: 'invalid filterNode: outside idealTree/actualTree', |
| 350 |
}) |
| 351 |
|
| 352 |
// filtering an extraneous node is ok though |
| 353 |
delete actual.package.dependencies.e |
| 354 |
actual.package = { ...actual.package } |
| 355 |
const eRemovedExtraneous = Diff.calculate({ actual, ideal, filterNodes: [e] }) |
| 356 |
t.matchSnapshot(eRemovedExtraneous, 'e is removed (extraneous)') |
| 357 |
}) |
| 358 |
|
| 359 |
t.test('diff doesnt break unchanged shrinkwrapped deps', async t => { |
| 360 |
const actual = new Node({ |
| 361 |
name: 'a', |
| 362 |
path: '/path/to/actual', |
| 363 |
children: [{ |
| 364 |
name: 'shrinkwrapped-dep', |
| 365 |
hasShrinkwrap: true, |
| 366 |
integrity: 'sha512-ddd', |
| 367 |
children: [ |
| 368 |
{ name: 'shrinkwrap-inner-a', integrity: 'sha512-aaa' }, |
| 369 |
{ name: 'shrinkwrap-inner-b', integrity: 'sha512-bbb' }, |
| 370 |
], |
| 371 |
}, { |
| 372 |
name: 'regular-dep', |
| 373 |
integrity: 'sha512-eee', |
| 374 |
children: [{ |
| 375 |
name: 'shrinkwrapped-inner', |
| 376 |
hasShrinkwrap: true, |
| 377 |
integrity: 'sha512-fff', |
| 378 |
children: [{ |
| 379 |
name: 'shrinkwrap-inner-c', |
| 380 |
integrity: 'sha512-ccc', |
| 381 |
}], |
| 382 |
}], |
| 383 |
}], |
| 384 |
}) |
| 385 |
|
| 386 |
const ideal = new Node({ |
| 387 |
name: 'a', |
| 388 |
path: '/path/to/actual', |
| 389 |
children: [{ |
| 390 |
name: 'shrinkwrapped-dep', |
| 391 |
hasShrinkwrap: true, |
| 392 |
integrity: 'sha512-ddd', |
| 393 |
}, { |
| 394 |
name: 'regular-dep', |
| 395 |
integrity: 'sha512-eee', |
| 396 |
children: [{ |
| 397 |
name: 'shrinkwrapped-inner', |
| 398 |
hasShrinkwrap: true, |
| 399 |
integrity: 'sha512-fff', |
| 400 |
children: [{ |
| 401 |
name: 'shrinkwrap-inner-c', |
| 402 |
integrity: 'sha512-ccc', |
| 403 |
}], |
| 404 |
}], |
| 405 |
}], |
| 406 |
}) |
| 407 |
|
| 408 |
// putting the shrinkwrapped-inner node in the shrinkwrapInflated set |
| 409 |
// allows us to fake the behavior of reifying the node and then |
| 410 |
// running a second diff that includes that node's children. this |
| 411 |
// is reflected by the children of the shrinkwrapped-inner node being |
| 412 |
// marked as unchanged. |
| 413 |
const shrinkwrappedInner = ideal.children.get('regular-dep') |
| 414 |
.children.get('shrinkwrapped-inner') |
| 415 |
const diff = Diff.calculate({ |
| 416 |
actual, |
| 417 |
ideal, |
| 418 |
shrinkwrapInflated: new Set([shrinkwrappedInner]), |
| 419 |
}) |
| 420 |
t.matchSnapshot(diff, 'made no changes') |
| 421 |
|
| 422 |
t.equal(diff.leaves.length, 1, 'diff has exactly one leaf') |
| 423 |
t.match(diff.leaves[0], { action: null, ideal: { name: 'shrinkwrapped-dep' } }, |
| 424 |
'the shrinkwrapped dep is in the leaves with a null action') |
| 425 |
}) |
| 426 |
|
| 427 |
t.test('extraneous pruning in workspaces', async t => { |
| 428 |
// just load the virtual tree here twice, |
| 429 |
// and then remove the extraneous ones from the 'ideal' |
| 430 |
const path = t.testdir({ |
| 431 |
'package.json': JSON.stringify({ |
| 432 |
workspaces: [ |
| 433 |
'packages/*', |
| 434 |
], |
| 435 |
}), |
| 436 |
'package-lock.json': JSON.stringify({ |
| 437 |
lockfileVersion: 3, |
| 438 |
packages: { |
| 439 |
'node_modules/a': { |
| 440 |
resolved: 'packages/a', |
| 441 |
link: true, |
| 442 |
}, |
| 443 |
'node_modules/b': { |
| 444 |
resolved: 'packages/b', |
| 445 |
link: true, |
| 446 |
}, |
| 447 |
'node_modules/wrappy': { |
| 448 |
version: '1.0.2', |
| 449 |
extraneous: true, |
| 450 |
}, |
| 451 |
'packages/a': { |
| 452 |
version: '1.2.3', |
| 453 |
}, |
| 454 |
'packages/a/node_modules/once': { |
| 455 |
version: '1.4.0', |
| 456 |
extraneous: true, |
| 457 |
dependencies: { |
| 458 |
wrappy: '1', |
| 459 |
}, |
| 460 |
}, |
| 461 |
'packages/b': { |
| 462 |
version: '1.2.3', |
| 463 |
dependencies: { |
| 464 |
once: '*', |
| 465 |
}, |
| 466 |
}, |
| 467 |
'packages/b/node_modules/abbrev': { |
| 468 |
version: '1.1.1', |
| 469 |
extraneous: true, |
| 470 |
}, |
| 471 |
'packages/b/node_modules/once': { |
| 472 |
version: '1.4.0', |
| 473 |
dependencies: { |
| 474 |
wrappy: '1', |
| 475 |
}, |
| 476 |
}, |
| 477 |
'packages/b/node_modules/wrappy': { |
| 478 |
version: '1.0.2', |
| 479 |
}, |
| 480 |
}, |
| 481 |
}), |
| 482 |
}) |
| 483 |
|
| 484 |
const actual = await new Arborist({ path }).loadVirtual() |
| 485 |
const ideal = await new Arborist({ path }).loadVirtual() |
| 486 |
for (const node of ideal.inventory.values()) { |
| 487 |
if (node.extraneous) { |
| 488 |
node.root = null |
| 489 |
} |
| 490 |
} |
| 491 |
const idealA = ideal.children.get('a').target |
| 492 |
const actualA = actual.children.get('a').target |
| 493 |
const pruneWsA = Diff.calculate({ |
| 494 |
actual, |
| 495 |
ideal, |
| 496 |
filterNodes: [idealA, actualA], |
| 497 |
}) |
| 498 |
t.matchSnapshot(pruneWsA, 'prune in workspace A') |
| 499 |
|
| 500 |
const idealB = ideal.children.get('b').target |
| 501 |
const actualB = actual.children.get('b').target |
| 502 |
const pruneWsB = Diff.calculate({ |
| 503 |
actual, |
| 504 |
ideal, |
| 505 |
filterNodes: [idealB, actualB], |
| 506 |
}) |
| 507 |
t.matchSnapshot(pruneWsB, 'prune in workspace B') |
| 508 |
}) |
| 509 |
|
| 510 |
t.test('check versions (even if all other metadata is missing)', t => { |
| 511 |
const actual = new Node({ |
| 512 |
path: '/some/path', |
| 513 |
pkg: { |
| 514 |
dependencies: { |
| 515 |
foo: '', |
| 516 |
}, |
| 517 |
}, |
| 518 |
children: [ |
| 519 |
{ name: 'foo', pkg: { name: 'foo', version: '1.0.0' } }, |
| 520 |
], |
| 521 |
}) |
| 522 |
|
| 523 |
const ideal = new Node({ |
| 524 |
path: '/some/path', |
| 525 |
pkg: { |
| 526 |
dependencies: { |
| 527 |
foo: '', |
| 528 |
}, |
| 529 |
}, |
| 530 |
children: [ |
| 531 |
{ name: 'foo', pkg: { name: 'foo', version: '1.2.3' } }, |
| 532 |
], |
| 533 |
}) |
| 534 |
|
| 535 |
const diff = Diff.calculate({ actual, ideal }) |
| 536 |
t.match(diff.children, [ |
| 537 |
{ |
| 538 |
actual: actual.children.get('foo'), |
| 539 |
ideal: ideal.children.get('foo'), |
| 540 |
action: 'CHANGE', |
| 541 |
}, |
| 542 |
]) |
| 543 |
t.equal(diff.children.length, 1) |
| 544 |
|
| 545 |
t.match(diff.leaves, [ |
| 546 |
{ |
| 547 |
actual: actual.children.get('foo'), |
| 548 |
ideal: ideal.children.get('foo'), |
| 549 |
action: 'CHANGE', |
| 550 |
}, |
| 551 |
]) |
| 552 |
t.equal(diff.leaves.length, 1) |
| 553 |
t.end() |
| 554 |
}) |