|
1 | 1 | import test from 'ava'; |
| 2 | +import {execa} from '../../index.js'; |
2 | 3 | import {setFixtureDirectory} from '../helpers/fixtures-directory.js'; |
3 | 4 | import {foobarString} from '../helpers/input.js'; |
4 | 5 | import {runVerboseSubprocess} from '../helpers/verbose.js'; |
@@ -65,3 +66,37 @@ const testCommandThrowWrap = async (t, type, options, isSync) => { |
65 | 66 | test('Propagate wrapped exception in "verbose" function, "output", spawn error', testCommandThrowWrap, 'output', earlyErrorOptions, false); |
66 | 67 | test('Propagate wrapped exception in "verbose" function, "ipc", spawn error', testCommandThrowWrap, 'ipc', earlyErrorOptions, false); |
67 | 68 | test('Propagate wrapped exception in "verbose" function, "output", spawn error, sync', testCommandThrowWrap, 'output', earlyErrorOptionsSync, true); |
| 69 | + |
| 70 | +// A throwing `verbose` function is a logging error: it must not discard IPC messages which have already been buffered |
| 71 | +const getThrowingVerbose = (type, thrownValue = new Error(foobarString)) => ({ |
| 72 | + verbose: (line, verboseObject) => { |
| 73 | + if (verboseObject.type === type) { |
| 74 | + throw thrownValue; |
| 75 | + } |
| 76 | + }, |
| 77 | +}); |
| 78 | + |
| 79 | +test('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 | +}); |
| 84 | + |
| 85 | +// A `verbose` function might throw a non-Error, or a falsy value. Neither must be mistaken for no error at all |
| 86 | +test('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 | +}); |
| 91 | + |
| 92 | +test('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 | +}); |
| 97 | + |
| 98 | +test('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 | +}); |
0 commit comments