Skip to content

Commit

Permalink
Throw an error upon unexpected termination of child process
Browse files Browse the repository at this point in the history
  • Loading branch information
berkowitzi committed Jul 16, 2019
1 parent 026944d commit 16ac426
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ module.exports = function (ops, coverage) {
that.emit('end');
});
// When done...
this._child.on('close', function (code) {
this._child.on('close', function (code, signal) {
// If code is not zero (falsy)
if (code) {
that.emit('error', new PluginError('gulp-spawn-mocha', 'Mocha exited with code ' + code));
}
// If code is null (process was terminated due to a signal)
if (code == null && signal) {
that.emit('error', new PluginError('gulp-spawn-mocha', 'Mocha was terminated by ' + signal));
}
that.emit('end');
});
// Output to a file
Expand Down
11 changes: 11 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ describe('gulp-spawn-mocha tests', function () {
stream.emit.should.be.calledWith('error', sinon.match.instanceOf(PluginError));
});

it('should handle terminations from mocha', function () {
this.childOn.withArgs('close').yields(null, 'SIGTERM');
var stream = this.stream = mocha();
stream.write({path: 'foo'});
sinon.stub(stream, 'emit');
stream.emit.withArgs('error').returns();
stream.end();
this.childOn.should.be.calledTwice;
stream.emit.should.be.calledWith('error', sinon.match.instanceOf(PluginError));
});

it('can output to a writable stream from a string argument', function () {
var fakeStream = {};
sinon.stub(fs, 'createWriteStream').returns(fakeStream);
Expand Down

0 comments on commit 16ac426

Please sign in to comment.