Permalink
Browse files

Reporting for istanbul works now too

  • Loading branch information...
1 parent 0ac235d commit 07679607d1bde55fbeb9a2b3d7af20a4c82f3a65 @davglass committed Sep 16, 2012
Showing with 147 additions and 15 deletions.
  1. +147 −15 lib/coverage.js
View
@@ -11,25 +11,52 @@ exports.options = function(o) {
options = o;
};
+var isIstanbul = function(json) {
+ var first = Object.keys(json)[0],
+ ret = false;
+
+ if (json[first].s !== undefined && json[first].fnMap !== undefined) {
+ ret = true;
+ }
+
+ if (json.s !== undefined && json.fnMap !== undefined) {
+ ret = true;
+ }
+ if (ret) {
+ coverageType = 'istanbul';
+ }
+ return ret;
+};
+
var set = function(json) {
var d = {}, i;
+
for (i in json) {
d[i] = json[i];
delete d[i].code;
}
- for (i in json) {
- coverageInfo[i] = coverageInfo[i] || {};
- coverageInfo[i].path = i;
- coverageInfo[i].calledLines = coverageInfo[i].calledLines || 0;
- coverageInfo[i].coveredLines = coverageInfo[i].coveredLines || 0;
- coverageInfo[i].calledFunctions = coverageInfo[i].calledFunctions || 0;
- coverageInfo[i].coveredFunctions = coverageInfo[i].coveredFunctions || 0;
-
- coverageInfo[i].calledLines += json[i].calledLines;
- coverageInfo[i].coveredLines += json[i].coveredLines;
- coverageInfo[i].calledFunctions += json[i].calledFunctions;
- coverageInfo[i].coveredFunctions += json[i].coveredFunctions;
+ if (isIstanbul(json)) {
+ if (!Array.isArray(coverageInfo)) {
+ coverageInfo = [];
+ }
+ coverageInfo.push(json);
+ } else {
+ for (i in json) {
+ if (json[i].coveredLines !== undefined) { //YUITest
+ coverageInfo[i] = coverageInfo[i] || {};
+ coverageInfo[i].path = i;
+ coverageInfo[i].calledLines = coverageInfo[i].calledLines || 0;
+ coverageInfo[i].coveredLines = coverageInfo[i].coveredLines || 0;
+ coverageInfo[i].calledFunctions = coverageInfo[i].calledFunctions || 0;
+ coverageInfo[i].coveredFunctions = coverageInfo[i].coveredFunctions || 0;
+
+ coverageInfo[i].calledLines += json[i].calledLines;
+ coverageInfo[i].coveredLines += json[i].coveredLines;
+ coverageInfo[i].calledFunctions += json[i].calledFunctions;
+ coverageInfo[i].coveredFunctions += json[i].coveredFunctions;
+ }
+ }
}
};
@@ -100,6 +127,97 @@ var printYUITestReport = function() {
};
var printIstanbulReport = function() {
+ var collect = new istanbul.Collector(),
+ table = new Table({
+ head: ['path', 'lines', '%', 'statements', '%', 'functions', '%', 'branches', '%' ],
+ colAligns: [ 'left', 'center', 'right', 'center', 'right', 'center', 'right', 'center', 'right' ],
+ style: {
+ 'padding-left': 2,
+ 'padding-right': 2,
+ head: ['blue']
+ }
+ });
+
+ coverageInfo.forEach(function(coverage) {
+ collect.add(coverage);
+ });
+
+ collect.files().sort().forEach(function(file) {
+ var err = false,
+ fileCoverage = collect.fileCoverageFor(file),
+ row = istanbul.utils.summarizeFileCoverage(fileCoverage),
+ cell = [
+ file,
+ row.lines.covered + '/' + row.lines.total,
+ row.lines.pct + '%',
+ row.statements.covered + '/' + row.statements.total,
+ row.statements.pct + '%',
+ row.functions.covered + '/' + row.functions.total,
+ row.functions.pct + '%',
+ row.branches.covered + '/' + row.branches.total,
+ row.branches.pct + '%'
+ ];
+
+ if (row.lines.pct <= options.coverageWarn) {
+ err = true;
+ cell[1] = String(cell[1]).red;
+ cell[2] = String(cell[2]).red;
+ }
+ if (row.statements.pct <= options.coverageWarn) {
+ err = true;
+ cell[3] = String(cell[3]).red;
+ cell[4] = String(cell[4]).red;
+ }
+ if (row.functions.pct <= options.coverageWarn) {
+ err = true;
+ cell[5] = String(cell[5]).red;
+ cell[6] = String(cell[6]).red;
+ }
+ if (row.branches.pct <= options.coverageWarn) {
+ err = true;
+ cell[7] = String(cell[7]).red;
+ cell[8] = String(cell[8]).red;
+ }
+ if (err) {
+ cell[0] = util.bad.red + ' ' + cell[0];
+ } else {
+ cell[0] = util.good.green + ' ' + cell[0];
+ }
+ table.push(cell);
+ });
+
+ /*
+ items.forEach(function(row) {
+ var err,
+ percentLine = Math.floor((row.calledLines / row.coveredLines) * 100),
+ percentFunction = Math.floor((row.calledFunctions / row.coveredFunctions) * 100),
+ cell = [
+ row.path,
+ row.calledLines + '/' + row.coveredLines,
+ percentLine + '%',
+ row.calledFunctions + '/' + row.coveredFunctions,
+ percentFunction + '%',
+ ];
+
+ if (percentLine <= options.coverageWarn) {
+ err = true;
+ cell[1] = String(cell[1]).red;
+ cell[2] = String(cell[2]).red;
+ }
+ if (percentFunction <= options.coverageWarn) {
+ err = true;
+ cell[3] = String(cell[3]).red;
+ cell[4] = String(cell[4]).red;
+ }
+ if (err) {
+ cell[0] = util.bad.red + ' ' + cell[0];
+ } else {
+ cell[0] = util.good.green + ' ' + cell[0];
+ }
+ table.push(cell);
+ });
+ */
+ console.log(table.toString());
};
var report = function() {
@@ -131,13 +249,27 @@ var getYUIStatus = function(coverage) {
return str;
};
-var getIstanbulStatus = function() {
-};
+var getIstanbulStatus = function(coverage) {
+ var collect = new istanbul.Collector(),
+ lines = [],
+ str = '',
+ pct = 0;
+ collect.add(coverage);
+ collect.files().forEach(function(file) {
+ var fileCoverage = collect.fileCoverageFor(file),
+ summary = istanbul.utils.summarizeFileCoverage(fileCoverage);
+ lines.push(summary.lines.pct);
+ pct += summary.lines.pct;
+ });
+
+ str = (' ' + Math.floor(pct / lines.length) + '%').blue;
+ return str;
+};
var status = function(json) {
var out;
- if (json.coverageType && json.coverageType === 'istanbul') {
+ if (isIstanbul(json)) {
coverageType = 'istanbul';
out = getIstanbulStatus(json);
} else {

0 comments on commit 0767960

Please sign in to comment.