/
JaCoCoParser.cs
405 lines (335 loc) · 16.1 KB
/
JaCoCoParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
using Palmmedia.ReportGenerator.Core.Common;
using Palmmedia.ReportGenerator.Core.Logging;
using Palmmedia.ReportGenerator.Core.Parser.Analysis;
using Palmmedia.ReportGenerator.Core.Parser.Filtering;
using Palmmedia.ReportGenerator.Core.Properties;
namespace Palmmedia.ReportGenerator.Core.Parser
{
/// <summary>
/// Parser for XML reports generated by JaCoCo.
/// </summary>
internal class JaCoCoParser : ParserBase
{
/// <summary>
/// The Logger.
/// </summary>
private static readonly ILogger Logger = LoggerFactory.GetLogger(typeof(JaCoCoParser));
/// <summary>
/// Regex to extract short method name.
/// </summary>
private static readonly Regex MethodRegex = new Regex(@"^(?<MethodName>.+)\((?<Arguments>.*)\).*$", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="JaCoCoParser" /> class.
/// </summary>
/// <param name="assemblyFilter">The assembly filter.</param>
/// <param name="classFilter">The class filter.</param>
/// <param name="fileFilter">The file filter.</param>
internal JaCoCoParser(IFilter assemblyFilter, IFilter classFilter, IFilter fileFilter)
: base(assemblyFilter, classFilter, fileFilter)
{
}
/// <summary>
/// Parses the given XML report.
/// </summary>
/// <param name="report">The XML report.</param>
/// <returns>The parser result.</returns>
public ParserResult Parse(XContainer report)
{
if (report == null)
{
throw new ArgumentNullException(nameof(report));
}
var assemblies = new List<Assembly>();
var modules = report.Descendants("package")
.ToArray();
var assemblyNames = modules
.Select(m => m.Attribute("name").Value)
.Distinct()
.Where(a => this.AssemblyFilter.IsElementIncludedInReport(a))
.OrderBy(a => a)
.ToArray();
foreach (var assemblyName in assemblyNames)
{
assemblies.Add(this.ProcessAssembly(modules, assemblyName));
}
var result = new ParserResult(assemblies.OrderBy(a => a.Name).ToList(), true, this.ToString());
if (report.Element("sessioninfo")?.Attribute("start") != null)
{
try
{
DateTime timeStamp = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
timeStamp = timeStamp.AddMilliseconds(double.Parse(report.Element("sessioninfo").Attribute("start").Value)).ToLocalTime();
result.MinimumTimeStamp = timeStamp;
result.MaximumTimeStamp = timeStamp;
}
catch (Exception)
{
// Ignore since timestamp is not relevant. If timestamp is missing or in wrong format the information is just missing in the report(s)
}
}
return result;
}
/// <summary>
/// Processes the given assembly.
/// </summary>
/// <param name="modules">The modules.</param>
/// <param name="assemblyName">Name of the assembly.</param>
/// <returns>The <see cref="Assembly"/>.</returns>
private Assembly ProcessAssembly(XElement[] modules, string assemblyName)
{
Logger.DebugFormat(Resources.CurrentAssembly, assemblyName);
var classNames = modules
.Where(m => m.Attribute("name").Value.Equals(assemblyName))
.Elements("class")
.Select(c => c.Attribute("name").Value)
.Where(c => !c.Contains("$"))
.Distinct()
.Where(c => this.ClassFilter.IsElementIncludedInReport(c))
.OrderBy(name => name)
.ToArray();
var assembly = new Assembly(assemblyName);
Parallel.ForEach(classNames, className => this.ProcessClass(modules, assembly, className));
return assembly;
}
/// <summary>
/// Processes the given class.
/// </summary>
/// <param name="modules">The modules.</param>
/// <param name="assembly">The assembly.</param>
/// <param name="className">Name of the class.</param>
private void ProcessClass(XElement[] modules, Assembly assembly, string className)
{
var classes = modules
.Where(m => m.Attribute("name").Value.Equals(assembly.Name))
.Elements("class")
.Where(c => c.Attribute("name").Value.Equals(className)
|| c.Attribute("name").Value.StartsWith(className + "$", StringComparison.Ordinal))
.ToArray();
var files = classes
.Select(c => c.Attribute("sourcefilename")?.Value)
.Where(f => f != null) // This attribute is not present in older JaCoCo versions
.Distinct()
.ToArray();
var filteredFiles = files
.Where(f => this.FileFilter.IsElementIncludedInReport(f))
.ToArray();
// If all files are removed by filters, then the whole class is omitted
if ((files.Length == 0 && !this.FileFilter.HasCustomFilters) || filteredFiles.Length > 0)
{
var @class = new Class(className, assembly);
foreach (var file in filteredFiles)
{
var codeFile = ProcessFile(modules, @class, file, out int numberOrLines);
var methodsOfFile = classes
.Where(c => c.Attribute("sourcefilename") != null && c.Attribute("sourcefilename").Value.Equals(file))
.Elements("method")
.ToArray();
SetMethodMetrics(codeFile, methodsOfFile);
SetCodeElements(codeFile, methodsOfFile, numberOrLines);
@class.AddFile(codeFile);
}
assembly.AddClass(@class);
}
}
/// <summary>
/// Processes the file.
/// </summary>
/// <param name="modules">The modules.</param>
/// <param name="class">The class.</param>
/// <param name="filePath">The file path.</param>
/// <param name="numberOrLines">The number of lines in the file.</param>
/// <returns>The <see cref="CodeFile"/>.</returns>
private static CodeFile ProcessFile(XElement[] modules, Class @class, string filePath, out int numberOrLines)
{
var linesOfFile = modules
.Where(m => m.Attribute("name").Value.Equals(@class.Assembly.Name))
.Elements("sourcefile")
.Where(c => c.Attribute("name").Value.Equals(filePath))
.Elements("line")
.Select(line => new JaCoCoLineCoverage()
{
LineNumber = int.Parse(line.Attribute("nr").Value, CultureInfo.InvariantCulture),
MissedInstructions = (line.Attribute("mi")?.Value ?? "0").ParseLargeInteger(),
CoveredInstructions = (line.Attribute("ci")?.Value ?? "0").ParseLargeInteger(),
MissedBranches = (line.Attribute("mb")?.Value ?? "0").ParseLargeInteger(),
CoveredBranches = (line.Attribute("cb")?.Value ?? "0").ParseLargeInteger()
})
.OrderBy(seqpnt => seqpnt.LineNumber)
.ToArray();
var branches = GetBranches(linesOfFile);
int[] coverage = new int[] { };
LineVisitStatus[] lineVisitStatus = new LineVisitStatus[] { };
if (linesOfFile.Length > 0)
{
coverage = new int[linesOfFile[linesOfFile.LongLength - 1].LineNumber + 1];
lineVisitStatus = new LineVisitStatus[linesOfFile[linesOfFile.LongLength - 1].LineNumber + 1];
for (int i = 0; i < coverage.Length; i++)
{
coverage[i] = -1;
}
foreach (var line in linesOfFile)
{
coverage[line.LineNumber] = line.CoveredInstructions > 0 ? 1 : 0;
bool partiallyCovered = line.MissedInstructions > 0;
LineVisitStatus statusOfLine = line.CoveredInstructions > 0 ? (partiallyCovered ? LineVisitStatus.PartiallyCovered : LineVisitStatus.Covered) : LineVisitStatus.NotCovered;
lineVisitStatus[line.LineNumber] = statusOfLine;
}
}
numberOrLines = coverage.Length - 1;
return new CodeFile(filePath, coverage, lineVisitStatus, branches);
}
/// <summary>
/// Extracts the metrics from the given <see cref="XElement">XElements</see>.
/// </summary>
/// <param name="codeFile">The code file.</param>
/// <param name="methodsOfFile">The methods of the file.</param>
private static void SetMethodMetrics(CodeFile codeFile, IEnumerable<XElement> methodsOfFile)
{
foreach (var method in methodsOfFile)
{
string fullName = method.Attribute("name").Value + method.Attribute("desc").Value;
if (fullName.StartsWith("lambda$"))
{
continue;
}
string shortName = MethodRegex.Replace(fullName, m => string.Format(CultureInfo.InvariantCulture, "{0}({1})", m.Groups["MethodName"].Value, m.Groups["Arguments"].Value.Length > 0 ? "..." : string.Empty));
var metrics = new List<Metric>();
var lineRate = method.Elements("counter")
.Where(e => e.Attribute("type") != null && e.Attribute("type").Value == "LINE")
.FirstOrDefault();
if (lineRate != null)
{
decimal missed = decimal.Parse(lineRate.Attribute("missed").Value, CultureInfo.InvariantCulture);
decimal covered = decimal.Parse(lineRate.Attribute("covered").Value, CultureInfo.InvariantCulture);
decimal total = missed + covered;
metrics.Add(Metric.Coverage(total == 0 ? (decimal?)null : Math.Round((100 * covered) / total, 2, MidpointRounding.AwayFromZero)));
}
else
{
// If no line rate available, do not add branch coverage too
continue;
}
var branchRate = method.Elements("counter")
.Where(e => e.Attribute("type") != null && e.Attribute("type").Value == "BRANCH")
.FirstOrDefault();
if (branchRate != null)
{
decimal missed = decimal.Parse(branchRate.Attribute("missed").Value, CultureInfo.InvariantCulture);
decimal covered = decimal.Parse(branchRate.Attribute("covered").Value, CultureInfo.InvariantCulture);
decimal total = missed + covered;
metrics.Add(Metric.BranchCoverage(total == 0 ? (decimal?)null : Math.Round((100 * covered) / total, 2, MidpointRounding.AwayFromZero)));
}
else
{
// If no branch coverage is available, add default to avoid empty columns
metrics.Add(Metric.BranchCoverage(null));
}
var methodMetric = new MethodMetric(fullName, shortName, metrics);
methodMetric.Line = method.Attribute("line") != null ? int.Parse(method.Attribute("line").Value, CultureInfo.InvariantCulture) : default(int?);
codeFile.AddMethodMetric(methodMetric);
}
}
/// <summary>
/// Extracts the methods/properties of the given <see cref="XElement">XElements</see>.
/// </summary>
/// <param name="codeFile">The code file.</param>
/// <param name="methodsOfFile">The methods of the file.</param>
/// <param name="numberOrLines">The number of lines in the file.</param>
private static void SetCodeElements(CodeFile codeFile, IEnumerable<XElement> methodsOfFile, int numberOrLines)
{
var codeElements = new List<CodeElementBase>();
foreach (var method in methodsOfFile)
{
string methodName = method.Attribute("name").Value + method.Attribute("desc").Value;
if (methodName.StartsWith("lambda$"))
{
continue;
}
methodName = MethodRegex.Replace(methodName, m => string.Format(CultureInfo.InvariantCulture, "{0}({1})", m.Groups["MethodName"].Value, m.Groups["Arguments"].Value));
int lineNumber = int.Parse(method.Attribute("line")?.Value ?? "0", CultureInfo.InvariantCulture);
codeElements.Add(new CodeElementBase(methodName, lineNumber));
}
codeElements.Sort((x, y) => x.FirstLine.CompareTo(y.FirstLine));
for (int i = 0; i < codeElements.Count; i++)
{
var codeElement = codeElements[i];
int lastLine = numberOrLines;
if (i < codeElements.Count - 1)
{
lastLine = codeElements[i + 1].FirstLine - 1;
}
codeFile.AddCodeElement(new CodeElement(
codeElement.Name,
CodeElementType.Method,
codeElement.FirstLine,
lastLine,
codeFile.CoverageQuotaInRange(codeElement.FirstLine, lastLine)));
}
}
/// <summary>
/// Gets the branches by line number.
/// </summary>
/// <param name="lines">The lines.</param>
/// <returns>The branches by line number.</returns>
private static Dictionary<int, ICollection<Branch>> GetBranches(IEnumerable<JaCoCoLineCoverage> lines)
{
var result = new Dictionary<int, ICollection<Branch>>();
foreach (var line in lines)
{
if (line.MissedBranches == 0 && line.CoveredBranches == 0)
{
continue;
}
int numberOfTotalBranches = line.MissedBranches + line.CoveredBranches;
var branches = new HashSet<Branch>();
for (int i = 0; i < numberOfTotalBranches; i++)
{
string identifier = string.Format(
CultureInfo.InvariantCulture,
"{0}_{1}",
line.LineNumber,
i);
branches.Add(new Branch(i < line.CoveredBranches ? 1 : 0, identifier));
}
if (!result.ContainsKey(line.LineNumber))
{
result.Add(line.LineNumber, branches);
}
}
return result;
}
/// <summary>
/// Represents the coverage information of a line.
/// </summary>
private class JaCoCoLineCoverage
{
/// <summary>
/// Gets or sets the line number.
/// </summary>
public int LineNumber { get; set; }
/// <summary>
/// Gets or sets the number of missed instructions.
/// </summary>
public int MissedInstructions { get; set; }
/// <summary>
/// Gets or sets the number of covered instructions.
/// </summary>
public int CoveredInstructions { get; set; }
/// <summary>
/// Gets or sets the number of missed branches.
/// </summary>
public int MissedBranches { get; set; }
/// <summary>
/// Gets or sets the number of covered branches.
/// </summary>
public int CoveredBranches { get; set; }
}
}
}