Skip to content
Myr

Improve `timeout` option validation (#715)

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

authored by ehmicky · committed by GitHub · parent 163d6ee · Browse files · 4 files+16 added−15 removed
This commit

State

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

Diff stats

4 files+16 added−15 removed

  • tests:+15 added−14 removed
  • other:+1 added−1 removed

4 files+16 added−15 removed

modified:test/error.js+1 added-1 removed
234 unmodified lines
235 t.is(signal, 'SIGTERM');236 });237238 test('custom error.signal', async t => {239 const {signal} = await t.throwsAsync(execa('noop.js', {killSignal: 'SIGHUP', timeout: 1, message: TIMEOUT_REGEXP}));239 const {signal} = await t.throwsAsync(execa('forever.js', {killSignal: 'SIGHUP', timeout: 1, message: TIMEOUT_REGEXP}));240 t.is(signal, 'SIGHUP');241 });242243 test('exitCode is undefined on signal termination', async t => {
44 unmodified lines
modified:test/kill.js+13 added-12 removed
69 unmodified lines
70 await subprocess;71});7273test('timeout kills the process if it times out', async t => {74 const {isTerminated, timedOut} = await t.throwsAsync(execa('noop.js', {timeout: 1}), {message: TIMEOUT_REGEXP});74 const {isTerminated, signal, timedOut} = await t.throwsAsync(execa('forever.js', {timeout: 1}), {message: TIMEOUT_REGEXP});75 t.true(isTerminated);76 t.is(signal, 'SIGTERM');77 t.true(timedOut);78});7980test('timeout kills the process if it times out, in sync mode', async t => {80 const {isTerminated, timedOut} = await t.throws(() => {81 execaSync('noop.js', {timeout: 1, message: TIMEOUT_REGEXP});81 const {isTerminated, signal, timedOut} = await t.throws(() => {82 execaSync('forever.js', {timeout: 1, message: TIMEOUT_REGEXP});83 });84 t.true(isTerminated);85 t.is(signal, 'SIGTERM');86 t.true(timedOut);87});8889test('timeout does not kill the process if it does not time out', async t => {
2 unmodified lines
92});9394const INVALID_TIMEOUT_REGEXP = /`timeout` option to be a non-negative integer/;9594test('timeout must not be negative', async t => {95 await t.throws(() => {96 execa('noop.js', {timeout: -1});96const testTimeoutValidation = (t, timeout, execaMethod) => {97 t.throws(() => {98 execaMethod('empty.js', {timeout});99 }, {message: INVALID_TIMEOUT_REGEXP});98});100};101100test('timeout must be an integer', async t => {101 await t.throws(() => {102 execa('noop.js', {timeout: false});103 }, {message: INVALID_TIMEOUT_REGEXP});104});102test('timeout must not be negative', testTimeoutValidation, -1, execa);103test('timeout must be an integer', testTimeoutValidation, false, execa);104test('timeout must not be negative - sync', testTimeoutValidation, -1, execaSync);105test('timeout must be an integer - sync', testTimeoutValidation, false, execaSync);106107test('timedOut is false if timeout is undefined', async t => {108 const {timedOut} = await execa('noop.js');109 t.false(timedOut);
146 unmodified lines
modified:test/test.js+1 added-1 removed
173 unmodified lines
174});175176test('child_process.spawnSync() errors are propagated with a correct shape', t => {177 const {failed} = t.throws(() => {178 execaSync('noop.js', {timeout: -1});178 execaSync('noop.js', {uid: -1});179 });180 t.true(failed);181});182
134 unmodified lines
modified:index.js+1 added-1 removed
48 unmodified lines
4950 const {command: file, args, options: initialOptions} = crossSpawn._parse(filePath, rawArgs, rawOptions);5152 const options = addDefaultOptions(initialOptions);53 validateTimeout(options);54 options.shell = normalizeFileUrl(options.shell);55 options.env = getEnv(options);5657 if (process.platform === 'win32' && path.basename(file, '.exe') === 'cmd') {
59 unmodified lines
117};118119export function execa(rawFile, rawArgs, rawOptions) {120 const {file, args, command, escapedCommand, options} = handleArguments(rawFile, rawArgs, rawOptions);120 validateTimeout(options);121122 const stdioStreamsGroups = handleInputAsync(options);123124 let spawned;
214 unmodified lines