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,16 @@ 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
+ @ Parameter ( property = "scoverage.minimumCoverageBranchTotal" , defaultValue = "0" )
95
+ private Double minimumCoverageBranchTotal ;
96
+
84
97
/**
85
98
* Fail the build if minimum coverage was not reached.
86
99
* <br>
@@ -168,31 +181,15 @@ public void execute() throws MojoFailureException
168
181
int invokedBranchesCount = coverage .invokedBranchesCount ();
169
182
int invokedStatementCount = coverage .invokedStatementCount ();
170
183
171
- getLog ().info ( String .format ( "Statement coverage.: %s%%" , coverage .statementCoverageFormatted () ) );
172
- getLog ().info ( String .format ( "Branch coverage....: %s%%" , coverage .branchCoverageFormatted () ) );
173
184
getLog ().debug ( String .format ( "invokedBranchesCount:%d / branchCount:%d, invokedStatementCount:%d / statementCount:%d" ,
174
185
invokedBranchesCount , branchCount , invokedStatementCount , statementCount ) );
175
- if ( minimumCoverage > 0.0 )
186
+
187
+ boolean ok = checkCoverage ( getLog (), "Total" , coverage ,
188
+ minimumCoverage , minimumCoverageBranchTotal , true );
189
+
190
+ if ( !ok && failOnMinimumCoverage )
176
191
{
177
- String minimumCoverageFormatted = scoverage .domain .DoubleFormat .twoFractionDigits ( minimumCoverage );
178
- if ( is100 ( minimumCoverage ) && 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
- }
192
+ throw new MojoFailureException ( "Coverage minimum was not reached" );
196
193
}
197
194
198
195
long te = System .currentTimeMillis ();
@@ -201,9 +198,64 @@ else if ( coverage.statementCoveragePercent() < minimumCoverage )
201
198
202
199
// Private utility methods
203
200
204
- private boolean is100 ( Double d )
201
+ private static boolean is100 ( Double d )
205
202
{
206
203
return Math .abs ( 100 - d ) <= 0.00001d ;
207
204
}
208
205
209
- }
206
+ private static boolean checkCoverage ( Log logger , String metric , CoverageMetrics metrics ,
207
+ double minimumStmt , double minimimBranch , boolean logSuccessInfo )
208
+ {
209
+ boolean stmt = checkCoverage ( logger , "Statement:" + metric ,
210
+ minimumStmt , metrics .statementCoveragePercent (), logSuccessInfo );
211
+ boolean branch = checkCoverage ( logger , "Branch:" + metric ,
212
+ minimimBranch , metrics .branchCoveragePercent (), logSuccessInfo );
213
+ return stmt && branch ;
214
+ }
215
+
216
+ private static boolean checkCoverage ( Log logger , String metric ,
217
+ double minimum , double actual , boolean logSuccessInfo )
218
+ {
219
+ if ( minimum <= 0 )
220
+ {
221
+ return true ;
222
+ }
223
+
224
+ if ( is100 ( minimum ) && is100 ( actual ) )
225
+ {
226
+ logSuccess ( logger , String .format ( "Coverage is 100%: %s!" , metric ), logSuccessInfo );
227
+ return true ;
228
+ }
229
+
230
+ String minimumFormatted = DoubleFormat .twoFractionDigits ( minimum );
231
+ String actualFormatted = DoubleFormat .twoFractionDigits ( actual );
232
+ boolean ok = minimum <= actual ;
233
+
234
+ if ( ok )
235
+ {
236
+ String message = String .format ( "Coverage is above minimum [%s%% >= %s%%]: %s" ,
237
+ actualFormatted , minimumFormatted , metric );
238
+ logSuccess ( logger , message , logSuccessInfo );
239
+ }
240
+ else
241
+ {
242
+ logger .error ( String .format ( "Coverage is below minimum [%s%% < %s%%]: %s" ,
243
+ actualFormatted , minimumFormatted , metric ) );
244
+ }
245
+
246
+ return ok ;
247
+ }
248
+
249
+ private static void logSuccess ( Log logger , String message , boolean logSuccessInfo )
250
+ {
251
+ if ( logSuccessInfo )
252
+ {
253
+ logger .info (message );
254
+ }
255
+ else
256
+ {
257
+ logger .debug (message );
258
+ }
259
+ }
260
+
261
+ }
0 commit comments