Skip to content

Commit 756c68f

Browse files
committed
Coverage minima: add parameter for branch minima
Also, extract a coverage computation and logging method.
1 parent aa9fef2 commit 756c68f

File tree

3 files changed

+83
-32
lines changed

3 files changed

+83
-32
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/validate.groovy

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@ try {
1515

1616
checkModule(logText, "module01",
1717
"""
18-
|[INFO] Statement coverage.: 100.00%
19-
|[INFO] Branch coverage....: 100.00%
20-
|[INFO] Coverage is above minimum [100.00% >= 95.00%]
18+
|[INFO] Coverage is above minimum [100.00% >= 95.00%]: Statement:Total
19+
|[INFO] Coverage is above minimum [100.00% >= 90.00%]: 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 above minimum [100.00% >= 90.00%]: Branch:Total
2826
|""".stripMargin()
2927
)
3028

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

Lines changed: 77 additions & 25 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,16 @@ 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+
@Parameter( property = "scoverage.minimumCoverageBranchTotal", defaultValue = "0" )
95+
private Double minimumCoverageBranchTotal;
96+
8497
/**
8598
* Fail the build if minimum coverage was not reached.
8699
* <br>
@@ -168,31 +181,15 @@ public void execute() throws MojoFailureException
168181
int invokedBranchesCount = coverage.invokedBranchesCount();
169182
int invokedStatementCount = coverage.invokedStatementCount();
170183

171-
getLog().info( String.format( "Statement coverage.: %s%%", coverage.statementCoverageFormatted() ) );
172-
getLog().info( String.format( "Branch coverage....: %s%%", coverage.branchCoverageFormatted() ) );
173184
getLog().debug( String.format( "invokedBranchesCount:%d / branchCount:%d, invokedStatementCount:%d / statementCount:%d",
174185
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 )
176191
{
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" );
196193
}
197194

198195
long te = System.currentTimeMillis();
@@ -201,9 +198,64 @@ else if ( coverage.statementCoveragePercent() < minimumCoverage )
201198

202199
// Private utility methods
203200

204-
private boolean is100( Double d )
201+
private static boolean is100( Double d )
205202
{
206203
return Math.abs( 100 - d ) <= 0.00001d;
207204
}
208205

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

Comments
 (0)