This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
/
edge.js
506 lines (465 loc) · 10.5 KB
/
edge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
const util = require('util')
const Edge = require('../lib/edge.js')
const t = require('tap')
// slight hack to snapshot the getters
// would be nice if tcompare.format showed these by default,
// but it's tricky to know when to show non-iterables and
// when not to. Really, it'd be best if class getters were
// iterable by default, or had some syntax to allow it, but
// that's outside my sphere of direct influence, and using
// Object.defineProperty(this, 'foo', { get ... }) is a pita.
t.formatSnapshot = obj =>
obj instanceof Edge ? {
...obj,
spec: obj.spec,
name: obj.name,
type: obj.type,
valid: obj.valid,
error: obj.error,
from: obj.from,
to: obj.to,
peer: obj.peer,
dev: obj.dev,
optional: obj.optional,
workspace: obj.workspace,
missing: obj.missing,
peerLocal: obj.peerLocal,
invalid: obj.invalid,
__proto__: { constructor: Edge },
} : obj
const reset = node => {
node.edgesOut = new Map()
node.edgesIn = new Set()
}
// mock nodes
const top = {
edgesOut: new Map(),
edgesIn: new Set(),
explain: () => 'top node explanation',
package: { name: 'top', version: '1.2.3' },
get version () {
return this.package.version
},
isTop: true,
parent: null,
resolve (n) {
return n === 'a' ? a : n === 'b' ? b : null
},
addEdgeOut (edge) {
this.edgesOut.set(edge.name, edge)
},
addEdgeIn (edge) {
this.edgesIn.add(edge)
},
}
const a = {
edgesOut: new Map(),
edgesIn: new Set(),
explain: () => 'a explanation',
package: { name: 'a', version: '1.2.3' },
get version () {
return this.package.version
},
isTop: false,
parent: top,
resolve (n) {
return n === 'aa' ? aa : this.parent.resolve(n)
},
addEdgeOut (edge) {
this.edgesOut.set(edge.name, edge)
},
addEdgeIn (edge) {
this.edgesIn.add(edge)
},
}
const b = {
edgesOut: new Map(),
edgesIn: new Set(),
explain: () => 'b explanation',
package: { name: 'b', version: '1.2.3' },
get version () {
return this.package.version
},
isTop: false,
parent: top,
resolve (n) {
return n === 'aa' ? aa : this.parent.resolve(n)
},
addEdgeOut (edge) {
this.edgesOut.set(edge.name, edge)
},
addEdgeIn (edge) {
this.edgesIn.add(edge)
},
}
const bb = {
edgesOut: new Map(),
edgesIn: new Set(),
explain: () => 'bb explanation',
package: { name: 'bb', version: '1.2.3' },
get version () {
return this.package.version
},
isTop: false,
parent: b,
resolve (n) {
return this.parent.resolve(n)
},
addEdgeOut (edge) {
this.edgesOut.set(edge.name, edge)
},
addEdgeIn (edge) {
this.edgesIn.add(edge)
},
}
const aa = {
edgesOut: new Map(),
edgesIn: new Set(),
explain: () => 'aa explanation',
package: { name: 'aa', version: '1.2.3' },
get version () {
return this.package.version
},
isTop: false,
parent: a,
resolve (n) {
return this.parent.resolve(n)
},
addEdgeOut (edge) {
this.edgesOut.set(edge.name, edge)
},
addEdgeIn (edge) {
this.edgesIn.add(edge)
},
}
const c = {
edgesOut: new Map(),
edgesIn: new Set(),
explain: () => 'c explanation',
package: { name: 'c', version: '2.3.4' },
get version () {
return this.package.version
},
isTop: false,
parent: top,
resolve (n) {
return this.parent.resolve(n)
},
addEdgeOut (edge) {
this.edgesOut.set(edge.name, edge)
},
addEdgeIn (edge) {
this.edgesIn.add(edge)
},
}
t.matchSnapshot(new Edge({
from: top,
type: 'peer',
name: 'a',
spec: '1.2.3',
}), 'peer dep at top level, nesting is ok')
reset(top)
reset(a)
t.matchSnapshot(new Edge({
from: top,
type: 'optional',
name: 'missing',
spec: '',
}), 'missing optional dep is fine')
reset(a)
t.matchSnapshot(new Edge({
from: a,
type: 'peer',
name: 'aa',
spec: '1.2.3',
}), 'peer dep below top level, nesting not ok')
reset(a)
reset(aa)
t.matchSnapshot(new Edge({
from: a,
type: 'peer',
name: 'b',
spec: '1.2.3',
}), 'peer dep below top level, parallel ok')
reset(a)
reset(b)
t.ok(new Edge({
from: a,
type: 'prod',
name: 'b',
spec: '1.x',
}).satisfiedBy(b), 'b satisfies b@1.x')
reset(a)
reset(b)
t.ok(new Edge({
from: a,
type: 'prod',
name: 'c',
spec: '1.x',
accept: '2.x',
}).satisfiedBy(c), 'c@2 satisfies spec:1.x, accept:2.x')
t.ok(new Edge({
from: a,
type: 'prod',
name: 'c',
spec: '1.x',
accept: '',
}).satisfiedBy(c), 'c@2 satisfies spec:1.x, accept:*')
const old = new Edge({
from: a,
type: 'peer',
name: 'b',
spec: '1.2.3',
})
const neu = new Edge({
from: a,
type: 'prod',
name: 'b',
spec: '*',
})
t.matchSnapshot(old, 'clobbered edge')
t.matchSnapshot(neu, 'clobbering edge')
reset(a)
reset(b)
const abInvalid = new Edge({
from: a,
name: 'b',
spec: '1.69.420-nice',
type: 'optional',
})
t.matchSnapshot(abInvalid, 'invalid dependency')
// make it valid, and then reload
b.package.version = '1.69.420-nice'
abInvalid.reload(true)
t.equal(abInvalid.valid, true, 'valid after updating target and hard reload')
// put it back for other tests
b.package.version = '1.2.3'
const moving = new Edge({
from: bb,
name: 'aa',
spec: '',
type: 'prod',
})
const explBeforeMove = moving.explain()
t.matchSnapshot(moving, 'old location, missing dep')
bb.parent = a
moving.reload()
const explAfterMove = moving.explain()
t.notEqual(explBeforeMove, explAfterMove, 'moving reloads the explanation')
t.matchSnapshot(moving, 'new location, found dep')
bb.parent = top
moving.reload()
t.matchSnapshot(moving, 'new location, missing dep again')
new Edge({
from: bb,
name: 'aa',
spec: '*',
type: 'dev',
})
t.matchSnapshot(moving, 'clobbered and detached')
t.throws(() => new Edge({
from: top,
type: 'prod',
name: 'yoinks',
spec: { yoinks: '1.2.3' },
}), new TypeError('must provide string spec'))
t.throws(() => new Edge({
from: top,
type: 'prod',
name: 'yoinks',
spec: '1.x',
accept: { yoinks: '2.2.3' },
}), new TypeError('accept field must be a string if provided'))
t.throws(() => new Edge({
from: top,
type: 'not a valid type',
name: 'yoinks',
spec: '*',
}), new TypeError('invalid type: not a valid type\n(valid types are: '))
t.throws(() => new Edge({
type: 'prod',
spec: '*',
from: top,
}), new TypeError('must provide dependency name'))
t.throws(() => new Edge({
type: 'prod',
name: 'foo',
spec: '',
}), new TypeError('must provide "from" node'))
t.throws(() => new Edge({
type: 'workspace',
name: 'foo',
spec: '*',
}), new TypeError('workspace edges must be a symlink'))
t.test('convenience type getter flags', async t => {
t.equal(new Edge({
from: a,
type: 'workspace',
name: 'foo',
spec: 'file:bar/baz',
}).workspace, true, 'workspace convenience getter')
t.equal(new Edge({
from: a,
type: 'prod',
name: 'foo',
spec: '*',
}).prod, true, 'prod convenience getter')
t.equal(new Edge({
from: a,
type: 'dev',
name: 'foo',
spec: '*',
}).dev, true, 'dev convenience getter')
t.equal(new Edge({
from: a,
type: 'optional',
name: 'foo',
spec: '*',
}).optional, true, 'optional convenience getter')
t.equal(new Edge({
from: a,
type: 'peer',
name: 'foo',
spec: '*',
}).peer, true, 'peer convenience getter')
t.equal(new Edge({
from: a,
type: 'optional',
name: 'foo',
spec: '*',
}).optional, true, 'optional convenience getter')
t.equal(new Edge({
from: a,
type: 'peerOptional',
name: 'foo',
spec: '*',
}).peer, true, 'peer convenience getter for peerOptional edge')
t.equal(new Edge({
from: a,
type: 'peerOptional',
name: 'foo',
spec: '*',
}).optional, true, 'optional convenience getter for peerOptional edge')
const explainEdge = new Edge({
from: a,
type: 'peerOptional',
name: 'foo',
spec: '*',
})
const expl = explainEdge.explain([])
t.equal(explainEdge.explain(), expl)
t.matchSnapshot(expl, 'explanation')
explainEdge.detach()
t.notEqual(explainEdge.explain(), expl)
})
// FIXME: once we drop support to node10 we can remove this
const normalizeNode10compatSnapshots = str =>
str
.replace(/\n +/g, '\n')
.replace(/\n\}/g, ' }')
.replace(/ArboristEdge /g, '')
const printableTo = {
...b,
location: '/node_modules/b',
}
const printableFrom = {
...a,
resolve () {
return printableTo
},
location: '',
}
const printable = new Edge({
from: printableFrom,
type: 'prod',
name: 'b',
spec: '1.2.3',
})
t.matchSnapshot(
normalizeNode10compatSnapshots(
util.inspect(printable)
),
'should return a human-readable representation of the edge obj'
)
const minimalPrintableFrom = {
...a,
resolve () {
return null
},
}
const minimalPrintable = new Edge({
from: minimalPrintableFrom,
type: 'dev',
name: 'b',
spec: '1.2.3',
})
t.match(
minimalPrintable.toJSON(),
{
name: 'b',
spec: '1.2.3',
type: 'dev',
error: 'MISSING',
},
'should return a minimal human-readable representation of the edge obj'
)
const bundleChild = {
name: 'bundle-child',
edgesOut: new Map(),
edgesIn: new Set(),
explain: () => 'bundleChild explanation',
package: { name: 'bundle-child', version: '1.2.3' },
get version () {
return this.package.version
},
isTop: false,
parent: top,
resolve (n) {
return this.parent.resolve(n)
},
addEdgeOut (edge) {
this.edgesOut.set(edge.name, edge)
},
addEdgeIn (edge) {
this.edgesIn.add(edge)
},
}
const bundleParent = {
name: 'bundle-parent',
edgesOut: new Map(),
edgesIn: new Set(),
explain: () => 'bundleParent explanation',
package: { name: 'bundle-parent', version: '5.6.7', bundleDependencies: ['bundle-child'] },
get version () {
return this.package.version
},
isTop: false,
parent: top,
resolve (n) {
return n === 'bundle-child' ? bundleChild : undefined
},
addEdgeOut (edge) {
this.edgesOut.set(edge.name, edge)
},
addEdgeIn (edge) {
this.edgesIn.add(edge)
},
}
const bundledEdge = new Edge({
from: bundleParent,
type: 'prod',
name: 'bundle-child',
spec: '1.2.3',
})
t.ok(bundledEdge.satisfiedBy(bundleChild), 'bundled dependency')
const fromBundleDependencies = bundledEdge.from && bundledEdge.from.package.bundleDependencies
t.deepEqual(fromBundleDependencies, ['bundle-child'], 'edge.from bundledDependencies as expected')
t.deepEqual(bundledEdge.name, 'bundle-child', 'edge name as expected')
t.equal(bundledEdge.bundled, true, 'bundled prop is true')
t.deepEqual(bundledEdge.explain(), {
type: 'prod',
name: 'bundle-child',
spec: '1.2.3',
bundled: true,
from: bundleParent.explain(),
}, 'bundled edge.explain as expected')