Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1dc6b38

Browse filesBrowse files
richardlauMylesBorins
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 07b92a3 commit 1dc6b38
Copy full SHA for 1dc6b38

File tree

Expand file treeCollapse file tree

8 files changed

+109
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

8 files changed

+109
-0
lines changed
Open diff view settings
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.string = '$HOME/.node_libraries';
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/test-module-loading-globalpaths/home-pkg-in-both/.node_modules/foo.js
+1Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.string = '$HOME/.node_libraries';
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/test-module-loading-globalpaths/home-pkg-in-node_modules/.node_modules/foo.js
+1Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/test-module-loading-globalpaths/local-pkg/node_modules/foo.js
+1Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file
+2Lines 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);
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.string = '$NODE_PATH';
Collapse file
+101Lines changed: 101 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.