From e7bd06ac91e510cbdd96753d0a8a580d7aa26a40 Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Mon, 17 Oct 2016 21:03:12 -0700 Subject: [PATCH] refactor: process.execPath -> common.nodeBinPath Move process.execPath with common.nodeBinPath until we have a fix for compatibility with electron. --- src/cmd.js | 2 +- src/common.js | 4 ++++ test/cmd.js | 15 ++++++++------- test/common.js | 2 +- test/config.js | 4 ++-- test/echo.js | 9 +++++---- test/exec.js | 25 +++++++++++++------------ test/make.js | 3 ++- test/set.js | 11 ++++++----- test/shjs.js | 3 ++- test/touch.js | 3 ++- 11 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/cmd.js b/src/cmd.js index 99c8a0f5..8e876ff7 100644 --- a/src/cmd.js +++ b/src/cmd.js @@ -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, diff --git a/src/common.js b/src/common.js index 7639b07d..cda2f217 100644 --- a/src/common.js +++ b/src/common.js @@ -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 diff --git a/test/cmd.js b/test/cmd.js index 3b34f2dc..321c5797 100644 --- a/test/cmd.js +++ b/test/cmd.js @@ -1,4 +1,5 @@ var shell = require('..'); +var common = require('../src/common'); var assert = require('assert'); var path = require('path'); @@ -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'); @@ -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()); diff --git a/test/common.js b/test/common.js index 746762c3..f9876326 100644 --- a/test/common.js +++ b/test/common.js @@ -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'); diff --git a/test/config.js b/test/config.js index 8392e76d..f68c273a 100644 --- a/test/config.js +++ b/test/config.js @@ -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')); // @@ -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); diff --git a/test/echo.js b/test/echo.js index ec83413c..1052a6b0 100644 --- a/test/echo.js +++ b/test/echo.js @@ -1,4 +1,5 @@ var shell = require('..'); +var common = require('../src/common'); var assert = require('assert'); var child = require('child_process'); @@ -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(); diff --git a/test/exec.js b/test/exec.js index 1c725c38..103db604 100644 --- a/test/exec.js +++ b/test/exec.js @@ -1,4 +1,5 @@ var shell = require('..'); +var common = require('../src/common'); var assert = require('assert'); var util = require('util'); @@ -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"); @@ -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()); } @@ -138,14 +139,14 @@ 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 @@ -153,7 +154,7 @@ shell.exec(JSON.stringify(process.execPath) + ' -e "console.log(5678);"', functi // // 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 @@ -161,7 +162,7 @@ shell.exec(JSON.stringify(process.execPath) + ' -e "console.log(5678);"', functi // // 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 diff --git a/test/make.js b/test/make.js index cc46e07b..6025d475 100644 --- a/test/make.js +++ b/test/make.js @@ -1,4 +1,5 @@ var shell = require('..'); +var common = require('../src/common'); var child = require('child_process'); var assert = require('assert'); @@ -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 diff --git a/test/set.js b/test/set.js index 005cab8f..2fd76326 100644 --- a/test/set.js +++ b/test/set.js @@ -1,4 +1,5 @@ var shell = require('..'); +var common = require('../src/common'); var assert = require('assert'); @@ -22,13 +23,13 @@ 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); @@ -36,13 +37,13 @@ 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); @@ -50,7 +51,7 @@ 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'); diff --git a/test/shjs.js b/test/shjs.js index 15a2a90b..6f73f142 100644 --- a/test/shjs.js +++ b/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 }); } diff --git a/test/touch.js b/test/touch.js index 3b8d5d91..c689da71 100644 --- a/test/touch.js +++ b/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'); @@ -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);