Skip to content

Commit

Permalink
Bug Fix: Added error message for missing lcov.info
Browse files Browse the repository at this point in the history
Also did a lot of refactoring and failed attempts at testing
  • Loading branch information
mattjmorrison committed Dec 9, 2013
1 parent 1f74c22 commit 7decfef
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"name": "grunt-karma-coveralls",
"description": "An npm module for using Karma and Coveralls with Grunt",
"version": "2.1.1",
"version": "2.2.0",
"repository": {
"type": "git",
"url": "https://github.com/mattjmorrison/grunt-karma-coveralls"
Expand All @@ -26,6 +26,17 @@
"dependencies": {
"coveralls": "~2.6.0",
"karma-coverage": "~0.1.0",
"glob": "*"
"glob": "*",
"log-driver": "1.2.1"
},
"peerDependencies": {
"grunt": "*"
},
"devDependencies": {
"grunt": "*",
"mocha": "*",
"should": "*",
"sinon": "*",
"request": "2.16.2"
}
}
85 changes: 52 additions & 33 deletions tasks/main.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
module.exports = function(grunt){
var fs = require('fs');
var glob = require('glob');

grunt.task.registerTask('coveralls', 'Coveralls coverage with Karma', function(){
var done = this.async();
var gruntOptions = grunt.config('coveralls.options');
process.env.NODE_COVERALLS_DEBUG = gruntOptions.debug ? 1 : 0;
var lcov_path = glob.sync(gruntOptions.coverage_dir + "/**/lcov.info")[0];
var input = fs.readFileSync(lcov_path).toString();
var coveralls = require('coveralls/index');

coveralls.getBaseOptions(function(err, options){
options.filepath = ".";
coveralls.convertLcovToCoveralls(input, options, function(err, postData){
if (err){
done();
throw err;
}
coveralls.sendToCoveralls(postData, function(err, response, body){
if (err){
done();
throw err;
}
if (response.statusCode >= 400){
done();
throw "Bad response: " + response.statusCode + " " + body;
}
done();
});
});
});
});
var fs = require('fs');
var glob = require('glob');
var logger = require('log-driver')({level: 'debug'});

function main(grunt){

grunt.task.registerTask('coveralls', 'Coveralls coverage with Karma', function(){
var done = this.async();
var gruntOptions = grunt.config('coveralls.options');
process.env.NODE_COVERALLS_DEBUG = gruntOptions.debug ? 1 : 0;
var input = getInput(gruntOptions.coverage_dir);
callCoveralls(done, input);
});

};

function callCoveralls(done, input){
var coveralls = require('coveralls/index');
coveralls.getBaseOptions(function(err, options){
options.filepath = ".";
coveralls.convertLcovToCoveralls(input, options, function(err, postData){
handleError(done, err);
coveralls.sendToCoveralls(postData, function(err, response, body){
sendToCoverallsCallback(done, err, response, body);
});
});
});
}

function handleError(done, err) {
if (err){
done();
throw err;
}
}

function sendToCoverallsCallback(done, err, response, body){
handleError(done, err);
if (response.statusCode >= 400){
handleError(done, "Bad response:" + response.statusCode + " " + body);
}
done();
}

function getInput(basePath){
var lcov_path = glob.sync(basePath + "/**/lcov.info")[0];
if (!lcov_path){
logger.error("lcov.info not found in `" + basePath + "`");
}
return fs.readFileSync(lcov_path).toString();
}

module.exports = main;
13 changes: 13 additions & 0 deletions test/data/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var App, something;

App = Ember.Application.create();

something = function(x) {
if (x < 10) {
return console.log("Less");
} else if (x === 10) {
return console.log("Equal");
} else {
return console.log("Greater");
}
};
23 changes: 23 additions & 0 deletions test/data/lcov.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
TN:
SF:./test/data/app.js
FN:5,(anonymous_1)
FNF:1
FNH:0
FNDA:0,(anonymous_1)
DA:1,1
DA:3,1
DA:5,1
DA:6,0
DA:7,0
DA:8,0
DA:9,0
DA:11,0
LF:8
LH:3
BRDA:6,1,0,0
BRDA:6,1,1,0
BRDA:8,2,0,0
BRDA:8,2,1,0
BRF:4
BRH:0
end_of_record
21 changes: 21 additions & 0 deletions test/main_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var should = require('should');
var sinon = require('sinon');
var request = require('request');
var grunt = require('grunt');
var sut = require('../tasks/main');
var assert = require('assert');

describe("grunt-karma-coveralls", function(){

beforeEach(function(){
sut(grunt);
});

it("shows error message when lcov data does not exist", function(done){
grunt.config('coveralls', {options: {coverage_dir: './dummy/asdf'}});
grunt.task.run('coveralls');
grunt.task.start();
done();
});

});

0 comments on commit 7decfef

Please sign in to comment.