Skip to content

Commit 7dbf820

Browse files
committed
Coverage minima: add parameter for branch minima
Also, extract a coverage computation and logging method.
1 parent 425c0bd commit 7dbf820

File tree

5 files changed

+89
-34
lines changed

5 files changed

+89
-34
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ Read [SBT SCoverage Plugin documentation](https://github.com/scoverage/sbt-scove
335335
<artifactId>scoverage-maven-plugin</artifactId>
336336
<version>${scoverage.plugin.version}</version>
337337
<configuration>
338-
<minimumCoverage>80</minimumCoverage>
338+
<minimumCoverage>95</minimumCoverage>
339+
<minimumCoverageBranchTotal>90</minimumCoverageBranchTotal>
339340
<failOnMinimumCoverage>true</failOnMinimumCoverage>
340341
</configuration>
341342
<executions>

src/it/test_coverage_minima/module03/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<configuration>
2222
<aggregate>true</aggregate>
2323
<minimumCoverage>100</minimumCoverage>
24+
<minimumCoverageBranchTotal>100</minimumCoverageBranchTotal>
2425
</configuration>
2526
<executions>
2627
<execution>

src/it/test_coverage_minima/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<configuration>
4949
<aggregate>true</aggregate> <!-- for aggregated report -->
5050
<minimumCoverage>95</minimumCoverage>
51+
<minimumCoverageBranchTotal>90</minimumCoverageBranchTotal>
5152
<failOnMinimumCoverage>false</failOnMinimumCoverage>
5253
</configuration>
5354
<executions>

src/it/test_coverage_minima/validate.groovy

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,20 @@ try {
1515

1616
checkModule(logText, "module01",
1717
"""
18-
|[INFO] Statement coverage.: 100.00%
19-
|[INFO] Branch coverage....: 100.00%
20-
|[INFO] 100% Coverage !
18+
|[INFO] Coverage is 100%: Statement:Total!
19+
|[INFO] Coverage is 100%: Branch:Total!
2120
|""".stripMargin()
2221
)
2322
checkModule(logText, "module02",
2423
"""
25-
|[INFO] Statement coverage.: 50.00%
26-
|[INFO] Branch coverage....: 100.00%
27-
|[ERROR] Coverage is below minimum [50.00% < 95.00%]
24+
|[ERROR] Coverage is below minimum [50.00% < 95.00%]: Statement:Total
25+
|[INFO] Coverage is 100%: Branch:Total!
2826
|""".stripMargin()
2927
)
3028
checkModule(logText, "module03",
3129
"""
32-
|[INFO] Statement coverage.: 100.00%
33-
|[INFO] Branch coverage....: 100.00%
34-
|[INFO] 100% Coverage !
30+
|[INFO] Coverage is 100%: Statement:Total!
31+
|[INFO] Coverage is 100%: Branch:Total!
3532
|""".stripMargin()
3633
)
3734

src/main/java/org/scoverage/plugin/SCoverageCheckMojo.java

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.apache.maven.plugin.AbstractMojo;
2525
import org.apache.maven.plugin.MojoFailureException;
26+
import org.apache.maven.plugin.logging.Log;
2627
import org.apache.maven.plugins.annotations.Execute;
2728
import org.apache.maven.plugins.annotations.LifecyclePhase;
2829
import org.apache.maven.plugins.annotations.Mojo;
@@ -33,6 +34,8 @@
3334
import scala.jdk.javaapi.CollectionConverters;
3435

3536
import scoverage.domain.Coverage;
37+
import scoverage.domain.CoverageMetrics;
38+
import scoverage.domain.DoubleFormat;
3639
import scoverage.reporter.IOUtils;
3740
import 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

Comments
 (0)