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

cobertura: emit line and branch coverage for each method #720

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
70 changes: 38 additions & 32 deletions packages/istanbul-reports/lib/cobertura/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,43 @@ class CoberturaReport extends ReportBase {
onDetail(node) {
const fileCoverage = node.getFileCoverage();
const metrics = node.getCoverageSummary();
const lines = fileCoverage.getLineCoverage();
const branchByLine = fileCoverage.getBranchCoverageByLine();

const emitLines = (startLine, endLine) => {
this.xml.openTag('lines');

Object.entries(lines).forEach(([k, hits]) => {
if (
(startLine !== undefined && k < startLine) ||
(endLine !== undefined && k > endLine)
) {
return;
}

const attrs = {
number: k,
hits,
branch: 'false'
};
const branchDetail = branchByLine[k];

if (branchDetail) {
attrs.branch = true;
attrs['condition-coverage'] =
branchDetail.coverage +
'% (' +
branchDetail.covered +
'/' +
branchDetail.total +
')';
}
this.xml.inlineTag('line', attrs);
});

this.xml.closeTag('lines');
};

this.xml.openTag('class', {
name: escape(asClassName(node)),
filename: path.relative(this.projectRoot, fileCoverage.path),
Expand All @@ -90,48 +125,19 @@ class CoberturaReport extends ReportBase {

this.xml.openTag('methods');
const fnMap = fileCoverage.fnMap;
Object.entries(fnMap).forEach(([k, { name, decl }]) => {
Object.entries(fnMap).forEach(([k, { name, loc }]) => {
const hits = fileCoverage.f[k];
this.xml.openTag('method', {
name: escape(name),
hits,
signature: '()V' //fake out a no-args void return
});
this.xml.openTag('lines');
//Add the function definition line and hits so that jenkins cobertura plugin records method hits
this.xml.inlineTag('line', {
number: decl.start.line,
hits
});
this.xml.closeTag('lines');
emitLines(loc.start.line, loc.end.line);
this.xml.closeTag('method');
});
this.xml.closeTag('methods');

this.xml.openTag('lines');
const lines = fileCoverage.getLineCoverage();
Object.entries(lines).forEach(([k, hits]) => {
const attrs = {
number: k,
hits,
branch: 'false'
};
const branchDetail = branchByLine[k];

if (branchDetail) {
attrs.branch = true;
attrs['condition-coverage'] =
branchDetail.coverage +
'% (' +
branchDetail.covered +
'/' +
branchDetail.total +
')';
}
this.xml.inlineTag('line', attrs);
});

this.xml.closeTag('lines');
emitLines();
this.xml.closeTag('class');
}
}
Expand Down