Print errors with verbose option (#890)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 7264bcf · Browse files · 7 files·+151 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+151 added−2 removed
- tests:+137 added−0 removed
- other:+14 added−2 removed
Provenance
- Commit
209f78f- Tree
9a9bf11- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
7264bcf
7 files changed+151 added−2 removed
modified:lib/verbose/complete.js+2 added
1234567899 unmodified lines19202122232425262712 unmodified linesimport prettyMs from 'pretty-ms';import {escapeLines} from '../arguments/escape.js';import {verboseLog} from './log.js';import {getCommandDuration} from './info.js';import {logError} from './error.js';
// When `verbose` is `short|full`, print each command's completion, duration and errorexport const logFinalResult = ({shortMessage, failed}, reject, verboseInfo) => { logResult(shortMessage, failed, reject, verboseInfo);9 unmodified lines return; }
const icon = getIcon(failed, reject); logError({message, failed, verboseId, icon}); logDuration(startTime, verboseId, icon);};
const logDuration = (startTime, verboseId, icon) => {12 unmodified linesadded:lib/verbose/error.js+10 added
12345678910import {verboseLog} from './log.js';
// When `verbose` is `short|full`, print each command's error when it failsexport const logError = ({message, failed, verboseId, icon}) => { if (!failed) { return; }
verboseLog(message, verboseId, icon);};modified:test/helpers/verbose.js+4 added
21 unmodified lines2223242526272829307 unmodified lines38394041424344454647488 unmodified lines21 unmodified lines};
export const runWarningProcess = async (t, execaMethod) => { const {stderr} = await execaMethod('noop-fail.js', ['1', foobarString], {verbose: 'short', reject: false}); t.true(stderr.includes('exit code 2')); return stderr;};
export const runEarlyErrorProcess = async (t, execaMethod) => {7 unmodified linesconst isCommandLine = line => line.includes(' $ ') || line.includes(' | ');export const getOutputLine = stderr => getOutputLines(stderr)[0];export const getOutputLines = stderr => getNormalizedLines(stderr).filter(line => isOutputLine(line));const isOutputLine = line => line.includes('] ');export const getErrorLine = stderr => getErrorLines(stderr)[0];export const getErrorLines = stderr => getNormalizedLines(stderr).filter(line => isErrorLine(line));const isErrorLine = line => (line.includes(' × ') || line.includes(' ‼ ')) && !isCompletionLine(line);export const getCompletionLine = stderr => getCompletionLines(stderr)[0];export const getCompletionLines = stderr => getNormalizedLines(stderr).filter(line => isCompletionLine(line));const isCompletionLine = line => line.includes('(done in');export const getNormalizedLines = stderr => splitLines(normalizeStderr(stderr));8 unmodified linesadded:test/verbose/error.js+132 added
1+import test from 'ava';2+import {red} from 'yoctocolors';3+import {execa} from '../../index.js';4+import {setFixtureDir} from '../helpers/fixtures-dir.js';5+import {foobarString} from '../helpers/input.js';6+import {7+ QUOTE,8+ nestedExeca,9+ nestedExecaAsync,10+ nestedExecaSync,11+ runEarlyErrorProcess,12+ getErrorLine,13+ getErrorLines,14+ testTimestamp,15+ getVerboseOption,16+} from '../helpers/verbose.js';17+18+setFixtureDir();19+20+const nestedExecaFail = nestedExeca.bind(undefined, 'nested-fail.js');21+22+const testPrintError = async (t, verbose, execaMethod) => {23+ const {stderr} = await t.throwsAsync(execaMethod('noop-fail.js', ['1', foobarString], {verbose}));24+ t.is(getErrorLine(stderr), `${testTimestamp} [0] × Command failed with exit code 2: noop-fail.js 1 ${foobarString}`);25+};26+27+test('Prints error, verbose "short"', testPrintError, 'short', nestedExecaAsync);28+test('Prints error, verbose "full"', testPrintError, 'full', nestedExecaAsync);29+test('Prints error, verbose "short", sync', testPrintError, 'short', nestedExecaSync);30+test('Prints error, verbose "full", sync', testPrintError, 'full', nestedExecaSync);31+32+const testNoPrintError = async (t, execaMethod) => {33+ const {stderr} = await t.throwsAsync(execaMethod('noop-fail.js', ['1', foobarString], {verbose: 'none'}));34+ t.not(stderr, '');35+ t.is(getErrorLine(stderr), undefined);36+};37+38+test('Does not print error, verbose "none"', testNoPrintError, nestedExecaAsync);39+test('Does not print error, verbose "none", sync', testNoPrintError, nestedExecaSync);40+41+const testPrintNoError = async (t, execaMethod) => {42+ const {stderr} = await execaMethod('noop.js', [foobarString], {verbose: 'short'});43+ t.is(getErrorLine(stderr), undefined);44+};45+46+test('Does not print error if none', testPrintNoError, nestedExecaAsync);47+test('Does not print error if none, sync', testPrintNoError, nestedExecaSync);48+49+const testPrintErrorEarly = async (t, execaMethod) => {50+ const stderr = await runEarlyErrorProcess(t, execaMethod);51+ t.is(getErrorLine(stderr), `${testTimestamp} [0] × TypeError: The "cwd" option must be a string or a file URL: true.`);52+};53+54+test('Prints early validation error', testPrintErrorEarly, nestedExecaAsync);55+test('Prints early validation error, sync', testPrintErrorEarly, nestedExecaSync);56+57+test('Does not repeat stdout|stderr with error', async t => {58+ const {stderr} = await t.throwsAsync(nestedExecaAsync('noop-fail.js', ['1', foobarString], {verbose: 'short'}));59+ t.deepEqual(getErrorLines(stderr), [`${testTimestamp} [0] × Command failed with exit code 2: noop-fail.js 1 ${foobarString}`]);60+});61+62+test('Prints error differently if "reject" is false', async t => {63+ const {stderr} = await nestedExecaAsync('noop-fail.js', ['1', foobarString], {verbose: 'short', reject: false});64+ t.deepEqual(getErrorLines(stderr), [`${testTimestamp} [0] ‼ Command failed with exit code 2: noop-fail.js 1 ${foobarString}`]);65+});66+67+const testPipeError = async (t, fixtureName, sourceVerbose, destinationVerbose) => {68+ const {stderr} = await t.throwsAsync(execa(`nested-pipe-${fixtureName}.js`, [69+ JSON.stringify(getVerboseOption(sourceVerbose)),70+ 'noop-fail.js',71+ '1',72+ JSON.stringify(getVerboseOption(destinationVerbose)),73+ 'stdin-fail.js',74+ ]));75+76+ const lines = getErrorLines(stderr);77+ t.is(lines.includes(`${testTimestamp} [0] × Command failed with exit code 2: noop-fail.js 1`), sourceVerbose);78+ t.is(lines.includes(`${testTimestamp} [${sourceVerbose ? 1 : 0}] × Command failed with exit code 2: stdin-fail.js`), destinationVerbose);79+};80+81+test('Prints both errors piped with .pipe("file")', testPipeError, 'file', true, true);82+test('Prints both errors piped with .pipe`command`', testPipeError, 'script', true, true);83+test('Prints both errors piped with .pipe(childProcess)', testPipeError, 'process', true, true);84+test('Prints first error piped with .pipe("file")', testPipeError, 'file', true, false);85+test('Prints first error piped with .pipe`command`', testPipeError, 'script', true, false);86+test('Prints first error piped with .pipe(childProcess)', testPipeError, 'process', true, false);87+test('Prints second error piped with .pipe("file")', testPipeError, 'file', false, true);88+test('Prints second error piped with .pipe`command`', testPipeError, 'script', false, true);89+test('Prints second error piped with .pipe(childProcess)', testPipeError, 'process', false, true);90+test('Prints neither errors piped with .pipe("file")', testPipeError, 'file', false, false);91+test('Prints neither errors piped with .pipe`command`', testPipeError, 'script', false, false);92+test('Prints neither errors piped with .pipe(childProcess)', testPipeError, 'process', false, false);93+94+test('Quotes spaces from error', async t => {95+ const {stderr} = await t.throwsAsync(nestedExecaFail('noop-forever.js', ['foo bar'], {verbose: 'short'}));96+ t.deepEqual(getErrorLines(stderr), [97+ `${testTimestamp} [0] × Command was killed with SIGTERM (Termination): noop-forever.js ${QUOTE}foo bar${QUOTE}`,98+ `${testTimestamp} [0] × foo bar`,99+ ]);100+});101+102+test('Quotes special punctuation from error', async t => {103+ const {stderr} = await t.throwsAsync(nestedExecaFail('noop-forever.js', ['%'], {verbose: 'short'}));104+ t.deepEqual(getErrorLines(stderr), [105+ `${testTimestamp} [0] × Command was killed with SIGTERM (Termination): noop-forever.js ${QUOTE}%${QUOTE}`,106+ `${testTimestamp} [0] × %`,107+ ]);108+});109+110+test('Does not escape internal characters from error', async t => {111+ const {stderr} = await t.throwsAsync(nestedExecaFail('noop-forever.js', ['ã'], {verbose: 'short'}));112+ t.deepEqual(getErrorLines(stderr), [113+ `${testTimestamp} [0] × Command was killed with SIGTERM (Termination): noop-forever.js ${QUOTE}ã${QUOTE}`,114+ `${testTimestamp} [0] × ã`,115+ ]);116+});117+118+test('Escapes and strips color sequences from error', async t => {119+ const {stderr} = await t.throwsAsync(nestedExecaFail('noop-forever.js', [red(foobarString)], {verbose: 'short'}, {env: {FORCE_COLOR: '1'}}));120+ t.deepEqual(getErrorLines(stderr), [121+ `${testTimestamp} [0] × Command was killed with SIGTERM (Termination): noop-forever.js ${QUOTE}\\u001b[31m${foobarString}\\u001b[39m${QUOTE}`,122+ `${testTimestamp} [0] × ${foobarString}`,123+ ]);124+});125+126+test('Escapes control characters from error', async t => {127+ const {stderr} = await t.throwsAsync(nestedExecaFail('noop-forever.js', ['\u0001'], {verbose: 'short'}));128+ t.deepEqual(getErrorLines(stderr), [129+ `${testTimestamp} [0] × Command was killed with SIGTERM (Termination): noop-forever.js ${QUOTE}\\u0001${QUOTE}`,130+ `${testTimestamp} [0] × \\u0001`,131+ ]);132+});modified:test/verbose/info.js+1 added
19 unmodified lines20212223242526272814 unmodified lines19 unmodified lines `${testTimestamp} [0] $ node -e ${QUOTE}console.error(1)${QUOTE}`, '1', `${testTimestamp} [0] √ (done in 0ms)`, `${testTimestamp} [1] $ node -e ${QUOTE}process.exit(2)${QUOTE}`, `${testTimestamp} [1] ‼ Command failed with exit code 2: node -e ${QUOTE}process.exit(2)${QUOTE}`, `${testTimestamp} [1] ‼ (done in 0ms)`, ]);});
14 unmodified linesmodified:index.d.ts+1 added-1 removed
541 unmodified lines542 */543 readonly windowsHide?: boolean;544 545 /**546- If `verbose` is `'short'` or `'full'`, prints each command on `stderr` before executing it. When the command completes, prints its duration.546+ If `verbose` is `'short'` or `'full'`, prints each command on `stderr` before executing it. When the command completes, prints its duration and (if it failed) its error.547 548 If `verbose` is `'full'`, the command's `stdout` and `stderr` are printed too, unless either:549 - the `stdout`/`stderr` option is `ignore` or `inherit`.550 - the `stdout`/`stderr` is redirected to [a stream](https://nodejs.org/api/stream.html#readablepipedestination-options), a file, a file descriptor, or another child process.835 unmodified linesmodified:readme.md+1 added-1 removed
667 unmodified lines668 669 Type: `'none' | 'short' | 'full'`\670 Default: `'none'`671 672-If `verbose` is `'short'` or `'full'`, [prints each command](#verbose-mode) on `stderr` before executing it. When the command completes, prints its duration.672+If `verbose` is `'short'` or `'full'`, [prints each command](#verbose-mode) on `stderr` before executing it. When the command completes, prints its duration and (if it failed) its error.673 674 If `verbose` is `'full'`, the command's `stdout` and `stderr` are printed too, unless either:675 - the [`stdout`](#stdout-1)/[`stderr`](#stderr-1) option is `ignore` or `inherit`.676 - the `stdout`/`stderr` is redirected to [a stream](https://nodejs.org/api/stream.html#readablepipedestination-options), [a file](#stdout-1), a file descriptor, or [another child process](#pipefile-arguments-options).373 unmodified lines