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
Provenance
- Commit
0fbd3a2- Tree
019df31- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
163d6ee
4 files changed+16 added−15 removed
modified:test/error.js+1 added-1 removed
234 unmodified lines235 t.is(signal, 'SIGTERM');236 });237 238 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 });242 243 test('exitCode is undefined on signal termination', async t => {44 unmodified linesmodified:test/kill.js+13 added-12 removed
69 unmodified lines70 await subprocess;71 });72 73 test('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 });79 80 test('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 });88 89 test('timeout does not kill the process if it does not time out', async t => {2 unmodified lines92 });93 94 const INVALID_TIMEOUT_REGEXP = /`timeout` option to be a non-negative integer/;95 94-test('timeout must not be negative', async t => {95- await t.throws(() => {96- execa('noop.js', {timeout: -1});96+const testTimeoutValidation = (t, timeout, execaMethod) => {97+ t.throws(() => {98+ execaMethod('empty.js', {timeout});99 }, {message: INVALID_TIMEOUT_REGEXP});98-});100+};101 100-test('timeout must be an integer', async t => {101- await t.throws(() => {102- execa('noop.js', {timeout: false});103- }, {message: INVALID_TIMEOUT_REGEXP});104-});102+test('timeout must not be negative', testTimeoutValidation, -1, execa);103+test('timeout must be an integer', testTimeoutValidation, false, execa);104+test('timeout must not be negative - sync', testTimeoutValidation, -1, execaSync);105+test('timeout must be an integer - sync', testTimeoutValidation, false, execaSync);106 107 test('timedOut is false if timeout is undefined', async t => {108 const {timedOut} = await execa('noop.js');109 t.false(timedOut);146 unmodified linesmodified:test/test.js+1 added-1 removed
173 unmodified lines174 });175 176 test('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 linesmodified:index.js+1 added-1 removed
48 unmodified lines49 50 const {command: file, args, options: initialOptions} = crossSpawn._parse(filePath, rawArgs, rawOptions);51 52 const options = addDefaultOptions(initialOptions);53+ validateTimeout(options);54 options.shell = normalizeFileUrl(options.shell);55 options.env = getEnv(options);56 57 if (process.platform === 'win32' && path.basename(file, '.exe') === 'cmd') {59 unmodified lines117 };118 119 export function execa(rawFile, rawArgs, rawOptions) {120 const {file, args, command, escapedCommand, options} = handleArguments(rawFile, rawArgs, rawOptions);120- validateTimeout(options);121 122 const stdioStreamsGroups = handleInputAsync(options);123 124 let spawned;214 unmodified lines