This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
/
build-ideal-tree.js
2880 lines (2636 loc) · 94.2 KB
/
build-ideal-tree.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// the tree-checking is too abusively slow on Windows, and
// can make the test time out unnecessarily.
if (process.platform === 'win32') {
process.env.ARBORIST_DEBUG = 0
}
const {basename, resolve, relative} = require('path')
const pacote = require('pacote')
const t = require('tap')
const Arborist = require('../..')
const fixtures = resolve(__dirname, '../fixtures')
// load the symbolic links that we depend on
require(fixtures)
const {start, stop, registry, auditResponse} = require('../fixtures/registry-mocks/server.js')
const npa = require('npm-package-arg')
const fs = require('fs')
t.before(start)
t.teardown(stop)
const cache = t.testdir()
// track the warnings that are emitted. returns a function that removes
// the listener and provides the list of what it saw.
const warningTracker = () => {
const list = []
const onlog = (...msg) => msg[0] === 'warn' && list.push(msg)
process.on('log', onlog)
return () => {
process.removeListener('log', onlog)
return list
}
}
const {
normalizePath,
normalizePaths,
printTree,
} = require('../fixtures/utils.js')
const oldLockfileWarning = [
'warn',
'old lockfile',
`
The package-lock.json file was created with an old version of npm,
so supplemental metadata must be fetched from the registry.
This is a one-time fix-up, please be patient...
`,
]
const cwd = normalizePath(process.cwd())
t.cleanSnapshot = s => s.split(cwd).join('{CWD}')
.split(registry).join('https://registry.npmjs.org/')
const printIdeal = (path, opt) => buildIdeal(path, opt).then(printTree)
// give it a very long timeout so CI doesn't crash as easily
const OPT = { cache, registry, timeout: 30 * 60 * 1000 }
const newArb = (path, opt = {}) => new Arborist({ ...OPT, path, ...opt })
const buildIdeal = (path, opt) => newArb(path, opt).buildIdealTree(opt)
t.test('fail on mismatched engine when engineStrict is set', async t => {
const path = resolve(fixtures, 'engine-specification')
t.rejects(buildIdeal(path, {
...OPT,
nodeVersion: '12.18.4',
engineStrict: true,
}).then(() => {
throw new Error('failed to fail')
}), { code: 'EBADENGINE' })
})
t.test('fail on malformed package.json', t => {
const path = resolve(fixtures, 'malformed-json')
return t.rejects(
buildIdeal(path),
{ code: 'EJSONPARSE' },
'should fail with EJSONPARSE error'
)
})
t.test('ignore mismatched engine for optional dependencies', async t => {
const path = resolve(fixtures, 'optional-engine-specification')
await buildIdeal(path, {
...OPT,
nodeVersion: '12.18.4',
engineStrict: true,
})
})
t.test('warn on mismatched engine when engineStrict is false', t => {
const path = resolve(fixtures, 'engine-specification')
const check = warningTracker()
return buildIdeal(path, {
...OPT,
nodeVersion: '12.18.4',
engineStrict: false,
}).then(() => t.match(check(), [
['warn', 'EBADENGINE'],
]))
})
t.test('fail on mismatched platform', async t => {
const path = resolve(fixtures, 'platform-specification')
t.rejects(buildIdeal(path, {
...OPT,
nodeVersion: '4.0.0',
}).then(() => {
throw new Error('failed to fail')
}), { code: 'EBADPLATFORM' })
})
t.test('ignore mismatched platform for optional dependencies', async t => {
const path = resolve(fixtures, 'optional-platform-specification')
const tree = await buildIdeal(path, {
...OPT,
nodeVersion: '12.18.4',
engineStrict: true,
})
t.equal(tree.children.get('platform-specifying-test-package').package.version, '1.0.0', 'added the optional dep to the ideal tree')
})
t.test('no options', t => {
const arb = new Arborist()
t.match(
arb.registry,
'https://registry.npmjs.org',
'should use default registry'
)
t.end()
})
t.test('a workspace with a conflicted nested duplicated dep', async t => {
t.matchSnapshot(await printIdeal(resolve(fixtures, 'workspace4')))
})
t.test('a tree with an outdated dep, missing dep, no lockfile', async t => {
const path = resolve(fixtures, 'outdated-no-lockfile')
const tree = await buildIdeal(path)
const expected = {
once: '1.3.3',
wrappy: '1.0.1',
}
for (const [name, version] of Object.entries(expected)) {
t.equal(tree.children.get(name).package.version, version, `expect ${name}@${version}`)
}
t.matchSnapshot(printTree(tree), 'should not update all')
})
t.test('tarball deps with transitive tarball deps', t =>
t.resolveMatchSnapshot(printIdeal(
resolve(fixtures, 'tarball-dependencies'))))
t.test('testing-peer-deps-overlap package', async t => {
const path = resolve(fixtures, 'testing-peer-deps-overlap')
const idealTree = await buildIdeal(path)
const arb = new Arborist({ path, idealTree, ...OPT })
const tree2 = await arb.buildIdealTree()
t.equal(tree2, idealTree)
t.matchSnapshot(printTree(idealTree), 'build ideal tree with overlapping peer dep ranges')
})
t.test('testing-peer-deps package', async t => {
const path = resolve(fixtures, 'testing-peer-deps')
const idealTree = await buildIdeal(path)
const arb = new Arborist({path, idealTree, ...OPT})
const tree2 = await arb.buildIdealTree()
t.equal(tree2, idealTree)
t.matchSnapshot(printTree(idealTree), 'build ideal tree with peer deps')
})
t.test('testing-peer-deps package with symlinked root', t => {
const path = resolve(fixtures, 'testing-peer-deps-link')
return buildIdeal(path).then(idealTree => {
t.ok(idealTree.isLink, 'ideal tree is rooted on a Link')
return new Arborist({path, idealTree, ...OPT})
.buildIdealTree().then(tree2 => t.equal(tree2, idealTree))
.then(() => t.matchSnapshot(printTree(idealTree), 'build ideal tree with peer deps'))
})
})
t.test('testing-peer-deps nested', t => {
const path = resolve(fixtures, 'testing-peer-deps-nested')
return t.resolveMatchSnapshot(printIdeal(path), 'build ideal tree')
.then(() => t.resolveMatchSnapshot(printIdeal(path, {
// hit the branch where update is just a list of names
update: ['@isaacs/testing-peer-deps'],
}), 'can update a peer dep cycle'))
})
t.test('tap vs react15', t => {
const path = resolve(fixtures, 'tap-react15-collision')
return t.resolveMatchSnapshot(printIdeal(path), 'build ideal tree with tap collision')
})
t.test('tap vs react15 with legacy shrinkwrap', t => {
const path = resolve(fixtures, 'tap-react15-collision-legacy-sw')
return t.resolveMatchSnapshot(printIdeal(path), 'tap collision with legacy sw file')
})
t.test('bad shrinkwrap file', t => {
const path = resolve(fixtures, 'testing-peer-deps-bad-sw')
return t.resolveMatchSnapshot(printIdeal(path), 'bad shrinkwrap')
})
t.test('a direct link dep has a dep with optional dependencies', t => {
const path = resolve(fixtures, 'link-dep-has-dep-with-optional-dep')
return t.resolveMatchSnapshot(printIdeal(path), 'should not mark children of the optional dep as extraneous')
})
t.test('cyclical peer deps', t => {
const paths = [
resolve(fixtures, 'peer-dep-cycle'),
resolve(fixtures, 'peer-dep-cycle-with-sw'),
]
t.plan(paths.length)
paths.forEach(path => t.test(basename(path), t =>
t.resolveMatchSnapshot(printIdeal(path), 'cyclical peer deps')
.then(() => t.resolveMatchSnapshot(printIdeal(path, {
// just reload the dep at its current required version
add: ['@isaacs/peer-dep-cycle-a'],
}), 'cyclical peer deps - reload a dependency'))
.then(() => t.resolveMatchSnapshot(printIdeal(path, {
add: ['@isaacs/peer-dep-cycle-a@2.x'],
}), 'cyclical peer deps - upgrade a package'))
.then(() => t.rejects(printIdeal(path, {
// this conflicts with the direct dep on a@1 PEER-> b@1
add: ['@isaacs/peer-dep-cycle-b@2.x'],
})))
// this conflict is ok since we're using legacy peer deps
.then(() => t.resolveMatchSnapshot(printIdeal(path, {
add: ['@isaacs/peer-dep-cycle-b@2.x'],
legacyPeerDeps: true,
}), 'add b@2.x with legacy peer deps'))
.then(() => t.resolveMatchSnapshot(printIdeal(path, {
// use @latest rather than @2.x to exercise the 'explicit tag' path
add: ['@isaacs/peer-dep-cycle-b@latest'],
rm: ['@isaacs/peer-dep-cycle-a'],
}), 'can add b@2 if we remove a@1 dep'))
.then(() => t.resolveMatchSnapshot(printIdeal(path, {
rm: ['@isaacs/peer-dep-cycle-a'],
}), 'remove the dep, prune everything'))
))
})
t.test('nested cyclical peer deps', t => {
const paths = [
resolve(fixtures, 'peer-dep-cycle-nested'),
resolve(fixtures, 'peer-dep-cycle-nested-with-sw'),
]
// if we have a shrinkwrap, then we'll get a collision with the current
// version already there. if we don't, then we'll get a peerConflict
// when we try to put the second one there.
const ers = {
[paths[0]]: {
code: 'ERESOLVE',
},
[paths[1]]: {
code: 'ERESOLVE',
},
}
t.plan(paths.length)
paths.forEach(path => t.test(basename(path), async t => {
t.matchSnapshot(await printIdeal(path), 'nested peer deps cycle')
t.matchSnapshot(await printIdeal(path, {
// just make sure it works if it gets a spec object
add: [npa('@isaacs/peer-dep-cycle-a@2.x')],
}), 'upgrade a')
t.matchSnapshot(await printIdeal(path, {
// a dep whose name we don't yet know
add: [
'@isaacs/peer-dep-cycle-a@2.x',
`${registry}@isaacs/peer-dep-cycle-b/-/peer-dep-cycle-b-2.0.0.tgz`,
],
}), 'upgrade b')
t.matchSnapshot(await printIdeal(path, {
force: true,
add: ['@isaacs/peer-dep-cycle-c@2.x'],
}), 'upgrade c, forcibly')
await t.rejects(printIdeal(path, {
add: [
'@isaacs/peer-dep-cycle-a@1.x',
'@isaacs/peer-dep-cycle-c@2.x',
],
}), ers[path], 'try (and fail) to upgrade c and a incompatibly')
}))
})
t.test('dedupe example - not deduped', t => {
const path = resolve(fixtures, 'dedupe-tests')
return t.resolveMatchSnapshot(printIdeal(path), 'dedupe testing')
})
t.test('dedupe example - deduped because preferDedupe=true', t => {
const path = resolve(fixtures, 'dedupe-tests')
return t.resolveMatchSnapshot(printIdeal(path, { preferDedupe: true }))
})
t.test('dedupe example - nested because legacyBundling=true', t => {
const path = resolve(fixtures, 'dedupe-tests')
return t.resolveMatchSnapshot(printIdeal(path, {
legacyBundling: true,
preferDedupe: true,
}))
})
t.test('dedupe example - deduped', t => {
const path = resolve(fixtures, 'dedupe-tests-2')
return t.resolveMatchSnapshot(printIdeal(path), 'dedupe testing')
})
t.test('expose explicitRequest', async t => {
const path = resolve(fixtures, 'simple')
const arb = new Arborist({...OPT, path})
await arb.buildIdealTree({ add: ['abbrev'] })
t.match(arb.explicitRequests, Set, 'exposes the explicit request Set')
t.strictSame([...arb.explicitRequests].map(e => e.name), ['abbrev'])
t.end()
})
t.test('bundle deps example 1, empty', t => {
// NB: this results in ignoring the bundled deps when building the
// ideal tree. When we reify, we'll have to ignore the deps that
// got placed as part of the bundle.
const path = resolve(fixtures, 'testing-bundledeps-empty')
return t.resolveMatchSnapshot(printIdeal(path), 'bundle deps testing')
.then(() => t.resolveMatchSnapshot(printIdeal(path, {
saveBundle: true,
add: ['@isaacs/testing-bundledeps'],
}), 'should have some missing deps in the ideal tree'))
})
t.test('bundle deps example 1, full', t => {
// In this test, bundle deps show up, because they're present in
// the actual tree to begin with.
const path = resolve(fixtures, 'testing-bundledeps')
return t.resolveMatchSnapshot(printIdeal(path), 'no missing deps')
.then(() => t.resolveMatchSnapshot(printIdeal(path, {
saveBundle: true,
add: ['@isaacs/testing-bundledeps'],
}), 'add stuff, no missing deps'))
})
t.test('bundle deps example 1, complete:true', async t => {
// When complete:true is set, we extract into a temp dir to read
// the bundled deps, so they ARE included, just like during reify()
const path = resolve(fixtures, 'testing-bundledeps-empty')
// wrap pacote.extract in a spy so we can be sure the integrity and resolved
// options both made it through
const _extract = pacote.extract
// restore in a teardown, just in case
t.teardown(() => {
pacote.extract = _extract
})
pacote.extract = (uri, dir, opts) => {
t.ok(uri.endsWith('testing-bundledeps-1.0.0.tgz'))
t.equal(uri, opts.resolved, 'passed resolved')
t.ok(opts.integrity, 'passed integrity')
const res = _extract(uri, dir, opts)
pacote.extract = _extract
return res
}
t.matchSnapshot(await printIdeal(path, {
complete: true,
}), 'no missing deps, because complete: true')
t.matchSnapshot(await printIdeal(path, {
saveBundle: true,
add: ['@isaacs/testing-bundledeps'],
complete: true,
}), 'no missing deps, because complete: true, add dep, save bundled')
})
t.test('bundle deps example 2', t => {
// bundled deps at the root level are NOT ignored when building ideal trees
const path = resolve(fixtures, 'testing-bundledeps-2')
return t.resolveMatchSnapshot(printIdeal(path), 'bundle deps testing')
.then(() => t.resolveMatchSnapshot(printIdeal(path, {
saveBundle: true,
add: ['@isaacs/testing-bundledeps-c'],
}), 'add new bundled dep c'))
.then(() => t.resolveMatchSnapshot(printIdeal(path, {
rm: ['@isaacs/testing-bundledeps-a'],
}), 'remove bundled dependency a'))
})
t.test('bundle deps example 2, link', t => {
// bundled deps at the root level are NOT ignored when building ideal trees
const path = resolve(fixtures, 'testing-bundledeps-link')
return t.resolveMatchSnapshot(printIdeal(path), 'bundle deps testing')
.then(() => t.resolveMatchSnapshot(printIdeal(path, {
saveBundle: true,
add: ['@isaacs/testing-bundledeps-c'],
}), 'add new bundled dep c'))
.then(() => t.resolveMatchSnapshot(printIdeal(path, {
rm: ['@isaacs/testing-bundledeps-a'],
}), 'remove bundled dependency a'))
})
t.test('unresolvable peer deps', t => {
const path = resolve(fixtures, 'testing-peer-deps-unresolvable')
return t.rejects(printIdeal(path, { strictPeerDeps: true }), {
message: 'unable to resolve dependency tree',
code: 'ERESOLVE',
}, 'unacceptable')
})
t.test('do not add shrinkwrapped deps', t => {
const path = resolve(fixtures, 'shrinkwrapped-dep-no-lock')
return t.resolveMatchSnapshot(printIdeal(path, { update: true }))
})
t.test('do add shrinkwrapped deps when complete:true is set', t => {
const path = resolve(fixtures, 'shrinkwrapped-dep-no-lock')
return t.resolveMatchSnapshot(printIdeal(path, {
complete: true,
update: true,
}))
})
t.test('do not update shrinkwrapped deps', t => {
const path = resolve(fixtures, 'shrinkwrapped-dep-with-lock')
return t.resolveMatchSnapshot(printIdeal(path,
{ update: { names: ['abbrev']}}))
})
t.test('do not update shrinkwrapped deps, ignore lockfile', t => {
const path = resolve(fixtures, 'shrinkwrapped-dep-with-lock')
return t.resolveMatchSnapshot(printIdeal(path,
{ packageLock: false, update: { names: ['abbrev']}}))
})
t.test('do not update shrinkwrapped deps when complete:true is set', t => {
const path = resolve(fixtures, 'shrinkwrapped-dep-with-lock')
return t.resolveMatchSnapshot(printIdeal(path,
{ update: { names: ['abbrev']}, complete: true }))
})
t.test('deduped transitive deps with asymmetrical bin declaration', t => {
const path =
resolve(fixtures, 'testing-asymmetrical-bin-no-lock')
return t.resolveMatchSnapshot(printIdeal(path), 'with no lockfile')
})
t.test('deduped transitive deps with asymmetrical bin declaration', t => {
const path =
resolve(fixtures, 'testing-asymmetrical-bin-with-lock')
return t.resolveMatchSnapshot(printIdeal(path), 'with lockfile')
})
t.test('update', t => {
t.test('flow outdated', t => {
const flowOutdated = resolve(fixtures, 'flow-outdated')
t.resolveMatchSnapshot(printIdeal(flowOutdated, {
update: {
names: ['flow-parser'],
},
}), 'update flow parser')
t.resolveMatchSnapshot(printIdeal(flowOutdated, {
update: true,
}), 'update everything')
t.end()
})
t.test('tap and flow', t => {
const tapAndFlow = resolve(fixtures, 'tap-and-flow')
t.resolveMatchSnapshot(printIdeal(tapAndFlow, {
update: {
all: true,
},
}), 'update everything')
t.resolveMatchSnapshot(printIdeal(tapAndFlow, {
update: {
names: ['ink'],
},
}), 'update ink')
t.end()
})
t.end()
})
t.test('link meta deps', t =>
t.resolveMatchSnapshot(printIdeal(
resolve(fixtures, 'link-meta-deps-empty'))))
t.test('respect the yarn.lock file', t =>
t.resolveMatchSnapshot(printIdeal(
resolve(fixtures, 'yarn-lock-mkdirp'))))
t.test('respect the yarn.lock file version, if lacking resolved', t =>
t.resolveMatchSnapshot(printIdeal(
resolve(fixtures, 'yarn-lock-mkdirp-no-resolved'))))
t.test('optional dependency failures', async t => {
const cases = [
'optional-ok',
'optional-dep-enotarget',
'optional-dep-missing',
'optional-metadep-enotarget',
'optional-metadep-missing',
]
t.plan(cases.length)
for (const c of cases) {
const tree = await printIdeal(resolve(fixtures, c))
t.matchSnapshot(tree, c)
}
})
t.test('prod dependency failures', t => {
const cases = [
'prod-dep-enotarget',
'prod-dep-missing',
]
t.plan(cases.length)
cases.forEach(c => t.rejects(printIdeal(
resolve(fixtures, c)), c))
})
t.test('link dep with a link dep', t => {
const path = resolve(fixtures, 'cli-750')
return Promise.all([
t.resolveMatchSnapshot(printIdeal(path), 'link metadeps with lockfile'),
t.resolveMatchSnapshot(printIdeal(path, { update: true }), 'link metadeps without lockfile'),
])
})
t.test('link dep within node_modules and outside root', t => {
const path = resolve(fixtures, 'external-link-dep')
return Promise.all([
t.resolveMatchSnapshot(printIdeal(path), 'linky deps with lockfile'),
t.resolveMatchSnapshot(printIdeal(path, { update: true }), 'linky deps without lockfile'),
t.resolveMatchSnapshot(printIdeal(path, { follow: true }), 'linky deps followed'),
t.resolveMatchSnapshot(printIdeal(path, { update: true, follow: true }), 'linky deps followed without lockfile'),
])
})
t.test('global style', t => t.resolveMatchSnapshot(printIdeal(t.testdir(), {
globalStyle: true,
add: ['rimraf'],
})))
t.test('global', t => t.resolveMatchSnapshot(printIdeal(t.testdir(), {
global: true,
add: ['rimraf'],
})))
t.test('global has to add or remove', t => t.rejects(printIdeal(t.testdir(), {
global: true,
})))
// somewhat copy-pasta from the test/arborist/audit.js to exercise
// the buildIdealTree code paths
t.test('update mkdirp to non-minimist-using version', async t => {
const path = resolve(fixtures, 'deprecated-dep')
t.teardown(auditResponse(resolve(fixtures, 'audit-nyc-mkdirp/audit.json')))
const arb = new Arborist({ path, ...OPT })
await arb.audit()
t.matchSnapshot(printTree(await arb.buildIdealTree()))
})
t.test('force a new nyc (and update mkdirp nicely)', async t => {
const path = resolve(fixtures, 'audit-nyc-mkdirp')
t.teardown(auditResponse(resolve(fixtures, 'audit-nyc-mkdirp/audit.json')))
const arb = new Arborist({
force: true,
path,
...OPT,
})
await arb.audit()
t.matchSnapshot(printTree(await arb.buildIdealTree()))
t.equal(arb.idealTree.children.get('mkdirp').package.version, '0.5.5')
t.equal(arb.idealTree.children.get('nyc').package.version, '15.1.0')
})
t.test('force a new mkdirp (but not semver major)', async t => {
const path = resolve(fixtures, 'mkdirp-pinned')
t.teardown(auditResponse(resolve(fixtures, 'audit-nyc-mkdirp/audit.json')))
const arb = new Arborist({
force: true,
path,
...OPT,
})
await arb.audit()
t.matchSnapshot(printTree(await arb.buildIdealTree()))
t.equal(arb.idealTree.children.get('mkdirp').package.version, '0.5.5')
t.equal(arb.idealTree.children.get('minimist').package.version, '1.2.5')
})
t.test('empty update should not trigger old lockfile', async t => {
const path = t.testdir({
'package.json': JSON.stringify({
name: 'empty-update',
version: '1.0.0',
}),
'package-lock.json': JSON.stringify({
name: 'empty-update',
version: '1.0.0',
lockfileVersion: 2,
requires: true,
packages: {
'': {
name: 'empty-update',
version: '1.0.0',
},
},
}),
})
const checkLogs = warningTracker()
const arb = newArb(path)
await arb.reify({ update: true })
t.strictSame(checkLogs(), [])
})
t.test('no fix available', async t => {
const path = resolve(fixtures, 'audit-mkdirp/mkdirp-unfixable')
const checkLogs = warningTracker()
t.teardown(auditResponse(resolve(path, 'audit.json')))
const arb = new Arborist({
force: true,
path,
...OPT,
})
await arb.audit()
t.matchSnapshot(printTree(await arb.buildIdealTree()))
t.equal(arb.idealTree.children.get('mkdirp').package.version, '0.5.1')
t.match(checkLogs(), [
oldLockfileWarning,
['warn', 'audit', 'No fix available for mkdirp@*'],
])
})
t.test('no fix available, linked top package', async t => {
const path = resolve(fixtures, 'audit-mkdirp')
const checkLogs = warningTracker()
t.teardown(auditResponse(resolve(path, 'mkdirp-unfixable/audit.json')))
const arb = new Arborist({
force: true,
path,
...OPT,
})
await arb.audit()
t.matchSnapshot(printTree(await arb.buildIdealTree()))
t.strictSame(checkLogs(), [
oldLockfileWarning,
['warn', 'audit',
'Manual fix required in linked project at ./mkdirp-unfixable for mkdirp@*.\n' +
"'cd ./mkdirp-unfixable' and run 'npm audit' for details.",
]])
})
t.test('workspaces', t => {
t.test('should install a simple example', t => {
const path = resolve(__dirname, '../fixtures/workspaces-simple')
return t.resolveMatchSnapshot(printIdeal(path))
})
t.test('should update a simple example', t => {
const path = resolve(__dirname, '../fixtures/workspaces-simple')
return t.resolveMatchSnapshot(printIdeal(path, { update: { all: true }}))
})
t.test('should install a simple scoped pkg example', t => {
const path = resolve(__dirname, '../fixtures/workspaces-scoped-pkg')
return t.resolveMatchSnapshot(printIdeal(path))
})
t.test('should not work with duplicate names', t => {
const path = resolve(__dirname, '../fixtures/workspaces-duplicate')
return t.rejects(printIdeal(path), { code: 'EDUPLICATEWORKSPACE' }, 'throws EDUPLICATEWORKSPACE error')
})
t.test('should install shared dependencies into root folder', t => {
const path = resolve(__dirname, '../fixtures/workspaces-shared-deps')
return t.resolveMatchSnapshot(printIdeal(path))
})
t.test('should install conflicting dep versions', t => {
const path = resolve(__dirname, '../fixtures/workspaces-conflicting-versions')
return t.resolveMatchSnapshot(printIdeal(path))
})
t.test('should prefer linking nested workspaces', t => {
const path = resolve(__dirname, '../fixtures/workspaces-prefer-linking')
return t.resolveMatchSnapshot(printIdeal(path))
})
t.test('should install from registry on version not satisfied', t => {
const path = resolve(__dirname, '../fixtures/workspaces-version-unsatisfied')
return t.resolveMatchSnapshot(printIdeal(path))
})
t.test('should link top level nested workspaces', t => {
const path = resolve(__dirname, '../fixtures/workspaces-top-level-link')
return t.resolveMatchSnapshot(printIdeal(path))
})
t.test('should install workspace transitive dependencies', t => {
const path = resolve(__dirname, '../fixtures/workspaces-transitive-deps')
return t.resolveMatchSnapshot(printIdeal(path))
})
t.test('should ignore nested node_modules folders', t => {
// packages/a/node_modules/nested-workspaces should not be installed
const path = resolve(__dirname, '../fixtures/workspaces-ignore-nm')
return t.resolveMatchSnapshot(printIdeal(path))
})
t.test('should work with files spec', t => {
const path = resolve(__dirname, '../fixtures/workspaces-with-files-spec')
return t.resolveMatchSnapshot(printIdeal(path))
})
t.test('should handle conflicting peer deps ranges', t => {
const path = resolve(__dirname, '../fixtures/workspaces-peer-ranges')
return t.rejects(
printIdeal(path),
{
code: 'ERESOLVE',
},
'throws ERESOLVE error'
)
})
t.end()
})
t.test('adding tarball to global prefix that is a symlink at a different path depth', async t => {
const fixt = t.testdir({
'real-root': {},
'another-path': {
'global-root': t.fixture('symlink', '../real-root'),
},
})
const path = resolve(fixt, 'another-path/global-root')
const arb = new Arborist({
path,
global: true,
...OPT,
})
const tarballpath = resolve(__dirname, '../fixtures/registry-mocks/content/mkdirp/-/mkdirp-1.0.2.tgz')
const tree = await arb.buildIdealTree({
path,
global: true,
add: [
// this will be a relative path to the tarball above
relative(process.cwd(), tarballpath),
],
})
t.matchSnapshot(printTree(tree))
})
t.test('add symlink that points to a symlink', t => {
const fixt = t.testdir({
'global-prefix': {
lib: {
node_modules: {
a: t.fixture('symlink', '../../../linked-pkg'),
},
},
},
'linked-pkg': {
'package.json': JSON.stringify({
name: 'a',
version: '1.0.0',
}),
},
'my-project': {
'package.json': JSON.stringify({}),
},
})
const path = resolve(fixt, 'my-project')
const arb = new Arborist({
path,
...OPT,
})
return arb.buildIdealTree({
add: [
// simulates the string used by `npm link <pkg>` when
// installing a package available in the global space
`file:${resolve(fixt, 'global-prefix/lib/node_modules/a')}`,
],
}).then(tree =>
t.matchSnapshot(
printTree(tree),
'should follow symlinks to find final realpath destination'
)
)
})
t.test('update global space single dep', t => {
const fixt = t.testdir({
'global-prefix': {
lib: {
node_modules: {
abbrev: {
'package.json': JSON.stringify({
name: 'abbrev',
version: '1.0.0',
}),
},
},
},
},
})
const path = resolve(fixt, 'global-prefix/lib')
const opts = {
path,
global: true,
update: true,
...OPT,
}
const arb = new Arborist(opts)
return arb.buildIdealTree(opts).then(tree =>
t.matchSnapshot(
printTree(tree),
'should update global dependencies'
)
)
})
// if we get this wrong, it'll spin forever and use up all the memory
t.test('pathologically nested dependency cycle', async t => {
t.matchSnapshot(await printIdeal(
resolve(fixtures, 'pathological-dep-nesting-cycle')))
})
t.test('resolve file deps from cwd', t => {
const cwd = process.cwd()
t.teardown(() => process.chdir(cwd))
const path = t.testdir({
global: {},
local: {},
})
const fixturedir = resolve(fixtures, 'root-bundler')
process.chdir(fixturedir)
const arb = new Arborist({
global: true,
path: resolve(path, 'global'),
...OPT,
})
return arb.buildIdealTree({
path: `${path}/local`,
add: ['child-1.2.3.tgz'],
global: true,
}).then(tree => {
const resolved = `file:${resolve(fixturedir, 'child-1.2.3.tgz')}`
t.equal(normalizePath(tree.children.get('child').resolved), normalizePath(resolved))
})
})
t.test('resolve links in global mode', t => {
const cwd = process.cwd()
t.teardown(() => process.chdir(cwd))
const path = t.testdir({
global: {},
lib: {
'my-project': {},
},
'linked-dep': {
'package.json': JSON.stringify({
name: 'linked-dep',
version: '1.0.0',
}),
},
})
const fixturedir = resolve(path, 'lib', 'my-project')
process.chdir(fixturedir)
const arb = new Arborist({
...OPT,
global: true,
path: resolve(path, 'global'),
})
return arb.buildIdealTree({
add: ['file:../../linked-dep'],
global: true,
}).then(tree => {
const resolved = 'file:../../linked-dep'
t.equal(tree.children.get('linked-dep').resolved, resolved)
})
})
t.test('dont get confused if root matches duped metadep', async t => {
const path = resolve(fixtures, 'test-root-matches-metadep')
const arb = new Arborist({ path, ...OPT })
const tree = await arb.buildIdealTree()
t.matchSnapshot(printTree(tree))
})
t.test('inflate an ancient lockfile by hitting the registry', async t => {
const checkLogs = warningTracker()
const path = resolve(fixtures, 'sax')
const arb = new Arborist({ path, ...OPT })
const tree = await arb.buildIdealTree()
t.matchSnapshot(printTree(tree))
t.strictSame(checkLogs(), [
[
'warn',
'ancient lockfile',
`
The package-lock.json file was created with an old version of npm,
so supplemental metadata must be fetched from the registry.
This is a one-time fix-up, please be patient...
`,
],
])
})
t.test('warn for ancient lockfile, even if we use v1', async t => {
const checkLogs = warningTracker()
const path = resolve(fixtures, 'sax')
const arb = new Arborist({ path, lockfileVersion: 1, ...OPT })
const tree = await arb.buildIdealTree()
t.matchSnapshot(printTree(tree))
t.strictSame(checkLogs(), [
[
'warn',
'ancient lockfile',
`
The package-lock.json file was created with an old version of npm,
so supplemental metadata must be fetched from the registry.
This is a one-time fix-up, please be patient...
`,
],
])
})
t.test('no old lockfile warning if we write back v1', async t => {
const checkLogs = warningTracker()
const path = resolve(fixtures, 'old-package-lock')
const arb = new Arborist({ path, lockfileVersion: 1, ...OPT })
const tree = await arb.buildIdealTree()
t.matchSnapshot(printTree(tree))
t.strictSame(checkLogs(), [])
})
t.test('inflate an ancient lockfile with a dep gone missing', async t => {
const checkLogs = warningTracker()
const path = resolve(fixtures, 'ancient-lockfile-invalid')
const arb = new Arborist({ path, ...OPT })
const tree = await arb.buildIdealTree()
t.matchSnapshot(printTree(tree))
t.match(checkLogs(), [
[
'warn',
'ancient lockfile',
`
The package-lock.json file was created with an old version of npm,
so supplemental metadata must be fetched from the registry.
This is a one-time fix-up, please be patient...
`,
],
[
'warn',
'ancient lockfile',
'Could not fetch metadata for @isaacs/this-does-not-exist-at-all@1.2.3',
{ code: 'E404', method: 'GET' },
],
])
})
t.test('complete build for project with old lockfile', async t => {
const checkLogs = warningTracker()
const path = resolve(fixtures, 'link-dep-empty')
const arb = new Arborist({ path, ...OPT })