Skip to content

Commit e589b2d

Browse files
committed
change Threshold implementation and update documentation
1 parent caaee9d commit e589b2d

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,25 @@ Coverlet allows you to specify a coverage threshold below which it fails the bui
7474
dotnet test /p:CollectCoverage=true /p:Threshold=80
7575
```
7676

77-
The above command will automatically fail the build if the average code coverage of all instrumented modules falls below 80%.
77+
The above command will automatically fail the build if the line, branch or method coverage of _any_ of the instrumented modules falls below 80%. You can specify what type of coverage to apply the threshold value to using the `ThresholdType` property. For example to apply the threshold check to only **line** coverage:
78+
79+
```bash
80+
dotnet test /p:CollectCoverage=true /p:Threshold=80 /p:ThresholdType=line
81+
```
82+
83+
You can specify multiple values for `ThresholdType` by separating them with commas. Valid values include `line`, `branch` and `method`.
7884

7985
### Excluding From Coverage
8086

81-
#### Attributes
87+
#### Attributes
8288
You can ignore a method or an entire class from code coverage by creating and applying any of the following attributes:
8389

8490
* ExcludeFromCoverage
8591
* ExcludeFromCoverageAttribute
8692

8793
Coverlet just uses the type name, so the attributes can be created under any namespace of your choosing.
8894

89-
#### Source Files
95+
#### Source Files
9096
You can also ignore specific source files from code coverage using the `ExcludeByFile` property
9197
- Use single or multiple paths (separate by comma)
9298
- Use absolute or relative paths (relative to the project directory)

src/coverlet.msbuild.tasks/CoverageResultTask.cs

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class CoverageResultTask : Task
1515
private string _filename;
1616
private string _format;
1717
private int _threshold;
18-
private string _thresholdTypes;
18+
private string _thresholdType;
1919

2020
[Required]
2121
public string Output
@@ -41,8 +41,8 @@ public int Threshold
4141
[Required]
4242
public string ThresholdType
4343
{
44-
get { return _thresholdTypes; }
45-
set { _thresholdTypes = value; }
44+
get { return _thresholdType; }
45+
set { _thresholdType = value; }
4646
}
4747

4848
public override bool Execute()
@@ -71,10 +71,11 @@ public override bool Execute()
7171
File.WriteAllText(report, reporter.Report(result));
7272
}
7373

74-
var branchTotal = 0d;
75-
var methodTotal = 0d;
76-
var lineTotal = 0d;
74+
var thresholdFailed = false;
75+
var thresholdTypes = _thresholdType.Split(',').Select(t => t.Trim());
76+
7777
var summary = new CoverageSummary();
78+
var exceptionBuilder = new StringBuilder();
7879
var table = new ConsoleTable("Module", "Line", "Branch", "Method");
7980

8081
foreach (var module in result.Modules)
@@ -84,53 +85,32 @@ public override bool Execute()
8485
var methodPercent = summary.CalculateMethodCoverage(module.Value).Percent * 100;
8586
table.AddRow(Path.GetFileNameWithoutExtension(module.Key), $"{linePercent}%", $"{branchPercent}%", $"{methodPercent}%");
8687

87-
lineTotal += linePercent;
88-
branchTotal += branchPercent;
89-
methodTotal += methodPercent;
90-
}
91-
92-
Console.WriteLine();
93-
Console.WriteLine(table.ToStringAlternative());
94-
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)
88+
if (_threshold > 0)
10489
{
105-
if (thresholdType == "line" && lineAverage < _threshold)
90+
if (linePercent < _threshold && thresholdTypes.Contains("line"))
10691
{
92+
exceptionBuilder.AppendLine($"'{Path.GetFileNameWithoutExtension(module.Key)}' has a line coverage '{linePercent}%' below specified threshold '{_threshold}%'");
10793
thresholdFailed = true;
108-
exceptionBuilder.AppendLine($"Overall average '{thresholdType}' coverage '{lineAverage}%' is lower than specified threshold '{_threshold}%'");
10994
}
11095

111-
else if (thresholdType == "branch" && branchAverage < _threshold)
96+
if (branchPercent < _threshold && thresholdTypes.Contains("branch"))
11297
{
98+
exceptionBuilder.AppendLine($"'{Path.GetFileNameWithoutExtension(module.Key)}' has a branch coverage '{branchPercent}%' below specified threshold '{_threshold}%'");
11399
thresholdFailed = true;
114-
exceptionBuilder.AppendLine($"Overall average '{thresholdType}' coverage '{branchAverage}%' is lower than specified threshold '{_threshold}%'");
115100
}
116101

117-
else if (thresholdType == "method" && methodAverage < _threshold)
102+
if (methodPercent < _threshold && thresholdTypes.Contains("method"))
118103
{
104+
exceptionBuilder.AppendLine($"'{Path.GetFileNameWithoutExtension(module.Key)}' has a method coverage '{methodPercent}%' below specified threshold '{_threshold}%'");
119105
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.");
126106
}
127107
}
128-
129-
if (thresholdFailed)
130-
{
131-
throw new Exception(exceptionBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray()));
132-
}
133108
}
109+
110+
Console.WriteLine();
111+
Console.WriteLine(table.ToStringAlternative());
112+
if (thresholdFailed)
113+
throw new Exception(exceptionBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray()));
134114
}
135115
catch (Exception ex)
136116
{

0 commit comments

Comments
 (0)