diff --git a/lib/child_process.js b/lib/child_process.js index 4fda6e20e53902..434ccef40373c6 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); +}