11using System ;
2- using System . Collections . Generic ;
32using System . IO ;
3+ using System . Linq ;
4+ using System . Text ;
45using ConsoleTables ;
5-
66using Coverlet . Core ;
77using Coverlet . Core . Reporters ;
8-
98using Microsoft . Build . Framework ;
109using 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 ( "\n Calculating 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 {
0 commit comments