From 928b4d81ee8cda27f15252c14bbda80a6e2dc997 Mon Sep 17 00:00:00 2001 From: Adam Meadows Date: Tue, 10 Jun 2014 07:07:20 -0700 Subject: [PATCH 1/3] Fix for Issue #39 regarding `NODE_PATH` * Added tests to `test/node_path.js` to ensure `$NODE_PATH` environment variable is respected when searching for a module. * Updated the `node-modules-paths` module to include the directories specified by `process.env.NODE_PATH`. --- lib/node-modules-paths.js | 8 +++- test/node_path.js | 79 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/lib/node-modules-paths.js b/lib/node-modules-paths.js index 7c58e106..8888117d 100644 --- a/lib/node-modules-paths.js +++ b/lib/node-modules-paths.js @@ -20,6 +20,8 @@ module.exports = function (start, opts) { var parts = start.split(splitRe); var dirs = []; + var envDirs = []; + var envDelimeter = ':'; for (var i = parts.length - 1; i >= 0; i--) { if (modules.indexOf(parts[i]) !== -1) continue; dirs = dirs.concat(modules.map(function(module_dir) { @@ -31,6 +33,10 @@ module.exports = function (start, opts) { } if (process.platform === 'win32'){ dirs[dirs.length-1] = dirs[dirs.length-1].replace(":", ":\\"); + envDelimeter = ';'; } - return dirs.concat(opts.paths); + if (process.env.NODE_PATH) { + envDirs = process.env.NODE_PATH.split(envDelimeter); + } + return dirs.concat(opts.paths, envDirs); } diff --git a/test/node_path.js b/test/node_path.js index 1472bdb7..7b1f8a06 100644 --- a/test/node_path.js +++ b/test/node_path.js @@ -2,9 +2,10 @@ var path = require('path'); var test = require('tap').test; var resolve = require('../'); -test('$NODE_PATH', function (t) { - t.plan(3); - +// Ensure that the `opts.paths` paths are searched +test('opts.paths', function (t) { + t.plan(4); + resolve('aaa', { paths: [ __dirname + '/node_path/x', @@ -14,7 +15,7 @@ test('$NODE_PATH', function (t) { }, function (err, res) { t.equal(res, __dirname + '/node_path/x/aaa/index.js'); }); - + resolve('bbb', { paths: [ __dirname + '/node_path/x', @@ -24,7 +25,7 @@ test('$NODE_PATH', function (t) { }, function (err, res) { t.equal(res, __dirname + '/node_path/y/bbb/index.js'); }); - + resolve('ccc', { paths: [ __dirname + '/node_path/x', @@ -46,3 +47,71 @@ test('$NODE_PATH', function (t) { t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap/lib/main.js')); }); }); + +// Ensure that the `NODE_PATH` environment is searched +test('$NODE_PATH async', function (t) { + t.plan(4); + + // Construct the NODE_PATH environment variable + var nodePaths = [ + __dirname + '/node_path/x', + __dirname + '/node_path/y' + ]; + + var delimeter = process.platform === 'win32' ? ';' : ':'; + process.env.NODE_PATH = nodePaths.join(delimeter); + + resolve('aaa', { + }, function (err, res) { + t.equal(res, __dirname + '/node_path/x/aaa/index.js'); + }); + + resolve('bbb', { + }, function (err, res) { + t.equal(res, __dirname + '/node_path/y/bbb/index.js'); + }); + + resolve('ccc', { + }, function (err, res) { + t.equal(res, __dirname + '/node_path/x/ccc/index.js'); + }); + + // ensure that relative paths still resolve against the + // regular `node_modules` correctly + resolve('tap', { + paths: [ + 'node_path', + ], + basedir: 'node_path/x', + }, function (err, res) { + t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap/lib/main.js')); + }); +}); + +// Ensure that the `NODE_PATH` environment is searched in sync +test('$NODE_PATH sync', function (t) { + t.plan(4); + + // Construct the NODE_PATH environment variable + var nodePaths = [ + __dirname + '/node_path/x', + __dirname + '/node_path/y' + ]; + + var delimeter = process.platform === 'win32' ? ';' : ':'; + process.env.NODE_PATH = nodePaths.join(delimeter); + + var res = resolve.sync('aaa'); + t.equal(res, __dirname + '/node_path/x/aaa/index.js'); + + res = resolve.sync('bbb'); + t.equal(res, __dirname + '/node_path/y/bbb/index.js'); + + res = resolve.sync('ccc'); + t.equal(res, __dirname + '/node_path/x/ccc/index.js'); + + // ensure that relative paths still resolve against the + // regular `node_modules` correctly + res = resolve.sync('tap'); + t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap/lib/main.js')); +}); From ab06633af8009faec98c0548198cd6c66a5f7691 Mon Sep 17 00:00:00 2001 From: Adam Meadows Date: Tue, 10 Jun 2014 07:15:04 -0700 Subject: [PATCH 2/3] bumping the version number --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7165b233..19d7ee41 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name" : "resolve", "description" : "resolve like require.resolve() on behalf of files asynchronously and synchronously", - "version" : "0.7.1", + "version" : "0.7.2", "repository" : { "type" : "git", "url" : "git://github.com/substack/node-resolve.git" From 98eb532781874d6aefb3bf21241a5617716c1063 Mon Sep 17 00:00:00 2001 From: Adam Meadows Date: Tue, 10 Jun 2014 11:31:48 -0700 Subject: [PATCH 3/3] Removing version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 19d7ee41..7165b233 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name" : "resolve", "description" : "resolve like require.resolve() on behalf of files asynchronously and synchronously", - "version" : "0.7.2", + "version" : "0.7.1", "repository" : { "type" : "git", "url" : "git://github.com/substack/node-resolve.git"