| 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 t = require('tap') |
| 5 |
const Link = require('../lib/link.js') |
| 6 |
const Node = require('../lib/node.js') |
| 7 |
const Shrinkwrap = require('../lib/shrinkwrap.js') |
| 8 |
|
| 9 |
const { resolve } = require('node:path') |
| 10 |
const normalizePath = path => path.replace(/^[A-Z]:/, '').replace(/\\/g, '/') |
| 11 |
const normalizePaths = obj => { |
| 12 |
obj.path = obj.path && normalizePath(obj.path) |
| 13 |
obj.realpath = obj.realpath && normalizePath(obj.realpath) |
| 14 |
for (const key of obj.inventory.keys()) { |
| 15 |
const member = obj.inventory.get(key) |
| 16 |
member.path = member.path && normalizePath(member.path) |
| 17 |
member.realpath = member.realpath && normalizePath(member.realpath) |
| 18 |
} |
| 19 |
return obj |
| 20 |
} |
| 21 |
|
| 22 |
const meta = new Shrinkwrap({ path: '/home/user/projects/some/kind/of/path' }) |
| 23 |
meta.data = { |
| 24 |
lockfileVersion: 2, |
| 25 |
packages: {}, |
| 26 |
dependencies: {}, |
| 27 |
} |
| 28 |
|
| 29 |
const root = new Node({ |
| 30 |
pkg: { name: 'root' }, |
| 31 |
path: '/home/user/projects/some/kind/of/path', |
| 32 |
meta, |
| 33 |
}) |
| 34 |
|
| 35 |
const l1 = new Link({ |
| 36 |
pkg: { name: 'root' }, |
| 37 |
path: '/home/user/some/other/path', |
| 38 |
realpath: '/home/user/projects/some/kind/of/path', |
| 39 |
meta, |
| 40 |
}) |
| 41 |
|
| 42 |
t.matchSnapshot(normalizePaths(l1), 'instantiate without providing target') |
| 43 |
t.equal(l1.isLink, true, 'link is a link') |
| 44 |
t.same(l1.children.size, 0, 'children is empty') |
| 45 |
l1.children = new Map([[1, 2], [3, 4]]) |
| 46 |
t.same(l1.children.size, 0, 'children still empty after being assigned') |
| 47 |
l1.children.set('asdf', 'foo') |
| 48 |
t.same(l1.children.size, 0, 'children still empty after setting value') |
| 49 |
|
| 50 |
t.throws(() => new Link({ path: '/x' }), { |
| 51 |
message: 'must provide realpath for Link node', |
| 52 |
}) |
| 53 |
|
| 54 |
const resolver = new Link({ |
| 55 |
path: '/x/y/z', |
| 56 |
realpath: '/x/z/y/a/b/c', |
| 57 |
}) |
| 58 |
t.equal(resolver.resolved, |
| 59 |
'file:../z/y/a/b/c', 'link resolved is relpath to realpath') |
| 60 |
resolver.path = null |
| 61 |
t.equal(resolver.resolved, null, 'link resolved depends on path') |
| 62 |
resolver.path = '/x/z/y/a/b/d' |
| 63 |
t.equal(resolver.resolved, 'file:c', 'updates when path changes') |
| 64 |
|
| 65 |
t.matchSnapshot(normalizePaths(new Link({ |
| 66 |
path: '/home/user/some/other/path', |
| 67 |
target: root, |
| 68 |
})), 'instantiate with target specified') |
| 69 |
|
| 70 |
t.test('link.target setter', async t => { |
| 71 |
const link = new Link({ |
| 72 |
path: '/path/to/link', |
| 73 |
realpath: '/node-a', |
| 74 |
pkg: { name: 'node-a', version: '1.2.3' }, |
| 75 |
}) |
| 76 |
const oldTarget = link.target |
| 77 |
t.equal(oldTarget.linksIn.has(link), true, 'target takes note of link') |
| 78 |
t.equal(link.package, oldTarget.package, 'link has same package as target') |
| 79 |
|
| 80 |
const newTarget = new Node({ |
| 81 |
path: '/node-b', |
| 82 |
realpath: '/node-b', |
| 83 |
pkg: { name: 'node-b', version: '1.2.3' }, |
| 84 |
}) |
| 85 |
link.target = newTarget |
| 86 |
t.equal(oldTarget.linksIn.size, 0, 'old target has no links in now') |
| 87 |
t.equal(link.target, newTarget, 'new target is target') |
| 88 |
t.equal(newTarget.linksIn.has(link), true, 'new target notes the link') |
| 89 |
t.equal(link.package, newTarget.package, 'link package is new target package') |
| 90 |
|
| 91 |
link.target = null |
| 92 |
t.equal(link.target, null, 'link has no target') |
| 93 |
t.strictSame(link.package, {}, 'no package without link') |
| 94 |
t.equal(oldTarget.linksIn.size, 0, 'old target still has no links') |
| 95 |
t.equal(newTarget.linksIn.size, 0, 'new target has no links in now') |
| 96 |
|
| 97 |
link.target = null |
| 98 |
t.equal(link.target, null, 'target is now null') |
| 99 |
t.strictSame(link.package, {}, 'removed target, package is now empty') |
| 100 |
// just test the guard that setting to a different falsey value is fine |
| 101 |
link.target = undefined |
| 102 |
t.equal(link.target, null, 'target is still null') |
| 103 |
t.strictSame(link.package, {}, 'removed target, package is now empty') |
| 104 |
}) |
| 105 |
|
| 106 |
t.test('get root from various places', t => { |
| 107 |
const root = new Node({ |
| 108 |
path: '/path/to/root', |
| 109 |
}) |
| 110 |
|
| 111 |
t.test('get from root', t => { |
| 112 |
const fromRoot = new Link({ |
| 113 |
pkg: { name: 'from-root' }, |
| 114 |
path: '/path/to/root/from-root', |
| 115 |
realpath: '/path/to/root/from-root-target', |
| 116 |
root, |
| 117 |
}) |
| 118 |
t.equal(fromRoot.root, root) |
| 119 |
t.equal(fromRoot.fsParent, root) |
| 120 |
t.equal(fromRoot.parent, null) |
| 121 |
t.equal(fromRoot.target.root, root) |
| 122 |
t.equal(fromRoot.target.fsParent, root) |
| 123 |
t.equal(fromRoot.target.parent, null) |
| 124 |
t.end() |
| 125 |
}) |
| 126 |
|
| 127 |
t.test('get from fsParent', t => { |
| 128 |
const fromFsParent = new Link({ |
| 129 |
pkg: { name: 'from-fs-parent' }, |
| 130 |
path: '/path/to/root/from-fs-parent', |
| 131 |
realpath: '/path/to/root/from-root-fs-parent', |
| 132 |
fsParent: root, |
| 133 |
}) |
| 134 |
t.equal(fromFsParent.root, root) |
| 135 |
t.equal(fromFsParent.fsParent, root) |
| 136 |
t.equal(fromFsParent.parent, null) |
| 137 |
t.equal(fromFsParent.target.root, root) |
| 138 |
t.equal(fromFsParent.target.fsParent, root) |
| 139 |
t.equal(fromFsParent.target.parent, null) |
| 140 |
t.end() |
| 141 |
}) |
| 142 |
|
| 143 |
t.test('get from parent', t => { |
| 144 |
const fromParent = new Link({ |
| 145 |
pkg: { name: 'from-parent' }, |
| 146 |
parent: root, |
| 147 |
realpath: '/path/to/root/from-root-parent', |
| 148 |
}) |
| 149 |
t.equal(fromParent.root, root) |
| 150 |
t.equal(fromParent.fsParent, null) |
| 151 |
t.equal(fromParent.parent, root) |
| 152 |
t.equal(fromParent.target.root, root) |
| 153 |
t.equal(fromParent.target.fsParent, root) |
| 154 |
t.equal(fromParent.target.parent, null) |
| 155 |
t.end() |
| 156 |
}) |
| 157 |
|
| 158 |
t.end() |
| 159 |
}) |
| 160 |
|
| 161 |
t.test('temporary link node pending attachment to a tree', t => { |
| 162 |
const root = new Node({ path: '/path/to/node' }) |
| 163 |
const link = new Link({ name: 'foo', realpath: '/this/will/change' }) |
| 164 |
const target = new Node({ path: '/path/to/node/foo' }) |
| 165 |
t.equal(link.root, link) |
| 166 |
t.equal(target.root, target) |
| 167 |
t.equal(normalizePath(link.realpath), normalizePath('/this/will/change')) |
| 168 |
link.target = target |
| 169 |
t.equal(target.root, link) |
| 170 |
t.equal(link.realpath, target.path) |
| 171 |
t.equal(link.path, null) |
| 172 |
link.parent = root |
| 173 |
t.equal(normalizePath(link.realpath), normalizePath(target.path)) |
| 174 |
t.equal(normalizePath(link.path), normalizePath(root.path + '/node_modules/foo')) |
| 175 |
t.equal(link.root, root) |
| 176 |
t.equal(target.root, root) |
| 177 |
t.equal(target.fsParent, root) |
| 178 |
|
| 179 |
const link2 = new Link({ name: 'bar', realpath: '/this/will/change' }) |
| 180 |
const target2 = new Node({ name: 'bar' }) |
| 181 |
link2.target = target2 |
| 182 |
t.equal(normalizePath(target2.path), normalizePath('/this/will/change')) |
| 183 |
link2.target = null |
| 184 |
target2.realpath = target2.path = resolve('/path/to/node/bar') |
| 185 |
link2.target = target2 |
| 186 |
t.equal(normalizePath(link2.realpath), normalizePath(target2.path)) |
| 187 |
link2.parent = root |
| 188 |
t.equal(target2.fsParent, root) |
| 189 |
|
| 190 |
t.end() |
| 191 |
}) |
| 192 |
|
| 193 |
t.test('link gets version from target', t => { |
| 194 |
const link = new Link({ realpath: '/some/real/path', path: '/other/path' }) |
| 195 |
t.equal(link.version, '') |
| 196 |
link.target = null |
| 197 |
link.package = { name: 'bar', version: '2.3.4' } |
| 198 |
t.equal(link.version, '2.3.4') |
| 199 |
link.package = {} |
| 200 |
t.equal(link.version, '') |
| 201 |
new Node({ |
| 202 |
pkg: { name: 'foo', version: '1.2.3' }, |
| 203 |
path: '/some/real/path', |
| 204 |
root: link, |
| 205 |
}) |
| 206 |
t.equal(link.version, '1.2.3') |
| 207 |
t.end() |
| 208 |
}) |
| 209 |
|
| 210 |
t.test('recalculateOutEdgesOverrides forwards overrides to target', t => { |
| 211 |
const root = new Node({ |
| 212 |
path: '/path/to/root', |
| 213 |
pkg: { |
| 214 |
name: 'root', |
| 215 |
dependencies: { foo: '1.0.0' }, |
| 216 |
overrides: { bar: '2.0.0' }, |
| 217 |
}, |
| 218 |
loadOverrides: true, |
| 219 |
}) |
| 220 |
|
| 221 |
const target = new Node({ |
| 222 |
path: '/path/to/store/foo', |
| 223 |
pkg: { |
| 224 |
name: 'foo', |
| 225 |
version: '1.0.0', |
| 226 |
dependencies: { bar: '1.0.0' }, |
| 227 |
}, |
| 228 |
root, |
| 229 |
}) |
| 230 |
|
| 231 |
const link = new Link({ |
| 232 |
pkg: { name: 'foo', version: '1.0.0' }, |
| 233 |
path: '/path/to/root/node_modules/foo', |
| 234 |
realpath: '/path/to/store/foo', |
| 235 |
target, |
| 236 |
parent: root, |
| 237 |
}) |
| 238 |
|
| 239 |
// The root has overrides, and the edge from root -> link should propagate them |
| 240 |
t.ok(root.overrides, 'root has overrides') |
| 241 |
t.ok(link.overrides, 'link received overrides from root edge') |
| 242 |
t.ok(link.target.overrides, 'target received overrides forwarded from link') |
| 243 |
|
| 244 |
// The target's edge to "bar" should have the override applied |
| 245 |
const barEdge = link.target.edgesOut.get('bar') |
| 246 |
t.ok(barEdge, 'target has edge to bar') |
| 247 |
t.ok(barEdge.overrides, 'bar edge has overrides') |
| 248 |
t.equal(barEdge.spec, '2.0.0', 'bar edge spec is overridden to 2.0.0') |
| 249 |
t.equal(barEdge.rawSpec, '1.0.0', 'bar edge rawSpec is original 1.0.0') |
| 250 |
|
| 251 |
// recalculateOutEdgesOverrides is a no-op when target is null |
| 252 |
link.target = null |
| 253 |
t.doesNotThrow(() => link.recalculateOutEdgesOverrides(), |
| 254 |
'no-op when target is null') |
| 255 |
|
| 256 |
t.end() |
| 257 |
}) |
| 258 |
|
| 259 |
t.test('recalculateOutEdgesOverrides does not forward when no rule matches a target dep — npm/cli#9357', t => { |
| 260 |
// Regression: prior to the fix, Link.recalculateOutEdgesOverrides forwarded the link's full OverrideSet to the target unconditionally. |
| 261 |
// For a target whose edges are NOT named in any override rule, that flipped target.overrides from undefined to the root's OverrideSet. |
| 262 |
// Downstream, that "has overrides" state changed canPlaceDep's KEEP-vs-REPLACE decision and made `npm ci` re-resolve lockfile-pinned edges from the registry. |
| 263 |
// After the fix, propagation is gated on at least one rule whose name matches an edge in target.edgesOut. |
| 264 |
const root = new Node({ |
| 265 |
path: '/path/to/root', |
| 266 |
pkg: { |
| 267 |
name: 'root', |
| 268 |
dependencies: { foo: '1.0.0' }, |
| 269 |
// override is for "bar", but the linked target only depends on "baz" |
| 270 |
overrides: { bar: '2.0.0' }, |
| 271 |
}, |
| 272 |
loadOverrides: true, |
| 273 |
}) |
| 274 |
|
| 275 |
const target = new Node({ |
| 276 |
path: '/path/to/store/foo', |
| 277 |
pkg: { |
| 278 |
name: 'foo', |
| 279 |
version: '1.0.0', |
| 280 |
dependencies: { baz: '1.0.0' }, |
| 281 |
}, |
| 282 |
root, |
| 283 |
}) |
| 284 |
|
| 285 |
// eslint-disable-next-line no-new |
| 286 |
new Link({ |
| 287 |
pkg: { name: 'foo', version: '1.0.0' }, |
| 288 |
path: '/path/to/root/node_modules/foo', |
| 289 |
realpath: '/path/to/store/foo', |
| 290 |
target, |
| 291 |
parent: root, |
| 292 |
}) |
| 293 |
|
| 294 |
t.ok(root.overrides, 'root has overrides') |
| 295 |
t.notOk(target.overrides, |
| 296 |
'target.overrides stays undefined when no rule matches a target dep') |
| 297 |
const bazEdge = target.edgesOut.get('baz') |
| 298 |
t.notOk(bazEdge.overrides, |
| 299 |
'unrelated edge keeps edge.overrides undefined') |
| 300 |
t.equal(bazEdge.spec, '1.0.0', 'unrelated edge spec is unchanged') |
| 301 |
t.end() |
| 302 |
}) |
| 303 |
|
| 304 |
t.test('link to root path gets root as target', t => { |
| 305 |
const root = new Node({ |
| 306 |
path: '/project/root', |
| 307 |
pkg: { |
| 308 |
name: 'root', |
| 309 |
dependencies: { |
| 310 |
root: 'file:.', |
| 311 |
}, |
| 312 |
}, |
| 313 |
}) |
| 314 |
const link = new Link({ |
| 315 |
parent: root, |
| 316 |
realpath: root.path, |
| 317 |
pkg: { ...root.package }, |
| 318 |
}) |
| 319 |
t.equal(link.target, root) |
| 320 |
t.end() |
| 321 |
}) |