From 4ee863d9565e66be10747f452ff710728c310f8f Mon Sep 17 00:00:00 2001 From: cjihrig Date: Mon, 23 May 2016 13:21:33 -0400 Subject: [PATCH] child_process: allow buffer encoding in spawnSync When the 'buffer' encoding is passed to spawnSync(), an exception is thrown in Buffer's toString() method because 'buffer' is not a valid encoding there. This commit special cases the 'buffer' encoding. Fixes: https://github.com/nodejs/node/issues/6930 PR-URL: https://github.com/nodejs/node/pull/6939 Reviewed-By: Anna Henningsen Reviewed-By: Ben Noordhuis --- lib/child_process.js | 2 +- test/common.js | 11 +++++++++++ test/parallel/test-child-process-spawnsync.js | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/child_process.js b/lib/child_process.js index dda613afdeeec6..e43093ace1fa0d 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -428,7 +428,7 @@ function spawnSync(/*file, args, options*/) { var result = spawn_sync.spawn(options); - if (result.output && options.encoding) { + if (result.output && options.encoding && options.encoding !== 'buffer') { for (i = 0; i < result.output.length; i++) { if (!result.output[i]) continue; diff --git a/test/common.js b/test/common.js index 7a054fc167c9ab..0a7e0f82b31c39 100644 --- a/test/common.js +++ b/test/common.js @@ -237,6 +237,17 @@ exports.spawnPwd = function(options) { } }; + +exports.spawnSyncPwd = function(options) { + const spawnSync = require('child_process').spawnSync; + + if (exports.isWindows) { + return spawnSync('cmd.exe', ['/c', 'cd'], options); + } else { + return spawnSync('pwd', [], options); + } +}; + exports.platformTimeout = function(ms) { if (process.config.target_defaults.default_configuration === 'Debug') ms = 2 * ms; diff --git a/test/parallel/test-child-process-spawnsync.js b/test/parallel/test-child-process-spawnsync.js index 53e72c83ca7373..291d3287cff563 100644 --- a/test/parallel/test-child-process-spawnsync.js +++ b/test/parallel/test-child-process-spawnsync.js @@ -33,3 +33,17 @@ assert.deepEqual(ret_err.spawnargs, ['bar']); assert.strictEqual(response.stdout.toString().trim(), cwd); })(); + +{ + // Test the encoding option + const noEncoding = common.spawnSyncPwd(); + const bufferEncoding = common.spawnSyncPwd({encoding: 'buffer'}); + const utf8Encoding = common.spawnSyncPwd({encoding: 'utf8'}); + + assert.deepStrictEqual(noEncoding.output, bufferEncoding.output); + assert.deepStrictEqual([ + null, + noEncoding.stdout.toString(), + noEncoding.stderr.toString() + ], utf8Encoding.output); +}