diff options
author | cjihrig <cjihrig@gmail.com> | 2016-01-08 16:17:50 -0500 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2016-01-26 19:40:43 -0500 |
commit | c3bb4b1aa5e907d489619fb43d233c3336bfc03d (patch) | |
tree | bc6f1f4ec214f4b72e9cbb2f368b39317af12a99 /test/parallel/test-child-process-spawnsync-shell.js | |
parent | 34daaa764fa83557ec41a99f59d1701739321c33 (diff) | |
download | node-new-c3bb4b1aa5e907d489619fb43d233c3336bfc03d.tar.gz |
child_process: add shell option to spawn()
This commit adds a shell option, to spawn() and spawnSync(). This
option allows child processes to be spawned with or without a
shell. The option also allows a custom shell to be defined, for
compatibility with exec()'s shell option.
Fixes: https://github.com/nodejs/node/issues/1009
PR-URL: https://github.com/nodejs/node/pull/4598
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/parallel/test-child-process-spawnsync-shell.js')
-rw-r--r-- | test/parallel/test-child-process-spawnsync-shell.js | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/test/parallel/test-child-process-spawnsync-shell.js b/test/parallel/test-child-process-spawnsync-shell.js new file mode 100644 index 0000000000..620a01c453 --- /dev/null +++ b/test/parallel/test-child-process-spawnsync-shell.js @@ -0,0 +1,37 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const cp = require('child_process'); + +// Verify that a shell is, in fact, executed +const doesNotExist = cp.spawnSync('does-not-exist', {shell: true}); + +assert.notEqual(doesNotExist.file, 'does-not-exist'); +assert.strictEqual(doesNotExist.error, undefined); +assert.strictEqual(doesNotExist.signal, null); + +if (common.isWindows) + assert.strictEqual(doesNotExist.status, 1); // Exit code of cmd.exe +else + assert.strictEqual(doesNotExist.status, 127); // Exit code of /bin/sh + +// Verify that passing arguments works +const echo = cp.spawnSync('echo', ['foo'], {shell: true}); + +assert.strictEqual(echo.args[echo.args.length - 1].replace(/"/g, ''), + 'echo foo'); +assert.strictEqual(echo.stdout.toString().trim(), 'foo'); + +// Verify that shell features can be used +const cmd = common.isWindows ? 'echo bar | more' : 'echo bar | cat'; +const command = cp.spawnSync(cmd, {shell: true}); + +assert.strictEqual(command.stdout.toString().trim(), 'bar'); + +// Verify that the environment is properly inherited +const env = cp.spawnSync(`"${process.execPath}" -pe process.env.BAZ`, { + env: Object.assign({}, process.env, {BAZ: 'buzz'}), + shell: true +}); + +assert.strictEqual(env.stdout.toString().trim(), 'buzz'); |