/
CoverageResultTask.cs
144 lines (125 loc) · 5.41 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
136
137
138
139
140
141
142
143
144
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 _filename;
private string _format;
private int _threshold;
private string _thresholdTypes;
[Required]
public string Output
{
get { return _filename; }
set { _filename = 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 _thresholdTypes; }
set { _thresholdTypes = value; }
}
public override bool Execute()
{
try
{
Console.WriteLine("\nCalculating coverage result...");
var coverage = InstrumentationTask.Coverage;
var result = coverage.GetCoverageResult();
var directory = Path.GetDirectoryName(_filename);
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 report = _filename + "." + reporter.Extension;
Console.WriteLine($" Generating report '{report}'");
File.WriteAllText(report, reporter.Report(result));
}
var branchTotal = 0d;
var methodTotal = 0d;
var lineTotal = 0d;
var summary = new CoverageSummary();
var table = new ConsoleTable("Module", "Line", "Branch", "Method");
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;
table.AddRow(Path.GetFileNameWithoutExtension(module.Key), $"{linePercent}%", $"{branchPercent}%", $"{methodPercent}%");
lineTotal += linePercent;
branchTotal += branchPercent;
methodTotal += methodPercent;
}
Console.WriteLine();
Console.WriteLine(table.ToStringAlternative());
if (_threshold > 0)
{
var thresholdFailed = false;
var exceptionBuilder = new StringBuilder();
var lineAverage = lineTotal / result.Modules.Count;
var branchAverage = branchTotal / result.Modules.Count;
var methodAverage = methodTotal / result.Modules.Count;
var thresholdTypes = _thresholdTypes.Split(',').Select(t => t.ToLower());
foreach (var thresholdType in thresholdTypes)
{
if (thresholdType == "line" && lineAverage < _threshold)
{
thresholdFailed = true;
exceptionBuilder.AppendLine($"Overall average '{thresholdType}' coverage '{lineAverage}%' is lower than specified threshold '{_threshold}%'");
}
else if (thresholdType == "branch" && branchAverage < _threshold)
{
thresholdFailed = true;
exceptionBuilder.AppendLine($"Overall average '{thresholdType}' coverage '{branchAverage}%' is lower than specified threshold '{_threshold}%'");
}
else if (thresholdType == "method" && methodAverage < _threshold)
{
thresholdFailed = true;
exceptionBuilder.AppendLine($"Overall average '{thresholdType}' coverage '{methodAverage}%' is lower than specified threshold '{_threshold}%'");
}
else if (thresholdType != "line" && thresholdType != "branch" && thresholdType != "method")
{
Console.WriteLine($"Threshold type of {thresholdType} is not recognized/supported and will be ignored.");
}
}
if (thresholdFailed)
{
throw new Exception(exceptionBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray()));
}
}
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
return false;
}
return true;
}
}
}