Fix `reject` option with early spawning errors (#734)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 66d12bf · Browse files · 3 files·+31 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+31 added−11 removed
- tests:+21 added−8 removed
- other:+10 added−3 removed
Provenance
- Commit
8a18881- Tree
e5f3cb0- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
66d12bf
3 files changed+31 added−11 removed
modified:test/error.js+2 added-2 removed
48 unmodified lines49 // `error.code` is OS-specific here50 const SPAWN_ERROR_CODES = new Set(['EINVAL', 'ENOTSUP', 'EPERM']);51 52 test('stdout/stderr/stdio on process spawning errors', async t => {53- const {code, stdout, stderr, stdio} = await t.throwsAsync(execa('noop.js', {uid: -1}));53+ const {code, stdout, stderr, stdio} = await t.throwsAsync(execa('empty.js', {uid: -1}));54 t.true(SPAWN_ERROR_CODES.has(code));55 t.is(stdout, undefined);56 t.is(stderr, undefined);57 t.deepEqual(stdio, [undefined, undefined, undefined]);58 });59 60 test('stdout/stderr/all/stdio on process spawning errors - sync', t => {61 const {code, stdout, stderr, stdio} = t.throws(() => {62- execaSync('noop.js', {uid: -1});62+ execaSync('empty.js', {uid: -1});63 });64 t.true(SPAWN_ERROR_CODES.has(code));65 t.is(stdout, undefined);66 t.is(stderr, undefined);228 unmodified linesmodified:test/test.js+19 added-6 removed
151 unmodified lines152 t.is(typeof subprocess.pid, 'number');153 await subprocess;154 });155 156-test('child_process.spawn() propagated errors have correct shape', t => {157- const subprocess = execa('noop.js', {uid: -1});156+const testEarlyErrorShape = async (t, reject) => {157+ const subprocess = execa('', {reject});158 t.notThrows(() => {159 subprocess.catch(() => {});160 subprocess.unref();161 subprocess.on('error', () => {});162 });163+};164+165+test('child_process.spawn() early errors have correct shape', testEarlyErrorShape, true);166+test('child_process.spawn() early errors have correct shape - reject false', testEarlyErrorShape, false);167+168+test('child_process.spawn() early errors are propagated', async t => {169+ const {failed} = await t.throwsAsync(execa(''));170+ t.true(failed);171 });172 165-test('child_process.spawn() errors are propagated', async t => {166- const {failed} = await t.throwsAsync(execa('noop.js', {uid: -1}));173+test('child_process.spawn() early errors are returned', async t => {174+ const {failed} = await execa('', {reject: false});175 t.true(failed);176 });177 170-test('child_process.spawnSync() errors are propagated with a correct shape', t => {178+test('child_process.spawnSync() early errors are propagated with a correct shape', t => {179 const {failed} = t.throws(() => {172- execaSync('noop.js', {uid: -1});180+ execaSync('');181 });182 t.true(failed);183 });184 185+test('child_process.spawnSync() early errors are propagated with a correct shape - reject false', t => {186+ const {failed} = execaSync('', {reject: false});187+ t.true(failed);188+});189+190 test('do not try to consume streams twice', async t => {191 const subprocess = execa('noop.js', ['foo']);192 const {stdout} = await subprocess;193 const {stdout: stdout2} = await subprocess;130 unmodified linesmodified:index.js+10 added-3 removed
132 unmodified lines133 spawned = childProcess.spawn(file, args, options);134 } catch (error) {135 // Ensure the returned error is always both a promise and a child process136 const dummySpawned = new childProcess.ChildProcess();137- const errorPromise = Promise.reject(makeError({137+ const errorInstance = makeError({138 error,139 stdio: Array.from({length: stdioStreamsGroups.length}),140 command,141 escapedCommand,142 options,143 timedOut: false,144 isCanceled: false,145- }));145+ });146+ const errorPromise = options.reject ? Promise.reject(errorInstance) : Promise.resolve(errorInstance);147 mergePromise(dummySpawned, errorPromise);148 return dummySpawned;149 }150 68 unmodified lines219 let result;220 try {221 result = childProcess.spawnSync(file, args, options);222 } catch (error) {222- throw makeError({223+ const errorInstance = makeError({224 error,225 stdio: Array.from({length: stdioStreamsGroups.stdioLength}),226 command,227 escapedCommand,228 options,229 timedOut: false,230 isCanceled: false,231 });232+233+ if (!options.reject) {234+ return errorInstance;235+ }236+237+ throw errorInstance;238 }239 240 pipeOutputSync(stdioStreamsGroups, result);241 114 unmodified lines