2323
2424import org .apache .maven .plugin .AbstractMojo ;
2525import org .apache .maven .plugin .MojoFailureException ;
26+ import org .apache .maven .plugin .logging .Log ;
2627import org .apache .maven .plugins .annotations .Execute ;
2728import org .apache .maven .plugins .annotations .LifecyclePhase ;
2829import org .apache .maven .plugins .annotations .Mojo ;
3334import scala .jdk .javaapi .CollectionConverters ;
3435
3536import scoverage .domain .Coverage ;
37+ import scoverage .domain .CoverageMetrics ;
38+ import scoverage .domain .DoubleFormat ;
3639import scoverage .reporter .IOUtils ;
3740import scoverage .serialize .Serializer ;
3841
@@ -70,7 +73,7 @@ public class SCoverageCheckMojo
7073 private File dataDirectory ;
7174
7275 /**
73- * Required minimum coverage.
76+ * Required minimum total statement coverage.
7477 * <br>
7578 * <br>
7679 * 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
8184 @ Parameter ( property = "scoverage.minimumCoverage" , defaultValue = "0" )
8285 private Double minimumCoverage ;
8386
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+
8499 /**
85100 * Fail the build if minimum coverage was not reached.
86101 * <br>
@@ -168,31 +183,15 @@ public void execute() throws MojoFailureException
168183 int invokedBranchesCount = coverage .invokedBranchesCount ();
169184 int invokedStatementCount = coverage .invokedStatementCount ();
170185
171- getLog ().info ( String .format ( "Statement coverage.: %s%%" , coverage .statementCoverageFormatted () ) );
172- getLog ().info ( String .format ( "Branch coverage....: %s%%" , coverage .branchCoverageFormatted () ) );
173186 getLog ().debug ( String .format ( "invokedBranchesCount:%d / branchCount:%d, invokedStatementCount:%d / statementCount:%d" ,
174187 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 )
176193 {
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" );
196195 }
197196
198197 long te = System .currentTimeMillis ();
@@ -206,4 +205,60 @@ private static boolean is100( Double d )
206205 return Math .abs ( 100 - d ) <= 0.00001d ;
207206 }
208207
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