23
23
24
24
import org .apache .maven .plugin .AbstractMojo ;
25
25
import org .apache .maven .plugin .MojoFailureException ;
26
+ import org .apache .maven .plugin .logging .Log ;
26
27
import org .apache .maven .plugins .annotations .Execute ;
27
28
import org .apache .maven .plugins .annotations .LifecyclePhase ;
28
29
import org .apache .maven .plugins .annotations .Mojo ;
33
34
import scala .jdk .javaapi .CollectionConverters ;
34
35
35
36
import scoverage .domain .Coverage ;
37
+ import scoverage .domain .CoverageMetrics ;
38
+ import scoverage .domain .DoubleFormat ;
36
39
import scoverage .reporter .IOUtils ;
37
40
import scoverage .serialize .Serializer ;
38
41
@@ -70,7 +73,7 @@ public class SCoverageCheckMojo
70
73
private File dataDirectory ;
71
74
72
75
/**
73
- * Required minimum coverage.
76
+ * Required minimum total statement coverage.
74
77
* <br>
75
78
* <br>
76
79
* See <a href="https://github.com/scoverage/sbt-scoverage#minimum-coverage">https://github.com/scoverage/sbt-scoverage#minimum-coverage</a> for additional documentation.
@@ -81,6 +84,18 @@ public class SCoverageCheckMojo
81
84
@ Parameter ( property = "scoverage.minimumCoverage" , defaultValue = "0" )
82
85
private Double minimumCoverage ;
83
86
87
+ /**
88
+ * Required minimum total branch coverage.
89
+ * <br>
90
+ * <br>
91
+ * See <a href="https://github.com/scoverage/sbt-scoverage#minimum-coverage">https://github.com/scoverage/sbt-scoverage#minimum-coverage</a> for additional documentation.
92
+ * <br>
93
+ *
94
+ * @since 2.0.1
95
+ */
96
+ @ Parameter ( property = "scoverage.minimumCoverageBranchTotal" , defaultValue = "0" )
97
+ private Double minimumCoverageBranchTotal ;
98
+
84
99
/**
85
100
* Fail the build if minimum coverage was not reached.
86
101
* <br>
@@ -168,31 +183,15 @@ public void execute() throws MojoFailureException
168
183
int invokedBranchesCount = coverage .invokedBranchesCount ();
169
184
int invokedStatementCount = coverage .invokedStatementCount ();
170
185
171
- getLog ().info ( String .format ( "Statement coverage.: %s%%" , coverage .statementCoverageFormatted () ) );
172
- getLog ().info ( String .format ( "Branch coverage....: %s%%" , coverage .branchCoverageFormatted () ) );
173
186
getLog ().debug ( String .format ( "invokedBranchesCount:%d / branchCount:%d, invokedStatementCount:%d / statementCount:%d" ,
174
187
invokedBranchesCount , branchCount , invokedStatementCount , statementCount ) );
175
- if ( minimumCoverage > 0.0 )
188
+
189
+ boolean ok = checkCoverage ( getLog (), "Total" , coverage ,
190
+ minimumCoverage , minimumCoverageBranchTotal , true );
191
+
192
+ if ( !ok && failOnMinimumCoverage )
176
193
{
177
- String minimumCoverageFormatted = scoverage .domain .DoubleFormat .twoFractionDigits ( minimumCoverage );
178
- if ( is100 ( coverage .statementCoveragePercent () ) )
179
- {
180
- getLog ().info ( "100% Coverage !" );
181
- }
182
- else if ( coverage .statementCoveragePercent () < minimumCoverage )
183
- {
184
- getLog ().error ( String .format ( "Coverage is below minimum [%s%% < %s%%]" ,
185
- coverage .statementCoverageFormatted (), minimumCoverageFormatted ) );
186
- if ( failOnMinimumCoverage )
187
- {
188
- throw new MojoFailureException ( "Coverage minimum was not reached" );
189
- }
190
- }
191
- else
192
- {
193
- getLog ().info ( String .format ( "Coverage is above minimum [%s%% >= %s%%]" ,
194
- coverage .statementCoverageFormatted (), minimumCoverageFormatted ) );
195
- }
194
+ throw new MojoFailureException ( "Coverage minimum was not reached" );
196
195
}
197
196
198
197
long te = System .currentTimeMillis ();
@@ -206,4 +205,60 @@ private static boolean is100( Double d )
206
205
return Math .abs ( 100 - d ) <= 0.00001d ;
207
206
}
208
207
209
- }
208
+ private static boolean checkCoverage ( Log logger , String metric , CoverageMetrics metrics ,
209
+ double minStmt , double minBranch , boolean logSuccessInfo )
210
+ {
211
+ boolean stmt = checkCoverage ( logger , "Statement:" + metric ,
212
+ minStmt , metrics .statementCoveragePercent (), logSuccessInfo );
213
+ boolean branch = checkCoverage ( logger , "Branch:" + metric ,
214
+ minBranch , metrics .branchCoveragePercent (), logSuccessInfo );
215
+ return stmt && branch ;
216
+ }
217
+
218
+ private static boolean checkCoverage ( Log logger , String metric ,
219
+ double minimum , double actual , boolean logSuccessInfo )
220
+ {
221
+ if ( minimum <= 0 )
222
+ {
223
+ return true ;
224
+ }
225
+
226
+ if ( is100 ( actual ) )
227
+ {
228
+ logSuccess ( logger , String .format ( "Coverage is 100%%: %s!" , metric ), logSuccessInfo );
229
+ return true ;
230
+ }
231
+
232
+ String minimumFormatted = DoubleFormat .twoFractionDigits ( minimum );
233
+ String actualFormatted = DoubleFormat .twoFractionDigits ( actual );
234
+ boolean ok = minimum <= actual ;
235
+
236
+ if ( ok )
237
+ {
238
+ String message = String .format ( "Coverage is above minimum [%s%% >= %s%%]: %s" ,
239
+ actualFormatted , minimumFormatted , metric );
240
+ logSuccess ( logger , message , logSuccessInfo );
241
+ }
242
+ else
243
+ {
244
+ String message = String .format ( "Coverage is below minimum [%s%% < %s%%]: %s" ,
245
+ actualFormatted , minimumFormatted , metric );
246
+ logger .error ( message );
247
+ }
248
+
249
+ return ok ;
250
+ }
251
+
252
+ private static void logSuccess ( Log logger , String message , boolean logSuccessInfo )
253
+ {
254
+ if ( logSuccessInfo )
255
+ {
256
+ logger .info ( message );
257
+ }
258
+ else
259
+ {
260
+ logger .debug ( message );
261
+ }
262
+ }
263
+
264
+ }
0 commit comments