Skip to content

Commit

Permalink
fix: if abort is executed, it should stop listen of exit
Browse files Browse the repository at this point in the history
  • Loading branch information
59naga committed Mar 23, 2016
1 parent 27ef19c commit 45981b0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
21 changes: 18 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,33 @@ export default class Exit extends Plugin {
* @returns {undefined}
*/
pluginWillAttach() {
this.code = 1;
this._handleExit = () => this.opts.process.exit(this.code);

const task = this.parent.task || [];
if (task.length) {
this.parent.once('task-end', (results) => {
const scripts = flattenDeep(results);
const code = scripts.reduce((prev, current) => prev > 0 || current.exitCode > 0 ? 1 : 0, 0);
this.parent.once('exit', () => this.opts.process.exit(code));
this.code = scripts.reduce((prev, current) => prev > 0 || current.exitCode > 0 ? 1 : 0, 0);
this.parent.once('exit', this._handleExit);
});
return;
}

this.opts.process.stdout.write(this.constructor.output);
this.opts.process.stdout.write('\n');
this.opts.process.exit(1);
this.opts.process.exit(this.code);
}

/**
* @method abort
* @returns {undefined}
*/
abort() {
super.abort();

if (this._handleExit) {
this.parent.removeListener('exit', this._handleExit);
}
}
}
20 changes: 18 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ describe('Exit', () => {
it('if the exit code of the task is all 0, it should be exit in 0', () => {
const emitter = new AsyncEmitter;
const process = createProcess();

// eslint-disable-next-line no-unused-vars
const exit = new Exit(emitter, true, { process });

Expand All @@ -57,7 +56,6 @@ describe('Exit', () => {
it('unless the exit code of the task is all 0, it should be exit in 1', () => {
const emitter = new AsyncEmitter;
const process = createProcess();

// eslint-disable-next-line no-unused-vars
const exit = new Exit(emitter, true, { process });

Expand All @@ -73,5 +71,23 @@ describe('Exit', () => {
assert(process.exit.args[0][0] === 1);
});
});

it('if abort is executed, it should stop listen of exit', () => {
const emitter = new AsyncEmitter;
const process = createProcess();
const exit = new Exit(emitter, true, { process });

emitter.task = [[[
{ main: { raw: 'echo foo' } },
]]];

return emitter.emit('attach-plugins')
.then(() => emitter.emit('task-end', [{ exitCode: 0 }]))
.then(() => exit.abort())
.then(() => emitter.emit('exit'))
.then(() => {
assert(process.exit.calledOnce === false);
});
});
});
});

0 comments on commit 45981b0

Please sign in to comment.