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 reporter does not respect the provided DTD #603

Open
hamjo opened this issue Apr 4, 2021 · 5 comments
Open

Cobertura reporter does not respect the provided DTD #603

hamjo opened this issue Apr 4, 2021 · 5 comments
Labels

Comments

@hamjo
Copy link

hamjo commented Apr 4, 2021

The provided DTD for cobertura is not respected:

  • The package XML element is missing the complexity attribute
  • The class XML element is missing the complexity attribute
  • The method XML element is missing the complexity, line-rate and branch-rate and has the hits attribute, which should not be present.
@hamjo
Copy link
Author

hamjo commented Apr 4, 2021

From what I saw, the complexity attribute is fine if set to 0 in all cases, but I am unsure on how the line-rate and branch-rate should be set for a method.

@hamjo
Copy link
Author

hamjo commented Apr 7, 2021

I am slowly working on a PR over here

@rcampbel
Copy link

@hamjo,

I think what you are looking for is something like:

diff --git a/packages/istanbul-reports/lib/cobertura/index.js b/packages/istanbul-reports/lib/cobertura/index.js
index ac37fc4..8e24843 100644
--- a/packages/istanbul-reports/lib/cobertura/index.js
+++ b/packages/istanbul-reports/lib/cobertura/index.js
@@ -80,6 +80,7 @@ class CoberturaReport extends ReportBase {
         const fileCoverage = node.getFileCoverage();
         const metrics = node.getCoverageSummary();
         const branchByLine = fileCoverage.getBranchCoverageByLine();
+        const lines = fileCoverage.getLineCoverage();
 
         this.xml.openTag('class', {
             name: escape(asClassName(node)),
@@ -92,13 +93,33 @@ class CoberturaReport extends ReportBase {
         this.xml.openTag('methods');
[patch.txt](https://github.com/istanbuljs/istanbuljs/files/6385155/patch.txt)

         const fnMap = fileCoverage.fnMap;
         Object.entries(fnMap).forEach(([k, { name, decl }]) => {
+            const isLineInfunction = lineNumber =>
+                decl.start.line < lineNumber && lineNumber < decl.stop.line;
+
+            const branchesInFunction = Object.entries(branchByLine).filter(
+                ([lineNumber]) => isLineInfunction(lineNumber)
+            );
+
+            const branchesCoveredInFunction = branchesInFunction.filter(
+                ([, branchDetail]) => branchDetail.covered
+            );
+
+            const linesInFunction = Object.entries(lines).filter(
+                ([lineNumber]) => isLineInfunction(lineNumber)
+            );
+
+            const linesCoveredInFunction = linesInFunction.filter(
+                ([, hits]) => hits > 0
+            );
+
             const hits = fileCoverage.f[k];
             this.xml.openTag('method', {
                 name: escape(name),
                 signature: '()V', //fake out a no-args void return,
                 complexity: '0',
-                'line-rate': metrics.lines.pct / 100.0,
-                'branch-rate': metrics.branches.pct / 100.0
+                'line-rate': linesCoveredInFunction / linesInFunction.length,
+                'branch-rate':
+                    branchesCoveredInFunction / branchesInFunction.length
             });
             this.xml.openTag('lines');
             //Add the function definition line and hits so that jenkins cobertura plugin records method hits
@@ -112,7 +133,6 @@ class CoberturaReport extends ReportBase {
         this.xml.closeTag('methods');
 
         this.xml.openTag('lines');
-        const lines = fileCoverage.getLineCoverage();
         Object.entries(lines).forEach(([k, hits]) => {
             const attrs = {
                 number: k,

Note: I hope I got everything right for the data structure since this is my first time delving into this code base

@joeskeen
Copy link

Haha just found this issue after implementing the Cobertura format for the Pester PowerShell Test runner:
pester/Pester#2298 (comment)
Perhaps after I get that one up to spec I can come over and show this one some love ;)

@bcoe
Copy link
Member

bcoe commented Feb 16, 2023

@joeskeen would love the help! I've never used Cobertura format before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants