/
Program.cs
369 lines (314 loc) · 18.4 KB
/
Program.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
using CommandLine;
using Microsoft.Extensions.FileSystemGlobbing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace CodeCoverageSummary
{
internal static class Program
{
private static double lowerThreshold = 0.5;
private static double upperThreshold = 0.75;
private static int Main(string[] args)
{
return Parser.Default.ParseArguments<CommandLineOptions>(args)
.MapResult(o =>
{
try
{
// use glob patterns to match files
Matcher matcher = new();
matcher.AddIncludePatterns(o.Files.ToArray());
IEnumerable<string> matchingFiles = matcher.GetResultsInFullPath(".");
if (matchingFiles?.Any() == false)
{
Console.WriteLine("Error: No files found matching glob pattern.");
return -2; // error
}
// check files exist
foreach (var file in matchingFiles)
{
if (!File.Exists(file))
{
Console.WriteLine($"Error: Coverage file not found - {file}.");
return -2; // error
}
}
// parse code coverage file
CodeSummary summary = new();
foreach (var file in matchingFiles)
{
Console.WriteLine($"Coverage File: {file}");
summary = ParseTestResults(file, summary);
}
if (summary == null)
return -2; // error
summary.LineRate /= matchingFiles.Count();
summary.BranchRate /= matchingFiles.Count();
if (summary.Packages.Count == 0)
{
Console.WriteLine("Parsing Error: No packages found in coverage files.");
return -2; // error
}
else
{
// hide branch rate if metrics missing
bool hideBranchRate = o.HideBranchRate;
if (summary.BranchRate == 0 && summary.BranchesCovered == 0 && summary.BranchesValid == 0)
hideBranchRate = true;
// set health badge thresholds
if (!string.IsNullOrWhiteSpace(o.Thresholds))
SetThresholds(o.Thresholds);
// generate badge
string badgeUrl = o.Badge ? GenerateBadge(summary) : null;
// generate output
string output;
string fileExt;
if (o.Format.Equals("text", StringComparison.OrdinalIgnoreCase))
{
fileExt = "txt";
output = GenerateTextOutput(summary, badgeUrl, o.Indicators, hideBranchRate, o.HideComplexity);
if (o.FailBelowThreshold)
output += $"Minimum allowed line rate is {lowerThreshold * 100:N0}%{Environment.NewLine}";
}
else if (o.Format.Equals("md", StringComparison.OrdinalIgnoreCase) || o.Format.Equals("markdown", StringComparison.OrdinalIgnoreCase))
{
fileExt = "md";
output = GenerateMarkdownOutput(summary, badgeUrl, o.Indicators, hideBranchRate, o.HideComplexity);
if (o.FailBelowThreshold)
output += $"{Environment.NewLine}_Minimum allowed line rate is `{lowerThreshold * 100:N0}%`_{Environment.NewLine}";
}
else
{
Console.WriteLine("Error: Unknown output format.");
return -2; // error
}
// output
if (o.Output.Equals("console", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine();
Console.WriteLine(output);
}
else if (o.Output.Equals("file", StringComparison.OrdinalIgnoreCase))
{
File.WriteAllText($"code-coverage-results.{fileExt}", output);
}
else if (o.Output.Equals("both", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine();
Console.WriteLine(output);
File.WriteAllText($"code-coverage-results.{fileExt}", output);
}
else
{
Console.WriteLine("Error: Unknown output type.");
return -2; // error
}
if (o.FailBelowThreshold && summary.LineRate < lowerThreshold)
{
Console.WriteLine($"FAIL: Overall line rate below minimum threshold of {lowerThreshold * 100:N0}%.");
return -2;
}
return 0; // success
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.GetType()} - {ex.Message}");
return -3; // unhandled error
}
},
_ => -1); // invalid arguments
}
private static CodeSummary ParseTestResults(string filename, CodeSummary summary)
{
if (summary == null)
return null;
try
{
string rss = File.ReadAllText(filename);
var xdoc = XDocument.Parse(rss);
// test coverage for solution
var coverage = from item in xdoc.Descendants("coverage")
select item;
if (!coverage.Any())
throw new Exception("Coverage file invalid, data not found");
var lineR = from item in coverage.Attributes()
where item.Name == "line-rate"
select item;
if (!lineR.Any())
throw new Exception("Overall line rate not found");
summary.LineRate += double.Parse(lineR.First().Value);
var linesCovered = from item in coverage.Attributes()
where item.Name == "lines-covered"
select item;
if (!linesCovered.Any())
throw new Exception("Overall lines covered not found");
summary.LinesCovered += int.Parse(linesCovered.First().Value);
var linesValid = from item in coverage.Attributes()
where item.Name == "lines-valid"
select item;
if (!linesValid.Any())
throw new Exception("Overall lines valid not found");
summary.LinesValid += int.Parse(linesValid.First().Value);
var branchR = from item in coverage.Attributes()
where item.Name == "branch-rate"
select item;
if (branchR.Any())
{
summary.BranchRate += double.Parse(branchR.First().Value);
var branchesCovered = from item in coverage.Attributes()
where item.Name == "branches-covered"
select item;
if (branchesCovered.Any())
summary.BranchesCovered += int.Parse(branchesCovered.First().Value);
var branchesValid = from item in coverage.Attributes()
where item.Name == "branches-valid"
select item;
if (branchesValid.Any())
summary.BranchesValid += int.Parse(branchesValid.First().Value);
}
// test coverage for individual packages
var packages = from item in coverage.Descendants("package")
select item;
if (!packages.Any())
throw new Exception("No package data found");
int i = 1;
foreach (var item in packages)
{
CodeCoverage packageCoverage = new()
{
Name = string.IsNullOrWhiteSpace(item.Attribute("name")?.Value) ? $"{Path.GetFileNameWithoutExtension(filename)} Package {i}" : item.Attribute("name").Value,
LineRate = double.Parse(item.Attribute("line-rate")?.Value ?? "0"),
BranchRate = double.Parse(item.Attribute("branch-rate")?.Value ?? "0"),
Complexity = double.Parse(item.Attribute("complexity")?.Value ?? "0")
};
summary.Packages.Add(packageCoverage);
summary.Complexity += packageCoverage.Complexity;
i++;
}
return summary;
}
catch (Exception ex)
{
Console.WriteLine($"Parsing Error: {ex.Message} - {filename}");
return null;
}
}
private static void SetThresholds(string thresholds)
{
int lowerPercentage;
int upperPercentage = (int)(upperThreshold * 100);
int s = thresholds.IndexOf(" ");
if (s == 0)
{
throw new ArgumentException("Threshold parameter set incorrectly.");
}
else if (s < 0)
{
if (!int.TryParse(thresholds, out lowerPercentage))
throw new ArgumentException("Threshold parameter set incorrectly.");
}
else
{
if (!int.TryParse(thresholds.AsSpan(0, s), out lowerPercentage))
throw new ArgumentException("Threshold parameter set incorrectly.");
if (!int.TryParse(thresholds.AsSpan(s + 1), out upperPercentage))
throw new ArgumentException("Threshold parameter set incorrectly.");
}
lowerThreshold = lowerPercentage / 100.0;
upperThreshold = upperPercentage / 100.0;
if (lowerThreshold > 1.0)
lowerThreshold = 1.0;
if (lowerThreshold > upperThreshold)
upperThreshold = lowerThreshold + 0.1;
if (upperThreshold > 1.0)
upperThreshold = 1.0;
}
private static string GenerateBadge(CodeSummary summary)
{
string colour;
if (summary.LineRate < lowerThreshold)
{
colour = "critical";
}
else if (summary.LineRate < upperThreshold)
{
colour = "yellow";
}
else
{
colour = "success";
}
return $"https://img.shields.io/badge/Code%20Coverage-{summary.LineRate * 100:N0}%25-{colour}?style=flat";
}
private static string GenerateHealthIndicator(double rate)
{
if (rate < lowerThreshold)
{
return "❌";
}
else if (rate < upperThreshold)
{
return "➖";
}
else
{
return "✔";
}
}
private static string GenerateTextOutput(CodeSummary summary, string badgeUrl, bool indicators, bool hideBranchRate, bool hideComplexity)
{
StringBuilder textOutput = new();
if (!string.IsNullOrWhiteSpace(badgeUrl))
{
textOutput.AppendLine(badgeUrl)
.AppendLine();
}
foreach (CodeCoverage package in summary.Packages)
{
textOutput.Append($"{package.Name}: Line Rate = {package.LineRate * 100:N0}%")
.Append(hideBranchRate ? string.Empty : $", Branch Rate = {package.BranchRate * 100:N0}%")
.Append(hideComplexity ? string.Empty : (package.Complexity % 1 == 0) ? $", Complexity = {package.Complexity}" : $", Complexity = {package.Complexity:N4}")
.AppendLine(indicators ? $", {GenerateHealthIndicator(package.LineRate)}" : string.Empty);
}
textOutput.Append($"Summary: Line Rate = {summary.LineRate * 100:N0}% ({summary.LinesCovered} / {summary.LinesValid})")
.Append(hideBranchRate ? string.Empty : $", Branch Rate = {summary.BranchRate * 100:N0}% ({summary.BranchesCovered} / {summary.BranchesValid})")
.Append(hideComplexity ? string.Empty : (summary.Complexity % 1 == 0) ? $", Complexity = {summary.Complexity}" : $", Complexity = {summary.Complexity:N4}")
.AppendLine(indicators ? $", {GenerateHealthIndicator(summary.LineRate)}" : string.Empty);
return textOutput.ToString();
}
private static string GenerateMarkdownOutput(CodeSummary summary, string badgeUrl, bool indicators, bool hideBranchRate, bool hideComplexity)
{
StringBuilder markdownOutput = new();
if (!string.IsNullOrWhiteSpace(badgeUrl))
{
markdownOutput.AppendLine($"![Code Coverage]({badgeUrl})")
.AppendLine();
}
markdownOutput.Append("Package | Line Rate")
.Append(hideBranchRate ? string.Empty : " | Branch Rate")
.Append(hideComplexity ? string.Empty : " | Complexity")
.AppendLine(indicators ? " | Health" : string.Empty)
.Append("-------- | ---------")
.Append(hideBranchRate ? string.Empty : " | -----------")
.Append(hideComplexity ? string.Empty : " | ----------")
.AppendLine(indicators ? " | ------" : string.Empty);
foreach (CodeCoverage package in summary.Packages)
{
markdownOutput.Append($"{package.Name} | {package.LineRate * 100:N0}%")
.Append(hideBranchRate ? string.Empty : $" | {package.BranchRate * 100:N0}%")
.Append(hideComplexity ? string.Empty : (package.Complexity % 1 == 0) ? $" | {package.Complexity}" : $" | {package.Complexity:N4}" )
.AppendLine(indicators ? $" | {GenerateHealthIndicator(package.LineRate)}" : string.Empty);
}
markdownOutput.Append($"**Summary** | **{summary.LineRate * 100:N0}%** ({summary.LinesCovered} / {summary.LinesValid})")
.Append(hideBranchRate ? string.Empty : $" | **{summary.BranchRate * 100:N0}%** ({summary.BranchesCovered} / {summary.BranchesValid})")
.Append(hideComplexity ? string.Empty : (summary.Complexity % 1 == 0) ? $" | **{summary.Complexity}**" : $" | **{summary.Complexity:N4}**")
.AppendLine(indicators ? $" | {GenerateHealthIndicator(summary.LineRate)}" : string.Empty);
return markdownOutput.ToString();
}
}
}