Skip to content
Myr

Fix `verbose` discarding IPC messages when its function throws

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 59de065 · Browse files · 6 files+60 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+60 added−10 removed

  • tests:+40 added−0 removed
  • other:+20 added−10 removed

6 files+60 added−10 removed

modified:docs/debugging.md+2 added
114 unmodified lines
115It is called once per log line. The first argument is the default log line string. The second argument is the same information but as an object instead (documented [here](api.md#verbose-object)).116117If a string is returned, it is printed on `stderr`. If `undefined` is returned, nothing is printed.118119If the function throws, the subprocess fails with that error as [`error.cause`](api.md#errorcause). If it threw while logging the output of [`stdout`](api.md#resultstdout), [`stderr`](api.md#resultstderr) or another file descriptor, that file descriptor's output is then not kept.120121### Filter logs122123```js124import {execa as execa_} from 'execa';
80 unmodified lines
modified:lib/io/contents.js+4 added-3 removed
modified:lib/ipc/buffer-messages.js+11 added-1 removed
modified:lib/verbose/log.js+3 added-6 removed
41 unmodified lines
42 const verboseLine = defaultVerboseFunction(verboseObject);43 return {verboseLine, verboseObject};44};4546// Serialize any type to a line string, for logging46// Serialize any type to a line string, for logging.47// Tabs are escaped by `escapeLines()`, like any other control character.48export const serializeVerboseMessage = message => {49 const messageString = typeof message === 'string' ? message : inspect(message);49 const escapedMessage = escapeLines(messageString);50 return escapedMessage.replaceAll('\t', () => ' '.repeat(TAB_SIZE));50 return escapeLines(messageString);51};5253// Same as `util.inspect()`54const TAB_SIZE = 2;
modified:test/verbose/custom-throw.js+35 added
1import test from 'ava';2import {execa} from '../../index.js';3import {setFixtureDirectory} from '../helpers/fixtures-directory.js';4import {foobarString} from '../helpers/input.js';5import {runVerboseSubprocess} from '../helpers/verbose.js';6import {earlyErrorOptions, earlyErrorOptionsSync} from '../helpers/early-error.js';
58 unmodified lines
6566test('Propagate wrapped exception in "verbose" function, "output", spawn error', testCommandThrowWrap, 'output', earlyErrorOptions, false);67test('Propagate wrapped exception in "verbose" function, "ipc", spawn error', testCommandThrowWrap, 'ipc', earlyErrorOptions, false);68test('Propagate wrapped exception in "verbose" function, "output", spawn error, sync', testCommandThrowWrap, 'output', earlyErrorOptionsSync, true);6970// A throwing `verbose` function is a logging error: it must not discard IPC messages which have already been buffered71const getThrowingVerbose = (type, thrownValue = new Error(foobarString)) => ({72 verbose: (line, verboseObject) => {73 if (verboseObject.type === type) {74 throw thrownValue;75 }76 },77});7879test('IPC output is kept when the "verbose" function throws, "ipc"', async t => {80 const {cause, ipcOutput} = await t.throwsAsync(execa('ipc-send-twice.js', {ipc: true, ...getThrowingVerbose('ipc')}));81 t.is(cause.message, foobarString);82 t.deepEqual(ipcOutput, ['foo', 'bar']);83});8485// A `verbose` function might throw a non-Error, or a falsy value. Neither must be mistaken for no error at all86test('A non-Error thrown by the "verbose" function is propagated, "output"', async t => {87 const {cause, stdout} = await t.throwsAsync(execa('noop.js', [foobarString], getThrowingVerbose('output', foobarString)));88 t.is(cause, foobarString);89 t.is(stdout, undefined);90});9192test('A null value thrown by the "verbose" function is propagated, "output"', async t => {93 const {cause, stdout} = await t.throwsAsync(execa('noop.js', [foobarString], getThrowingVerbose('output', null)));94 t.is(cause, null);95 t.is(stdout, undefined);96});9798test('IPC output is kept when the "verbose" function throws a falsy value, "ipc"', async t => {99 const {cause, ipcOutput} = await t.throwsAsync(execa('ipc-send-twice.js', {ipc: true, ...getThrowingVerbose('ipc', 0)}));100 t.is(cause, 0);101 t.deepEqual(ipcOutput, ['foo', 'bar']);102});
modified:test/verbose/output-enable.js+5 added
112 unmodified lines
113 const {stderr} = await nestedSubprocess('noop.js', ['\u{1}'], {verbose: 'full'});114 t.is(getOutputLine(stderr), `${testTimestamp} [0] \\u0001`);115});116117test('Escapes tabs from stdout', async t => {118 const {stderr} = await nestedSubprocess('noop.js', [`${foobarString}\t${foobarString}`], {verbose: 'full'});119 t.is(getOutputLine(stderr), `${testTimestamp} [0] ${foobarString}\\t${foobarString}`);120});121122const testStdioSame = async (t, fdNumber) => {123 const {nestedResult: {stdio}} = await nestedSubprocess('noop-fd.js', [`${fdNumber}`, foobarString], {verbose: 'full'});124 t.is(stdio[fdNumber], foobarString);125};
24 unmodified lines