1
1
var test = require ( 'tape' ) ;
2
2
var path = require ( 'path' ) ;
3
+ var parse = path . parse || require ( 'path-parse' ) ;
4
+ var keys = require ( 'object-keys' ) ;
3
5
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 ;
11
30
}
12
- return expectedDirs ;
31
+ t . equal ( keys ( counts ) . length , 1 , 'all found module directories had the same count' ) ;
13
32
} ;
14
33
15
- var nodeModulesPaths = require ( '../lib/node-modules-paths' ) ;
16
-
17
34
test ( 'node-modules-paths' , function ( t ) {
18
35
t . test ( 'no options' , function ( t ) {
19
36
var start = path . join ( __dirname , 'resolver' ) ;
20
37
var dirs = nodeModulesPaths ( start ) ;
21
38
22
- var expectedDirs = getAllNodeModulePathsUpwards ( start ) ;
23
-
24
- t . deepEqual ( dirs , expectedDirs ) ;
39
+ verifyDirs ( t , start , dirs ) ;
25
40
26
41
t . end ( ) ;
27
42
} ) ;
@@ -30,9 +45,7 @@ test('node-modules-paths', function (t) {
30
45
var start = path . join ( __dirname , 'resolver' ) ;
31
46
var dirs = nodeModulesPaths ( start , { } ) ;
32
47
33
- var expectedDirs = getAllNodeModulePathsUpwards ( start ) ;
34
-
35
- t . deepEqual ( dirs , expectedDirs ) ;
48
+ verifyDirs ( t , start , dirs ) ;
36
49
37
50
t . end ( ) ;
38
51
} ) ;
@@ -42,34 +55,39 @@ test('node-modules-paths', function (t) {
42
55
var paths = [ 'a' , 'b' ] ;
43
56
var dirs = nodeModulesPaths ( start , { paths : paths } ) ;
44
57
45
- var expectedDirs = getAllNodeModulePathsUpwards ( start ) ;
46
-
47
- t . deepEqual ( dirs , expectedDirs . concat ( paths ) ) ;
58
+ verifyDirs ( t , start , dirs , null , paths ) ;
48
59
49
60
t . end ( ) ;
50
61
} ) ;
51
62
52
63
t . test ( 'with moduleDirectory option' , function ( t ) {
53
64
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 } ) ;
58
67
59
- t . deepEqual ( dirs , expectedDirs . concat ( paths ) ) ;
68
+ verifyDirs ( t , start , dirs , moduleDirectory ) ;
60
69
61
70
t . end ( ) ;
62
71
} ) ;
63
72
64
- t . test ( 'with moduleDirectory and paths options' , function ( t ) {
73
+ t . test ( 'with 1 moduleDirectory and paths options' , function ( t ) {
65
74
var start = path . join ( __dirname , 'resolver' ) ;
66
75
var paths = [ 'a' , 'b' ] ;
67
76
var moduleDirectory = 'not node modules' ;
68
77
var dirs = nodeModulesPaths ( start , { paths : paths , moduleDirectory : moduleDirectory } ) ;
69
78
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 } ) ;
71
89
72
- t . deepEqual ( dirs , expectedDirs . concat ( paths ) ) ;
90
+ verifyDirs ( t , start , dirs , moduleDirectories , paths ) ;
73
91
74
92
t . end ( ) ;
75
93
} ) ;
0 commit comments