Skip to content

Commit ff893e4

Browse files
committed
test: add real world case for node_modules paths
Add a real world global node_modules path test case come from npm's dependency to make test more effective.
1 parent 145f276 commit ff893e4

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lib/module.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,11 @@ if (process.platform === 'win32') {
231231
var last = from.length;
232232
for (var i = from.length - 1; i >= 0; --i) {
233233
const code = from.charCodeAt(i);
234-
// use colon as a split to add driver root node_modules
235-
// it's a little more tricky than posix version to avoid parse drive name.
234+
// The path segment separator check ('\' and '/') was used to get
235+
// node_modules path for every path segment.
236+
// Use colon as an extra condition since we can get node_modules
237+
// path for dirver root like 'C:\node_modules' and don't need to
238+
// parse driver name.
236239
if (code === 92/*\*/ || code === 47/*/*/ || code === 58/*:*/) {
237240
if (p !== nmLen)
238241
paths.push(from.slice(0, last) + '\\node_modules');
@@ -280,7 +283,8 @@ if (process.platform === 'win32') {
280283
}
281284
}
282285
}
283-
// append /node_modules since this approach didn't handle root path
286+
287+
// Append /node_modules to handle root paths.
284288
paths.push('/node_modules');
285289

286290
return paths;

test/parallel/test-module-nodemodulepaths.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ const _module = require('module');
66

77
const cases = {
88
'WIN': [{
9+
file: 'C:\\Users\\hefangshi\\AppData\\Roaming\
10+
\\npm\\node_modules\\npm\\node_modules\\minimatch',
11+
expect: [
12+
'C:\\Users\\hefangshi\\AppData\\Roaming\
13+
\\npm\\node_modules\\npm\\node_modules\\minimatch\\node_modules',
14+
'C:\\Users\\hefangshi\\AppData\\Roaming\
15+
\\npm\\node_modules\\npm\\node_modules',
16+
'C:\\Users\\hefangshi\\AppData\\Roaming\\npm\\node_modules',
17+
'C:\\Users\\hefangshi\\AppData\\Roaming\\node_modules',
18+
'C:\\Users\\hefangshi\\AppData\\node_modules',
19+
'C:\\Users\\hefangshi\\node_modules',
20+
'C:\\Users\\node_modules',
21+
'C:\\node_modules'
22+
]
23+
}, {
924
file: 'C:\\Users\\Rocko Artischocko\\node_stuff\\foo',
1025
expect: [
1126
'C:\\Users\\Rocko Artischocko\\node_stuff\\foo\\node_modules',
@@ -36,6 +51,20 @@ Artischocko\\node_stuff\\foo_node_modules\\node_modules',
3651
]
3752
}],
3853
'POSIX': [{
54+
file: '/usr/lib/node_modules/npm/node_modules/\
55+
node-gyp/node_modules/glob/node_modules/minimatch',
56+
expect: [
57+
'/usr/lib/node_modules/npm/node_modules/\
58+
node-gyp/node_modules/glob/node_modules/minimatch/node_modules',
59+
'/usr/lib/node_modules/npm/node_modules/\
60+
node-gyp/node_modules/glob/node_modules',
61+
'/usr/lib/node_modules/npm/node_modules/node-gyp/node_modules',
62+
'/usr/lib/node_modules/npm/node_modules',
63+
'/usr/lib/node_modules',
64+
'/usr/node_modules',
65+
'/node_modules'
66+
]
67+
}, {
3968
file: '/usr/test/lib/node_modules/npm/foo',
4069
expect: [
4170
'/usr/test/lib/node_modules/npm/foo/node_modules',

0 commit comments

Comments
 (0)