Remove `.cancel()` method (#711)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 472c065 · Browse files · 6 files·+43 added−112 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+43 added−112 removed
- tests:+35 added−84 removed
- other:+8 added−28 removed
Provenance
- Commit
16e5fd6- Tree
cc7be62- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
472c065
6 files changed+43 added−112 removed
modified:lib/kill.js-9 removed
40 unmodified lines414243444546474849505152534546474831 unmodified lines40 unmodified lines
return forceKillAfterTimeout;};
// `childProcess.cancel()`export const spawnedCancel = (spawned, context) => { const killResult = spawned.kill();
if (killResult) { context.isCanceled = true; }};
const killAfterTimeout = async ({spawned, timeout, killSignal, context, controller}) => { await pSetTimeout(timeout, undefined, {ref: false, signal: controller.signal}); spawned.kill(killSignal); Object.assign(context, {timedOut: true, signal: killSignal});31 unmodified linesmodified:test/kill.js+35 added-84 removed
180 unmodified lines181 await subprocess;182 t.false(exitListeners.includes(listener));183 });184 185-test('cancel method kills the subprocess', async t => {186- const subprocess = execa('node');187- subprocess.cancel();188- t.true(subprocess.killed);189- const {isTerminated} = await t.throwsAsync(subprocess);190- t.true(isTerminated);191-});192-193-test('result.isCanceled is false when spawned.cancel() isn\'t called (success)', async t => {185+test('result.isCanceled is false when abort isn\'t called (success)', async t => {186 const {isCanceled} = await execa('noop.js');187 t.false(isCanceled);188 });189 198-test('result.isCanceled is false when spawned.cancel() isn\'t called (failure)', async t => {190+test('result.isCanceled is false when abort isn\'t called (failure)', async t => {191 const {isCanceled} = await t.throwsAsync(execa('fail.js'));192 t.false(isCanceled);193 });194 203-test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (success)', t => {195+test('result.isCanceled is false when abort isn\'t called in sync mode (success)', t => {196 const {isCanceled} = execaSync('noop.js');197 t.false(isCanceled);198 });199 208-test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (failure)', t => {200+test('result.isCanceled is false when abort isn\'t called in sync mode (failure)', t => {201 const {isCanceled} = t.throws(() => {202 execaSync('fail.js');203 });204 t.false(isCanceled);205 });206 215-test('calling cancel method throws an error with message "Command was canceled"', async t => {216- const subprocess = execa('noop.js');217- subprocess.cancel();218- await t.throwsAsync(subprocess, {message: /Command was canceled/});207+test('calling abort is not considered a signal termination', async t => {208+ const abortController = new AbortController();209+ const subprocess = execa('noop.js', {signal: abortController.signal});210+ abortController.abort();211+ const {isTerminated, signal} = await t.throwsAsync(subprocess);212+ t.false(isTerminated);213+ t.is(signal, undefined);214 });215 221-test('error.isCanceled is true when cancel method is used', async t => {222- const subprocess = execa('noop.js');223- subprocess.cancel();216+test('error.isCanceled is true when abort is used', async t => {217+ const abortController = new AbortController();218+ const subprocess = execa('noop.js', {signal: abortController.signal});219+ abortController.abort();220 const {isCanceled} = await t.throwsAsync(subprocess);221 t.true(isCanceled);222 });223 224 test('error.isCanceled is false when kill method is used', async t => {229- const subprocess = execa('noop.js');225+ const abortController = new AbortController();226+ const subprocess = execa('noop.js', {signal: abortController.signal});227 subprocess.kill();228 const {isCanceled} = await t.throwsAsync(subprocess);229 t.false(isCanceled);230 });231 235-test('calling cancel method twice should show the same behaviour as calling it once', async t => {236- const subprocess = execa('noop.js');237- subprocess.cancel();238- subprocess.cancel();239- const {isCanceled} = await t.throwsAsync(subprocess);240- t.true(isCanceled);241- t.true(subprocess.killed);232+test('calling abort throws an error with message "Command was canceled"', async t => {233+ const abortController = new AbortController();234+ const subprocess = execa('noop.js', {signal: abortController.signal});235+ abortController.abort();236+ await t.throwsAsync(subprocess, {message: /Command was canceled/});237 });238 244-test('calling cancel method on a successfully completed process does not make result.isCanceled true', async t => {245- const subprocess = execa('noop.js');246- const {isCanceled} = await subprocess;247- subprocess.cancel();248- t.false(isCanceled);249-});250-251-test('calling cancel method on a process which has been killed does not make error.isCanceled true', async t => {252- const subprocess = execa('noop.js');253- subprocess.kill();239+test('calling abort twice should show the same behaviour as calling it once', async t => {240+ const abortController = new AbortController();241+ const subprocess = execa('noop.js', {signal: abortController.signal});242+ abortController.abort();243+ abortController.abort();244 const {isCanceled} = await t.throwsAsync(subprocess);255- t.false(isCanceled);245+ t.true(isCanceled);246 });247 258-if (globalThis.AbortController !== undefined) {259- test('calling abort throws an error with message "Command was canceled"', async t => {260- const abortController = new AbortController();261- const subprocess = execa('noop.js', [], {signal: abortController.signal});262- abortController.abort();263- await t.throwsAsync(subprocess, {message: /Command was canceled/});264- });265-266- test('calling abort twice should show the same behaviour as calling it once', async t => {267- const abortController = new AbortController();268- const subprocess = execa('noop.js', [], {signal: abortController.signal});269- abortController.abort();270- abortController.abort();271- const {isCanceled} = await t.throwsAsync(subprocess);272- t.true(isCanceled);273- t.true(subprocess.killed);274- });275-276- test('calling abort on a successfully completed process does not make result.isCanceled true', async t => {277- const abortController = new AbortController();278- const subprocess = execa('noop.js', [], {signal: abortController.signal});279- const {isCanceled} = await subprocess;280- abortController.abort();281- t.false(isCanceled);282- });248+test('calling abort on a successfully completed process does not make result.isCanceled true', async t => {249+ const abortController = new AbortController();250+ const subprocess = execa('noop.js', {signal: abortController.signal});251+ const result = await subprocess;252+ abortController.abort();253+ t.false(result.isCanceled);254+});283-284- test('calling cancel after abort should show the same behaviour as only calling cancel', async t => {285- const abortController = new AbortController();286- const subprocess = execa('noop.js', [], {signal: abortController.signal});287- abortController.abort();288- subprocess.cancel();289- const {isCanceled} = await t.throwsAsync(subprocess);290- t.true(isCanceled);291- t.true(subprocess.killed);292- });293-294- test('calling abort after cancel should show the same behaviour as only calling cancel', async t => {295- const abortController = new AbortController();296- const subprocess = execa('noop.js', [], {signal: abortController.signal});297- subprocess.cancel();298- abortController.abort();299- const {isCanceled} = await t.throwsAsync(subprocess);300- t.true(isCanceled);301- t.true(subprocess.killed);302- });303-}modified:index.d.ts+1 added-8 removed
668 unmodified lines669 */670 cwd: string;671 672 /**673- Whether the process was canceled.674-675- You can cancel the spawned process using the [`signal`](https://github.com/sindresorhus/execa#signal-1) option.673+ Whether the process was canceled using the [`signal`](https://github.com/sindresorhus/execa#signal-1) option.674 */675 isCanceled: boolean;676 677 /**103 unmodified lines781 */782 kill(signal?: string, options?: KillOptions): void;783 784 /**787- Similar to [`childProcess.kill()`](https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal). This used to be preferred when cancelling the child process execution as the error is more descriptive and [`childProcessResult.isCanceled`](#iscanceled) is set to `true`. But now this is deprecated and you should either use `.kill()` or the `signal` option when creating the child process.788- */789- cancel(): void;790-791- /**785 [Pipe](https://nodejs.org/api/stream.html#readablepipedestination-options) the child process's `stdout` to `target`, which can be:786 - Another `execa()` return value787 - A writable stream788 - A file path string501 unmodified linesmodified:index.js+6 added-7 removed
8 unmodified lines9 import {makeError} from './lib/error.js';10 import {handleInputAsync, pipeOutputAsync} from './lib/stdio/async.js';11 import {handleInputSync, pipeOutputSync} from './lib/stdio/sync.js';12 import {normalizeStdioNode} from './lib/stdio/normalize.js';13-import {spawnedKill, spawnedCancel, validateTimeout} from './lib/kill.js';13+import {spawnedKill, validateTimeout} from './lib/kill.js';14 import {addPipeMethods} from './lib/pipe.js';15 import {getSpawnedResult, makeAllStream} from './lib/stream.js';16 import {mergePromise} from './lib/promise.js';17 import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';123 unmodified lines141 }142 143 pipeOutputAsync(spawned, stdioStreamsGroups);144 145- const context = {isCanceled: false, timedOut: false};146-145 spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));148- spawned.cancel = spawnedCancel.bind(null, spawned, context);146 spawned.all = makeAllStream(spawned, options);147 148 addPipeMethods(spawned);149 153- const promise = handlePromise({spawned, options, context, stdioStreamsGroups, command, escapedCommand});150+ const promise = handlePromise({spawned, options, stdioStreamsGroups, command, escapedCommand});151 mergePromise(spawned, promise);152 return spawned;153 }154 158-const handlePromise = async ({spawned, options, context, stdioStreamsGroups, command, escapedCommand}) => {155+const handlePromise = async ({spawned, options, stdioStreamsGroups, command, escapedCommand}) => {156+ const context = {timedOut: false};157+158 const [159 [exitCode, signal, error],160 stdioResults,161 allResult,1 unmodified line163 const stdio = stdioResults.map(stdioResult => handleOutput(options, stdioResult));164 const all = handleOutput(options, allResult);165 166 if (error || exitCode !== 0 || signal !== null) {168- const isCanceled = context.isCanceled || Boolean(options.signal?.aborted);167+ const isCanceled = options.signal?.aborted === true;168 const returnedError = makeError({169 error,170 exitCode,171 signal,167 unmodified linesmodified:index.test-d.ts-1 removed
74 unmodified lines75 };76 77 try {78 const execaPromise = execa('unicorns', {all: true});79- execaPromise.cancel();79 80 const execaBufferPromise = execa('unicorns', {encoding: 'buffer', all: true});81 const writeStream = createWriteStream('output.txt');82 1226 unmodified linesmodified:readme.md+1 added-3 removed
459 unmodified lines460 #### isCanceled461 462 Type: `boolean`463 464-Whether the process was canceled.464+Whether the process was canceled using the [`signal`](#signal-1) option.465 466-You can cancel the spawned process using the [`signal`](#signal-1) option.467-466 #### isTerminated467 468 Type: `boolean`469 477 unmodified lines