Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

child_process: allow buffer encoding in spawnSync #6939

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
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
32 changes: 19 additions & 13 deletions test/parallel/test-child-process-spawnsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@ 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
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);
}