From 822dd877ebc60c80b7979d674a785485a9906229 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Mon, 23 May 2016 13:21:33 -0400 Subject: [PATCH 1/2] 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. --- 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 2d2e23bbf11672..0c6af12bc8e81b 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -438,7 +438,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 f59461e40a3301..b0fbfd275fd340 100644 --- a/test/common.js +++ b/test/common.js @@ -241,6 +241,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 858cd5bfe2bf5f..9640bbb85c03d0 100644 --- a/test/parallel/test-child-process-spawnsync.js +++ b/test/parallel/test-child-process-spawnsync.js @@ -33,3 +33,17 @@ assert.deepStrictEqual(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); +} From 55c3c3c3fdd1906fc111270e3fcfdc538796e629 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Mon, 23 May 2016 17:25:41 -0400 Subject: [PATCH 2/2] test: refactor spawnSync() cwd test This commit refactors test-child-process-spawnsync.js to use the reusable common.spawnSyncPwd(). --- test/parallel/test-child-process-spawnsync.js | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/test/parallel/test-child-process-spawnsync.js b/test/parallel/test-child-process-spawnsync.js index 9640bbb85c03d0..d45cc1e0dcea7a 100644 --- a/test/parallel/test-child-process-spawnsync.js +++ b/test/parallel/test-child-process-spawnsync.js @@ -18,21 +18,13 @@ assert.strictEqual(ret_err.syscall, 'spawnSync command_does_not_exist'); assert.strictEqual(ret_err.path, 'command_does_not_exist'); assert.deepStrictEqual(ret_err.spawnargs, ['bar']); -// Verify that the cwd option works - GH #7824 -(function() { - var response; - var cwd; - - if (common.isWindows) { - cwd = 'c:\\'; - response = spawnSync('cmd.exe', ['/c', 'cd'], {cwd: cwd}); - } else { - cwd = '/'; - response = spawnSync('pwd', [], {cwd: cwd}); - } +{ + // Test the cwd option + const cwd = common.isWindows ? 'c:\\' : '/'; + const response = common.spawnSyncPwd({cwd}); assert.strictEqual(response.stdout.toString().trim(), cwd); -})(); +} { // Test the encoding option