From b63dde5d1a2d5f55ff78a7a74de371124100ed0a Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Mon, 8 Feb 2016 23:33:25 -0800 Subject: [PATCH] fix(exec): now actually supports shell option --- src/exec.js | 7 +++++-- test/exec.js | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/exec.js b/src/exec.js index 4174adb..c56fe74 100644 --- a/src/exec.js +++ b/src/exec.js @@ -65,11 +65,14 @@ function execSync(cmd, opts) { var execCommand = '"'+process.execPath+'" '+scriptFile; var script; + opts.cwd = path.resolve(opts.cwd); + var optString = JSON.stringify(opts); + if (typeof child.execSync === 'function') { script = [ "var child = require('child_process')", " , fs = require('fs');", - "var childProcess = child.exec('"+escape(cmd)+"', {env: process.env, maxBuffer: "+opts.maxBuffer+"}, function(err) {", + "var childProcess = child.exec('"+escape(cmd)+"', "+optString+", function(err) {", " fs.writeFileSync('"+escape(codeFile)+"', err ? err.code.toString() : '0');", "});", "var stdoutStream = fs.createWriteStream('"+escape(stdoutFile)+"');", @@ -101,7 +104,7 @@ function execSync(cmd, opts) { script = [ "var child = require('child_process')", " , fs = require('fs');", - "var childProcess = child.exec('"+escape(cmd)+"', {env: process.env, maxBuffer: "+opts.maxBuffer+"}, function(err) {", + "var childProcess = child.exec('"+escape(cmd)+"', "+optString+", function(err) {", " fs.writeFileSync('"+escape(codeFile)+"', err ? err.code.toString() : '0');", "});" ].join('\n'); diff --git a/test/exec.js b/test/exec.js index e4edfef..48a26a3 100644 --- a/test/exec.js +++ b/test/exec.js @@ -107,6 +107,31 @@ if (process.version >= 'v0.11') { // this option doesn't work on v0.10 assert.ok(shell.error()); } +// check process.env works +assert.ok(!shell.env.FOO); +shell.env.FOO = 'Hello world'; +result = shell.exec(process.platform !== 'win32' ? 'echo $FOO' : 'echo %FOO%'); +assert.ok(!shell.error()); +assert.equal(result.code, 0); +assert.equal(result.stdout, 'Hello world' + os.EOL); +assert.equal(result.stderr, ''); + +// set shell option (TODO: add tests for Windows) +if (process.platform !== 'win32') { + result = shell.exec('echo $0'); + assert.ok(!shell.error()); + assert.equal(result.code, 0); + assert.equal(result.stdout, '/bin/sh\n'); // sh by default + var bashPath = shell.which('bash').trim(); + // this option doesn't work on v0.10 + if (bashPath && process.version >= 'v0.11') { + result = shell.exec('echo $0', {shell: '/bin/bash'}); + assert.ok(!shell.error()); + assert.equal(result.code, 0); + assert.equal(result.stdout, '/bin/bash\n'); + } +} + // // async //