Skip to content

Commit

Permalink
Close #531 PR: Support NODE_PATH in forked process. Fixes #515
Browse files Browse the repository at this point in the history
  • Loading branch information
ingro authored and sindresorhus committed Feb 16, 2016
1 parent e50f3de commit 0675d34
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
18 changes: 17 additions & 1 deletion lib/fork.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ var debug = require('debug')('ava');
var AvaError = require('./ava-error');
var send = require('./send');

var env = process.env;

if (process.env.NODE_PATH) {
var nodePath = process.env.NODE_PATH
.split(path.delimiter)
.map(function (p) {
return path.resolve(p);
})
.join(path.delimiter);

env = objectAssign({
NODE_PATH: nodePath
}, env);
}

module.exports = function (file, opts) {
opts = objectAssign({
file: file,
Expand All @@ -18,7 +33,8 @@ module.exports = function (file, opts) {

var ps = childProcess.fork(path.join(__dirname, 'test-worker.js'), [JSON.stringify(opts)], {
cwd: path.dirname(file),
silent: true
silent: true,
env: env
});

var relFile = path.relative('.', file);
Expand Down
31 changes: 21 additions & 10 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ var arrify = require('arrify');
var touch = require('touch');
var cliPath = path.join(__dirname, '../cli.js');

function execCli(args, dirname, cb) {
if (typeof dirname === 'function') {
cb = dirname;
function execCli(args, opts, cb) {
var dirname;
var env;

if (typeof opts === 'function') {
cb = opts;
dirname = __dirname;
env = {};
} else {
dirname = path.join(__dirname, dirname);
dirname = path.join(__dirname, opts.dirname ? opts.dirname : '');
env = opts.env || {};
}

var env = {};

if (process.env.AVA_APPVEYOR) {
env.AVA_APPVEYOR = 1;
}
Expand Down Expand Up @@ -95,21 +98,21 @@ test('log failed tests', function (t) {
});

test('pkg-conf: defaults', function (t) {
execCli([], 'fixture/pkg-conf/defaults', function (err) {
execCli([], {dirname: 'fixture/pkg-conf/defaults'}, function (err) {
t.ifError(err);
t.end();
});
});

test('pkg-conf: pkg-overrides', function (t) {
execCli([], 'fixture/pkg-conf/pkg-overrides', function (err) {
execCli([], {dirname: 'fixture/pkg-conf/pkg-overrides'}, function (err) {
t.ifError(err);
t.end();
});
});

test('pkg-conf: cli takes precedence', function (t) {
execCli(['--no-serial', '--cache', '--no-fail-fast', '--require=./required.js', 'c.js'], 'fixture/pkg-conf/precedence', function (err) {
execCli(['--no-serial', '--cache', '--no-fail-fast', '--require=./required.js', 'c.js'], {dirname: 'fixture/pkg-conf/precedence'}, function (err) {
t.ifError(err);
t.end();
});
Expand All @@ -124,7 +127,7 @@ test('watcher works', function (t) {
hasChokidar = true;
} catch (err) {}

var child = execCli(['--verbose', '--watch', 'test.js'], 'fixture/watcher', function (err, stdout) {
var child = execCli(['--verbose', '--watch', 'test.js'], {dirname: 'fixture/watcher'}, function (err, stdout) {
if (err && err.code === 1 && !hasChokidar) {
t.comment('chokidar dependency is missing, cannot test watcher');
t.match(stdout, 'The optional dependency chokidar failed to install and is required for --watch. Chokidar is likely not supported on your platform.');
Expand Down Expand Up @@ -155,3 +158,11 @@ test('watcher works', function (t) {
}
});
});

test('handles NODE_PATH', function (t) {
var nodePaths = 'node-paths/modules' + path.delimiter + 'node-paths/deep/nested';
execCli('fixture/node-paths.js', {env: {NODE_PATH: nodePaths}}, function (err) {
t.notOk(err);
t.end();
});
});
9 changes: 9 additions & 0 deletions test/fixture/node-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import test from '../../';

import foo from 'nested/foo';
import bar from 'path/bar';

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

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

0 comments on commit 0675d34

Please sign in to comment.