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

Fix stripFinalNewline not applied on error.stdout|stderr|all #240

Merged
merged 1 commit into from May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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