Skip to content

Commit

Permalink
feat: add skip-empty option for html & text reports (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
moos authored and bcoe committed Feb 22, 2018
1 parent 67918e2 commit d2a4262
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 12 deletions.
7 changes: 7 additions & 0 deletions packages/istanbul-lib-coverage/lib/file.js
Expand Up @@ -96,6 +96,13 @@ CoverageSummary.prototype.toJSON = function () {
return this.data;
};

/**
* return true if summary has no lines of code
*/
CoverageSummary.prototype.isEmpty = function () {
return this.lines.total === 0;
};

// returns a data object that represents empty coverage
function emptyCoverage(filePath) {
return {
Expand Down
5 changes: 5 additions & 0 deletions packages/istanbul-lib-coverage/test/file.test.js
Expand Up @@ -101,6 +101,11 @@ describe('coverage summary', function () {
});
assert.equal(data.branches.pct, 100);
});

it('isEmpty() by default', function () {
var cs = new CoverageSummary();
assert.equal(cs.isEmpty(), true);
});
});

describe('base coverage', function () {
Expand Down
8 changes: 8 additions & 0 deletions packages/istanbul-reports/lib/html/assets/base.css
Expand Up @@ -183,6 +183,14 @@ table.coverage td span.cline-any {
span.cline-neutral { background: #eaeaea; }
.medium { background: #eaeaea; }

.coverage-summary td.empty {
opacity: .5;
padding-top: 4px;
padding-bottom: 4px;
line-height: 1;
color: #888;
}

.cover-fill, .cover-empty {
display:inline-block;
height: 12px;
Expand Down
23 changes: 20 additions & 3 deletions packages/istanbul-reports/lib/html/index.js
Expand Up @@ -57,7 +57,13 @@ var fs = require('fs'),
'</tbody>',
'</table>',
'</div>'
].join('\n');
].join('\n'),
emptyClasses = {
statements: 'empty',
lines: 'empty',
functions: 'empty',
branches: 'empty'
};

helpers.registerHelpers(handlebars);

Expand Down Expand Up @@ -139,6 +145,7 @@ function HtmlReport(opts) {
this.linkMapper = opts.linkMapper || standardLinkMapper;
this.subdir = opts.subdir || '';
this.date = Date();
this.skipEmpty = opts.skipEmpty;
}

HtmlReport.prototype.getTemplateData = function () {
Expand Down Expand Up @@ -177,10 +184,18 @@ HtmlReport.prototype.onStart = function (root, context) {
});
};

function fixPct(metrics) {
Object.keys(emptyClasses).forEach(function(key) {
metrics[key].pct = 0;
});
return metrics;
}

HtmlReport.prototype.onSummary = function (node, context) {
var linkMapper = this.linkMapper,
templateData = this.getTemplateData(),
children = node.getChildren(),
skipEmpty = this.skipEmpty,
cw;

fillTemplate(node, templateData, linkMapper, context);
Expand All @@ -189,14 +204,16 @@ HtmlReport.prototype.onSummary = function (node, context) {
cw.write(summaryTableHeader);
children.forEach(function (child) {
var metrics = child.getCoverageSummary(),
reportClasses = {
isEmpty = metrics.isEmpty();
if (skipEmpty && isEmpty) { return; }
var reportClasses = isEmpty ? emptyClasses : {
statements: context.classForPercent('statements', metrics.statements.pct),
lines: context.classForPercent('lines', metrics.lines.pct),
functions: context.classForPercent('functions', metrics.functions.pct),
branches: context.classForPercent('branches', metrics.branches.pct)
},
data = {
metrics: metrics,
metrics: isEmpty ? fixPct(metrics) : metrics,
reportClasses: reportClasses,
file: child.getRelativeName(),
output: linkMapper.relativePath(node, child)
Expand Down
20 changes: 12 additions & 8 deletions packages/istanbul-reports/lib/text/index.js
Expand Up @@ -127,16 +127,18 @@ function missingBranches (node, colorizer) {
return colorizer(formatPct(missingLines.join(','), MISSING_COL), 'medium');
}

function tableRow(node, context, colorizer, maxNameCols, level) {
function tableRow(node, context, colorizer, maxNameCols, level, skipEmpty) {
var name = nodeName(node),
metrics = node.getCoverageSummary(),
mm = {
statements: metrics.statements.pct,
branches: metrics.branches.pct,
functions: metrics.functions.pct,
lines: metrics.lines.pct,
isEmpty = metrics.isEmpty();
if (skipEmpty && isEmpty) { return ''; }
var mm = {
statements: isEmpty ? 0 : metrics.statements.pct,
branches: isEmpty ? 0 : metrics.branches.pct,
functions: isEmpty ? 0 : metrics.functions.pct,
lines: isEmpty ? 0 : metrics.lines.pct,
},
colorize = function (str, key) {
colorize = isEmpty ? function(str){ return str; } : function (str, key) {
return colorizer(str, context.classForPercent(key, mm[key]));
},
elements = [];
Expand All @@ -159,6 +161,7 @@ function TextReport(opts) {
this.file = opts.file || null;
this.maxCols = opts.maxCols || 0;
this.cw = null;
this.skipEmpty = opts.skipEmpty;
}

TextReport.prototype.onStart = function (root, context) {
Expand All @@ -182,7 +185,8 @@ TextReport.prototype.onStart = function (root, context) {

TextReport.prototype.onSummary = function (node, context) {
var nodeDepth = depthFor(node);
this.cw.println(tableRow(node, context, this.cw.colorize.bind(this.cw),this.nameWidth, nodeDepth));
var row = tableRow(node, context, this.cw.colorize.bind(this.cw),this.nameWidth, nodeDepth, this.skipEmpty);
if (row) { this.cw.println(row); }
};

TextReport.prototype.onDetail = function (node, context) {
Expand Down
@@ -0,0 +1,18 @@
{
"title": "empty file - skip empty",
"opts": {
"skipEmpty": true
},
"textReportExpected": "----------|----------|----------|----------|----------|----------------|\nFile | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |\n----------|----------|----------|----------|----------|----------------|\n----------|----------|----------|----------|----------|----------------|\n",
"map": {
"/dev/git/istanbuljs/packages/istanbul-reports/lib/text/empty.js": {
"path": "/dev/git/istanbuljs/packages/istanbul-reports/lib/text/empty.js",
"statementMap": {},
"fnMap": {},
"branchMap": {},
"s": {},
"f": {},
"b": {}
}
}
}
15 changes: 15 additions & 0 deletions packages/istanbul-reports/test/fixtures/specs/empty-file.json
@@ -0,0 +1,15 @@
{
"title": "empty file - show 0%",
"textReportExpected": "----------|----------|----------|----------|----------|----------------|\nFile | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |\n----------|----------|----------|----------|----------|----------------|\nAll files | 0 | 0 | 0 | 0 |\u001b[31;1m \u001b[0m |\n empty.js | 0 | 0 | 0 | 0 |\u001b[31;1m \u001b[0m |\n----------|----------|----------|----------|----------|----------------|\n",
"map": {
"/dev/git/istanbuljs/packages/istanbul-reports/lib/text/empty.js": {
"path": "/dev/git/istanbuljs/packages/istanbul-reports/lib/text/empty.js",
"statementMap": {},
"fnMap": {},
"branchMap": {},
"s": {},
"f": {},
"b": {}
}
}
}
2 changes: 1 addition & 1 deletion packages/istanbul-reports/test/text/index.js
Expand Up @@ -31,7 +31,7 @@ describe('TextReport', function () {
dir: './'
});
var tree = fixture.map;
var report = new TextReport(context);
var report = new TextReport(fixture.opts);
tree.visit(report, context);
var output = FileWriter.getOutput();
output.should.equal(fixture.textReportExpected);
Expand Down

0 comments on commit d2a4262

Please sign in to comment.