/
CoverageResultTask.cs
135 lines (115 loc) · 5.46 KB
/
CoverageResultTask.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
using System;
using System.IO;
using System.Linq;
using System.Text;
using ConsoleTables;
using Coverlet.Core;
using Coverlet.Core.Reporters;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Coverlet.MSbuild.Tasks
{
public class CoverageResultTask : Task
{
private string _output;
private string _format;
private int _threshold;
private string _thresholdType;
[Required]
public string Output
{
get { return _output; }
set { _output = value; }
}
[Required]
public string OutputFormat
{
get { return _format; }
set { _format = value; }
}
[Required]
public int Threshold
{
get { return _threshold; }
set { _threshold = value; }
}
[Required]
public string ThresholdType
{
get { return _thresholdType; }
set { _thresholdType = value; }
}
public override bool Execute()
{
try
{
Console.WriteLine("\nCalculating coverage result...");
var coverage = InstrumentationTask.Coverage;
var result = coverage.GetCoverageResult();
var directory = Path.GetDirectoryName(_output);
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
var formats = _format.Split(',');
foreach (var format in formats)
{
var reporter = new ReporterFactory(format).CreateReporter();
if (reporter == null)
throw new Exception($"Specified output format '{format}' is not supported");
var filename = Path.GetFileName(_output);
filename = (filename == string.Empty) ? $"coverage.{reporter.Extension}" : filename;
filename = Path.HasExtension(filename) ? filename : $"{filename}.{reporter.Extension}";
var report = Path.Combine(directory, filename);
Console.WriteLine($" Generating report '{report}'");
File.WriteAllText(report, reporter.Report(result));
}
var thresholdFailed = false;
var thresholdTypes = _thresholdType.Split(',').Select(t => t.Trim());
var summary = new CoverageSummary();
var exceptionBuilder = new StringBuilder();
var coverageTable = new ConsoleTable("Module", "Line", "Branch", "Method");
var overallLineCoverage = summary.CalculateLineCoverage(result.Modules).Percent * 100;
var overallBranchCoverage = summary.CalculateBranchCoverage(result.Modules).Percent * 100;
var overallMethodCoverage = summary.CalculateMethodCoverage(result.Modules).Percent * 100;
foreach (var module in result.Modules)
{
var linePercent = summary.CalculateLineCoverage(module.Value).Percent * 100;
var branchPercent = summary.CalculateBranchCoverage(module.Value).Percent * 100;
var methodPercent = summary.CalculateMethodCoverage(module.Value).Percent * 100;
coverageTable.AddRow(Path.GetFileNameWithoutExtension(module.Key), $"{linePercent}%", $"{branchPercent}%", $"{methodPercent}%");
if (_threshold > 0)
{
if (linePercent < _threshold && thresholdTypes.Contains("line", StringComparer.OrdinalIgnoreCase))
{
exceptionBuilder.AppendLine($"'{Path.GetFileNameWithoutExtension(module.Key)}' has a line coverage '{linePercent}%' below specified threshold '{_threshold}%'");
thresholdFailed = true;
}
if (branchPercent < _threshold && thresholdTypes.Contains("branch", StringComparer.OrdinalIgnoreCase))
{
exceptionBuilder.AppendLine($"'{Path.GetFileNameWithoutExtension(module.Key)}' has a branch coverage '{branchPercent}%' below specified threshold '{_threshold}%'");
thresholdFailed = true;
}
if (methodPercent < _threshold && thresholdTypes.Contains("method", StringComparer.OrdinalIgnoreCase))
{
exceptionBuilder.AppendLine($"'{Path.GetFileNameWithoutExtension(module.Key)}' has a method coverage '{methodPercent}%' below specified threshold '{_threshold}%'");
thresholdFailed = true;
}
}
}
Console.WriteLine();
Console.WriteLine(coverageTable.ToStringAlternative());
Console.WriteLine();
Console.WriteLine($"Total Line {overallLineCoverage}%");
Console.WriteLine($"Total Branch {overallBranchCoverage}%");
Console.WriteLine($"Total Method {overallMethodCoverage}%");
if (thresholdFailed)
throw new Exception(exceptionBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray()));
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
return false;
}
return true;
}
}
}