Skip to content

Commit 179e830

Browse files
committed
Fix bug where absolute paths were not used and add tests
1 parent 96ceb4d commit 179e830

8 files changed

Lines changed: 345 additions & 3 deletions

File tree

tools/ls/module-names/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ To search from a descendant directory, set the `dir` option.
3434

3535
``` javascript
3636
var opts = {
37-
'dir': '@stdlib/math/base'
37+
'dir': './@stdlib/math/base'
3838
};
3939

4040
ls( opts, onList );
@@ -62,6 +62,15 @@ The function accepts the same `options` as `ls()` above.
6262
<!-- </usage> -->
6363

6464

65+
<!-- <notes> -->
66+
67+
## Notes
68+
69+
* The function only returns modules under the `@stdlib` scope.
70+
71+
<!-- </notes> -->
72+
73+
6574
<!-- <examples> -->
6675

6776
## Examples

tools/ls/module-names/lib/async.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ function ls() {
5252
throw new TypeError( 'invalid input argument. Callback argument must be a function. Value: `' + clbk + '`.' );
5353
}
5454
opts = {
55-
'cwd': getRoot( opts.dir || '' )
55+
'cwd': getRoot( opts.dir || '' ),
56+
'realpath': true // return absolute file paths
5657
};
5758
glob( config.pattern, opts, onGlob );
5859

tools/ls/module-names/lib/sync.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ function ls( options ) {
3434
}
3535
}
3636
opts = {
37-
'cwd': getRoot( opts.dir || '' )
37+
'cwd': getRoot( opts.dir || '' ),
38+
'realpath': true // return absolute file paths
3839
};
3940
names = glob( config.pattern, opts );
4041
return transform( names );

tools/ls/module-names/test/test.async.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// MODULES //
44

55
var tape = require( 'tape' );
6+
var proxyquire = require( 'proxyquire' );
7+
var stdlib = require( './../lib/stdlib.js' );
8+
var noop = require( stdlib+'@stdlib/utils/noop' );
9+
var isStringArray = require( stdlib+'@stdlib/utils/is-string' ).isPrimitiveStringArray;
610
var ls = require( './../lib/async.js' );
711

812

@@ -14,4 +18,113 @@ tape( 'main export is a function', function test( t ) {
1418
t.end();
1519
});
1620

21+
tape( 'the function throws an error if provided an invalid option', function test( t ) {
22+
t.throws( foo, TypeError, 'throws error' );
23+
t.end();
24+
function foo() {
25+
var opts = {
26+
'dir': null
27+
};
28+
ls( opts, noop );
29+
}
30+
});
31+
32+
tape( 'if provided a callback argument which is not a function, the function throws an error (no options)', function test( t ) {
33+
var values;
34+
var i;
35+
36+
values = [
37+
'5',
38+
5,
39+
NaN,
40+
true,
41+
null,
42+
undefined,
43+
[],
44+
{}
45+
];
46+
47+
for ( i = 0; i < values.length; i++ ) {
48+
t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] );
49+
}
50+
t.end();
51+
52+
function badValue( value ) {
53+
return function badValue() {
54+
ls( value );
55+
};
56+
}
57+
});
58+
59+
tape( 'if provided a callback argument which is not a function, the function throws an error (options)', function test( t ) {
60+
var values;
61+
var i;
62+
63+
values = [
64+
'5',
65+
5,
66+
NaN,
67+
true,
68+
null,
69+
undefined,
70+
[],
71+
{}
72+
];
73+
74+
for ( i = 0; i < values.length; i++ ) {
75+
t.throws( badValue( values[i] ), TypeError, 'throws a type error when provided '+values[i] );
76+
}
77+
t.end();
78+
79+
function badValue( value ) {
80+
return function badValue() {
81+
ls( {}, value );
82+
};
83+
}
84+
});
85+
86+
tape( 'the function returns an error to a provided callback if an error is encountered while searching a directory', function test( t ) {
87+
var ls = proxyquire( './../lib/async.js', {
88+
'glob': glob
89+
});
90+
91+
ls( clbk );
92+
93+
function glob() {
94+
var cb = arguments[ arguments.length-1 ];
95+
setTimeout( onTimeout, 0 );
96+
function onTimeout() {
97+
cb( new Error( 'beep' ) );
98+
}
99+
}
17100

101+
function clbk( error ) {
102+
t.ok( error, 'returns an error' );
103+
t.end();
104+
}
105+
});
106+
107+
tape( 'the function returns a string array', function test( t ) {
108+
ls( clbk );
109+
function clbk( error, names ) {
110+
if ( error ) {
111+
t.ok( false, error.message );
112+
}
113+
t.equal( isStringArray( names ), true, 'returns a string array' );
114+
t.end();
115+
}
116+
});
117+
118+
tape( 'the function returns a string array (dir option)', function test( t ) {
119+
var opts = {
120+
'dir': './@stdlib/math/base'
121+
};
122+
ls( opts, clbk );
123+
function clbk( error, names ) {
124+
if ( error ) {
125+
t.ok( false, error.message );
126+
}
127+
t.equal( isStringArray( names ), true, 'returns a string array' );
128+
t.end();
129+
}
130+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var tape = require( 'tape' );
6+
var path = require( 'path' );
7+
var getRoot = require( './../lib/root.js' );
8+
9+
10+
// TESTS //
11+
12+
tape( 'main export is a function', function test( t ) {
13+
t.ok( true, __filename );
14+
t.equal( typeof getRoot, 'function', 'main export is a function' );
15+
t.end();
16+
});
17+
18+
tape( 'the function returns a string', function test( t ) {
19+
t.equal( typeof getRoot( '' ), 'string', 'returns a string' );
20+
t.end();
21+
});
22+
23+
tape( 'the function resolves a relative descendant directory', function test( t ) {
24+
var dpath;
25+
var root;
26+
var dir;
27+
28+
dir = './@stdlib/math/base';
29+
30+
root = getRoot( '' );
31+
dpath = getRoot( dir );
32+
33+
t.equal( dpath, path.resolve( root, dir ), 'returns resolved descendant directory' );
34+
t.end();
35+
});
36+
37+
tape( 'the function does not allow accessing parent directories', function test( t ) {
38+
var dpath;
39+
var root;
40+
var dir;
41+
42+
dir = '../../../../';
43+
44+
root = getRoot( '' );
45+
dpath = getRoot( dir );
46+
47+
t.equal( dpath, root, 'does not return parent directory' );
48+
49+
dir = '/proc';
50+
dpath = getRoot( dir );
51+
52+
t.equal( dpath, root, 'does not return parent directory' );
53+
54+
t.end();
55+
});

tools/ls/module-names/test/test.sync.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// MODULES //
44

55
var tape = require( 'tape' );
6+
var stdlib = require( './../lib/stdlib.js' );
7+
var isStringArray = require( stdlib+'@stdlib/utils/is-string' ).isPrimitiveStringArray;
68
var ls = require( './../lib/sync.js' );
79

810

@@ -14,4 +16,36 @@ tape( 'main export is a function', function test( t ) {
1416
t.end();
1517
});
1618

19+
tape( 'the function throws an error if provided an invalid option', function test( t ) {
20+
t.throws( foo, TypeError, 'throws error' );
21+
t.end();
22+
function foo() {
23+
var opts = {
24+
'dir': null
25+
};
26+
ls( opts );
27+
}
28+
});
29+
30+
tape( 'the function returns a string array', function test( t ) {
31+
var names = ls();
32+
t.equal( isStringArray( names ), true, 'returns a string array' );
33+
t.end();
34+
});
35+
36+
tape( 'the function returns a string array (dir option)', function test( t ) {
37+
var names;
38+
var opts;
39+
40+
opts = {
41+
'dir': './@stdlib/math/base'
42+
};
43+
names = ls( opts );
44+
45+
t.equal( isStringArray( names ), true, 'returns a string array' );
46+
47+
t.end();
48+
});
49+
50+
1751

tools/ls/module-names/test/test.transform.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,46 @@ tape( 'main export is a function', function test( t ) {
1414
t.end();
1515
});
1616

17+
tape( 'the function transforms a list of `package.json` files into a list of module names', function test( t ) {
18+
var expected;
19+
var actual;
20+
var files;
1721

22+
files = [
23+
'/path/to/stdlib/lib/node_modules/@stdlib/beep/package.json',
24+
'/path/to/stdlib/lib/node_modules/@stdlib/boop/package.json',
25+
'/path/to/stdlib/lib/node_modules/@stdlib/foo/bar/package.json'
26+
];
27+
28+
expected = [
29+
'@stdlib/beep',
30+
'@stdlib/boop',
31+
'@stdlib/foo/bar'
32+
];
33+
34+
actual = transform( files );
35+
t.deepEqual( actual, expected, 'returns expected names' );
36+
37+
t.end();
38+
});
39+
40+
tape( 'the function ignores `package.json` files which are not under the `@stdlib` scope', function test( t ) {
41+
var expected;
42+
var actual;
43+
var files;
44+
45+
files = [
46+
'/path/to/stdlib/lib/node_modules/package.json',
47+
'/path/to/stdlib/lib/node_modules/boop/package.json',
48+
'/path/to/stdlib/lib/node_modules/@stdlib/foo/bar/package.json'
49+
];
50+
51+
expected = [
52+
'@stdlib/foo/bar'
53+
];
54+
55+
actual = transform( files );
56+
t.deepEqual( actual, expected, 'returns expected names' );
57+
58+
t.end();
59+
});

0 commit comments

Comments
 (0)