Skip to content

Commit

Permalink
Changed options name and added test to support NODE_PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
ingro committed Feb 9, 2016
1 parent d37a356 commit 082708b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 13 deletions.
15 changes: 9 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ if (debug.enabled) {
require('time-require');
}

var path = require('path');
var updateNotifier = require('update-notifier');
var figures = require('figures');
var arrify = require('arrify');
Expand Down Expand Up @@ -93,20 +94,22 @@ if (cli.flags.init) {
return;
}

var additionalPaths = [];
var nodePaths;
if (process.env.NODE_PATH) {
var osSplitChar = process.platform === 'win32' ? ';' : ':';
process.env.NODE_PATH.split(osSplitChar).forEach(function (additionalPath) {
additionalPaths.push(path.resolve(opts.projectRoot, additionalPath));
});
var osSplitChar = process.platform === 'win32' ? ';' : ':';
nodePaths = process.env.NODE_PATH.split(osSplitChar).map(function (p) {
return path.resolve(process.cwd(), p)
});
} else {
nodePaths = []
}

var api = new Api(cli.input.length ? cli.input : arrify(conf.files), {
failFast: cli.flags.failFast,
serial: cli.flags.serial,
require: arrify(cli.flags.require),
cacheEnabled: cli.flags.cache !== false,
additionalPaths: additionalPaths
nodePaths: nodePaths
});

var logger = new Logger();
Expand Down
3 changes: 1 addition & 2 deletions lib/fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ module.exports = function (file, opts) {
tty: process.stdout.isTTY ? {
columns: process.stdout.columns,
rows: process.stdout.rows
} : false,
additionalPaths: opts.additionalPaths
} : false
}, opts);

var ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), [JSON.stringify(opts)], {
Expand Down
6 changes: 2 additions & 4 deletions lib/test-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ var oldNodeModulesPaths = module.constructor._nodeModulePaths;
module.constructor._nodeModulePaths = function () {
var ret = oldNodeModulesPaths.apply(this, arguments);
ret.push(nodeModulesDir);
if (opts.additionalPaths && opts.additionalPaths.length > 0) {
opts.additionalPaths.forEach(function (additionalPath) {
ret.push(additionalPath);
});
if (opts.nodePaths && opts.nodePaths.length > 0) {
ret = ret.concat(opts.nodePaths);
}
return ret;
};
Expand Down
12 changes: 11 additions & 1 deletion test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ function execCli(args, dirname, cb) {
dirname = path.join(__dirname, dirname);
}

var env = {};
var env = {
// This probably should be set only for the corresponding test
NODE_PATH: 'node-paths/modules'
};

if (process.env.AVA_APPVEYOR) {
env.AVA_APPVEYOR = 1;
Expand Down Expand Up @@ -110,3 +113,10 @@ test('pkg-conf: cli takes precedence', function (t) {
t.end();
});
});

test('handles NODE_PATH', function (t) {
execCli('fixture/node-paths.js', function (err) {
t.notOk(err);
t.end();
});
});
7 changes: 7 additions & 0 deletions test/fixture/node-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import test from '../../';

import foo from 'nested/foo';

test('relative require', t => {
t.is(foo(), 'bar');
});
3 changes: 3 additions & 0 deletions test/fixture/node-paths/modules/nested/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function() {
return 'bar';
}

0 comments on commit 082708b

Please sign in to comment.