Skip to content

Commit

Permalink
Clean up #189
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Mar 20, 2019
1 parent 79356cc commit f24e7c7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
25 changes: 13 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ const execa = require('execa');
const {stdout} = await execa.shell('echo unicorns');
//=> 'unicorns'

// Cancelling a spawned process
const subprocess = execa('node');
setTimeout(() => { spawned.cancel() }, 1000);
try {
await subprocess;
} catch (error) {
console.log(subprocess.killed); // true
console.log(error.isCanceled); // true
}

// Catching an error
try {
await execa.shell('exit 3');
Expand All @@ -83,6 +73,18 @@ const execa = require('execa');
}
*/
}

// Cancelling a spawned process
const subprocess = execa('node');
setTimeout(() => {
subprocess.cancel();
}, 1000);
try {
await subprocess;
} catch (error) {
console.log(subprocess.killed); // true
console.log(error.isCanceled); // true
}
})();

// Catching an error with a sync method
Expand Down Expand Up @@ -119,8 +121,7 @@ Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#c

It exposes an additional `.all` stream, with `stdout` and `stderr` interleaved.

It can be canceled with `.cancel` method which throws an error with `error.isCanceled` equal to `true`, provided that the process gets canceled.
Process would not get canceled if it has already exited.
The 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.

The promise result is an `Object` with `stdout`, `stderr` and `all` properties.

Expand Down
50 changes: 25 additions & 25 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,10 @@ if (Promise.prototype.finally) {
});
}

test('cancel method kills the spawned process', t => {
const spawned = execa('node');
spawned.cancel();
t.true(spawned.killed);
test('cancel method kills the subprocess', t => {
const subprocess = execa('node');
subprocess.cancel();
t.true(subprocess.killed);
});

test('result.isCanceled is false when spawned.cancel isn\'t called', async t => {
Expand All @@ -599,44 +599,44 @@ test('result.isCanceled is false when spawned.cancel isn\'t called', async t =>
});

test('calling cancel method throws an error with message "Command was canceled"', async t => {
const spawned = execa('noop');
spawned.cancel();
await t.throwsAsync(spawned, {message: /Command was canceled/});
const subprocess = execa('noop');
subprocess.cancel();
await t.throwsAsync(subprocess, {message: /Command was canceled/});
});

test('error.isCanceled is true when cancel method is used', async t => {
const spawned = execa('noop');
spawned.cancel();
const error = await t.throwsAsync(spawned);
const subprocess = execa('noop');
subprocess.cancel();
const error = await t.throwsAsync(subprocess);
t.true(error.isCanceled);
});

test('error.isCanceled is false when kill method is used', async t => {
const spawned = execa('noop');
spawned.kill();
const error = await t.throwsAsync(spawned);
const subprocess = execa('noop');
subprocess.kill();
const error = await t.throwsAsync(subprocess);
t.false(error.isCanceled);
});

test('calling cancel method twice should show the same behaviour as calling it once', async t => {
const spawned = execa('noop');
spawned.cancel();
spawned.cancel();
const error = await t.throwsAsync(spawned);
const subprocess = execa('noop');
subprocess.cancel();
subprocess.cancel();
const error = await t.throwsAsync(subprocess);
t.true(error.isCanceled);
t.true(spawned.killed);
t.true(subprocess.killed);
});

test('calling cancel method on a successfuly completed process does not make result.cancel true', async t => {
const spawned = execa('noop');
const result = await spawned;
spawned.cancel();
test('calling cancel method on a successfuly completed process does not make result.isCanceled true', async t => {
const subprocess = execa('noop');
const result = await subprocess;
subprocess.cancel();
t.false(result.isCanceled);
});

test('calling cancel method on a process which has been killed does not make error.isCanceled true', async t => {
const spawned = execa('noop');
spawned.kill();
const error = await t.throwsAsync(spawned);
const subprocess = execa('noop');
subprocess.kill();
const error = await t.throwsAsync(subprocess);
t.false(error.isCanceled);
});

0 comments on commit f24e7c7

Please sign in to comment.