Add support for `verbose` option to `execaSync()` (#950)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 7c2f575 · Browse files · 5 files·+36 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+36 added−25 removed
- tests:+25 added−15 removed
- other:+11 added−10 removed
Provenance
- Commit
ed052d4- Tree
4677dfc- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
7c2f575
5 files changed+36 added−25 removed
modified:lib/stdio/handle.js+1 added-1 removed
107 unmodified linesExpand all10810911011111211211311411511618 unmodified lines107 unmodified linesExpand allconst addInternalStdioItems = ({stdioItems, fdNumber, optionName, options, isSync, direction, stdioState, verboseInfo, outputLines, objectMode}) => willPipeFileDescriptor(stdioItems) ? [ ...stdioItems, ...handleStreamsEncoding({options, direction, optionName, objectMode}), ...handleStreamsVerbose({stdioItems, options, isSync, stdioState, verboseInfo, fdNumber, optionName}), ...handleStreamsVerbose({stdioItems, options, stdioState, verboseInfo, fdNumber, optionName}), ...handleStreamsLines({options, isSync, direction, optionName, objectMode, outputLines}), ] : stdioItems;
18 unmodified linesmodified:lib/verbose/output.js+7 added-6 removed
3 unmodified lines45678891011124 unmodified lines17181920212221222324253 unmodified lines2930313234353334353637385 unmodified lines444546474948495051525354556 unmodified lines3 unmodified linesimport {PIPED_STDIO_VALUES} from '../stdio/forward.js';import {TRANSFORM_TYPES} from '../stdio/generator.js';import {verboseLog} from './log.js';
export const handleStreamsVerbose = ({stdioItems, options, isSync, stdioState, verboseInfo, fdNumber, optionName}) => shouldLogOutput({stdioItems, options, isSync, verboseInfo, fdNumber})export const handleStreamsVerbose = ({stdioItems, options, stdioState, verboseInfo, fdNumber, optionName}) => shouldLogOutput({stdioItems, options, verboseInfo, fdNumber}) ? [{ type: 'generator', value: verboseGenerator.bind(undefined, {stdioState, fdNumber, verboseInfo}), optionName,4 unmodified lines// `ipc` cannot use piping.// `inherit` would result in double printing.// They can also lead to double printing when passing file descriptor integers or `process.std*`.// This only leaves with `pipe` and `overlapped`.const shouldLogOutput = ({stdioItems, options: {encoding}, isSync, verboseInfo: {verbose}, fdNumber}) => verbose === 'full' && !isSyncconst shouldLogOutput = ({stdioItems, options: {encoding}, verboseInfo: {verbose}, fdNumber}) => verbose === 'full' && !BINARY_ENCODINGS.has(encoding) && fdUsesVerbose(fdNumber) && (stdioItems.some(({type, value}) => type === 'native' && PIPED_STDIO_VALUES.has(value)) || stdioItems.every(({type}) => TRANSFORM_TYPES.has(type)));3 unmodified lines// We could print `stdio[3+]` but it often is redirected to files and streams, with the same issue.// So we only print stdout and stderr.const fdUsesVerbose = fdNumber => fdNumber === 1 || fdNumber === 2;
const verboseGenerator = function * ({stdioState: {subprocess: {stdio}}, fdNumber, verboseInfo}, line) { if (!isPiping(stdio[fdNumber])) {const verboseGenerator = function * ({stdioState, fdNumber, verboseInfo}, line) { if (!isPiping(stdioState, fdNumber)) { logOutput(line, verboseInfo); }
yield line;5 unmodified lines// For example, `subprocess.stdout.pipe(process.stdin)` would print each line twice.// - When chaining subprocesses with `subprocess.pipe(otherSubprocess)`, only the last one should print its output.// Detecting whether `.pipe()` is impossible without monkey-patching it, so we use the following undocumented property.// This is not a critical behavior since changes of the following property would only make `verbose` more verbose.const isPiping = stream => stream._readableState.pipes.length > 0;const isPiping = (stdioState, fdNumber) => stdioState.subprocess !== undefined && isPipingStream(stdioState.subprocess.stdio[fdNumber]);
const isPipingStream = stream => stream._readableState.pipes.length > 0;
// When `verbose` is `full`, print stdout|stderrconst logOutput = (line, {verboseId}) => { const lines = typeof line === 'string' ? line : inspect(line); const escapedLines = escapeLines(lines);6 unmodified linesmodified:test/verbose/output.js+25 added-15 removed
27 unmodified lines28 t.is(getOutputLine(stderr), `${testTimestamp} [0] ${foobarString}`);29 };30 31 test('Prints stdout, verbose "full"', testPrintOutput, 1, nestedExecaAsync);32+test('Prints stdout, verbose "full", sync', testPrintOutput, 1, nestedExecaSync);33 test('Prints stderr, verbose "full"', testPrintOutput, 2, nestedExecaAsync);34+test('Prints stderr, verbose "full", sync', testPrintOutput, 2, nestedExecaSync);35 36 const testNoPrintOutput = async (t, verbose, fdNumber, execaMethod) => {37 const {stderr} = await execaMethod('noop-fd.js', [`${fdNumber}`, foobarString], {verbose, ...fullStdio});38 t.is(getOutputLine(stderr), undefined);39 };40 41 test('Does not print stdout, verbose "none"', testNoPrintOutput, 'none', 1, nestedExecaAsync);42 test('Does not print stdout, verbose "short"', testNoPrintOutput, 'short', 1, nestedExecaAsync);41-test('Does not print stdout, verbose "full", sync', testNoPrintOutput, 'full', 1, nestedExecaSync);43 test('Does not print stdout, verbose "none", sync', testNoPrintOutput, 'none', 1, nestedExecaSync);44 test('Does not print stdout, verbose "short", sync', testNoPrintOutput, 'short', 1, nestedExecaSync);45 test('Does not print stderr, verbose "none"', testNoPrintOutput, 'none', 2, nestedExecaAsync);46 test('Does not print stderr, verbose "short"', testNoPrintOutput, 'short', 2, nestedExecaAsync);46-test('Does not print stderr, verbose "full", sync', testNoPrintOutput, 'full', 2, nestedExecaSync);47 test('Does not print stderr, verbose "none", sync', testNoPrintOutput, 'none', 2, nestedExecaSync);48 test('Does not print stderr, verbose "short", sync', testNoPrintOutput, 'short', 2, nestedExecaSync);49 test('Does not print stdio[*], verbose "none"', testNoPrintOutput, 'none', 3, nestedExecaAsync);50 test('Does not print stdio[*], verbose "short"', testNoPrintOutput, 'short', 3, nestedExecaAsync);1 unmodified line52 test('Does not print stdio[*], verbose "none", sync', testNoPrintOutput, 'none', 3, nestedExecaSync);53 test('Does not print stdio[*], verbose "short", sync', testNoPrintOutput, 'short', 3, nestedExecaSync);54 test('Does not print stdio[*], verbose "full", sync', testNoPrintOutput, 'full', 3, nestedExecaSync);55 56-test('Prints stdout after errors', async t => {57- const stderr = await runErrorSubprocess(t, 'full', nestedExecaAsync);56+const testPrintError = async (t, execaMethod) => {57+ const stderr = await runErrorSubprocess(t, 'full', execaMethod);58 t.is(getOutputLine(stderr), `${testTimestamp} [0] ${foobarString}`);59-});59+};60 61+test('Prints stdout after errors', testPrintError, nestedExecaAsync);62+test('Prints stdout after errors, sync', testPrintError, nestedExecaSync);63+64 const testPipeOutput = async (t, fixtureName, sourceVerbose, destinationVerbose) => {65 const {stderr} = await execa(`nested-pipe-${fixtureName}.js`, [66 JSON.stringify(getVerboseOption(sourceVerbose, 'full')),67 'noop.js',118 unmodified lines186 subprocess.kill();187 await t.throwsAsync(subprocess);188 });189 187-test('Prints stdout, single newline', async t => {188- const {stderr} = await nestedExecaAsync('noop-fd.js', ['1', '\n'], {verbose: 'full'});190+const testSingleNewline = async (t, execaMethod) => {191+ const {stderr} = await execaMethod('noop-fd.js', ['1', '\n'], {verbose: 'full'});192 t.deepEqual(getOutputLines(stderr), [`${testTimestamp} [0] `]);190-});193+};194 195+test('Prints stdout, single newline', testSingleNewline, nestedExecaAsync);196+test('Prints stdout, single newline, sync', testSingleNewline, nestedExecaSync);197+198 test('Can use encoding UTF16, verbose "full"', async t => {199 const {stderr} = await nestedExeca('nested-input.js', 'stdin.js', {verbose: 'full', encoding: 'utf16le'});200 t.is(getOutputLine(stderr), `${testTimestamp} [0] ${foobarString}`);201 });25 unmodified lines227 228 test('Does not print stdout, stdout { file }', testStdoutFile, 'nested.js', file => ({file}));229 test('Does not print stdout, stdout fileUrl', testStdoutFile, 'nested-file-url.js', file => file);230 225-const testPrintOutputOptions = async (t, options) => {226- const {stderr} = await nestedExecaAsync('noop.js', [foobarString], {verbose: 'full', ...options});231+const testPrintOutputOptions = async (t, options, execaMethod) => {232+ const {stderr} = await execaMethod('noop.js', [foobarString], {verbose: 'full', ...options});233 t.is(getOutputLine(stderr), `${testTimestamp} [0] ${foobarString}`);234 };235 230-test('Prints stdout, stdout "pipe"', testPrintOutputOptions, {stdout: 'pipe'});231-test('Prints stdout, stdout "overlapped"', testPrintOutputOptions, {stdout: 'overlapped'});232-test('Prints stdout, stdout null', testPrintOutputOptions, {stdout: null});233-test('Prints stdout, stdout ["pipe"]', testPrintOutputOptions, {stdout: ['pipe']});234-test('Prints stdout, buffer false', testPrintOutputOptions, {buffer: false});236+test('Prints stdout, stdout "pipe"', testPrintOutputOptions, {stdout: 'pipe'}, nestedExecaAsync);237+test('Prints stdout, stdout "overlapped"', testPrintOutputOptions, {stdout: 'overlapped'}, nestedExecaAsync);238+test('Prints stdout, stdout null', testPrintOutputOptions, {stdout: null}, nestedExecaAsync);239+test('Prints stdout, stdout ["pipe"]', testPrintOutputOptions, {stdout: ['pipe']}, nestedExecaAsync);240+test('Prints stdout, buffer false', testPrintOutputOptions, {buffer: false}, nestedExecaAsync);241+test('Prints stdout, stdout "pipe", sync', testPrintOutputOptions, {stdout: 'pipe'}, nestedExecaSync);242+test('Prints stdout, stdout null, sync', testPrintOutputOptions, {stdout: null}, nestedExecaSync);243+test('Prints stdout, stdout ["pipe"], sync', testPrintOutputOptions, {stdout: ['pipe']}, nestedExecaSync);244+test('Prints stdout, buffer false, sync', testPrintOutputOptions, {buffer: false}, nestedExecaSync);245 246 const testPrintOutputFixture = async (t, fixtureName, ...args) => {247 const {stderr} = await nestedExeca(fixtureName, 'noop.js', [foobarString, ...args], {verbose: 'full'});248 t.is(getOutputLine(stderr), `${testTimestamp} [0] ${foobarString}`);4 unmodified linesmodified:index.d.ts+2 added-2 removed
1401 unmodified lines1402 Same as `execa()` but synchronous.1403 1404 Returns or throws a `subprocessResult`. The `subprocess` is not returned: its methods and properties are not available. This includes [`.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal), [`.pid`](https://nodejs.org/api/child_process.html#subprocesspid), `.pipe()`, `.iterable()`, `.readable()`, `.writable()`, `.duplex()` and the [`.stdin`/`.stdout`/`.stderr`](https://nodejs.org/api/child_process.html#subprocessstdout) streams.1405 1406-Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `cancelSignal`, `forceKillAfterDelay`, `lines` and `verbose: 'full'`. Also, the `stdin`, `stdout`, `stderr` and `stdio` options cannot be a `['pipe', 'inherit']` array, [`'overlapped'`](https://nodejs.org/api/child_process.html#optionsstdio), an async iterable, an async transform, a `Duplex`, or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.1406+Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `cancelSignal`, `forceKillAfterDelay` and `lines`. Also, the `stdin`, `stdout`, `stderr` and `stdio` options cannot be a `['pipe', 'inherit']` array, [`'overlapped'`](https://nodejs.org/api/child_process.html#optionsstdio), an async iterable, an async transform, a `Duplex`, or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.1407 1408 @param file - The program/script to execute, as a string or file URL1409 @param arguments - Arguments to pass to `file` on execution.1410 @returns A `subprocessResult` object105 unmodified lines1516 Same as `execaCommand()` but synchronous.1517 1518 Returns or throws a `subprocessResult`. The `subprocess` is not returned: its methods and properties are not available. This includes [`.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal), [`.pid`](https://nodejs.org/api/child_process.html#subprocesspid), `.pipe()`, `.iterable()`, `.readable()`, `.writable()`, `.duplex()` and the [`.stdin`/`.stdout`/`.stderr`](https://nodejs.org/api/child_process.html#subprocessstdout) streams.1519 1520-Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `cancelSignal`, `forceKillAfterDelay`, `lines` and `verbose: 'full'`. Also, the `stdin`, `stdout`, `stderr` and `stdio` options cannot be a `['pipe', 'inherit']` array, [`'overlapped'`](https://nodejs.org/api/child_process.html#optionsstdio), an async iterable, an async transform, a `Duplex`, or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.1520+Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `cancelSignal`, `forceKillAfterDelay` and `lines`. Also, the `stdin`, `stdout`, `stderr` and `stdio` options cannot be a `['pipe', 'inherit']` array, [`'overlapped'`](https://nodejs.org/api/child_process.html#optionsstdio), an async iterable, an async transform, a `Duplex`, or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.1521 1522 @param command - The program/script to execute and its arguments.1523 @returns A `subprocessResult` object1524 @throws A `subprocessResult` error128 unmodified linesmodified:readme.md+1 added-1 removed
317 unmodified lines318 Same as [`execa()`](#execafile-arguments-options) but synchronous.319 320 Returns or throws a [`subprocessResult`](#subprocessResult). The [`subprocess`](#subprocess) is not returned: its methods and properties are not available. This includes [`.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal), [`.pid`](https://nodejs.org/api/child_process.html#subprocesspid), [`.pipe()`](#pipefile-arguments-options), [`.iterable()`](#iterablereadableoptions), [`.readable()`](#readablereadableoptions), [`.writable()`](#writablewritableoptions), [`.duplex()`](#duplexduplexoptions) and the [`.stdin`/`.stdout`/`.stderr`](https://nodejs.org/api/child_process.html#subprocessstdout) streams.321 322-Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`ipc`](#ipc), [`serialization`](#serialization), [`cancelSignal`](#cancelsignal), [`forceKillAfterDelay`](#forcekillafterdelay), [`lines`](#lines) and [`verbose: 'full'`](#verbose). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1) and [`stdio`](#stdio-1) cannot be a [`['pipe', 'inherit']`](#redirect-stdinstdoutstderr-to-multiple-destinations) array, [`'overlapped'`](https://nodejs.org/api/child_process.html#optionsstdio), an async iterable, an async [transform](docs/transform.md), a [`Duplex`](docs/transform.md#duplextransform-streams), or a web stream. Node.js streams [must have a file descriptor](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr) unless the `input` option is used.322+Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`ipc`](#ipc), [`serialization`](#serialization), [`cancelSignal`](#cancelsignal), [`forceKillAfterDelay`](#forcekillafterdelay) and [`lines`](#lines). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1) and [`stdio`](#stdio-1) cannot be a [`['pipe', 'inherit']`](#redirect-stdinstdoutstderr-to-multiple-destinations) array, [`'overlapped'`](https://nodejs.org/api/child_process.html#optionsstdio), an async iterable, an async [transform](docs/transform.md), a [`Duplex`](docs/transform.md#duplextransform-streams), or a web stream. Node.js streams [must have a file descriptor](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr) unless the `input` option is used.323 324 #### $(file, arguments?, options?)325 326 `file`: `string | URL`\899 unmodified lines