Skip to content

Commit

Permalink
Merge pull request #6 from douglasduteil/feat-pass-errors-to-async-ca…
Browse files Browse the repository at this point in the history
…llback

Feat pass errors to async callback
  • Loading branch information
jveski committed Nov 16, 2015
2 parents 620f96b + a4ad178 commit 66e47ea
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ Sets `child_process` gid option.
.gid(0)
```

### .end(fn)
Executes command and evaluates assertions. Callback is fired without arguments.
### .end([fn])
Executes command and evaluates assertions. `end()` will throw with no callback.
Callback is fired with `fn(err, stdout, stderr)` :
- `err`: null or the assert or process error
- `stdout`: the output string
- `stderr`: the error string
```javascript
.end(callback_function)
```
Expand Down
43 changes: 29 additions & 14 deletions shelltest.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,39 @@ var shelltest = function() {
});

shelltest.prototype.end = function(cb) {
var me = this;
var expectations = this.expectations;
if (this.cmd === null) { throw new Error(".end called before command set") }
process.exec(this.cmd, this.options, function(err, stdout, stderr){
me.expectations.forEach(function(exp){
// Set value
if (exp.matcher === 'stdout') { var value = stdout; }
if (exp.matcher === 'stderr') { var value = stderr; }
// Make assertions
if (exp.type === 'Number' && err) { assert.equal(err.code, exp.value,
"Expected exit code of "+exp.value+" got "+err.code); }
if (exp.type === 'String') { assert.equal(value, exp.value,
"Expected "+exp.matcher+" to equal "+exp.value+" got "+value); }
if (exp.type === 'RegExp') { assert(exp.value.test(value),
"Expected "+exp.matcher+" to match "+exp.value+" got "+value) }
});
try {
runAllAsserts(expectations, err, stdout, stderr)
} catch (e) {
if (cb) {
cb(e, stdout, stderr);
return
} else {
throw e
}
}
if (cb) {
cb(err ? err : null, stdout, stderr);
}
});
if (cb) { cb(); }
return this;
};

};

function runAllAsserts (expectations, err, stdout, stderr) {
expectations.forEach(function(exp){
// Set value
if (exp.matcher === 'stdout') { var value = stdout; }
if (exp.matcher === 'stderr') { var value = stderr; }
// Make assertions
if (exp.type === 'Number' && err) { assert.equal(err.code, exp.value,
"Expected exit code of "+exp.value+" got "+err.code); }
if (exp.type === 'String') { assert.equal(value, exp.value,
"Expected "+exp.matcher+" to equal "+exp.value+" got "+value); }
if (exp.type === 'RegExp') { assert(exp.value.test(value),
"Expected "+exp.matcher+" to match "+exp.value+" got "+value) }
});
}
27 changes: 27 additions & 0 deletions test/acceptance.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ describe('shelltest', function(){
exec.yields(null, "test_stdout", "test_stderr");
expect(function(){shelltest().cmd(testCmd).expect(0).end()}).to.not.throw();
});

it('should pass the assert errors in the callback when defined', function(){
exec.yields({"code": 0}, "test_stdout", "test_stderr");
var callbackSpy = sinon.spy()
function testWithCallback() {shelltest().cmd(testCmd).expect(1).end(callbackSpy)}

expect(testWithCallback).not.to.throw();
expect(callbackSpy).have.been.calledWith(
sinon.match.instanceOf(Error)
.and(sinon.match.hasOwn("message", 'Expected exit code of 1 got 0')),
"test_stdout",
"test_stderr"
);
});

it('should pass unmatched errors in the callback when defined', function(){
exec.yields({"code": 1}, "test_stdout", "test_stderr");
var callbackSpy = sinon.spy()
function testWithCallback() {shelltest().cmd(testCmd).end(callbackSpy)}

expect(testWithCallback).not.to.throw();
expect(callbackSpy).have.been.calledWith(
{"code": 1},
"test_stdout",
"test_stderr"
);
});
});

});

0 comments on commit 66e47ea

Please sign in to comment.