Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support NODE_PATH in forked process #531

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, discovered this too late. This makes no sense. Here you overwrite the modified NODE_PATH with the original one. This means the tests are also invalid. Tests still pass when the .map is commented out.

// @ingro @novemberborn

}

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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use path.join() instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sindresorhus do you mean path.join('node-paths', 'modules')? path.delimiter is used for building the environment variable itself, which comprises multiple paths.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nvm, I read it as path.sep...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Node probably handles any platform issues with the slashes in node-paths/modules, so no need to change anything then.

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';
};