Skip to content

Commit fb5f20f

Browse files
committed
fix cobertura report generator output
1 parent e2f8ebe commit fb5f20f

File tree

1 file changed

+56
-9
lines changed

1 file changed

+56
-9
lines changed

src/coverlet.core/Reporters/CoberturaReporter.cs

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public string Report(CoverageResult result)
1818
{
1919
CoverageSummary summary = new CoverageSummary();
2020

21+
int totalLines = 0, coveredLines = 0, totalBranches = 0, coveredBranches = 0;
22+
2123
XDocument xml = new XDocument();
2224
XElement coverage = new XElement("coverage");
2325
coverage.Add(new XAttribute("line-rate", summary.CalculateLineCoverage(result.Modules).ToString()));
@@ -26,16 +28,14 @@ public string Report(CoverageResult result)
2628
coverage.Add(new XAttribute("timestamp", ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString()));
2729

2830
XElement sources = new XElement("sources");
29-
foreach (var src in GetSources(result.Modules))
30-
{
31-
XElement source = new XElement("source", src);
32-
sources.Add(source);
33-
}
31+
var basePath = GetBasePath(result.Modules);
32+
sources.Add(new XElement("source", basePath));
3433

3534
XElement packages = new XElement("packages");
3635
foreach (var module in result.Modules)
3736
{
3837
XElement package = new XElement("package");
38+
package.Add(new XAttribute("name", Path.GetFileNameWithoutExtension(module.Key)));
3939
package.Add(new XAttribute("line-rate", summary.CalculateLineCoverage(module.Value).ToString()));
4040
package.Add(new XAttribute("branch-rate", summary.CalculateBranchCoverage(module.Value).ToString()));
4141
package.Add(new XAttribute("complexity", "0"));
@@ -47,17 +47,19 @@ public string Report(CoverageResult result)
4747
{
4848
XElement @class = new XElement("class");
4949
@class.Add(new XAttribute("name", cls.Key));
50-
@class.Add(new XAttribute("filename", Path.GetFileName(document.Key)));
50+
@class.Add(new XAttribute("filename", GetRelativePathFromBase(basePath, document.Key)));
5151
@class.Add(new XAttribute("line-rate", summary.CalculateLineCoverage(cls.Value).ToString()));
5252
@class.Add(new XAttribute("branch-rate", summary.CalculateBranchCoverage(cls.Value).ToString()));
5353
@class.Add(new XAttribute("complexity", "0"));
5454

55+
XElement classLines = new XElement("lines");
5556
XElement methods = new XElement("methods");
57+
5658
foreach (var meth in cls.Value)
5759
{
5860
XElement method = new XElement("method");
5961
method.Add(new XAttribute("name", meth.Key.Split(':')[2].Split('(')[0]));
60-
method.Add(new XAttribute("signature", meth.Key));
62+
method.Add(new XAttribute("signature", "(" + meth.Key.Split(':')[2].Split('(')[1]));
6163
method.Add(new XAttribute("line-rate", summary.CalculateLineCoverage(meth.Value).ToString()));
6264
method.Add(new XAttribute("branch-rate", summary.CalculateBranchCoverage(meth.Value).ToString()));
6365

@@ -69,14 +71,37 @@ public string Report(CoverageResult result)
6971
line.Add(new XAttribute("hits", ln.Value.Hits.ToString()));
7072
line.Add(new XAttribute("branch", ln.Value.IsBranchPoint.ToString()));
7173

74+
totalLines++;
75+
if (ln.Value.Hits > 0) coveredLines++;
76+
77+
78+
if (ln.Value.IsBranchPoint)
79+
{
80+
line.Add(new XAttribute("condition-coverage", "100% (1/1)"));
81+
XElement conditions = new XElement("conditions");
82+
XElement condition = new XElement("condition");
83+
condition.Add(new XAttribute("number", "0"));
84+
condition.Add(new XAttribute("type", "jump"));
85+
condition.Add(new XAttribute("coverage", "100%"));
86+
87+
totalBranches++;
88+
if (ln.Value.Hits > 0) coveredBranches++;
89+
90+
conditions.Add(condition);
91+
line.Add(conditions);
92+
}
93+
94+
7295
lines.Add(line);
96+
classLines.Add(line);
7397
}
7498

7599
method.Add(lines);
76100
methods.Add(method);
77101
}
78102

79103
@class.Add(methods);
104+
@class.Add(classLines);
80105
classes.Add(@class);
81106
}
82107
}
@@ -85,6 +110,11 @@ public string Report(CoverageResult result)
85110
packages.Add(package);
86111
}
87112

113+
coverage.Add(new XAttribute("lines-covered", coveredLines.ToString()));
114+
coverage.Add(new XAttribute("lines-valid", totalLines.ToString()));
115+
coverage.Add(new XAttribute("branches-covered", coveredBranches.ToString()));
116+
coverage.Add(new XAttribute("branches-valid", totalBranches.ToString()));
117+
88118
coverage.Add(sources);
89119
coverage.Add(packages);
90120
xml.Add(coverage);
@@ -95,16 +125,33 @@ public string Report(CoverageResult result)
95125
return Encoding.UTF8.GetString(stream.ToArray());
96126
}
97127

98-
private string[] GetSources(Modules modules)
128+
private string GetBasePath(Modules modules)
99129
{
100130
List<string> sources = new List<string>();
131+
string source = string.Empty;
132+
101133
foreach (var module in modules)
102134
{
103135
sources.AddRange(
104136
module.Value.Select(d => Path.GetDirectoryName(d.Key)));
105137
}
106138

107-
return sources.Distinct().ToArray();
139+
sources = sources.Distinct().ToList();
140+
var segments = sources[0].Split(Path.DirectorySeparatorChar);
141+
142+
foreach (var segment in segments)
143+
{
144+
var startsWith = sources.All(s => s.StartsWith(source + segment));
145+
if (!startsWith)
146+
break;
147+
148+
source += segment + Path.DirectorySeparatorChar;
149+
}
150+
151+
return source;
108152
}
153+
154+
private string GetRelativePathFromBase(string source, string path)
155+
=> path.Replace(source, string.Empty);
109156
}
110157
}

0 commit comments

Comments
 (0)