1
1
using System ;
2
- using System . Collections . Generic ;
3
2
using System . IO ;
3
+ using System . Linq ;
4
+ using System . Text ;
4
5
using ConsoleTables ;
5
-
6
6
using Coverlet . Core ;
7
7
using Coverlet . Core . Reporters ;
8
-
9
8
using Microsoft . Build . Framework ;
10
9
using Microsoft . Build . Utilities ;
11
10
@@ -16,6 +15,7 @@ public class CoverageResultTask : Task
16
15
private string _filename ;
17
16
private string _format ;
18
17
private int _threshold ;
18
+ private string _thresholdTypes ;
19
19
20
20
[ Required ]
21
21
public string Output
@@ -38,17 +38,26 @@ public int Threshold
38
38
set { _threshold = value ; }
39
39
}
40
40
41
+ [ Required ]
42
+ public string ThresholdType
43
+ {
44
+ get { return _thresholdTypes ; }
45
+ set { _thresholdTypes = value ; }
46
+ }
47
+
41
48
public override bool Execute ( )
42
49
{
43
50
try
44
51
{
45
52
Console . WriteLine ( "\n Calculating coverage result..." ) ;
46
53
var coverage = InstrumentationTask . Coverage ;
47
- CoverageResult result = coverage . GetCoverageResult ( ) ;
54
+ var result = coverage . GetCoverageResult ( ) ;
48
55
49
56
var directory = Path . GetDirectoryName ( _filename ) ;
50
57
if ( ! Directory . Exists ( directory ) )
58
+ {
51
59
Directory . CreateDirectory ( directory ) ;
60
+ }
52
61
53
62
var formats = _format . Split ( ',' ) ;
54
63
foreach ( var format in formats )
@@ -62,25 +71,66 @@ public override bool Execute()
62
71
File . WriteAllText ( report , reporter . Report ( result ) ) ;
63
72
}
64
73
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" ) ;
68
79
69
80
foreach ( var module in result . Modules )
70
81
{
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 ;
74
85
table . AddRow ( Path . GetFileNameWithoutExtension ( module . Key ) , $ "{ linePercent } %", $ "{ branchPercent } %", $ "{ methodPercent } %") ;
75
- total += linePercent ;
86
+
87
+ lineTotal += linePercent ;
88
+ branchTotal += branchPercent ;
89
+ methodTotal += methodPercent ;
76
90
}
77
91
78
92
Console . WriteLine ( ) ;
79
93
Console . WriteLine ( table . ToStringAlternative ( ) ) ;
80
94
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
+ }
84
134
}
85
135
catch ( Exception ex )
86
136
{
0 commit comments