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

Make stdout and stderr properties undefined instead of null #246

Merged
merged 8 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,14 @@ function handleInput(spawned, input) {
}
}

function handleOutput(options, value) {
if (value && options.stripFinalNewline) {
value = stripFinalNewline(value);
function handleOutput(options, value, error) {
if (typeof value !== 'string' && !Buffer.isBuffer(value)) {
// When `execa.sync()` errors, we normalize it to '' to mimic `execa()`
return error === undefined ? undefined : '';
ehmicky marked this conversation as resolved.
Show resolved Hide resolved
}

if (options.stripFinalNewline) {
return stripFinalNewline(value);
}

return value;
Expand Down Expand Up @@ -423,8 +428,8 @@ module.exports.sync = (command, args, options) => {
}

const result = childProcess.spawnSync(parsed.command, parsed.args, parsed.options);
result.stdout = handleOutput(parsed.options, result.stdout);
result.stderr = handleOutput(parsed.options, result.stderr);
result.stdout = handleOutput(parsed.options, result.stdout, result.error);
result.stderr = handleOutput(parsed.options, result.stderr, result.error);

if (result.error || result.status !== 0 || result.signal !== null) {
const error = makeError(result, {
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ try {
command: 'wrong command',
exitCode: 2,
exitCodeName: 'ENOENT',
stdout: null,
stderr: null,
ehmicky marked this conversation as resolved.
Show resolved Hide resolved
stdout: '',
stderr: '',
failed: true,
timedOut: false,
isCanceled: false,
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ test('stdout/stderr/all available on errors', async t => {
t.is(typeof error.all, 'string');
});

test('stdout/stderr/all are undefined if ignored', async t => {
const {stdout, stderr, all} = await execa('noop', {stdio: 'ignore'});
t.is(stdout, undefined);
t.is(stderr, undefined);
t.is(all, undefined);
});

test('stdout/stderr/all are undefined if ignored in sync mode', t => {
const {stdout, stderr, all} = execa.sync('noop', {stdio: 'ignore'});
t.is(stdout, undefined);
t.is(stderr, undefined);
t.is(all, undefined);
});

test('pass `stdout` to a file descriptor', async t => {
const file = tempfile('.txt');
await execa('fixtures/noop', ['foo bar'], {stdout: fs.openSync(file, 'w')});
Expand Down