Skip to content

Commit

Permalink
Make error.killed and error.isCanceled always boolean (not `undef…
Browse files Browse the repository at this point in the history
…ined`) (#229)
  • Loading branch information
ehmicky authored and sindresorhus committed May 10, 2019
1 parent 5f8bd52 commit bb36e6d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
10 changes: 7 additions & 3 deletions index.js
Expand Up @@ -196,7 +196,7 @@ function makeError(result, options) {
// it to `undefined`
error.signal = signal || undefined;
error.command = joinedCommand;
error.timedOut = Boolean(timedOut);
error.timedOut = timedOut;
error.isCanceled = isCanceled;

if ('all' in result) {
Expand Down Expand Up @@ -449,7 +449,9 @@ module.exports.sync = (command, args, options) => {
if (result.error || result.status !== 0 || result.signal !== null) {
const error = makeError(result, {
joinedCommand,
parsed
parsed,
timedOut: false,
isCanceled: false
});

if (!parsed.options.reject) {
Expand All @@ -466,7 +468,9 @@ module.exports.sync = (command, args, options) => {
exitCode: 0,
exitCodeName: 'SUCCESS',
failed: false,
killed: false,
command: joinedCommand,
timedOut: false
timedOut: false,
isCanceled: false
};
};
32 changes: 31 additions & 1 deletion test.js
Expand Up @@ -305,6 +305,16 @@ test('error.killed is false if process was killed indirectly', async t => {
t.false(error.killed);
});

test('result.killed is false if not killed', async t => {
const result = await execa('noop');
t.false(result.killed);
});

test('result.killed is false if not killed, in sync mode', t => {
const result = execa.sync('noop');
t.false(result.killed);
});

if (process.platform === 'darwin') {
test.cb('sanity check: child_process.exec also has killed.false if killed indirectly', t => {
const cp = childProcess.exec('forever', error => {
Expand Down Expand Up @@ -375,6 +385,11 @@ test('timedOut is false if no timeout was set', async t => {
t.false(result.timedOut);
});

test('timedOut will be false if no timeout was set and zero exit code in sync mode', t => {
const result = execa.sync('noop');
t.false(result.timedOut);
});

async function errorMessage(t, expected, ...args) {
await t.throwsAsync(execa('exit', args), {message: expected});
}
Expand Down Expand Up @@ -578,11 +593,26 @@ test('cancel method kills the subprocess', t => {
t.true(subprocess.killed);
});

test('result.isCanceled is false when spawned.cancel isn\'t called', async t => {
test('result.isCanceled is false when spawned.cancel() isn\'t called (success)', async t => {
const result = await execa('noop');
t.false(result.isCanceled);
});

test('result.isCanceled is false when spawned.cancel() isn\'t called (failure)', async t => {
const error = await t.throwsAsync(execa('fail'));
t.false(error.isCanceled);
});

test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (success)', t => {
const result = execa.sync('noop');
t.false(result.isCanceled);
});

test('result.isCanceled is false when spawned.cancel() isn\'t called in sync mode (failure)', t => {
const error = t.throws(() => execa.sync('fail'));
t.false(error.isCanceled);
});

test('calling cancel method throws an error with message "Command was canceled"', async t => {
const subprocess = execa('noop');
subprocess.cancel();
Expand Down

0 comments on commit bb36e6d

Please sign in to comment.