aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Gansterer <[email protected]>2014-01-26 19:02:34 +0100
committerPatrick Gansterer <[email protected]>2014-01-26 19:24:59 +0100
commite0b5ccc5de04f1c8d2e06ff62ece865cfd597e75 (patch)
treea0c6c98f1572121b663d834f4ab335c035402253
parentcbfe6bcad8db471d712c4e7fac6e293bb9688608 (diff)
downloadnode-coveralls-e0b5ccc5de04f1c8d2e06ff62ece865cfd597e75.tar.xz
node-coveralls-e0b5ccc5de04f1c8d2e06ff62ece865cfd597e75.zip
Add callback to handleInput() for easier use in other projects
Since handleInput works completely asynchronous it is necessary to provide a callback function for signaling finished operation. This allows other project to call the handleInput function.
-rwxr-xr-xbin/coveralls.js6
-rw-r--r--lib/handleInput.js15
-rw-r--r--test/handleInput.js71
3 files changed, 62 insertions, 30 deletions
diff --git a/bin/coveralls.js b/bin/coveralls.js
index b003b15..324ec6d 100755
--- a/bin/coveralls.js
+++ b/bin/coveralls.js
@@ -13,6 +13,10 @@ process.stdin.on('data', function(chunk) {
});
process.stdin.on('end', function() {
- handleInput(input);
+ handleInput(input, function(err) {
+ if (err) {
+ throw err;
+ }
+ });
});
diff --git a/lib/handleInput.js b/lib/handleInput.js
index 807e31d..5f88394 100644
--- a/lib/handleInput.js
+++ b/lib/handleInput.js
@@ -1,31 +1,36 @@
var index = require('../index');
var logger = require('./logger')();
-function handleInput(input) {
+function handleInput(input, cb) {
logger.debug(input);
var options = index.getOptions(function(err, options){
if (err){
logger.error("error from getOptions");
- throw err;
+ cb(err);
+ return;
}
logger.debug(options);
index.convertLcovToCoveralls(input, options, function(err, postData){
if (err){
logger.error("error from convertLcovToCoveralls");
- throw err;
+ cb(err);
+ return;
}
logger.info("sending this to coveralls.io: ", JSON.stringify(postData));
index.sendToCoveralls(postData, function(err, response, body){
if (err){
- throw err;
+ cb(err);
+ return;
}
if (response.statusCode >= 400){
- throw "Bad response: " + response.statusCode + " " + body;
+ cb("Bad response: " + response.statusCode + " " + body);
+ return;
}
logger.debug(response.statusCode);
logger.debug(body);
+ cb(null);
});
});
});
diff --git a/test/handleInput.js b/test/handleInput.js
index f06e8a4..dc88902 100644
--- a/test/handleInput.js
+++ b/test/handleInput.js
@@ -8,48 +8,71 @@ describe("handleInput", function(){
afterEach(function() {
sinon.restoreAll();
});
- it ("throws an error when there's an error sending", function(done){
+ it ("returns an error when there's an error getting options", function(done){
+ sinon.stub(index, 'getOptions', function(cb){
+ return cb("some error", {});
+ });
+ var path = __dirname + "/../fixtures/onefile.lcov";
+ var input = fs.readFileSync(path, "utf8");
+ index.handleInput(input, function(err){
+ err.should.equal("some error");
+ done();
+ });
+ });
+ it ("returns an error when there's an error converting", function(done){
+ sinon.stub(index, 'getOptions', function(cb){
+ return cb(null, {});
+ });
+ sinon.stub(index, 'convertLcovToCoveralls', function(input, options, cb){
+ cb("some error");
+ });
+ var path = __dirname + "/../fixtures/onefile.lcov";
+ var input = fs.readFileSync(path, "utf8");
+ index.handleInput(input, function(err){
+ err.should.equal("some error");
+ done();
+ });
+ });
+ it ("returns an error when there's an error sending", function(done){
sinon.stub(index, 'getOptions', function(cb){
return cb(null, {});
});
sinon.stub(index, 'sendToCoveralls', function(postData, cb){
- try {
- cb("some error");
- should.fail("expected exception was not raised");
- } catch (ex) {
- done();
- }
- });
- var path = __dirname + "/../fixtures/onefile.lcov";
+ cb("some error");
+ });
+ var path = __dirname + "/../fixtures/onefile.lcov";
var input = fs.readFileSync(path, "utf8");
- index.handleInput(input);
+ index.handleInput(input, function(err){
+ err.should.equal("some error");
+ done();
+ });
});
- it ("throws an error when there's a bad status code", function(done){
+ it ("returns an error when there's a bad status code", function(done){
sinon.stub(index, 'getOptions', function(cb){
return cb(null, {});
});
sinon.stub(index, 'sendToCoveralls', function(postData, cb){
- try {
- cb(null, {statusCode : 500}, "body");
- should.fail("expected exception was not raised");
- } catch (ex) {
- done();
- }
- });
- var path = __dirname + "/../fixtures/onefile.lcov";
+ cb(null, {statusCode : 500}, "body");
+ });
+ var path = __dirname + "/../fixtures/onefile.lcov";
var input = fs.readFileSync(path, "utf8");
- index.handleInput(input);
+ index.handleInput(input, function(err){
+ err.should.equal("Bad response: 500 body");
+ done();
+ });
});
- it ("completes successfully when there are now errors", function(done){
+ it ("completes successfully when there are no errors", function(done){
sinon.stub(index, 'getOptions', function(cb){
return cb(null, {});
});
sinon.stub(index, 'sendToCoveralls', function(postData, cb){
cb(null, {statusCode : 200}, "body");
- done();
});
- var path = __dirname + "/../fixtures/onefile.lcov";
+ var path = __dirname + "/../fixtures/onefile.lcov";
var input = fs.readFileSync(path, "utf8");
- index.handleInput(input);
+ index.handleInput(input, function(err){
+ (err === null).should.equal(true);
+ done();
+ });
});
});