Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrong output truecount and falsecount of clover #699

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,3 +9,4 @@ dist
lerna-debug.log
packages/*/yarn.lock
packages/nyc/
.vscode/*
26 changes: 21 additions & 5 deletions packages/istanbul-reports/lib/clover/index.js
Expand Up @@ -119,7 +119,7 @@ class CloverReport extends ReportBase {
onDetail(node) {
const fileCoverage = node.getFileCoverage();
const metrics = node.getCoverageSummary();
const branchByLine = fileCoverage.getBranchCoverageByLine();
const branchDetails = getBranchDetails(fileCoverage);

this.xml.openTag('file', {
name: asClassName(node),
Expand All @@ -135,12 +135,12 @@ class CloverReport extends ReportBase {
count,
type: 'stmt'
};
const branchDetail = branchByLine[k];
const branchDetail = branchDetails[k];

if (branchDetail) {
if (branchDetail && branchDetail.type === 'if') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there other branch types we should consider, e.g., ternary operations?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks for point out. I'll dig it further later.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have done a simple search in source code and a testing (Check it in my repo). Except if and switch, there are three types of branches:

default-arg:
The states always have one member and it presents the number of times that default arguments are applied.

cond-expr: (ternary operator)
The states always have two members. The first presents the number of times that condition was judged as true, and the second presents the number of times that condition was judged as false.

binary-expr:
The number of members is equal to the number of conditions. The xth member presents the number of times that xth condition was calculated. And it has nothing to do with the nest and calculating order.


For default-arg, I think it should be considered a statement according to the standard mentioned at #695 .

For cond-expr, it could be deal with the same as if.

I have no good idea to deal with binary-expr. Maybe we can consider it as covered when all conditions are calculated?

attrs.type = 'cond';
attrs.truecount = branchDetail.covered;
attrs.falsecount = branchDetail.total - branchDetail.covered;
attrs.truecount = branchDetail.states[0];
attrs.falsecount = branchDetail.states[1];
}
this.xml.inlineTag('line', attrs);
});
Expand All @@ -161,4 +161,20 @@ function asClassName(node) {
return node.getRelativeName().replace(/.*[\\/]/, '');
}

function getBranchDetails(fileCoverage) {
const branchMeta = fileCoverage.branchMap;
const branchStats = fileCoverage.b;

const branchDetails = {};

Object.entries(branchMeta).forEach(([index, branch]) => {
branchDetails[branch.loc.start.line] = {
type: branch.type,
states: branchStats[index]
};
});

return branchDetails;
}

module.exports = CloverReport;