Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
cjihrig committed May 23, 2016
commit 822dd877ebc60c80b7979d674a785485a9906229
2 changes: 1 addition & 1 deletion lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-child-process-spawnsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}