Skip to content

Commit 2b1e56b

Browse files
authored
Merge pull request #93 from StephenMP/issue-92
Issue #92 : Added a ThresholdType switch
2 parents 46a07ef + 6a6dc3a commit 2b1e56b

File tree

3 files changed

+67
-15
lines changed

3 files changed

+67
-15
lines changed

src/coverlet.msbuild.tasks/CoverageResultTask.cs

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.IO;
3+
using System.Linq;
4+
using System.Text;
45
using ConsoleTables;
5-
66
using Coverlet.Core;
77
using Coverlet.Core.Reporters;
8-
98
using Microsoft.Build.Framework;
109
using Microsoft.Build.Utilities;
1110

@@ -16,6 +15,7 @@ public class CoverageResultTask : Task
1615
private string _filename;
1716
private string _format;
1817
private int _threshold;
18+
private string _thresholdTypes;
1919

2020
[Required]
2121
public string Output
@@ -38,17 +38,26 @@ public int Threshold
3838
set { _threshold = value; }
3939
}
4040

41+
[Required]
42+
public string ThresholdType
43+
{
44+
get { return _thresholdTypes; }
45+
set { _thresholdTypes = value; }
46+
}
47+
4148
public override bool Execute()
4249
{
4350
try
4451
{
4552
Console.WriteLine("\nCalculating coverage result...");
4653
var coverage = InstrumentationTask.Coverage;
47-
CoverageResult result = coverage.GetCoverageResult();
54+
var result = coverage.GetCoverageResult();
4855

4956
var directory = Path.GetDirectoryName(_filename);
5057
if (!Directory.Exists(directory))
58+
{
5159
Directory.CreateDirectory(directory);
60+
}
5261

5362
var formats = _format.Split(',');
5463
foreach (var format in formats)
@@ -62,25 +71,66 @@ public override bool Execute()
6271
File.WriteAllText(report, reporter.Report(result));
6372
}
6473

65-
double total = 0;
66-
CoverageSummary summary = new CoverageSummary();
67-
ConsoleTable table = new ConsoleTable("Module", "Line", "Branch", "Method");
74+
var branchTotal = 0d;
75+
var methodTotal = 0d;
76+
var lineTotal = 0d;
77+
var summary = new CoverageSummary();
78+
var table = new ConsoleTable("Module", "Line", "Branch", "Method");
6879

6980
foreach (var module in result.Modules)
7081
{
71-
double linePercent = summary.CalculateLineCoverage(module.Value).Percent * 100;
72-
double branchPercent = summary.CalculateBranchCoverage(module.Value).Percent * 100;
73-
double methodPercent = summary.CalculateMethodCoverage(module.Value).Percent * 100;
82+
var linePercent = summary.CalculateLineCoverage(module.Value).Percent * 100;
83+
var branchPercent = summary.CalculateBranchCoverage(module.Value).Percent * 100;
84+
var methodPercent = summary.CalculateMethodCoverage(module.Value).Percent * 100;
7485
table.AddRow(Path.GetFileNameWithoutExtension(module.Key), $"{linePercent}%", $"{branchPercent}%", $"{methodPercent}%");
75-
total += linePercent;
86+
87+
lineTotal += linePercent;
88+
branchTotal += branchPercent;
89+
methodTotal += methodPercent;
7690
}
7791

7892
Console.WriteLine();
7993
Console.WriteLine(table.ToStringAlternative());
8094

81-
double average = total / result.Modules.Count;
82-
if (average < _threshold)
83-
throw new Exception($"Overall average coverage '{average}%' is lower than specified threshold '{_threshold}%'");
95+
if (_threshold > 0)
96+
{
97+
var thresholdFailed = false;
98+
var exceptionBuilder = new StringBuilder();
99+
var lineAverage = lineTotal / result.Modules.Count;
100+
var branchAverage = branchTotal / result.Modules.Count;
101+
var methodAverage = methodTotal / result.Modules.Count;
102+
var thresholdTypes = _thresholdTypes.Split(',').Select(t => t.ToLower());
103+
foreach (var thresholdType in thresholdTypes)
104+
{
105+
if (thresholdType == "line" && lineAverage < _threshold)
106+
{
107+
thresholdFailed = true;
108+
exceptionBuilder.AppendLine($"Overall average '{thresholdType}' coverage '{lineAverage}%' is lower than specified threshold '{_threshold}%'");
109+
}
110+
111+
else if (thresholdType == "branch" && branchAverage < _threshold)
112+
{
113+
thresholdFailed = true;
114+
exceptionBuilder.AppendLine($"Overall average '{thresholdType}' coverage '{branchAverage}%' is lower than specified threshold '{_threshold}%'");
115+
}
116+
117+
else if (thresholdType == "method" && methodAverage < _threshold)
118+
{
119+
thresholdFailed = true;
120+
exceptionBuilder.AppendLine($"Overall average '{thresholdType}' coverage '{methodAverage}%' is lower than specified threshold '{_threshold}%'");
121+
}
122+
123+
else if (thresholdType != "line" && thresholdType != "branch" && thresholdType != "method")
124+
{
125+
Console.WriteLine($"Threshold type of {thresholdType} is not recognized/supported and will be ignored.");
126+
}
127+
}
128+
129+
if (thresholdFailed)
130+
{
131+
throw new Exception(exceptionBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray()));
132+
}
133+
}
84134
}
85135
catch (Exception ex)
86136
{

src/coverlet.msbuild/coverlet.msbuild.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<CoverletOutput>$([MSBuild]::EnsureTrailingSlash('$(CoverletOutputDirectory)'))$(CoverletOutputName)</CoverletOutput>
88
<Exclude Condition="$(Exclude) == ''"></Exclude>
99
<Threshold Condition="$(Threshold) == ''">0</Threshold>
10+
<ThresholdType Condition="$(ThresholdType) == ''">line,branch,method</ThresholdType>
1011
<CollectCoverage Condition="$(CollectCoverage) == ''">false</CollectCoverage>
1112
</PropertyGroup>
1213

src/coverlet.msbuild/coverlet.msbuild.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
Condition="$(CollectCoverage) == 'true'"
2323
Output="$(CoverletOutput)"
2424
OutputFormat="$(CoverletOutputFormat)"
25-
Threshold="$(Threshold)" />
25+
Threshold="$(Threshold)"
26+
ThresholdType="$(ThresholdType)" />
2627
</Target>
2728

2829
</Project>

0 commit comments

Comments
 (0)