From 16d01245c9b5ea807162c3a74962e6eb9600aae3 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 14 May 2019 17:10:21 +0200 Subject: [PATCH 1/8] Make stdout|stderr properties undefined instead of null --- index.js | 8 ++++++-- test.js | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4c2cbb1f0..d39a491c3 100644 --- a/index.js +++ b/index.js @@ -110,8 +110,12 @@ function handleInput(spawned, input) { } function handleOutput(options, value) { - if (value && options.stripFinalNewline) { - value = stripFinalNewline(value); + if (typeof value !== 'string' && !Buffer.isBuffer(value)) { + return; + } + + if (options.stripFinalNewline) { + return stripFinalNewline(value); } return value; diff --git a/test.js b/test.js index da64172d9..e7250fd94 100644 --- a/test.js +++ b/test.js @@ -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')}); From c7d42b76f80b31bdd67f6519016497b833db1cff Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 14 May 2019 17:14:25 +0200 Subject: [PATCH 2/8] Fix README --- readme.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/readme.md b/readme.md index 7e573f179..15775e44a 100644 --- a/readme.md +++ b/readme.md @@ -107,8 +107,6 @@ try { command: 'wrong command', exitCode: 2, exitCodeName: 'ENOENT', - stdout: null, - stderr: null, failed: true, timedOut: false, isCanceled: false, From ace8ef2d6b9d82a87c6b1c7ea173d69f4a8c8db0 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 14 May 2019 20:11:29 +0200 Subject: [PATCH 3/8] Make stdout and stderr empty strings with `execa.sync()` on errors --- index.js | 9 +++++---- readme.md | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index d39a491c3..e2941e1e2 100644 --- a/index.js +++ b/index.js @@ -109,9 +109,10 @@ function handleInput(spawned, input) { } } -function handleOutput(options, value) { +function handleOutput(options, value, error) { if (typeof value !== 'string' && !Buffer.isBuffer(value)) { - return; + // When `execa.sync()` errors, we normalize it to '' to mimic `execa()` + return error === undefined ? undefined : ''; } if (options.stripFinalNewline) { @@ -427,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, { diff --git a/readme.md b/readme.md index 15775e44a..ac1d5eb9a 100644 --- a/readme.md +++ b/readme.md @@ -107,6 +107,8 @@ try { command: 'wrong command', exitCode: 2, exitCodeName: 'ENOENT', + stdout: '', + stderr: '', failed: true, timedOut: false, isCanceled: false, From f4eb94b8a612b31caa642feb5fcd652b86d15dc4 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 14 May 2019 20:47:02 +0200 Subject: [PATCH 4/8] Add tests for stdout|stderr|all on process errors --- test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test.js b/test.js index e7250fd94..326e929ec 100644 --- a/test.js +++ b/test.js @@ -65,6 +65,22 @@ test('stdout/stderr/all are undefined if ignored in sync mode', t => { t.is(all, undefined); }); +test('stdout/stderr/all on procss errors', async t => { + const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command')); + t.is(stdout, ''); + t.is(stderr, ''); + t.is(all, ''); +}); + +test('stdout/stderr/all on procss errors, in sync mode', t => { + const {stdout, stderr, all} = t.throws(() => { + execa.sync('wrong command'); + }); + t.is(stdout, ''); + t.is(stderr, ''); + 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')}); From b159eb58d30ba8ca493a376f5ad8300d7d0b2f11 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 14 May 2019 21:32:18 +0200 Subject: [PATCH 5/8] Fix test names --- test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index 326e929ec..f8f745bf3 100644 --- a/test.js +++ b/test.js @@ -65,14 +65,14 @@ test('stdout/stderr/all are undefined if ignored in sync mode', t => { t.is(all, undefined); }); -test('stdout/stderr/all on procss errors', async t => { +test('stdout/stderr/all on process errors', async t => { const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command')); t.is(stdout, ''); t.is(stderr, ''); t.is(all, ''); }); -test('stdout/stderr/all on procss errors, in sync mode', t => { +test('stdout/stderr/all on process errors, in sync mode', t => { const {stdout, stderr, all} = t.throws(() => { execa.sync('wrong command'); }); From a2907afc2bad48d84cbe8f6b145a921275900ad7 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 14 May 2019 21:53:11 +0200 Subject: [PATCH 6/8] Fix Windows tests --- test.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index f8f745bf3..f581cafa9 100644 --- a/test.js +++ b/test.js @@ -65,9 +65,13 @@ test('stdout/stderr/all are undefined if ignored in sync mode', t => { t.is(all, undefined); }); +const WRONG_COMMAND_STDOUT = process.platform === 'win32' ? + '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' : + ''; + test('stdout/stderr/all on process errors', async t => { const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command')); - t.is(stdout, ''); + t.is(stdout, WRONG_COMMAND_STDOUT); t.is(stderr, ''); t.is(all, ''); }); @@ -76,7 +80,7 @@ test('stdout/stderr/all on process errors, in sync mode', t => { const {stdout, stderr, all} = t.throws(() => { execa.sync('wrong command'); }); - t.is(stdout, ''); + t.is(stdout, WRONG_COMMAND_STDOUT); t.is(stderr, ''); t.is(all, undefined); }); From 72a99220af01f959a3af464ad96f31e894e118e2 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 14 May 2019 22:02:48 +0200 Subject: [PATCH 7/8] Fix Windows tests again --- test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test.js b/test.js index f581cafa9..c249673c3 100644 --- a/test.js +++ b/test.js @@ -65,14 +65,14 @@ test('stdout/stderr/all are undefined if ignored in sync mode', t => { t.is(all, undefined); }); -const WRONG_COMMAND_STDOUT = process.platform === 'win32' ? +const WRONG_COMMAND_STDERR = process.platform === 'win32' ? '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' : ''; test('stdout/stderr/all on process errors', async t => { const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command')); - t.is(stdout, WRONG_COMMAND_STDOUT); - t.is(stderr, ''); + t.is(stdout, ''); + t.is(stderr, WRONG_COMMAND_STDERR); t.is(all, ''); }); @@ -80,8 +80,8 @@ test('stdout/stderr/all on process errors, in sync mode', t => { const {stdout, stderr, all} = t.throws(() => { execa.sync('wrong command'); }); - t.is(stdout, WRONG_COMMAND_STDOUT); - t.is(stderr, ''); + t.is(stdout, ''); + t.is(stderr, WRONG_COMMAND_STDERR); t.is(all, undefined); }); From da54c6eb1a151dfe567be3f8878ccc2fcccf7cbf Mon Sep 17 00:00:00 2001 From: ehmicky Date: Tue, 14 May 2019 22:08:59 +0200 Subject: [PATCH 8/8] Fix test --- test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.js b/test.js index c249673c3..6544c2671 100644 --- a/test.js +++ b/test.js @@ -73,7 +73,7 @@ test('stdout/stderr/all on process errors', async t => { const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command')); t.is(stdout, ''); t.is(stderr, WRONG_COMMAND_STDERR); - t.is(all, ''); + t.is(all, WRONG_COMMAND_STDERR); }); test('stdout/stderr/all on process errors, in sync mode', t => {