Skip to content

Commit aaf8536

Browse files
richardlauitaloacasas
authored andcommittedMar 13, 2017
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 <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: João Reis <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent c01c7a4 commit aaf8536

File tree

8 files changed

+109
-0
lines changed

8 files changed

+109
-0
lines changed
 
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

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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

+1
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

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict';
2+
console.log(require('foo').string);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.string = '$NODE_PATH';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const path = require('path');
5+
const fs = require('fs');
6+
const child_process = require('child_process');
7+
const pkgName = '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+
const prefixPath = path.join(common.tmpDir, 'install');
16+
fs.mkdirSync(prefixPath);
17+
let testExecPath;
18+
if (common.isWindows) {
19+
testExecPath = path.join(prefixPath, path.basename(process.execPath));
20+
} else {
21+
const prefixBinPath = path.join(prefixPath, 'bin');
22+
fs.mkdirSync(prefixBinPath);
23+
testExecPath = path.join(prefixBinPath, path.basename(process.execPath));
24+
}
25+
const mode = fs.statSync(process.execPath).mode;
26+
fs.writeFileSync(testExecPath, fs.readFileSync(process.execPath));
27+
fs.chmodSync(testExecPath, mode);
28+
29+
const runTest = (expectedString, env) => {
30+
const child = child_process.execFileSync(testExecPath,
31+
[ __filename, 'child' ],
32+
{ encoding: 'utf8', env: env });
33+
assert.strictEqual(child.trim(), expectedString);
34+
};
35+
36+
const testFixturesDir = path.join(common.fixturesDir,
37+
path.basename(__filename, '.js'));
38+
39+
const env = Object.assign({}, process.env);
40+
// Turn on module debug to aid diagnosing failures.
41+
env['NODE_DEBUG'] = 'module';
42+
// Unset NODE_PATH.
43+
delete env['NODE_PATH'];
44+
45+
// Test empty global path.
46+
const noPkgHomeDir = 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+
new RegExp('Cannot find module \'' + pkgName + '\''));
55+
56+
// Test module in $HOME/.node_modules.
57+
const modHomeDir = 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+
const libHomeDir = 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+
const bothHomeDir = 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+
const expectedString = '$PREFIX/lib/node';
74+
const prefixLibPath = path.join(prefixPath, 'lib');
75+
fs.mkdirSync(prefixLibPath);
76+
const prefixLibNodePath = path.join(prefixLibPath, 'node');
77+
fs.mkdirSync(prefixLibNodePath);
78+
const pkgPath = 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+
const localDir = path.join(testFixturesDir, 'local-pkg');
95+
env['HOME'] = env['USERPROFILE'] = bothHomeDir;
96+
env['NODE_PATH'] = path.join(testFixturesDir, 'node_path');
97+
const child = 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)
Please sign in to comment.