Skip to content

Commit 58b99a3

Browse files
committed
[Tests] [Refactor] refactor node-modules-paths and add tests.
Add `path-parse` since `node` <= 0.10 doesn’t have `path.parse`
1 parent ddca9ed commit 58b99a3

File tree

4 files changed

+64
-48
lines changed

4 files changed

+64
-48
lines changed

lib/node-modules-paths.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var path = require('path');
2+
var parse = path.parse || require('path-parse');
23

34
module.exports = function nodeModulesPaths(start, opts) {
45
var modules = opts && opts.moduleDirectory
@@ -17,22 +18,18 @@ module.exports = function nodeModulesPaths(start, opts) {
1718
prefix = '\\\\';
1819
}
1920

20-
var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;
21-
22-
var parts = start.split(splitRe);
21+
var paths = [start];
22+
var parsed = parse(start);
23+
while (parsed.dir !== paths[paths.length - 1]) {
24+
paths.push(parsed.dir);
25+
parsed = parse(parsed.dir);
26+
}
2327

24-
var dirs = [];
25-
for (var i = parts.length - 1; i >= 0; i--) {
26-
if (modules.indexOf(parts[i]) !== -1) continue;
27-
dirs = dirs.concat(modules.map(function (module_dir) {
28-
return prefix + path.join(prefix,
29-
path.join.apply(path, parts.slice(0, i + 1)),
30-
module_dir
31-
);
28+
var dirs = paths.reduce(function (dirs, aPath) {
29+
return dirs.concat(modules.map(function (moduleDir) {
30+
return path.join(prefix, aPath, moduleDir);
3231
}));
33-
}
34-
if (process.platform === 'win32') {
35-
dirs[dirs.length - 1] = dirs[dirs.length - 1].replace(':', ':\\');
36-
}
32+
}, []);
33+
3734
return opts && opts.paths ? dirs.concat(opts.paths) : dirs;
3835
};

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@
1919
"test": "npm run --silent tests-only"
2020
},
2121
"devDependencies": {
22-
"tape": "^4.6.3",
22+
"object-keys": "^1.0.11",
23+
"safe-publish-latest": "^1.1.1",
2324
"tap": "0.4.13",
24-
"safe-publish-latest": "^1.1.1"
25+
"tape": "^4.6.3"
2526
},
2627
"license": "MIT",
2728
"author": {
2829
"name": "James Halliday",
2930
"email": "[email protected]",
3031
"url": "http://substack.net"
32+
},
33+
"dependencies": {
34+
"path-parse": "^1.0.5"
3135
}
3236
}

test/faulty_basedir.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
var test = require('tape');
22
var resolve = require('../');
33

4-
// not sure what's up with this test anymore
5-
if (process.platform !== 'win32') return;
6-
7-
test('faulty basedir must produce error in windows', function (t) {
4+
test('faulty basedir must produce error in windows', { skip: process.platform !== 'win32' }, function (t) {
85
t.plan(1);
96

107
var resolverDir = 'C:\\a\\b\\c\\d';

test/node-modules-paths.js

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
11
var test = require('tape');
22
var 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-
1734
test('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

Comments
 (0)