Fix `result.all` and `subprocess.all` when only some file descriptors are buffered
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by Sindre Sorhus · parent b548d8b · Browse files · 17 files·+239 added−44 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
17 files+239 added−44 removed
- tests:+132 added−0 removed
- other:+107 added−44 removed
Provenance
- Commit
758768f- Tree
b3fe450- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
b548d8b
17 files changed+239 added−44 removed
modified:docs/api.md+2 added-2 removed
415 unmodified lines416 Stream combining/interleaving [`subprocess.stdout`](#subprocessstdout) and [`subprocess.stderr`](#subprocessstderr).417 418 This requires the [`all`](#optionsall) option to be `true`.419 420-This is `undefined` if [`stdout`](#optionsstdout) and [`stderr`](#optionsstderr) options are set to [`'inherit'`](output.md#terminal-output), [`'ignore'`](output.md#ignore-output), [`Writable`](streams.md#output) or [`integer`](output.md#terminal-output), or if the [`buffer`](#optionsbuffer) option is `false`.420+This is `undefined` if both [`stdout`](#optionsstdout) and [`stderr`](#optionsstderr) options are set to [`'inherit'`](output.md#terminal-output), [`'ignore'`](output.md#ignore-output), [`Writable`](streams.md#output) or [`integer`](output.md#terminal-output).421 422 More info on [interleaving](output.md#interleaved-output) and [streaming](streams.md#manual-streaming).423 424 ### subprocess.stdio150 unmodified lines575 The output of the subprocess with [`result.stdout`](#resultstdout) and [`result.stderr`](#resultstderr) interleaved.576 577 This requires the [`all`](#optionsall) option to be `true`.578 579-This is `undefined` if both [`stdout`](#optionsstdout) and [`stderr`](#optionsstderr) options are set to only [`'inherit'`](output.md#terminal-output), [`'ignore'`](output.md#ignore-output), [`Writable`](streams.md#output) or [`integer`](output.md#terminal-output), or if the [`buffer`](#optionsbuffer) option is `false`.579+This is `undefined` if both [`stdout`](#optionsstdout) and [`stderr`](#optionsstderr) options are set to only [`'inherit'`](output.md#terminal-output), [`'ignore'`](output.md#ignore-output), [`Writable`](streams.md#output) or [`integer`](output.md#terminal-output), or if the [`buffer`](#optionsbuffer) option is `false` for both.580 581 This is an array if the [`lines`](#optionslines) option is `true`, or if either the `stdout` or `stderr` option is a [transform in object mode](transform.md#object-mode).582 583 [More info.](output.md#interleaved-output)782 unmodified linesmodified:lib/io/contents.js+3 added-3 removed
5 unmodified lines67891010111213147 unmodified lines2223242526262728293037 unmodified lines6869707172727374757641 unmodified lines5 unmodified linesimport {handleMaxBuffer} from './max-buffer.js';import {getStripFinalNewline} from './strip-newline.js';
// Retrieve `result.stdout|stderr|all|stdio[*]`export const getStreamOutput = async ({stream, onStreamEnd, fdNumber, encoding, buffer, maxBuffer, lines, allMixed, stripFinalNewline, verboseInfo, streamInfo}) => {export const getStreamOutput = async ({stream, onStreamEnd, fdNumber, encoding, buffer, maxBuffer, lines, allMixed, stripFinalNewline, allFds, verboseInfo, streamInfo}) => { const logPromise = logOutputAsync({ stream, onStreamEnd, fdNumber,7 unmodified lines await Promise.all([resumeStream(stream), logPromise]); return; }
const stripFinalNewlineValue = getStripFinalNewline(stripFinalNewline, fdNumber); const stripFinalNewlineValue = getStripFinalNewline(stripFinalNewline, fdNumber, allFds); const iterable = iterateForResult({ stream, onStreamEnd, lines,37 unmodified lines};
// When using `buffer: false`, users need to read `subprocess.stdout|stderr|all` right away// See https://github.com/sindresorhus/execa/issues/730 and https://github.com/sindresorhus/execa/pull/729#discussion_r1465496310const resumeStream = async stream => {export const resumeStream = async stream => { await setImmediate(); if (stream.readableFlowing === null) { stream.resume(); }41 unmodified linesmodified:lib/io/strip-newline.js+4 added-3 removed
1234556789101110111213import stripFinalNewlineFunction from 'strip-final-newline';
// Apply `stripFinalNewline` option, which applies to `result.stdout|stderr|all|stdio[*]`.// If the `lines` option is used, it is applied on each line, but using a different function.export const stripNewline = (value, {stripFinalNewline}, fdNumber) => getStripFinalNewline(stripFinalNewline, fdNumber) && value !== undefined && !Array.isArray(value)export const stripNewline = (value, {stripFinalNewline}, fdNumber, allFds) => getStripFinalNewline(stripFinalNewline, fdNumber, allFds) && value !== undefined && !Array.isArray(value) ? stripFinalNewlineFunction(value) : value;
// Retrieve `stripFinalNewline` option value, including with `subprocess.all`export const getStripFinalNewline = (stripFinalNewline, fdNumber) => fdNumber === 'all' ? stripFinalNewline[1] || stripFinalNewline[2]// With `subprocess.all`, `allFds` are the file descriptors it actually interleaves. Only those are considered, otherwise a single one would be stripped because another one defaults to being stripped.export const getStripFinalNewline = (stripFinalNewline, fdNumber, allFds) => fdNumber === 'all' ? allFds.some(allFd => stripFinalNewline[allFd]) : stripFinalNewline[fdNumber];modified:lib/methods/main-async.js+2 added-2 removed
15 unmodified lines16 import {subprocessKill} from '../terminate/kill.js';17 import {getSpawnOptions, getKillFunction} from '../terminate/kill-descendants.js';18 import {cleanupOnExit} from '../terminate/cleanup.js';19 import {pipeToSubprocess} from '../pipe/setup.js';20-import {makeAllStream} from '../resolve/all-async.js';20+import {makeAllStream, getAllFds} from '../resolve/all-async.js';21 import {waitForSubprocessResult} from '../resolve/wait-subprocess.js';22 import {addConvertedStreams} from '../convert/add.js';23 import {createDeferred} from '../utils/deferred.js';24 import {mergePromise} from './promise.js';158 unmodified lines183 controller.abort();184 onInternalError.resolve();185 186 const stdio = stdioResults.map((stdioResult, fdNumber) => stripNewline(stdioResult, options, fdNumber));187- const all = stripNewline(allResult, options, 'all');187+ const all = stripNewline(allResult, options, 'all', getAllFds(subprocess, options.buffer));188 const result = getAsyncResult({189 errorInfo,190 exitCode,191 signal,40 unmodified linesmodified:lib/methods/main-sync.js+3 added-2 removed
7 unmodified lines8 import {stripNewline} from '../io/strip-newline.js';9 import {addInputOptionsSync} from '../io/input-sync.js';10 import {transformOutputSync} from '../io/output-sync.js';11 import {getMaxBufferSync} from '../io/max-buffer.js';12-import {getAllSync} from '../resolve/all-sync.js';12+import {getAllSync, getAllSyncFds} from '../resolve/all-sync.js';13 import {getExitResultSync} from '../resolve/exit-sync.js';14 15 // Main shared logic for all sync methods: `execaSync()`, `$.sync()`16 export const execaCoreSync = (rawFile, rawArguments, rawOptions) => {82 unmodified lines99 options,100 verboseInfo,101 });102 const stdio = output.map((stdioOutput, fdNumber) => stripNewline(stdioOutput, options, fdNumber));103- const all = stripNewline(getAllSync(output, options), options, 'all');103+ const allFds = getAllSyncFds(output);104+ const all = stripNewline(getAllSync(output, allFds, options), options, 'all', allFds);105 return getSyncResult({106 error,107 exitCode,108 signal,60 unmodified linesmodified:lib/resolve/all-async.js+48 added-16 removed
123456567789101112131415161718192021222324252627101112131415161718192028293031323334353637383940414243444546474849505152535455565758596029616263643365666768699 unmodified linesimport mergeStreams from '@sindresorhus/merge-streams';import {setAllStream} from '../utils/standard-stream.js';import {resumeStream} from '../io/contents.js';import {waitForSubprocessStream} from './stdio.js';
// `all` interleaves `stdout` and `stderr`export const makeAllStream = ({stdout, stderr}, {all}) => all && (stdout || stderr) ? mergeStreams([stdout, stderr].filter(Boolean)) : undefined;export const makeAllStream = ({stdout, stderr}, {all}) => { if (!all) { return; }
const fileDescriptors = [[stdout, 1], [stderr, 2]].filter(([stream]) => stream); if (fileDescriptors.length === 0) { return; }
return setAllStream(mergeStreams(fileDescriptors.map(([stream]) => stream)));};
// The file descriptors which `result.all` contains, which the `all` options must follow// Unlike `subprocess.all`, this excludes the file descriptors which are not buffered, since their output is not includedexport const getAllFds = ({stdout, stderr}, [, bufferStdout, bufferStderr]) => [ ...(stdout !== null && bufferStdout ? [1] : []), ...(stderr !== null && bufferStderr ? [2] : []),];
// Read the contents of `subprocess.all` and|or wait for its completionexport const waitForAllStream = ({subprocess, all, encoding, buffer, maxBuffer, lines, stripFinalNewline, verboseInfo, streamInfo}) => waitForSubprocessStream({ ...getAllStream(subprocess, all, buffer), fdNumber: 'all', encoding, maxBuffer: maxBuffer[1] + maxBuffer[2], lines: lines[1] || lines[2], allMixed: getAllMixed(subprocess, all), stripFinalNewline, verboseInfo, streamInfo,});export const waitForAllStream = ({subprocess, all, encoding, buffer, maxBuffer, lines, stripFinalNewline, verboseInfo, streamInfo}) => { const {stream, buffer: bufferAll, drainedStream} = getAllStream(subprocess, all, buffer); return Promise.all([ waitForSubprocessStream({ stream, buffer: bufferAll, fdNumber: 'all', encoding, maxBuffer: maxBuffer[1] + maxBuffer[2], lines: lines[1] || lines[2], allFds: getAllFds(subprocess, buffer), allMixed: getAllMixed(subprocess, all), stripFinalNewline, verboseInfo, streamInfo, }), // When only one of the two file descriptors is buffered, its stream is read instead of the merged one. // The merged one is then drained, otherwise it would apply backpressure on that file descriptor and hang the subprocess. drainedStream === undefined ? undefined : resumeStream(drainedStream), ]).then(([output]) => output);};
const getAllStream = ({stdout, stderr}, all, [, bufferStdout, bufferStderr]) => { const buffer = bufferStdout || bufferStderr; if (!buffer) { return {stream: all, buffer}; }
if (!all) { return {stream: undefined, buffer}; }
if (!bufferStdout) { return {stream: stderr, buffer}; return {stream: stderr, buffer, drainedStream: all}; }
if (!bufferStderr) { return {stream: stdout, buffer}; return {stream: stdout, buffer, drainedStream: all}; }
return {stream: all, buffer};};9 unmodified linesmodified:lib/resolve/all-sync.js+26 added-10 removed
1 import {isUint8Array, concatUint8Arrays} from '../utils/uint-array.js';2+import {splitLinesSync} from '../transform/split.js';2-import {stripNewline} from '../io/strip-newline.js';3+import {getStripFinalNewline} from '../io/strip-newline.js';4 5+// The file descriptors which `result.all` interleaves. Those are the ones with an output, like with the `all` stream.6+export const getAllSyncFds = ([, stdout, stderr]) => [7+ ...(stdout === undefined ? [] : [1]),8+ ...(stderr === undefined ? [] : [2]),9+];10+11 // Retrieve `result.all` with synchronous methods5-export const getAllSync = ([, stdout, stderr], options) => {12+export const getAllSync = ([, stdout, stderr], allFds, options) => {13 if (!options.all) {14 return;15 }16 17 if (stdout === undefined) {11- return stderr;18+ return splitAllLines(stderr, allFds, options);19 }20 21 if (stderr === undefined) {15- return stdout;22+ return splitAllLines(stdout, allFds, options);23 }24 18- if (Array.isArray(stdout)) {19- return Array.isArray(stderr)20- ? [...stdout, ...stderr]21- : [...stdout, stripNewline(stderr, options, 'all')];25+ // Like asynchronous methods, `all` is split into lines as soon as the `lines` option applies to either file descriptor.26+ // A file descriptor which uses the `lines` option is already split with its own `stripFinalNewline` value, like `result.stdout` and `result.stderr` are. The other one is split with the `stripFinalNewline` value of `result.all`.27+ // This differs from asynchronous methods, which cannot tell which file descriptor the merged output's last newline belongs to.28+ if (hasLines(options)) {29+ return [splitAllLines(stdout, allFds, options), splitAllLines(stderr, allFds, options)].flat();30 }31 24- if (Array.isArray(stderr)) {25- return [stripNewline(stdout, options, 'all'), ...stderr];32+ // Without the `lines` option, arrays can only come from object mode, which does not apply to the other file descriptor33+ if (Array.isArray(stdout) || Array.isArray(stderr)) {34+ return [stdout, stderr].flat();35 }36 37 if (isUint8Array(stdout) && isUint8Array(stderr)) {38 return concatUint8Arrays([stdout, stderr]);39 }40 41 return `${stdout}${stderr}`;42 };43+44+const hasLines = ({lines}) => Boolean(lines[1] || lines[2]);45+46+// Each file descriptor is either already split into lines, or is a string which still needs to be split47+const splitAllLines = (value, allFds, options) => Array.isArray(value) || !hasLines(options)48+ ? value49+ : splitLinesSync(value, !getStripFinalNewline(options.stripFinalNewline, 'all', allFds), false);modified:lib/resolve/stdio.js+2 added-1 removed
14 unmodified lines15 streamInfo,16 }));17 18 // Read the contents of `subprocess.std*` or `subprocess.all` and|or wait for its completion19-export const waitForSubprocessStream = async ({stream, fdNumber, encoding, buffer, maxBuffer, lines, allMixed, stripFinalNewline, verboseInfo, streamInfo}) => {19+export const waitForSubprocessStream = async ({stream, fdNumber, encoding, buffer, maxBuffer, lines, allMixed, stripFinalNewline, allFds, verboseInfo, streamInfo}) => {20 if (!stream) {21 return;22 }23 13 unmodified lines37 maxBuffer,38 lines,39 allMixed,40 stripFinalNewline,41+ allFds,42 verboseInfo,43 streamInfo,44 }),45 onStreamEnd,3 unmodified linesmodified:lib/stdio/stdio-option.js+6 added-2 removed
73 unmodified lines74 };75 76 // Using `buffer: false` with synchronous methods implies `stdout`/`stderr`: `ignore`.77 // Unless the output is needed, e.g. due to `verbose: 'full'` or to redirecting to a file.78+// This is only done for `stdout`/`stderr`, since `ignore` redirects them to `/dev/null`.79+// With additional file descriptors, it does not pass them to the subprocess at all, which would make it fail when writing to them.80 const normalizeStdioSync = (stdioArray, buffer, verboseInfo) => stdioArray.map((stdioOption, fdNumber) =>81 !buffer[fdNumber]80- && fdNumber !== 082+ && isStandardOutputFd(fdNumber)83 && !isFullVerbose(verboseInfo, fdNumber)84 && isOutputPipeOnly(stdioOption, fdNumber)85 ? 'ignore'86 : stdioOption);8 unmodified lines95 && stdioOption.value === 'pipe'96 && (stdioOption.input === undefined || stdioOption.input === false || isFixedOutputPipe(fdNumber, stdioOption.input));97 98 // On `stdout`/`stderr`, the direction is always output, so `input: true` is ignored and the pipe stays output.97-const isFixedOutputPipe = (fdNumber, input) => input === true && (fdNumber === 1 || fdNumber === 2);99+const isFixedOutputPipe = (fdNumber, input) => input === true && isStandardOutputFd(fdNumber);100+101+const isStandardOutputFd = fdNumber => fdNumber === 1 || fdNumber === 2;modified:lib/utils/standard-stream.js+6 added
2 unmodified lines3 export const isStandardStream = stream => STANDARD_STREAMS.includes(stream);4 export const STANDARD_STREAMS = [process.stdin, process.stdout, process.stderr];5 export const STANDARD_STREAMS_ALIASES = ['stdin', 'stdout', 'stderr'];6 export const getStreamName = fdNumber => STANDARD_STREAMS_ALIASES[fdNumber] ?? `stdio[${fdNumber}]`;7+8+// `subprocess.all` is created by Execa itself, which then pipes `stdout`/`stderr` into it.9+// `verbose` needs to tell that pipe apart from a user pipe, which it disables.10+const allStreamSymbol = Symbol('execa.allStream');11+export const setAllStream = stream => Object.assign(stream, {[allStreamSymbol]: true});12+export const isAllStream = stream => stream?.[allStreamSymbol] === true;modified:lib/verbose/output.js+3 added-1 removed
1 import {BINARY_ENCODINGS} from '../arguments/encoding-option.js';2 import {TRANSFORM_TYPES} from '../stdio/type.js';3+import {isAllStream} from '../utils/standard-stream.js';4 import {verboseLog, serializeVerboseMessage} from './log.js';5 import {isFullVerbose} from './values.js';6 7 // `ignore` opts-out of `verbose` for a specific stream.37 unmodified lines45 // - `.pipe()` achieves the same result as using `stdout: 'inherit'`, `stdout: stream`, etc. which also make `verbose` a noop.46 // For example, `subprocess.stdout.pipe(process.stdin)` would print each line twice.47 // - When chaining subprocesses with `subprocess.pipe(otherSubprocess)`, only the last one should print its output.48 // Detecting whether `.pipe()` is impossible without monkey-patching it, so we use the following undocumented property.49+// The pipe into `subprocess.all` is done by Execa itself, so it is excluded.50 // This is not a critical behavior since changes of the following property would only make `verbose` more verbose.49-const isPipingStream = stream => stream._readableState.pipes.length > 0;51+const isPipingStream = stream => stream._readableState.pipes.some(pipe => !isAllStream(pipe));52 53 // When `verbose` is `full`, print stdout|stderr54 const logLine = (line, fdNumber, verboseInfo) => {55 const verboseMessage = serializeVerboseMessage(line);7 unmodified linesadded:test/fixtures/noop-fd-large.js+7 added
1+#!/usr/bin/env node2+import {Buffer} from 'node:buffer';3+import {getWriteStream} from '../helpers/fs.js';4+5+const fdNumber = Number(process.argv[2]);6+const bytes = Number(process.argv[3]);7+getWriteStream(fdNumber).write(Buffer.alloc(bytes, 'a'));modified:test/resolve/all.js+48 added
173 unmodified lines174 t.is(stdout, undefined);175 t.is(stderr, undefined);176 t.is(all, undefined);177 });178+179+// `all` is split into lines when the `lines` option applies to either file descriptor, even when only one of them uses it180+const foobarTwoLines = `${foobarString}\n${foobarString}`;181+// The fixture writes the same two lines to both `stdout` and `stderr`182+const foobarTwoLinesArray = Array.from({length: 4}, () => `${foobarString}\n`);183+const testAllBothLines = async (t, lines, execaMethod) => {184+ const {all} = await execaMethod('noop-both.js', [foobarTwoLines], {all: true, lines, stripFinalNewline: false});185+ t.deepEqual(all, foobarTwoLinesArray);186+};187+188+test('result.all is split into lines when only stdout uses the lines option', testAllBothLines, {stdout: true}, execa);189+test('result.all is split into lines when only stderr uses the lines option', testAllBothLines, {stderr: true}, execa);190+test('result.all is split into lines when only stdout uses the lines option, sync', testAllBothLines, {stdout: true}, execaSync);191+test('result.all is split into lines when only stderr uses the lines option, sync', testAllBothLines, {stderr: true}, execaSync);192+193+// When only one file descriptor is interleaved into `all`, it must follow that one exactly, including its `stripFinalNewline` value194+const testAllOneFd = async (t, lines, execaMethod) => {195+ const {stderr, all} = await execaMethod('noop-both.js', [foobarString], {196+ all: true,197+ stdout: 'ignore',198+ lines,199+ stripFinalNewline: {stderr: false},200+ });201+ t.deepEqual(stderr, lines ? [`${foobarString}\n`] : `${foobarString}\n`);202+ t.deepEqual(all, stderr);203+};204+205+test('result.all follows stderr when stdout is ignored', testAllOneFd, false, execa);206+test('result.all follows stderr when stdout is ignored, lines', testAllOneFd, true, execa);207+test('result.all follows stderr when stdout is ignored, sync', testAllOneFd, false, execaSync);208+test('result.all follows stderr when stdout is ignored, lines, sync', testAllOneFd, true, execaSync);209+210+// `all` is split into lines as soon as the `lines` option applies to either file descriptor, even the one which is not interleaved into `all`211+const testAllOneFdOtherLines = async (t, linesFdName, ignoredFdName, stripFinalNewline, execaMethod) => {212+ const {all} = await execaMethod('noop-both.js', [foobarString], {213+ all: true,214+ [ignoredFdName]: 'ignore',215+ lines: {[linesFdName]: true},216+ stripFinalNewline,217+ });218+ t.deepEqual(all, [stripFinalNewline ? foobarString : `${foobarString}\n`]);219+};220+221+test('result.all is split into lines when only stdout uses the lines option and stderr is ignored', testAllOneFdOtherLines, 'stdout', 'stderr', true, execa);222+test('result.all is split into lines when only stdout uses the lines option and stderr is ignored, stripFinalNewline false', testAllOneFdOtherLines, 'stdout', 'stderr', false, execa);223+test('result.all is split into lines when only stderr uses the lines option and stdout is ignored', testAllOneFdOtherLines, 'stderr', 'stdout', true, execa);224+test('result.all is split into lines when only stdout uses the lines option and stderr is ignored, sync', testAllOneFdOtherLines, 'stdout', 'stderr', true, execaSync);225+test('result.all is split into lines when only stderr uses the lines option and stdout is ignored, sync', testAllOneFdOtherLines, 'stderr', 'stdout', true, execaSync);modified:test/resolve/no-buffer.js+62 added
121 unmodified lines122 test('buffer: false does not return output, stdout [undefined], sync', testNoOutput, [undefined], false, execaSync);123 test('buffer: false does not return output, stdout [null], sync', testNoOutput, [null], false, execaSync);124 test('buffer: false does not return output, stdout ["pipe", undefined], sync', testNoOutput, ['pipe', undefined], false, execaSync);125 126+/*127+With `buffer: false`, additional file descriptors must still be passed to the subprocess.128+With synchronous methods, `ignore` is used to avoid buffering, but that only redirects `stdout`/`stderr` to `/dev/null`.129+For other file descriptors, it does not pass them at all, which would make the subprocess fail when writing to them.130+*/131+const testNoOutputFdStays = async (t, buffer, execaMethod) => {132+ const {stdio, failed} = await execaMethod('noop-fd.js', ['3', foobarString], {...fullStdio, buffer});133+ t.false(failed);134+ t.is(stdio[3], undefined);135+};136+137+test('buffer: false keeps stdio[*] open', testNoOutputFdStays, false, execa);138+test('buffer: false keeps stdio[*] open, fd-specific', testNoOutputFdStays, {fd3: false}, execa);139+test('buffer: false keeps stdio[*] open, sync', testNoOutputFdStays, false, execaSync);140+test('buffer: false keeps stdio[*] open, fd-specific, sync', testNoOutputFdStays, {fd3: false}, execaSync);141+142 const testNoOutputFail = async (t, execaMethod) => {143 const {exitCode, stdout} = await execaMethod('fail.js', {buffer: false, reject: false});144 t.is(exitCode, 2);145 t.is(stdout, undefined);19 unmodified lines165 test('buffer: {stdout: false}, all: true, sync', testNoOutputAll, {stdout: false}, false, true, execaSync);166 test('buffer: {stderr: false}, all: true, sync', testNoOutputAll, {stderr: false}, true, false, execaSync);167 test('buffer: {all: false}, all: true, sync', testNoOutputAll, {all: false}, false, false, execaSync);168 169+// `result.all` only contains the buffered file descriptors, so its `stripFinalNewline` must only follow those170+const testNoOutputAllStripFinalNewline = async (t, buffer, stripFinalNewline, execaMethod) => {171+ const {all} = await execaMethod('noop-both.js', {all: true, buffer, stripFinalNewline});172+ t.is(all, `${foobarString}\n`);173+};174+175+test('buffer: {stderr: false} only follows the buffered "stripFinalNewline" in result.all', testNoOutputAllStripFinalNewline, {stderr: false}, {stdout: false, stderr: true}, execa);176+test('buffer: {stdout: false} only follows the buffered "stripFinalNewline" in result.all', testNoOutputAllStripFinalNewline, {stdout: false}, {stdout: true, stderr: false}, execa);177+test('buffer: {stderr: false} only follows the buffered "stripFinalNewline" in result.all, sync', testNoOutputAllStripFinalNewline, {stderr: false}, {stdout: false, stderr: true}, execaSync);178+test('buffer: {stdout: false} only follows the buffered "stripFinalNewline" in result.all, sync', testNoOutputAllStripFinalNewline, {stdout: false}, {stdout: true, stderr: false}, execaSync);179+180+// `result.all` requires the `all` option, even when only one of the two file descriptors is buffered181+const testNoAllWithoutOption = async (t, buffer, execaMethod) => {182+ const {all} = await execaMethod('noop-both.js', {buffer, stripFinalNewline: false});183+ t.is(all, undefined);184+};185+186+test('buffer: {stdout: false} does not set result.all', testNoAllWithoutOption, {stdout: false}, execa);187+test('buffer: {stderr: false} does not set result.all', testNoAllWithoutOption, {stderr: false}, execa);188+test('buffer: {stdout: false} does not set result.all, sync', testNoAllWithoutOption, {stdout: false}, execaSync);189+test('buffer: {stderr: false} does not set result.all, sync', testNoAllWithoutOption, {stderr: false}, execaSync);190+191 const testTransform = async (t, objectMode, execaMethod) => {192 const lines = [];193 const {stdout} = await execaMethod('noop.js', {194 buffer: false,37 unmodified lines232 test('buffer: false > emits end event on stdio[*] when promise is rejected', testStreamEnd, 3, false);233 test('buffer: true > emits end event on stdout when promise is rejected', testStreamEnd, 1, true);234 test('buffer: true > emits end event on stderr when promise is rejected', testStreamEnd, 2, true);235 test('buffer: true > emits end event on stdio[*] when promise is rejected', testStreamEnd, 3, true);236+237+// When only one file descriptor is buffered, `result.all` is read from its own stream, not from `subprocess.all`.238+// `subprocess.all` still merges both file descriptors, so it must be drained. Otherwise, it fills up and applies backpressure on them, hanging the subprocess which is still writing to them.239+const LARGE_OUTPUT = 1e6;240+241+const testNoBufferAllBackpressure = async (t, fdNumber, buffer) => {242+ const {exitCode, all} = await execa('noop-fd-large.js', [`${fdNumber}`, `${LARGE_OUTPUT}`], {all: true, buffer});243+ t.is(exitCode, 0);244+ t.is(all.length, LARGE_OUTPUT);245+};246+247+// Without the fix, the subprocess hangs, so it is given a `timeout` to turn the hang into a failed assertion248+test('all: true does not hang on a large stdout with buffer: {stderr: false}', testNoBufferAllBackpressure, 1, {stderr: false});249+test('all: true does not hang on a large stderr with buffer: {stdout: false}', testNoBufferAllBackpressure, 2, {stdout: false});250+251+// `subprocess.all` must keep interleaving a file descriptor which is not buffered, even though Execa does not read it itself252+const testAllStreamWithNoBuffer = async (t, fdNumber, optionName) => {253+ const subprocess = execa('noop-fd.js', [`${fdNumber}`, foobarString], {all: true, buffer: {[optionName]: false}});254+ const [, allContents] = await Promise.all([subprocess, getStream(subprocess.all)]);255+ t.is(allContents, foobarString);256+};257+258+test('subprocess.all includes an unbuffered stdout', testAllStreamWithNoBuffer, 1, 'stdout');259+test('subprocess.all includes an unbuffered stderr', testAllStreamWithNoBuffer, 2, 'stderr');modified:test/verbose/output-enable.js+15 added
33 unmodified lines34 test('Prints stderr, verbose "full", sync', testPrintOutput, 'full', 2, true);35 test('Prints stdout, verbose "full", fd-specific, sync', testPrintOutput, stdoutFullOption, 1, true);36 test('Prints stderr, verbose "full", fd-specific, sync', testPrintOutput, stderrFullOption, 2, true);37 38+// `subprocess.all` is created and piped into by Execa itself, which must not disable `verbose`39+const testPrintOutputAll = async (t, verbose, isSync) => {40+ const {stderr} = await nestedSubprocess('noop-both.js', [foobarString], {verbose, all: true, isSync});41+ // An fd-specific `verbose` prints a single file descriptor, while `'full'` prints both42+ const lineCount = verbose === 'full' ? 2 : 1;43+ t.deepEqual(getOutputLines(stderr), Array.from({length: lineCount}, () => `${testTimestamp} [0] ${foobarString}`));44+};45+46+test('Prints stdout and stderr, verbose "full", all: true', testPrintOutputAll, 'full', false);47+test('Prints stdout, verbose "full", all: true, fd-specific', testPrintOutputAll, stdoutFullOption, false);48+test('Prints stderr, verbose "full", all: true, fd-specific', testPrintOutputAll, stderrFullOption, false);49+test('Prints stdout and stderr, verbose "full", all: true, sync', testPrintOutputAll, 'full', true);50+test('Prints stdout, verbose "full", all: true, fd-specific, sync', testPrintOutputAll, stdoutFullOption, true);51+test('Prints stderr, verbose "full", all: true, fd-specific, sync', testPrintOutputAll, stderrFullOption, true);52+53 const testNoPrintOutput = async (t, verbose, fdNumber, isSync) => {54 const {stderr} = await nestedSubprocess('noop-fd.js', [`${fdNumber}`, foobarString], {verbose, ...fullStdio, isSync});55 t.is(getOutputLine(stderr), undefined);56 };108 unmodified linesmodified:types/return/result.d.ts+1 added-1 removed
32 unmodified lines33 The output of the subprocess with `result.stdout` and `result.stderr` interleaved.34 35 This requires the `all` option to be `true`.36 37- This is `undefined` if both `stdout` and `stderr` options are set to only `'inherit'`, `'ignore'`, `Writable` or `integer`, or if the `buffer` option is `false`.37+ This is `undefined` if both `stdout` and `stderr` options are set to only `'inherit'`, `'ignore'`, `Writable` or `integer`, or if the `buffer` option is `false` for both.38 39 This is an array if the `lines` option is `true`, or if either the `stdout` or `stderr` option is a transform in object mode.40 */41 all: ResultAll<OptionsType>;162 unmodified linesmodified:types/subprocess/subprocess.d.ts+1 added-1 removed
23 unmodified lines24 Stream combining/interleaving `subprocess.stdout` and `subprocess.stderr`.25 26 This requires the `all` option to be `true`.27 28- This is `undefined` if `stdout` and `stderr` options are set to `'inherit'`, `'ignore'`, `Writable` or `integer`, or if the `buffer` option is `false`.28+ This is `undefined` if both `stdout` and `stderr` options are set to `'inherit'`, `'ignore'`, `Writable` or `integer`.29 */30 all: SubprocessAll<OptionsType>;31 32 /**116 unmodified lines