Fix inconsistency between `result.stdout` and `error.stdout` (#686)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 73f4b8c · Browse files · 3 files·+54 added−47 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+54 added−47 removed
- tests:+52 added−42 removed
- other:+2 added−5 removed
Provenance
- Commit
ec383fe- Tree
9a7d47c- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
73f4b8c
3 files changed+54 added−47 removed
modified:lib/error.js-2 removed
35 unmodified lines3637383940414041424343 unmodified lines35 unmodified lines timedOut, isCanceled, options: {timeout, cwd = process.cwd()},}) => { stdio = stdio.map(stdioOutput => stdioOutput ?? '');
// `signal` and `exitCode` emitted on `spawned.on('exit')` event can be `null`. // We normalize them to `undefined` exitCode = exitCode === null ? undefined : exitCode; signal = signal === null ? undefined : signal;43 unmodified linesmodified:test/error.js+52 added-42 removed
8 unmodified lines9 setFixtureDir();10 11 const TIMEOUT_REGEXP = /timed out after/;12 13-test('empty error.stdout/stderr/stdio', async t => {14- const {stdout, stderr, stdio} = await t.throwsAsync(execa('fail.js'));13+const testEmptyErrorStdio = async (t, execaMethod) => {14+ const {failed, stdout, stderr, stdio} = await execaMethod('fail.js', {reject: false});15+ t.true(failed);16 t.is(stdout, '');17 t.is(stderr, '');17- t.deepEqual(stdio, ['', '', '']);18-});18+ t.deepEqual(stdio, [undefined, '', '']);19+};20 20-test('empty error.all', async t => {21- const {all} = await t.throwsAsync(execa('fail.js', {all: true}));22- t.is(all, '');23-});21+test('empty error.stdout/stderr/stdio', testEmptyErrorStdio, execa);22+test('empty error.stdout/stderr/stdio - sync', testEmptyErrorStdio, execaSync);23 25-test('undefined error.all', async t => {26- const {all} = await t.throwsAsync(execa('fail.js'));27- t.is(all, undefined);28-});24+const testUndefinedErrorStdio = async (t, execaMethod) => {25+ const {stdout, stderr, stdio} = await execaMethod('empty.js', {stdio: 'ignore'});26+ t.is(stdout, undefined);27+ t.is(stderr, undefined);28+ t.deepEqual(stdio, [undefined, undefined, undefined]);29+};30 31+test('undefined error.stdout/stderr/stdio', testUndefinedErrorStdio, execa);32+test('undefined error.stdout/stderr/stdio - sync', testUndefinedErrorStdio, execaSync);33+34+const testEmptyAll = async (t, options, expectedValue) => {35+ const {all} = await t.throwsAsync(execa('fail.js', options));36+ t.is(all, expectedValue);37+};38+39+test('empty error.all', testEmptyAll, {all: true}, '');40+test('undefined error.all', testEmptyAll, {}, undefined);41+test('ignored error.all', testEmptyAll, {all: true, stdio: 'ignore'}, undefined);42+43 test('empty error.stdio[0] even with input', async t => {44 const {stdio} = await t.throwsAsync(execa('fail.js', {input: 'test'}));32- t.is(stdio[0], '');45+ t.is(stdio[0], undefined);46 });47 35-const WRONG_COMMAND = isWindows36- ? '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.'37- : '';48+// `error.code` is OS-specific here49+const SPAWN_ERROR_CODES = new Set(['EINVAL', 'ENOTSUP', 'EPERM']);50 39-test('stdout/stderr/all/stdio on process errors', async t => {40- const {stdout, stderr, all, stdio} = await t.throwsAsync(execa('wrong command', {all: true}));41- t.is(stdout, '');42- t.is(stderr, WRONG_COMMAND);43- t.is(all, WRONG_COMMAND);44- t.deepEqual(stdio, ['', '', WRONG_COMMAND]);51+test('stdout/stderr/stdio on process spawning errors', async t => {52+ const {code, stdout, stderr, stdio} = await t.throwsAsync(execa('noop.js', {uid: -1}));53+ t.true(SPAWN_ERROR_CODES.has(code));54+ t.is(stdout, undefined);55+ t.is(stderr, undefined);56+ t.deepEqual(stdio, [undefined, undefined, undefined]);57 });58 47-test('stdout/stderr/all/stdio on process errors, in sync mode', t => {48- const {stdout, stderr, all, stdio} = t.throws(() => {49- execaSync('wrong command');59+test('stdout/stderr/all/stdio on process spawning errors - sync', t => {60+ const {code, stdout, stderr, stdio} = t.throws(() => {61+ execaSync('noop.js', {uid: -1});62 });51- t.is(stdout, '');52- t.is(stderr, WRONG_COMMAND);53- t.is(all, undefined);54- t.deepEqual(stdio, ['', '', WRONG_COMMAND]);63+ t.true(SPAWN_ERROR_CODES.has(code));64+ t.is(stdout, undefined);65+ t.is(stderr, undefined);66+ t.deepEqual(stdio, [undefined, undefined, undefined]);67 });68 57-test('error.stdout/stderr/stdio is defined', async t => {58- const {stdout, stderr, stdio} = await t.throwsAsync(execa('echo-fail.js', fullStdio));69+const testErrorOutput = async (t, execaMethod) => {70+ const {failed, stdout, stderr, stdio} = await execaMethod('echo-fail.js', {...fullStdio, reject: false});71+ t.true(failed);72 t.is(stdout, 'stdout');73 t.is(stderr, 'stderr');61- t.deepEqual(stdio, ['', 'stdout', 'stderr', 'fd3']);62-});74+ t.deepEqual(stdio, [undefined, 'stdout', 'stderr', 'fd3']);75+};76 64-test('error.stdout/stderr/stdio is defined, in sync mode', t => {65- const {stdout, stderr, stdio} = t.throws(() => {66- execaSync('echo-fail.js', fullStdio);67- });68- t.is(stdout, 'stdout');69- t.is(stderr, 'stderr');70- t.deepEqual(stdio, ['', 'stdout', 'stderr', 'fd3']);71-});77+test('error.stdout/stderr/stdio is defined', testErrorOutput, execa);78+test('error.stdout/stderr/stdio is defined - sync', testErrorOutput, execaSync);79 80 test('exitCode is 0 on success', async t => {81 const {exitCode} = await execa('noop.js', ['foo']);82 t.is(exitCode, 0);83 });84 85 const testExitCode = async (t, number) => {79- const {exitCode} = await t.throwsAsync(execa('exit.js', [`${number}`]), {message: new RegExp(`failed with exit code ${number}`)});86+ const {exitCode} = await t.throwsAsync(87+ execa('exit.js', [`${number}`]),88+ {message: new RegExp(`failed with exit code ${number}`)},89+ );90 t.is(exitCode, number);91 };92 93 test('exitCode is 2', testExitCode, 2);179 unmodified linesmodified:index.js+2 added-3 removed
79 unmodified lines80 return {file, args, command, escapedCommand, options};81 };82 83 const handleOutput = (options, value) => {84- if (typeof value !== 'string' && !ArrayBuffer.isView(value)) {84+ if (value === undefined || value === null) {85 return;86 }87 88 if (Buffer.isBuffer(value)) {17 unmodified lines106 const dummySpawned = new childProcess.ChildProcess();107 const errorPromise = Promise.reject(makeError({108 error,109 stdio: Array.from({length: stdioLength}),110- all: options.all ? '' : undefined,110 command,111 escapedCommand,112 options,113 timedOut: false,86 unmodified lines200 }201 202 pipeOutputSync(stdioStreams, result);203 205- const output = result.output || [undefined, undefined, undefined];204+ const output = result.output || Array.from({length: 3});205 const stdio = output.map(stdioOutput => handleOutput(options, stdioOutput));206 207 if (result.error || result.status !== 0 || result.signal !== null) {208 const error = makeError({109 unmodified lines