Skip to content

Commit aaf8536

Browse files
richardlauItalo A. Casas
authored andcommitted
test: add test for loading from global folders
Test executes with a copy of the node executable since $PREFIX/lib/node is relative to the executable location. PR-URL: #9283 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com> Reviewed-By: João Reis <reis@janeasystems.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent c01c7a4 commit aaf8536

8 files changed

Lines changed: 109 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.string='$HOME/.node_libraries';

‎test/fixtures/test-module-loading-globalpaths/home-pkg-in-both/.node_modules/foo.js‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.string='$HOME/.node_libraries';

‎test/fixtures/test-module-loading-globalpaths/home-pkg-in-node_modules/.node_modules/foo.js‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/fixtures/test-module-loading-globalpaths/local-pkg/node_modules/foo.js‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict';
2+
console.log(require('foo').string);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.string='$NODE_PATH';
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
constpath=require('path');
5+
constfs=require('fs');
6+
constchild_process=require('child_process');
7+
constpkgName='foo';
8+
9+
if(process.argv[2]==='child'){
10+
console.log(require(pkgName).string);
11+
}else{
12+
common.refreshTmpDir();
13+
14+
// Copy node binary into a test $PREFIX directory.
15+
constprefixPath=path.join(common.tmpDir,'install');
16+
fs.mkdirSync(prefixPath);
17+
lettestExecPath;
18+
if(common.isWindows){
19+
testExecPath=path.join(prefixPath,path.basename(process.execPath));
20+
}else{
21+
constprefixBinPath=path.join(prefixPath,'bin');
22+
fs.mkdirSync(prefixBinPath);
23+
testExecPath=path.join(prefixBinPath,path.basename(process.execPath));
24+
}
25+
constmode=fs.statSync(process.execPath).mode;
26+
fs.writeFileSync(testExecPath,fs.readFileSync(process.execPath));
27+
fs.chmodSync(testExecPath,mode);
28+
29+
construnTest=(expectedString,env)=>{
30+
constchild=child_process.execFileSync(testExecPath,
31+
[__filename,'child'],
32+
{encoding: 'utf8',env: env});
33+
assert.strictEqual(child.trim(),expectedString);
34+
};
35+
36+
consttestFixturesDir=path.join(common.fixturesDir,
37+
path.basename(__filename,'.js'));
38+
39+
constenv=Object.assign({},process.env);
40+
// Turn on module debug to aid diagnosing failures.
41+
env['NODE_DEBUG']='module';
42+
// Unset NODE_PATH.
43+
deleteenv['NODE_PATH'];
44+
45+
// Test empty global path.
46+
constnoPkgHomeDir=path.join(common.tmpDir,'home-no-pkg');
47+
fs.mkdirSync(noPkgHomeDir);
48+
env['HOME']=env['USERPROFILE']=noPkgHomeDir;
49+
assert.throws(
50+
()=>{
51+
child_process.execFileSync(testExecPath,[__filename,'child'],
52+
{encoding: 'utf8',env: env});
53+
},
54+
newRegExp('Cannot find module \''+pkgName+'\''));
55+
56+
// Test module in $HOME/.node_modules.
57+
constmodHomeDir=path.join(testFixturesDir,'home-pkg-in-node_modules');
58+
env['HOME']=env['USERPROFILE']=modHomeDir;
59+
runTest('$HOME/.node_modules',env);
60+
61+
// Test module in $HOME/.node_libraries.
62+
constlibHomeDir=path.join(testFixturesDir,'home-pkg-in-node_libraries');
63+
env['HOME']=env['USERPROFILE']=libHomeDir;
64+
runTest('$HOME/.node_libraries',env);
65+
66+
// Test module both $HOME/.node_modules and $HOME/.node_libraries.
67+
constbothHomeDir=path.join(testFixturesDir,'home-pkg-in-both');
68+
env['HOME']=env['USERPROFILE']=bothHomeDir;
69+
runTest('$HOME/.node_modules',env);
70+
71+
// Test module in $PREFIX/lib/node.
72+
// Write module into $PREFIX/lib/node.
73+
constexpectedString='$PREFIX/lib/node';
74+
constprefixLibPath=path.join(prefixPath,'lib');
75+
fs.mkdirSync(prefixLibPath);
76+
constprefixLibNodePath=path.join(prefixLibPath,'node');
77+
fs.mkdirSync(prefixLibNodePath);
78+
constpkgPath=path.join(prefixLibNodePath,pkgName+'.js');
79+
fs.writeFileSync(pkgPath,'exports.string = \''+expectedString+'\';');
80+
81+
env['HOME']=env['USERPROFILE']=noPkgHomeDir;
82+
runTest(expectedString,env);
83+
84+
// Test module in all global folders.
85+
env['HOME']=env['USERPROFILE']=bothHomeDir;
86+
runTest('$HOME/.node_modules',env);
87+
88+
// Test module in NODE_PATH is loaded ahead of global folders.
89+
env['HOME']=env['USERPROFILE']=bothHomeDir;
90+
env['NODE_PATH']=path.join(testFixturesDir,'node_path');
91+
runTest('$NODE_PATH',env);
92+
93+
// Test module in local folder is loaded ahead of global folders.
94+
constlocalDir=path.join(testFixturesDir,'local-pkg');
95+
env['HOME']=env['USERPROFILE']=bothHomeDir;
96+
env['NODE_PATH']=path.join(testFixturesDir,'node_path');
97+
constchild=child_process.execFileSync(testExecPath,
98+
[path.join(localDir,'test.js')],
99+
{encoding: 'utf8',env: env});
100+
assert.strictEqual(child.trim(),'local');
101+
}

0 commit comments

Comments
 (0)