forked from coverlet-coverage/coverlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoverageResultTask.cs
139 lines (119 loc) · 5.29 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
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 _thresholdType;
[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 _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(_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 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 averageTable = new ConsoleTable("", "Line", "Branch", "Method");
var lineAverage = 0d;
var branchAverage = 0d;
var methodAverage = 0d;
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;
lineAverage += linePercent;
branchAverage += branchPercent;
methodAverage += methodPercent;
coverageTable.AddRow(Path.GetFileNameWithoutExtension(module.Key), $"{linePercent}%", $"{branchPercent}%", $"{methodPercent}%");
if (_threshold > 0)
{
if (linePercent < _threshold && thresholdTypes.Contains("line"))
{
exceptionBuilder.AppendLine($"'{Path.GetFileNameWithoutExtension(module.Key)}' has a line coverage '{linePercent}%' below specified threshold '{_threshold}%'");
thresholdFailed = true;
}
if (branchPercent < _threshold && thresholdTypes.Contains("branch"))
{
exceptionBuilder.AppendLine($"'{Path.GetFileNameWithoutExtension(module.Key)}' has a branch coverage '{branchPercent}%' below specified threshold '{_threshold}%'");
thresholdFailed = true;
}
if (methodPercent < _threshold && thresholdTypes.Contains("method"))
{
exceptionBuilder.AppendLine($"'{Path.GetFileNameWithoutExtension(module.Key)}' has a method coverage '{methodPercent}%' below specified threshold '{_threshold}%'");
thresholdFailed = true;
}
}
}
lineAverage = lineAverage / result.Modules.Count;
branchAverage = branchAverage / result.Modules.Count;
methodAverage = methodAverage / result.Modules.Count;
averageTable.AddRow("Average", $"{lineAverage}%", $"{branchAverage}%", $"{methodAverage}%");
Console.WriteLine();
Console.WriteLine(coverageTable.ToStringAlternative());
Console.WriteLine(averageTable.ToStringAlternative());
if (thresholdFailed)
throw new Exception(exceptionBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray()));
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
return false;
}
return true;
}
}
}