11const t = require ( 'tap' )
22const { load : loadMockNpm } = require ( '../../fixtures/mock-npm' )
3+ const path = require ( 'path' )
4+ const fs = require ( 'fs' )
5+
6+ const MockRegistry = require ( '../../fixtures/mock-registry.js' )
37
48t . test ( 'should throw in global mode' , async ( t ) => {
59 const { npm } = await loadMockNpm ( t , {
@@ -14,45 +18,78 @@ t.test('should throw in global mode', async (t) => {
1418 )
1519} )
1620
17- t . test ( 'should remove dupes using Arborist' , async ( t ) => {
18- t . plan ( 5 )
19- const { npm } = await loadMockNpm ( t , {
20- mocks : {
21- '@npmcli/arborist' : function ( args ) {
22- t . ok ( args , 'gets options object' )
23- t . ok ( args . path , 'gets path option' )
24- t . ok ( args . dryRun , 'gets dryRun from user' )
25- this . dedupe = ( ) => {
26- t . ok ( true , 'dedupe is called' )
27- }
28- } ,
29- '../../lib/utils/reify-finish.js' : ( npm , arb ) => {
30- t . ok ( arb , 'gets arborist tree' )
21+ const testTop = {
22+ name : 'test-top' ,
23+ version : '1.0.0' ,
24+ dependencies : {
25+ 'test-dep-a' : '*' ,
26+ 'test-dep-b' : '*' ,
27+ } ,
28+ }
29+ const testDepA = {
30+ name : 'test-dep-a' ,
31+ version : '1.0.1' ,
32+ dependencies : { 'test-sub' : '*' } ,
33+ }
34+ const testDepB = {
35+ name : 'test-dep-b' ,
36+ version : '1.0.0' ,
37+ dependencies : { 'test-sub' : '*' } ,
38+ }
39+ const testSub = {
40+ name : 'test-sub' ,
41+ version : '1.0.0' ,
42+ }
43+
44+ const treeWithDupes = {
45+ 'package.json' : JSON . stringify ( testTop ) ,
46+ node_modules : {
47+ 'test-dep-a' : {
48+ 'package.json' : JSON . stringify ( testDepA ) ,
49+ node_modules : {
50+ 'test-sub' : {
51+ 'package.json' : JSON . stringify ( testSub ) ,
52+ } ,
3153 } ,
3254 } ,
33- config : {
34- 'dry-run' : 'true' ,
55+ 'test-dep-b' : {
56+ 'package.json' : JSON . stringify ( testDepB ) ,
57+ node_modules : {
58+ 'test-sub' : {
59+ 'package.json' : JSON . stringify ( testSub ) ,
60+ } ,
61+ } ,
3562 } ,
63+ } ,
64+ }
65+
66+ t . test ( 'dedupe' , async ( t ) => {
67+ const { npm, joinedOutput } = await loadMockNpm ( t , {
68+ prefixDir : treeWithDupes ,
69+ } )
70+ const registry = new MockRegistry ( {
71+ tap : t ,
72+ registry : npm . config . get ( 'registry' ) ,
73+ } )
74+ const manifestSub = registry . manifest ( {
75+ name : 'test-sub' ,
76+ packuments : [ { version : '1.0.0' } ] ,
3677 } )
37- await npm . exec ( 'dedupe' , [ ] )
38- } )
3978
40- t . test ( 'should remove dupes using Arborist - no arguments' , async ( t ) => {
41- t . plan ( 2 )
42- const { npm } = await loadMockNpm ( t , {
43- mocks : {
44- '@npmcli/arborist' : function ( args ) {
45- t . ok ( args . dryRun , 'gets dryRun from config' )
46- t . ok ( args . save , 'gets user-set save value from config' )
47- this . dedupe = ( ) => { }
48- } ,
49- '../../lib/utils/reify-output.js' : ( ) => { } ,
50- '../../lib/utils/reify-finish.js' : ( ) => { } ,
51- } ,
52- config : {
53- 'dry-run' : true ,
54- save : true ,
79+ await registry . package ( {
80+ manifest : manifestSub ,
81+ tarballs : {
82+ '1.0.0' : path . join ( npm . prefix , 'node_modules' , 'test-dep-a' , 'node_modules' , 'test-sub' ) ,
5583 } ,
5684 } )
5785 await npm . exec ( 'dedupe' , [ ] )
86+ t . match ( joinedOutput ( ) , / a d d e d 1 p a c k a g e , a n d r e m o v e d 2 p a c k a g e s / )
87+ t . ok ( fs . existsSync ( path . join ( npm . prefix , 'node_modules' , 'test-sub' ) ) , 'test-sub was hoisted' )
88+ t . notOk (
89+ fs . existsSync ( path . join ( npm . prefix , 'node_modules' , 'test-dep-a' , 'node_modules' , 'test-sub' ) ) ,
90+ 'test-dep-a/test-sub was removed'
91+ )
92+ t . notOk (
93+ fs . existsSync ( path . join ( npm . prefix , 'node_modules' , 'test-dep-b' , 'node_modules' , 'test-sub' ) ) ,
94+ 'test-dep-b/test-sub was removed' )
5895} )
0 commit comments