From 80f8d0e9d4ba224aad8909ff96807282d0ed5549 Mon Sep 17 00:00:00 2001 From: Douglas Duteil Date: Sat, 14 Nov 2015 12:05:10 +0100 Subject: [PATCH 1/2] feat: pass errors to async callback Fixes #2 --- shelltest.js | 43 +++++++++++++++++++++++++++++-------------- test/acceptance.js | 27 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/shelltest.js b/shelltest.js index 9bf7fb4..900505b 100644 --- a/shelltest.js +++ b/shelltest.js @@ -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) } + }); +} diff --git a/test/acceptance.js b/test/acceptance.js index 1dbaa0d..d3ef391 100644 --- a/test/acceptance.js +++ b/test/acceptance.js @@ -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" + ); + }); }); }); From a4ad1783693fff16f5b5d5da7808369cf3975c1e Mon Sep 17 00:00:00 2001 From: Douglas Duteil Date: Sat, 14 Nov 2015 12:15:34 +0100 Subject: [PATCH 2/2] doc: update README --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 271b0b2..a59c567 100644 --- a/README.md +++ b/README.md @@ -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) ```