11var test = require ( 'tape' ) ;
22var path = require ( 'path' ) ;
3+ var parse = path . parse || require ( 'path-parse' ) ;
4+ var keys = require ( 'object-keys' ) ;
35
4- var getAllNodeModulePathsUpwards = function ( start , moduleDirectory ) {
5- if ( ! moduleDirectory ) { moduleDirectory = 'node_modules' ; }
6- var currentPath = start ;
7- var expectedDirs = [ '/' + path . join ( currentPath , moduleDirectory ) ] ;
8- while ( currentPath && currentPath !== '/' ) {
9- currentPath = path . join ( currentPath , '../' ) ;
10- expectedDirs . push ( '/' + path . join ( currentPath , moduleDirectory ) ) ;
6+ var nodeModulesPaths = require ( '../lib/node-modules-paths' ) ;
7+
8+ var verifyDirs = function verifyDirs ( t , start , dirs , moduleDirectories , paths ) {
9+ moduleDirectories = [ ] . concat ( moduleDirectories || 'node_modules' ) ;
10+ if ( ! paths ) { paths = [ ] ; }
11+
12+ var foundModuleDirs = { } ;
13+ var uniqueDirs = { } ;
14+ var parsedDirs = { } ;
15+ for ( var i = 0 ; i < dirs . length ; ++ i ) {
16+ var parsed = parse ( dirs [ i ] ) ;
17+ if ( ! foundModuleDirs [ parsed . base ] ) { foundModuleDirs [ parsed . base ] = 0 ; }
18+ foundModuleDirs [ parsed . base ] += 1 ;
19+ parsedDirs [ parsed . dir ] = true ;
20+ uniqueDirs [ dirs [ i ] ] = true ;
21+ }
22+ t . equal ( keys ( parsedDirs ) . length >= start . split ( path . sep ) . length , true , 'there are >= dirs than "start" has' ) ;
23+ var foundModuleDirNames = keys ( foundModuleDirs ) ;
24+ t . deepEqual ( foundModuleDirNames , moduleDirectories . concat ( paths ) , 'all desired module dirs were found' ) ;
25+ t . equal ( keys ( uniqueDirs ) . length , dirs . length , 'all dirs provided were unique' ) ;
26+
27+ var counts = { } ;
28+ for ( var j = 0 ; j < foundModuleDirNames . length ; ++ j ) {
29+ counts [ foundModuleDirs [ j ] ] = true ;
1130 }
12- return expectedDirs ;
31+ t . equal ( keys ( counts ) . length , 1 , 'all found module directories had the same count' ) ;
1332} ;
1433
15- var nodeModulesPaths = require ( '../lib/node-modules-paths' ) ;
16-
1734test ( 'node-modules-paths' , function ( t ) {
1835 t . test ( 'no options' , function ( t ) {
1936 var start = path . join ( __dirname , 'resolver' ) ;
2037 var dirs = nodeModulesPaths ( start ) ;
2138
22- var expectedDirs = getAllNodeModulePathsUpwards ( start ) ;
23-
24- t . deepEqual ( dirs , expectedDirs ) ;
39+ verifyDirs ( t , start , dirs ) ;
2540
2641 t . end ( ) ;
2742 } ) ;
@@ -30,9 +45,7 @@ test('node-modules-paths', function (t) {
3045 var start = path . join ( __dirname , 'resolver' ) ;
3146 var dirs = nodeModulesPaths ( start , { } ) ;
3247
33- var expectedDirs = getAllNodeModulePathsUpwards ( start ) ;
34-
35- t . deepEqual ( dirs , expectedDirs ) ;
48+ verifyDirs ( t , start , dirs ) ;
3649
3750 t . end ( ) ;
3851 } ) ;
@@ -42,34 +55,39 @@ test('node-modules-paths', function (t) {
4255 var paths = [ 'a' , 'b' ] ;
4356 var dirs = nodeModulesPaths ( start , { paths : paths } ) ;
4457
45- var expectedDirs = getAllNodeModulePathsUpwards ( start ) ;
46-
47- t . deepEqual ( dirs , expectedDirs . concat ( paths ) ) ;
58+ verifyDirs ( t , start , dirs , null , paths ) ;
4859
4960 t . end ( ) ;
5061 } ) ;
5162
5263 t . test ( 'with moduleDirectory option' , function ( t ) {
5364 var start = path . join ( __dirname , 'resolver' ) ;
54- var paths = [ 'a' , 'b' ] ;
55- var dirs = nodeModulesPaths ( start , { paths : paths } ) ;
56-
57- var expectedDirs = getAllNodeModulePathsUpwards ( start ) ;
65+ var moduleDirectory = 'not node modules' ;
66+ var dirs = nodeModulesPaths ( start , { moduleDirectory : moduleDirectory } ) ;
5867
59- t . deepEqual ( dirs , expectedDirs . concat ( paths ) ) ;
68+ verifyDirs ( t , start , dirs , moduleDirectory ) ;
6069
6170 t . end ( ) ;
6271 } ) ;
6372
64- t . test ( 'with moduleDirectory and paths options' , function ( t ) {
73+ t . test ( 'with 1 moduleDirectory and paths options' , function ( t ) {
6574 var start = path . join ( __dirname , 'resolver' ) ;
6675 var paths = [ 'a' , 'b' ] ;
6776 var moduleDirectory = 'not node modules' ;
6877 var dirs = nodeModulesPaths ( start , { paths : paths , moduleDirectory : moduleDirectory } ) ;
6978
70- var expectedDirs = getAllNodeModulePathsUpwards ( start , moduleDirectory ) ;
79+ verifyDirs ( t , start , dirs , moduleDirectory , paths ) ;
80+
81+ t . end ( ) ;
82+ } ) ;
83+
84+ t . test ( 'with 1+ moduleDirectory and paths options' , function ( t ) {
85+ var start = path . join ( __dirname , 'resolver' ) ;
86+ var paths = [ 'a' , 'b' ] ;
87+ var moduleDirectories = [ 'not node modules' , 'other modules' ] ;
88+ var dirs = nodeModulesPaths ( start , { paths : paths , moduleDirectory : moduleDirectories } ) ;
7189
72- t . deepEqual ( dirs , expectedDirs . concat ( paths ) ) ;
90+ verifyDirs ( t , start , dirs , moduleDirectories , paths ) ;
7391
7492 t . end ( ) ;
7593 } ) ;
0 commit comments