Skip to content
Myr

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

6 files+43 added−112 removed

modified:lib/kill.js-9 removed
modified:test/kill.js+35 added-84 removed
180 unmodified lines
181 await subprocess;182 t.false(exitListeners.includes(listener));183});184185test('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});192193test('result.isCanceled is false when spawned.cancel() isn\'t called (success)', async t => {185test('result.isCanceled is false when abort isn\'t called (success)', async t => {186 const {isCanceled} = await execa('noop.js');187 t.false(isCanceled);188});189198test('result.isCanceled is false when spawned.cancel() isn\'t called (failure)', async t => {190test('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});194203test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (success)', t => {195test('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});199208test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (failure)', t => {200test('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});206215test('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/});207test('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});215221test('error.isCanceled is true when cancel method is used', async t => {222 const subprocess = execa('noop.js');223 subprocess.cancel();216test('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});223224test('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});231235test('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);232test('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});238244test('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});250251test('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();239test('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});247258if (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 });265266 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 });275276 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 });248test('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});283284 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 });293294 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 lines
669 */670 cwd: string;671672 /**673 Whether the process was canceled.674675 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;676677 /**
103 unmodified lines
781 */782 kill(signal?: string, options?: KillOptions): void;783784 /**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;790791 /**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 string
501 unmodified lines
modified:index.js+6 added-7 removed
8 unmodified lines
9import {makeError} from './lib/error.js';10import {handleInputAsync, pipeOutputAsync} from './lib/stdio/async.js';11import {handleInputSync, pipeOutputSync} from './lib/stdio/sync.js';12import {normalizeStdioNode} from './lib/stdio/normalize.js';13import {spawnedKill, spawnedCancel, validateTimeout} from './lib/kill.js';13import {spawnedKill, validateTimeout} from './lib/kill.js';14import {addPipeMethods} from './lib/pipe.js';15import {getSpawnedResult, makeAllStream} from './lib/stream.js';16import {mergePromise} from './lib/promise.js';17import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';
123 unmodified lines
141 }142143 pipeOutputAsync(spawned, stdioStreamsGroups);144145 const context = {isCanceled: false, timedOut: false};146145 spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned));148 spawned.cancel = spawnedCancel.bind(null, spawned, context);146 spawned.all = makeAllStream(spawned, options);147148 addPipeMethods(spawned);149153 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}154158const handlePromise = async ({spawned, options, context, stdioStreamsGroups, command, escapedCommand}) => {155const handlePromise = async ({spawned, options, stdioStreamsGroups, command, escapedCommand}) => {156 const context = {timedOut: false};157158 const [159 [exitCode, signal, error],160 stdioResults,161 allResult,
1 unmodified line
163 const stdio = stdioResults.map(stdioResult => handleOutput(options, stdioResult));164 const all = handleOutput(options, allResult);165166 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 lines
modified:index.test-d.ts-1 removed
74 unmodified lines
75};7677try {78 const execaPromise = execa('unicorns', {all: true});79 execaPromise.cancel();7980 const execaBufferPromise = execa('unicorns', {encoding: 'buffer', all: true});81 const writeStream = createWriteStream('output.txt');82
1226 unmodified lines
modified:readme.md+1 added-3 removed
459 unmodified lines
460#### isCanceled461462Type: `boolean`463464Whether the process was canceled.464Whether the process was canceled using the [`signal`](#signal-1) option.465466You can cancel the spawned process using the [`signal`](#signal-1) option.467466#### isTerminated467468Type: `boolean`469
477 unmodified lines