Skip to content

Commit

Permalink
Fix: Change & tests for failing child processes (fixes #24)
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore authored and phated committed May 17, 2016
1 parent 8d4d04c commit 0224d42
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 27 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function asyncDone(fn, cb){
if(result && typeof result.on === 'function'){
// assume node stream
d.add(result);
eos(exhaust(result), eosConfig, onSuccess);
eos(exhaust(result), eosConfig, done);
return;
}

Expand Down
56 changes: 56 additions & 0 deletions test/child_processes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

var lab = exports.lab = require('lab').script();
var describe = lab.describe;
var it = lab.it;
var expect = require('code').expect;

var cp = require('child_process');
var asyncDone = require('../');


function execSuccess(){
return cp.exec('echo hello world');
}

function execFail(){
return cp.exec('foo-bar-baz hello world');
}

function spawnSuccess(){
return cp.spawn('echo', ['hello world']);
}

function spawnFail(){
return cp.spawn('foo-bar-baz', ['hello world']);
}

describe('child processes', function(){
it('should handle successful exec', function(done){
asyncDone(execSuccess, function(err){
expect(err).to.not.be.instanceof(Error);
done();
});
});

it('should handle failing exec', function(done){
asyncDone(execFail, function(err){
expect(err).to.be.an.instanceof(Error);
done();
});
});

it('should handle successful spawn', function(done){
asyncDone(spawnSuccess, function(err){
expect(err).to.not.be.instanceof(Error);
done();
});
});

it('should handle failing spawn', function(done){
asyncDone(spawnFail, function(err){
expect(err).to.be.an.instanceof(Error);
done();
});
});
});
29 changes: 3 additions & 26 deletions test/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,10 @@ function unpiped(){
return fs.createReadStream(exists);
}

function exec(){
return cp.exec('echo hello world');
}

function spawn(){
return cp.spawn('echo', ['hello world']);
}

describe('streams', function(){

it('should handle a successful stream', function(done){
asyncDone(success, function(err){
expect(err).to.equal(null);
expect(err).to.not.be.instanceof(Error);
done();
});
});
Expand All @@ -65,29 +56,15 @@ describe('streams', function(){
asyncDone(function(cb){
return success().on('end', function(){ cb(null, 3); });
}, function(err, result){
expect(err).to.equal(null);
expect(err).to.not.be.instanceof(Error);
expect(result).to.equal(3); // to know we called the callback
done();
});
});

it('consumes an unpiped readable stream', function(done){
asyncDone(unpiped, function(err){
expect(err).to.equal(null);
done();
});
});

it('should handle exec', function(done){
asyncDone(exec, function(err){
expect(err).to.equal(null);
done();
});
});

it('should handle spawn', function(done){
asyncDone(spawn, function(err){
expect(err).to.equal(null);
expect(err).to.not.be.instanceof(Error);
done();
});
});
Expand Down

0 comments on commit 0224d42

Please sign in to comment.