Skip to content

Commit

Permalink
refactor: process.execPath -> common.nodeBinPath
Browse files Browse the repository at this point in the history
Move process.execPath with common.nodeBinPath until we have a fix for
compatibility with electron.
  • Loading branch information
nfischer committed Oct 27, 2016
1 parent f8ff395 commit e7bd06a
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/cmd.js
Expand Up @@ -39,7 +39,7 @@ function cmdSync(cmd, args, opts, pipe) {

var proc;
try {
proc = child.spawnSync(process.execPath, [
proc = child.spawnSync(common.nodeBinPath, [
path.join(__dirname, 'child.js'),
stdoutFile,
stderrFile,
Expand Down
4 changes: 4 additions & 0 deletions src/common.js
Expand Up @@ -43,6 +43,10 @@ function log() {
}
exports.log = log;

// TODO(nate): make this compatible with electron
// The path to the NodeJS binary. This is mainly used for exec() and cmd()
exports.nodeBinPath = process.execPath;

// Shows error message. Throws if config.fatal is true
function error(msg, _code, options) {
// Validate input
Expand Down
15 changes: 8 additions & 7 deletions test/cmd.js
@@ -1,4 +1,5 @@
var shell = require('..');
var common = require('../src/common');

var assert = require('assert');
var path = require('path');
Expand Down Expand Up @@ -36,33 +37,33 @@ shell.config.fatal = oldFatal;
//

// check if stdout goes to output
result = shell.cmd(process.execPath, '-e', 'console.log(1234);');
result = shell.cmd(common.nodeBinPath, '-e', 'console.log(1234);');
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, '1234\n');

// check if stderr goes to output
result = shell.cmd(process.execPath, '-e', 'console.error(1234);');
result = shell.cmd(common.nodeBinPath, '-e', 'console.error(1234);');
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, '');
assert.equal(result.stderr, '1234\n');

// check if stdout + stderr go to output
result = shell.cmd(process.execPath, '-e', 'console.error(1234); console.log(666);');
result = shell.cmd(common.nodeBinPath, '-e', 'console.error(1234); console.log(666);');
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, '666\n');
assert.equal(result.stderr, '1234\n');

// check exit code
result = shell.cmd(process.execPath, '-e', 'process.exit(12);');
result = shell.cmd(common.nodeBinPath, '-e', 'process.exit(12);');
assert.ok(shell.error());
assert.equal(result.code, 12);

// interaction with cd
shell.cd('resources/external');
result = shell.cmd(process.execPath, 'node_script.js');
result = shell.cmd(common.nodeBinPath, 'node_script.js');
assert.ok(!shell.error());
assert.equal(result.code, 0);
assert.equal(result.stdout, 'node_script_1234\n');
Expand Down Expand Up @@ -97,14 +98,14 @@ assert.strictEqual(result.toString(), result.stdout);

// TODO(nate): make it exactly equivalent to stderr, unless stderr === ''
// shell.error() contains the stderr of external command in the case of an error
result = shell.cmd(process.execPath, '-e', 'console.error(1234); process.exit(1);');
result = shell.cmd(common.nodeBinPath, '-e', 'console.error(1234); process.exit(1);');
assert.equal(shell.error(), 'cmd: ' + result.stderr);
assert.equal(result.code, 1);
assert.equal(result.stdout, '');
assert.equal(result.stderr, '1234\n');

// option: realtimeOutput === false
result = shell.cmd(process.execPath, '-e', 'console.error(1234); console.log(5678);', {
result = shell.cmd(common.nodeBinPath, '-e', 'console.error(1234); console.log(5678);', {
realtimeOutput: false
});
assert.ok(!shell.error());
Expand Down
2 changes: 1 addition & 1 deletion test/common.js
Expand Up @@ -107,7 +107,7 @@ assert.ok(result.to);
assert.ok(result.toEnd);

// Commands that fail will still output error messages to stderr
result = shell.exec(JSON.stringify(process.execPath) + ' -e "require(\'../global\'); ls(\'noexist\'); cd(\'noexist\');"');
result = shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "require(\'../global\'); ls(\'noexist\'); cd(\'noexist\');"');
assert.equal(result.stdout, '');
assert.equal(result.stderr, 'ls: no such file or directory: noexist\ncd: no such file or directory: noexist\n');

Expand Down
4 changes: 2 additions & 2 deletions test/config.js
Expand Up @@ -29,7 +29,7 @@ shell.mkdir('-p', 'tmp');
var file = 'tmp/tempscript' + Math.random() + '.js';
var script = 'require(\'../../global.js\'); config.silent=true; config.fatal=false; cp("this_file_doesnt_exist", "."); echo("got here");';
shell.ShellString(script).to(file);
child.exec(JSON.stringify(process.execPath) + ' ' + file, function (err, stdout) {
child.exec(JSON.stringify(common.nodeBinPath) + ' ' + file, function (err, stdout) {
assert.ok(stdout.match('got here'));

//
Expand All @@ -39,7 +39,7 @@ child.exec(JSON.stringify(process.execPath) + ' ' + file, function (err, stdout)
file = 'tmp/tempscript' + Math.random() + '.js';
script = 'require(\'../../global.js\'); config.silent=true; config.fatal=true; cp("this_file_doesnt_exist", "."); echo("got here");';
shell.ShellString(script).to(file);
child.exec(JSON.stringify(process.execPath) + ' ' + file, function (err2, stdout2) {
child.exec(JSON.stringify(common.nodeBinPath) + ' ' + file, function (err2, stdout2) {
assert.ok(!stdout2.match('got here'));

shell.exit(123);
Expand Down
9 changes: 5 additions & 4 deletions test/echo.js
@@ -1,4 +1,5 @@
var shell = require('..');
var common = require('../src/common');

var assert = require('assert');
var child = require('child_process');
Expand All @@ -21,26 +22,26 @@ shell.mkdir('-p', 'tmp');
var file = 'tmp/tempscript' + Math.random() + '.js';
var script = 'require(\'../../global.js\'); echo("-asdf", "111");'; // test '-' bug (see issue #20)
shell.ShellString(script).to(file);
child.exec(JSON.stringify(process.execPath) + ' ' + file, function (err, stdout) {
child.exec(JSON.stringify(common.nodeBinPath) + ' ' + file, function (err, stdout) {
assert.equal(stdout, '-asdf 111\n');

// using null as an explicit argument doesn't crash the function
file = 'tmp/tempscript' + Math.random() + '.js';
script = 'require(\'../../global.js\'); echo(null);';
shell.ShellString(script).to(file);
child.exec(JSON.stringify(process.execPath) + ' ' + file, function (err2, stdout2, stderr2) {
child.exec(JSON.stringify(common.nodeBinPath) + ' ' + file, function (err2, stdout2, stderr2) {
assert.equal(stdout2, 'null\n');
assert.equal(stderr2, '');

// simple test with silent(true)
script = 'require(\'../../global.js\'); config.silent=true; echo(555);';
shell.ShellString(script).to(file);
child.exec(JSON.stringify(process.execPath) + ' ' + file, function (err3, stdout3) {
child.exec(JSON.stringify(common.nodeBinPath) + ' ' + file, function (err3, stdout3) {
assert.equal(stdout3, '555\n');

script = "require('../../global.js'); echo('-e', '\\tmessage');";
shell.ShellString(script).to(file);
child.exec(JSON.stringify(process.execPath) + ' ' + file, function (err4, stdout4) {
child.exec(JSON.stringify(common.nodeBinPath) + ' ' + file, function (err4, stdout4) {
assert.equal(stdout4, '\tmessage\n');

theEnd();
Expand Down
25 changes: 13 additions & 12 deletions test/exec.js
@@ -1,4 +1,5 @@
var shell = require('..');
var common = require('../src/common');

var assert = require('assert');
var util = require('util');
Expand Down Expand Up @@ -37,40 +38,40 @@ shell.config.fatal = oldFatal;
//

// check if stdout goes to output
result = shell.exec(JSON.stringify(process.execPath) + ' -e "console.log(1234);"');
result = shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "console.log(1234);"');
assert.equal(shell.error(), null);
assert.equal(result.code, 0);
assert.ok(result.stdout === '1234\n' || result.stdout === '1234\nundefined\n'); // 'undefined' for v0.4

// check if stderr goes to output
result = shell.exec(JSON.stringify(process.execPath) + ' -e "console.error(1234);"');
result = shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "console.error(1234);"');
assert.equal(shell.error(), null);
assert.equal(result.code, 0);
assert.ok(result.stdout === '' || result.stdout === 'undefined\n'); // 'undefined' for v0.4
assert.ok(result.stderr === '1234\n' || result.stderr === '1234\nundefined\n'); // 'undefined' for v0.4

// check if stdout + stderr go to output
result = shell.exec(JSON.stringify(process.execPath) + ' -e "console.error(1234); console.log(666);"');
result = shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "console.error(1234); console.log(666);"');
assert.equal(shell.error(), null);
assert.equal(result.code, 0);
assert.ok(result.stdout === '666\n' || result.stdout === '666\nundefined\n'); // 'undefined' for v0.4
assert.ok(result.stderr === '1234\n' || result.stderr === '1234\nundefined\n'); // 'undefined' for v0.4

// check exit code
result = shell.exec(JSON.stringify(process.execPath) + ' -e "process.exit(12);"');
result = shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "process.exit(12);"');
assert.ok(shell.error());
assert.equal(result.code, 12);

// interaction with cd
shell.cd('resources/external');
result = shell.exec(JSON.stringify(process.execPath) + ' node_script.js');
result = shell.exec(JSON.stringify(common.nodeBinPath) + ' node_script.js');
assert.equal(shell.error(), null);
assert.equal(result.code, 0);
assert.equal(result.stdout, 'node_script_1234\n');
shell.cd('../..');

// check quotes escaping
result = shell.exec(util.format(JSON.stringify(process.execPath) + ' -e "console.log(%s);"', "\\\"\\'+\\'_\\'+\\'\\\""));
result = shell.exec(util.format(JSON.stringify(common.nodeBinPath) + ' -e "console.log(%s);"', "\\\"\\'+\\'_\\'+\\'\\\""));
assert.equal(shell.error(), null);
assert.equal(result.code, 0);
assert.equal(result.stdout, "'+'_'+'\n");
Expand All @@ -93,11 +94,11 @@ if (process.version >= 'v0.11') { // this option doesn't work on v0.10
}

// set timeout option
result = shell.exec(JSON.stringify(process.execPath) + ' resources/exec/slow.js 100'); // default timeout is ok
result = shell.exec(JSON.stringify(common.nodeBinPath) + ' resources/exec/slow.js 100'); // default timeout is ok
assert.ok(!shell.error());
assert.equal(result.code, 0);
if (process.version >= 'v0.11') { // this option doesn't work on v0.10
result = shell.exec(JSON.stringify(process.execPath) + ' resources/exec/slow.js 100', { timeout: 10 }); // times out
result = shell.exec(JSON.stringify(common.nodeBinPath) + ' resources/exec/slow.js 100', { timeout: 10 }); // times out
assert.ok(shell.error());
}

Expand Down Expand Up @@ -138,30 +139,30 @@ assert.strictEqual(result.toString(), result.stdout);
//

// no callback
var c = shell.exec(JSON.stringify(process.execPath) + ' -e "console.log(1234)"', { async: true });
var c = shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "console.log(1234)"', { async: true });
assert.equal(shell.error(), null);
assert.ok('stdout' in c, 'async exec returns child process object');

//
// callback as 2nd argument
//
shell.exec(JSON.stringify(process.execPath) + ' -e "console.log(5678);"', function (code, stdout, stderr) {
shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "console.log(5678);"', function (code, stdout, stderr) {
assert.equal(code, 0);
assert.ok(stdout === '5678\n' || stdout === '5678\nundefined\n'); // 'undefined' for v0.4
assert.ok(stderr === '' || stderr === 'undefined\n'); // 'undefined' for v0.4

//
// callback as 3rd argument
//
shell.exec(JSON.stringify(process.execPath) + ' -e "console.log(5566);"', { async: true }, function (code2, stdout2, stderr2) {
shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "console.log(5566);"', { async: true }, function (code2, stdout2, stderr2) {
assert.equal(code2, 0);
assert.ok(stdout2 === '5566\n' || stdout2 === '5566\nundefined\n'); // 'undefined' for v0.4
assert.ok(stderr2 === '' || stderr2 === 'undefined\n'); // 'undefined' for v0.4

//
// callback as 3rd argument (slient:true)
//
shell.exec(JSON.stringify(process.execPath) + ' -e "console.log(5678);"', { silent: true }, function (code3, stdout3, stderr3) {
shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "console.log(5678);"', { silent: true }, function (code3, stdout3, stderr3) {
assert.equal(code3, 0);
assert.ok(stdout3 === '5678\n' || stdout3 === '5678\nundefined\n'); // 'undefined' for v0.4
assert.ok(stderr3 === '' || stderr3 === 'undefined\n'); // 'undefined' for v0.4
Expand Down
3 changes: 2 additions & 1 deletion test/make.js
@@ -1,4 +1,5 @@
var shell = require('..');
var common = require('../src/common');
var child = require('child_process');
var assert = require('assert');

Expand All @@ -12,7 +13,7 @@ var script = 'require(\'../../make.js\');' +
'}';

shell.ShellString(script).to(file);
child.exec(JSON.stringify(process.execPath) + ' ' + file, function (err, stdout) {
child.exec(JSON.stringify(common.nodeBinPath) + ' ' + file, function (err, stdout) {
assert.ok(stdout.match('first'));
assert.ok(!stdout.match('second')); // Make should die on errors, so this should never get echoed

Expand Down
11 changes: 6 additions & 5 deletions test/set.js
@@ -1,4 +1,5 @@
var shell = require('..');
var common = require('../src/common');

var assert = require('assert');

Expand All @@ -22,35 +23,35 @@ assert.strictEqual(shell.config.noglob, false);
shell.cp('-R', 'resources/', 'tmp');

// default behavior
result = shell.exec(JSON.stringify(process.execPath) + ' -e "require(\'../global\'); ls(\'file_doesnt_exist\'); echo(1234);"');
result = shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "require(\'../global\'); ls(\'file_doesnt_exist\'); echo(1234);"');
assert.equal(result.code, 0);
assert.equal(result.stdout, '1234\n');
assert.equal(result.stderr, 'ls: no such file or directory: file_doesnt_exist\n');

// set -e
result = shell.exec(JSON.stringify(process.execPath) + ' -e "require(\'../global\'); set(\'-e\'); ls(\'file_doesnt_exist\'); echo(1234);"');
result = shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "require(\'../global\'); set(\'-e\'); ls(\'file_doesnt_exist\'); echo(1234);"');
var nodeVersion = process.versions.node.split('.').map(function (str) { return parseInt(str, 10); });
var uncaughtErrorExitCode = (nodeVersion[0] === 0 && nodeVersion[1] < 11) ? 8 : 1;
assert.equal(result.code, uncaughtErrorExitCode);
assert.equal(result.stdout, '');
assert(result.stderr.indexOf('Error: ls: no such file or directory: file_doesnt_exist') >= 0);

// set -v
result = shell.exec(JSON.stringify(process.execPath) + ' -e "require(\'../global\'); set(\'-v\'); ls(\'file_doesnt_exist\'); echo(1234);"');
result = shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "require(\'../global\'); set(\'-v\'); ls(\'file_doesnt_exist\'); echo(1234);"');
assert.equal(result.code, 0);
assert.equal(result.stdout, '1234\n');
assert.equal(result.stderr, 'ls file_doesnt_exist\nls: no such file or directory: file_doesnt_exist\necho 1234\n');

// set -ev
result = shell.exec(JSON.stringify(process.execPath) + ' -e "require(\'../global\'); set(\'-ev\'); ls(\'file_doesnt_exist\'); echo(1234);"');
result = shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "require(\'../global\'); set(\'-ev\'); ls(\'file_doesnt_exist\'); echo(1234);"');
assert.equal(result.code, uncaughtErrorExitCode);
assert.equal(result.stdout, '');
assert(result.stderr.indexOf('Error: ls: no such file or directory: file_doesnt_exist') >= 0);
assert(result.stderr.indexOf('ls file_doesnt_exist\n') >= 0);
assert.equal(result.stderr.indexOf('echo 1234\n'), -1);

// set -e, set +e
result = shell.exec(JSON.stringify(process.execPath) + ' -e "require(\'../global\'); set(\'-e\'); set(\'+e\'); ls(\'file_doesnt_exist\'); echo(1234);"');
result = shell.exec(JSON.stringify(common.nodeBinPath) + ' -e "require(\'../global\'); set(\'-e\'); set(\'+e\'); ls(\'file_doesnt_exist\'); echo(1234);"');
assert.equal(result.code, 0);
assert.equal(result.stdout, '1234\n');
assert.equal(result.stderr, 'ls: no such file or directory: file_doesnt_exist\n');
Expand Down
3 changes: 2 additions & 1 deletion test/shjs.js
@@ -1,10 +1,11 @@
var shell = require('..');
var common = require('../src/common');
var path = require('path');
var assert = require('assert');

function runScript(name) {
// prefix with 'node ' for Windows, don't prefix for OSX/Linux
var cmd = (process.platform === 'win32' ? JSON.stringify(process.execPath) + ' ' : '') + path.resolve(__dirname, '../bin/shjs');
var cmd = (process.platform === 'win32' ? JSON.stringify(common.nodeBinPath) + ' ' : '') + path.resolve(__dirname, '../bin/shjs');
var script = path.resolve(__dirname, 'resources', 'shjs', name);
return shell.exec(cmd + ' ' + script, { silent: true });
}
Expand Down
3 changes: 2 additions & 1 deletion test/touch.js
@@ -1,4 +1,5 @@
var shell = require('..');
var common = require('../src/common');
var assert = require('assert');
var fs = require('fs');
var crypto = require('crypto');
Expand Down Expand Up @@ -59,7 +60,7 @@ assert.ok(shell.error());
testFile = tmpFile(false);
var testFile2 = tmpFile();
shell.touch(testFile2);
shell.exec(JSON.stringify(process.execPath) + ' resources/exec/slow.js 3000');
shell.exec(JSON.stringify(common.nodeBinPath) + ' resources/exec/slow.js 3000');
result = shell.touch(testFile);
assert.ok(!shell.error());
assert.equal(result.code, 0);
Expand Down

0 comments on commit e7bd06a

Please sign in to comment.