Skip to content

Commit

Permalink
Coverage minima: add more fine-grained control
Browse files Browse the repository at this point in the history
Along with existing overall coverage, add parameters for statement and
branch minima at the package and file level.
  • Loading branch information
kitbellew committed Dec 31, 2023
1 parent 64377d5 commit 3d9c712
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ Read [SBT SCoverage Plugin documentation](https://github.com/scoverage/sbt-scove
<configuration>
<minimumCoverage>95</minimumCoverage>
<minimumCoverageBranchTotal>90</minimumCoverageBranchTotal>
<minimumCoverageStmtPerPackage>90</minimumCoverageStmtPerPackage>
<minimumCoverageBranchPerPackage>85</minimumCoverageBranchPerPackage>
<minimumCoverageStmtPerFile>85</minimumCoverageStmtPerFile>
<minimumCoverageBranchPerFile>80</minimumCoverageBranchPerFile>
<failOnMinimumCoverage>true</failOnMinimumCoverage>
</configuration>
<executions>
Expand Down
4 changes: 4 additions & 0 deletions src/it/test_coverage_minima/module03/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<aggregate>true</aggregate>
<minimumCoverage>100</minimumCoverage>
<minimumCoverageBranchTotal>100</minimumCoverageBranchTotal>
<minimumCoverageStmtPerPackage>100</minimumCoverageStmtPerPackage>
<minimumCoverageBranchPerPackage>100</minimumCoverageBranchPerPackage>
<minimumCoverageStmtPerFile>100</minimumCoverageStmtPerFile>
<minimumCoverageBranchPerFile>100</minimumCoverageBranchPerFile>
</configuration>
<executions>
<execution>
Expand Down
4 changes: 4 additions & 0 deletions src/it/test_coverage_minima/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
<aggregate>true</aggregate> <!-- for aggregated report -->
<minimumCoverage>95</minimumCoverage>
<minimumCoverageBranchTotal>90</minimumCoverageBranchTotal>
<minimumCoverageStmtPerPackage>90</minimumCoverageStmtPerPackage>
<minimumCoverageBranchPerPackage>85</minimumCoverageBranchPerPackage>
<minimumCoverageStmtPerFile>85</minimumCoverageStmtPerFile>
<minimumCoverageBranchPerFile>80</minimumCoverageBranchPerFile>
<failOnMinimumCoverage>false</failOnMinimumCoverage>
</configuration>
<executions>
Expand Down
4 changes: 4 additions & 0 deletions src/it/test_coverage_minima/validate.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ try {
"""
|[ERROR] Coverage is below minimum [50.00% < 95.00%]: Statement:Total
|[INFO] Coverage is 100%: Branch:Total!
|[ERROR] Coverage is below minimum [0.00% < 90.00%]: Statement:Package:pkg02
|[ERROR] Coverage is below minimum [0.00% < 85.00%]: Branch:Package:pkg02
|[ERROR] Coverage is below minimum [0.00% < 85.00%]: Statement:File:HelloService1.scala
|[ERROR] Coverage is below minimum [0.00% < 80.00%]: Branch:File:HelloService1.scala
|""".stripMargin()
)
checkModule(logText, "module03",
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/org/scoverage/plugin/SCoverageCheckMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -96,6 +98,54 @@ public class SCoverageCheckMojo
@Parameter( property = "scoverage.minimumCoverageBranchTotal", defaultValue = "0" )
private Double minimumCoverageBranchTotal;

/**
* Required minimum per-package statement coverage.
* <br>
* <br>
* See <a href="https://github.com/scoverage/sbt-scoverage#minimum-coverage">https://github.com/scoverage/sbt-scoverage#minimum-coverage</a> for additional documentation.
* <br>
*
* @since 2.0.1
*/
@Parameter( property = "scoverage.minimumCoverageStmtPerPackage", defaultValue = "0" )
private Double minimumCoverageStmtPerPackage;

/**
* Required minimum per-package branch coverage.
* <br>
* <br>
* See <a href="https://github.com/scoverage/sbt-scoverage#minimum-coverage">https://github.com/scoverage/sbt-scoverage#minimum-coverage</a> for additional documentation.
* <br>
*
* @since 2.0.1
*/
@Parameter( property = "scoverage.minimumCoverageBranchPerPackage", defaultValue = "0" )
private Double minimumCoverageBranchPerPackage;

/**
* Required minimum per-file statement coverage.
* <br>
* <br>
* See <a href="https://github.com/scoverage/sbt-scoverage#minimum-coverage">https://github.com/scoverage/sbt-scoverage#minimum-coverage</a> for additional documentation.
* <br>
*
* @since 2.0.1
*/
@Parameter( property = "scoverage.minimumCoverageStmtPerFile", defaultValue = "0" )
private Double minimumCoverageStmtPerFile;

/**
* Required minimum per-file branch coverage.
* <br>
* <br>
* See <a href="https://github.com/scoverage/sbt-scoverage#minimum-coverage">https://github.com/scoverage/sbt-scoverage#minimum-coverage</a> for additional documentation.
* <br>
*
* @since 2.0.1
*/
@Parameter( property = "scoverage.minimumCoverageBranchPerFile", defaultValue = "0" )
private Double minimumCoverageBranchPerFile;

/**
* Fail the build if minimum coverage was not reached.
* <br>
Expand Down Expand Up @@ -188,6 +238,10 @@ public void execute() throws MojoFailureException

boolean ok = checkCoverage( getLog(), "Total", coverage,
minimumCoverage, minimumCoverageBranchTotal, true );
ok = checkCoverage( getLog(), "Package:", coverage.packages(), x -> x.name(),
minimumCoverageStmtPerPackage, minimumCoverageBranchPerPackage ) && ok;
ok = checkCoverage( getLog(), "File:", coverage.files(), x -> x.filename(),
minimumCoverageStmtPerFile, minimumCoverageBranchPerFile ) && ok;

if ( !ok && failOnMinimumCoverage )
{
Expand All @@ -205,6 +259,17 @@ private static boolean is100( Double d )
return Math.abs( 100 - d ) <= 0.00001d;
}

private static <T extends CoverageMetrics >
boolean checkCoverage( Log logger, String metricPrefix,
scala.collection.Iterable< T > metrics,
Function< T, String > toName,
double minStmt, double minBranch )
{
return minStmt <= 0 && minBranch <= 0 || checkAll(metrics, cov ->
checkCoverage(logger, metricPrefix + toName.apply(cov), cov, minStmt, minBranch, false)
);
}

private static boolean checkCoverage( Log logger, String metric, CoverageMetrics metrics,
double minStmt, double minBranch, boolean logSuccessInfo )
{
Expand Down Expand Up @@ -261,4 +326,17 @@ private static void logSuccess( Log logger, String message, boolean logSuccessIn
}
}

private static <T> boolean checkAll( scala.collection.Iterable<T> iterable, Predicate<T> predicate )
{
boolean ok = true;
for ( T elem : CollectionConverters.asJava( iterable ) )
{
if ( !predicate.test( elem ) )
{
ok = false;
}
}
return ok;
}

}

0 comments on commit 3d9c712

Please sign in to comment.