Skip to content

Commit

Permalink
Fix stripFinalNewline not applied on error.stdout|stderr|all (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed May 13, 2019
1 parent 7c96b58 commit 943f040
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
4 changes: 4 additions & 0 deletions fixtures/noop-throw
@@ -0,0 +1,4 @@
#!/usr/bin/env node
'use strict';
console.error(process.argv[2]);
process.exit(2);
20 changes: 11 additions & 9 deletions index.js
Expand Up @@ -350,10 +350,10 @@ const execa = (command, args, options) => {
const finalize = async () => {
const results = await processComplete;

const result = results[0];
result.stdout = results[1];
result.stderr = results[2];
result.all = results[3];
const [result, stdout, stderr, all] = results;
result.stdout = handleOutput(parsed.options, stdout);
result.stderr = handleOutput(parsed.options, stderr);
result.all = handleOutput(parsed.options, all);

if (result.error || result.code !== 0 || result.signal !== null) {
const error = makeError(result, {
Expand All @@ -372,9 +372,9 @@ const execa = (command, args, options) => {
}

return {
stdout: handleOutput(parsed.options, result.stdout),
stderr: handleOutput(parsed.options, result.stderr),
all: handleOutput(parsed.options, result.all),
stdout: result.stdout,
stderr: result.stderr,
all: result.all,
code: 0,
exitCode: 0,
exitCodeName: 'SUCCESS',
Expand Down Expand Up @@ -425,6 +425,8 @@ module.exports.sync = (command, args, options) => {

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

if (result.error || result.status !== 0 || result.signal !== null) {
const error = makeError(result, {
Expand All @@ -442,8 +444,8 @@ module.exports.sync = (command, args, options) => {
}

return {
stdout: handleOutput(parsed.options, result.stdout),
stderr: handleOutput(parsed.options, result.stderr),
stdout: result.stdout,
stderr: result.stderr,
code: 0,
exitCode: 0,
exitCodeName: 'SUCCESS',
Expand Down
22 changes: 21 additions & 1 deletion test.js
Expand Up @@ -120,11 +120,31 @@ test('skip throwing when using reject option in sync mode', t => {
t.is(error.exitCode, 2);
});

test('stripFinalNewline option', async t => {
test('stripFinalNewline: true', async t => {
const {stdout} = await execa('noop', ['foo']);
t.is(stdout, 'foo');
});

test('stripFinalNewline: false', async t => {
const {stdout} = await execa('noop', ['foo'], {stripFinalNewline: false});
t.is(stdout, 'foo\n');
});

test('stripFinalNewline on failure', async t => {
const {stderr} = await t.throwsAsync(execa('noop-throw', ['foo'], {stripFinalNewline: true}));
t.is(stderr, 'foo');
});

test('stripFinalNewline in sync mode', t => {
const {stdout} = execa.sync('noop', ['foo'], {stripFinalNewline: true});
t.is(stdout, 'foo');
});

test('stripFinalNewline in sync mode on failure', t => {
const {stderr} = t.throws(() => execa.sync('noop-throw', ['foo'], {stripFinalNewline: true}));
t.is(stderr, 'foo');
});

test('preferLocal option', async t => {
await execa('ava', ['--version'], {env: {PATH: ''}});
const errorRegExp = process.platform === 'win32' ? /failed with exit code 1/ : /spawn ava ENOENT/;
Expand Down

0 comments on commit 943f040

Please sign in to comment.