Skip to content
Myr

Clean up #189

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

authored by Sindre Sorhus · committed by Sindre Sorhus · parent 79356cc · Browse files · 2 files+38 added−37 removed
This commit

State

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

Diff stats

2 files+38 added−37 removed

  • other:+38 added−37 removed

2 files+38 added−37 removed

modified:readme.md+13 added-12 removed
50 unmodified lines
51 // Run a shell command52 const {stdout} = await execa.shell('echo unicorns');53 //=> 'unicorns'5455 // Cancelling a spawned process56 const subprocess = execa('node');57 setTimeout(() => { spawned.cancel() }, 1000);58 try {59 await subprocess;60 } catch (error) {61 console.log(subprocess.killed); // true62 console.log(error.isCanceled); // true63 }6455 // Catching an error56 try {57 await execa.shell('exit 3');58 } catch (error) {
13 unmodified lines
72 killed: false73 }74 */75 }7677 // Cancelling a spawned process78 const subprocess = execa('node');79 setTimeout(() => {80 subprocess.cancel();81 }, 1000);82 try {83 await subprocess;84 } catch (error) {85 console.log(subprocess.killed); // true86 console.log(error.isCanceled); // true87 }88})();8990// Catching an error with a sync method91try {
28 unmodified lines
120Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) which is enhanced to be a `Promise`.121122It exposes an additional `.all` stream, with `stdout` and `stderr` interleaved.123122It can be canceled with `.cancel` method which throws an error with `error.isCanceled` equal to `true`, provided that the process gets canceled.123Process would not get canceled if it has already exited.124The spawned process can be canceled with the `.cancel()` method on the promise, which causes the promise to reject an error with a `.isCanceled = true` property, provided the process gets canceled. The process will not be canceled if it has already exited.125126The promise result is an `Object` with `stdout`, `stderr` and `all` properties.127128### execa.stdout(file, [arguments], [options])
225 unmodified lines
modified:test.js+25 added-25 removed
585 unmodified lines
586 t.is(result.message, 'called');587 });588}589590test('cancel method kills the spawned process', t => {591 const spawned = execa('node');592 spawned.cancel();593 t.true(spawned.killed);590test('cancel method kills the subprocess', t => {591 const subprocess = execa('node');592 subprocess.cancel();593 t.true(subprocess.killed);594});595596test('result.isCanceled is false when spawned.cancel isn\'t called', async t => {597 const result = await execa('noop');598 t.false(result.isCanceled);599});600601test('calling cancel method throws an error with message "Command was canceled"', async t => {602 const spawned = execa('noop');603 spawned.cancel();604 await t.throwsAsync(spawned, {message: /Command was canceled/});602 const subprocess = execa('noop');603 subprocess.cancel();604 await t.throwsAsync(subprocess, {message: /Command was canceled/});605});606607test('error.isCanceled is true when cancel method is used', async t => {608 const spawned = execa('noop');609 spawned.cancel();610 const error = await t.throwsAsync(spawned);608 const subprocess = execa('noop');609 subprocess.cancel();610 const error = await t.throwsAsync(subprocess);611 t.true(error.isCanceled);612});613614test('error.isCanceled is false when kill method is used', async t => {615 const spawned = execa('noop');616 spawned.kill();617 const error = await t.throwsAsync(spawned);615 const subprocess = execa('noop');616 subprocess.kill();617 const error = await t.throwsAsync(subprocess);618 t.false(error.isCanceled);619});620621test('calling cancel method twice should show the same behaviour as calling it once', async t => {622 const spawned = execa('noop');623 spawned.cancel();624 spawned.cancel();625 const error = await t.throwsAsync(spawned);622 const subprocess = execa('noop');623 subprocess.cancel();624 subprocess.cancel();625 const error = await t.throwsAsync(subprocess);626 t.true(error.isCanceled);627 t.true(spawned.killed);627 t.true(subprocess.killed);628});629630test('calling cancel method on a successfuly completed process does not make result.cancel true', async t => {631 const spawned = execa('noop');632 const result = await spawned;633 spawned.cancel();630test('calling cancel method on a successfuly completed process does not make result.isCanceled true', async t => {631 const subprocess = execa('noop');632 const result = await subprocess;633 subprocess.cancel();634 t.false(result.isCanceled);635});636637test('calling cancel method on a process which has been killed does not make error.isCanceled true', async t => {638 const spawned = execa('noop');639 spawned.kill();640 const error = await t.throwsAsync(spawned);638 const subprocess = execa('noop');639 subprocess.kill();640 const error = await t.throwsAsync(subprocess);641 t.false(error.isCanceled);642});