Improve validation of `execaSync()` options (#940)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 8f97132 · Browse files · 5 files·+54 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+54 added−6 removed
- tests:+26 added−2 removed
- other:+28 added−4 removed
Provenance
- Commit
f6e1ac1- Tree
618b70a- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
8f97132
5 files changed+54 added−6 removed
modified:lib/stdio/sync.js+14 added-2 removed
9 unmodified lines10111213141415161718192021222324252627282930313233343524363738394054 unmodified lines9 unmodified lines return fileDescriptors;};
const forbiddenIfSync = ({type, optionName}) => { throw new TypeError(`The \`${optionName}\` option cannot be ${TYPE_TO_MESSAGE[type]} in sync mode.`); throwInvalidSyncValue(optionName, TYPE_TO_MESSAGE[type]);};
const forbiddenNativeIfSync = ({optionName, value}) => { if (value === 'ipc') { throwInvalidSyncValue(optionName, `"${value}"`); }
return {};};
const throwInvalidSyncValue = (optionName, value) => { throw new TypeError(`The \`${optionName}\` option cannot be ${value} in sync mode.`);};
const addProperties = { generator: forbiddenIfSync, webStream: forbiddenIfSync, nodeStream: forbiddenIfSync, webTransform: forbiddenIfSync, duplex: forbiddenIfSync, iterable: forbiddenIfSync, native() {}, native: forbiddenNativeIfSync,};
const addPropertiesSync = { input: {54 unmodified linesmodified:lib/sync.js+14 added-2 removed
27 unmodified lines2829303132323334343536373839404142434445464748495051525327 unmodified lines27 unmodified lines};
const normalizeSyncOptions = options => options.node && !options.ipc ? {...options, ipc: false} : options;
const validateSyncOptions = ({ipc}) => {const validateSyncOptions = ({ipc, detached, cancelSignal}) => { if (ipc) { throw new TypeError('The "ipc: true" option cannot be used with synchronous methods.'); throwInvalidSyncOption('ipc: true'); }
if (detached) { throwInvalidSyncOption('detached: true'); }
if (cancelSignal) { throwInvalidSyncOption('cancelSignal'); }};
const throwInvalidSyncOption = value => { throw new TypeError(`The "${value}" option cannot be used with synchronous methods.`);};
const spawnSubprocessSync = ({file, args, options, command, escapedCommand, fileDescriptors, startTime}) => { let syncResult; try { syncResult = spawnSync(file, args, options);27 unmodified linesmodified:test/exit/cancel.js+8 added-1 removed
78 unmodified lines79 80 test('Throws when using the former "signal" option name', t => {81 const abortController = new AbortController();82 t.throws(() => {83- execa('noop.js', {signal: abortController.signal});83+ execa('empty.js', {signal: abortController.signal});84 }, {message: /renamed to "cancelSignal"/});85 });86+87+test('Cannot use cancelSignal, sync', t => {88+ const abortController = new AbortController();89+ t.throws(() => {90+ execaSync('empty.js', {cancelSignal: abortController.signal});91+ }, {message: /The "cancelSignal" option cannot be used/});92+});modified:test/exit/cleanup.js+7 added-1 removed
1 unmodified line2 import {setTimeout} from 'node:timers/promises';3 import test from 'ava';4 import {pEvent} from 'p-event';5 import isRunning from 'is-running';6-import {execa} from '../../index.js';6+import {execa, execaSync} from '../../index.js';7 import {setFixtureDir} from '../helpers/fixtures-dir.js';8 9 setFixtureDir();10 75 unmodified lines86 t.true(isRunning(pid));87 88 process.kill(pid, 'SIGKILL');89 });90+91+test('Cannot use "detached" option, sync', t => {92+ t.throws(() => {93+ execaSync('empty.js', {detached: true});94+ }, {message: /The "detached: true" option cannot be used/});95+});modified:test/stdio/handle.js+11 added
61 unmodified lines62 test('stdio[*] can be ["inherit"]', testNoPipeOption, ['inherit'], 3);63 test('stdio[*] can be 3', testNoPipeOption, 3, 3);64 test('stdio[*] can be [3]', testNoPipeOption, [3], 3);65 66+const testNoIpcSync = (t, fdNumber) => {67+ t.throws(() => {68+ execaSync('empty.js', getStdio(fdNumber, 'ipc'));69+ }, {message: /cannot be "ipc" in sync mode/});70+};71+72+test('stdin cannot be "ipc", sync', testNoIpcSync, 0);73+test('stdout cannot be "ipc", sync', testNoIpcSync, 1);74+test('stderr cannot be "ipc", sync', testNoIpcSync, 2);75+test('stdio[*] cannot be "ipc", sync', testNoIpcSync, 3);76+77 const testInvalidArrayValue = (t, invalidStdio, fdNumber, execaMethod) => {78 t.throws(() => {79 execaMethod('empty.js', getStdio(fdNumber, ['pipe', invalidStdio]));80 }, {message: /must not include/});18 unmodified lines