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 <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
parent
af61ebf6e2
commit
dc76afffb6
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user