-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Coverage minima: add more fine-grained control
Along with existing statement minimum, add branch minimum. Also, include this pair of control at the package and file level.
- Loading branch information
Albert Meltzer
committed
May 8, 2021
1 parent
db352b8
commit fdc20aa
Showing
47 changed files
with
574 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package scoverage | ||
|
||
import sbt._ | ||
import scoverage.DoubleFormat.twoFractionDigits | ||
|
||
case class CoverageMinimum( | ||
statement: Double, | ||
branch: Double | ||
) { | ||
def checkCoverage( | ||
metrics: CoverageMetrics, | ||
metric: String | ||
)(implicit log: Logger): Boolean = { | ||
CoverageMinimum.checkCoverage( | ||
s"Branch:$metric", | ||
branch, | ||
metrics.branchCoveragePercent | ||
) && | ||
CoverageMinimum.checkCoverage( | ||
s"Stmt:$metric", | ||
statement, | ||
metrics.statementCoveragePercent | ||
) | ||
} | ||
|
||
} | ||
|
||
object CoverageMinimum { | ||
|
||
def checkCoverage( | ||
metric: String, | ||
min: Double, | ||
cper: Double | ||
)(implicit log: Logger): Boolean = { | ||
// check for default minimum | ||
min <= 0 || { | ||
def is100(d: Double) = Math.abs(100 - d) <= 0.00001 | ||
|
||
if (is100(min) && is100(cper)) { | ||
log.debug(s"100% Coverage: $metric") | ||
true | ||
} else { | ||
val ok: Boolean = min <= cper | ||
def minfmt = twoFractionDigits(min) | ||
def cfmt = twoFractionDigits(cper) | ||
if (ok) { | ||
log.debug(s"Coverage is above minimum [$cfmt% >= $minfmt%]: $metric") | ||
} else { | ||
log.error(s"Coverage is below minimum [$cfmt% < $minfmt%]: $metric") | ||
} | ||
ok | ||
} | ||
} | ||
} | ||
|
||
case class All( | ||
total: CoverageMinimum, | ||
perPackage: CoverageMinimum, | ||
perFile: CoverageMinimum | ||
) { | ||
def checkCoverage( | ||
coverage: Coverage, | ||
failOnMin: Boolean | ||
)(implicit log: Logger): Unit = { | ||
val ok: Boolean = total.checkCoverage(coverage, "Total") && | ||
coverage.packages.forall(pkg => | ||
perPackage.checkCoverage(pkg, s"Package:${pkg.name}") | ||
) && | ||
coverage.files.forall(file => | ||
perFile.checkCoverage(file, s"File:${file.filename}") | ||
) | ||
|
||
if (!ok && failOnMin) | ||
throw new RuntimeException("Coverage minimum was not reached") | ||
|
||
log.info( | ||
s"All done. Coverage was" + | ||
s" stmt=[${coverage.statementCoverageFormatted}%]" + | ||
s" branch=[${coverage.branchCoverageFormatted}]" | ||
) | ||
} | ||
|
||
} | ||
|
||
def all = Def.setting { | ||
import ScoverageKeys._ | ||
val stmtTotal = | ||
math.max(coverageMinimum.value, coverageMinimumStmtTotal.value) | ||
All( | ||
total = CoverageMinimum( | ||
statement = stmtTotal, | ||
branch = coverageMinimumBranchTotal.value | ||
), | ||
perPackage = CoverageMinimum( | ||
statement = coverageMinimumStmtPerPackage.value, | ||
branch = coverageMinimumBranchPerPackage.value | ||
), | ||
perFile = CoverageMinimum( | ||
statement = coverageMinimumStmtPerFile.value, | ||
branch = coverageMinimumBranchPerFile.value | ||
) | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
version := "0.1" | ||
|
||
scalaVersion := "2.13.5" | ||
|
||
libraryDependencies += "org.scalameta" %% "munit" % "0.7.25" % Test | ||
|
||
coverageMinimumBranchPerFile := 80 | ||
|
||
coverageFailOnMinimum := true | ||
|
||
resolvers ++= { | ||
if (sys.props.get("plugin.version").map(_.endsWith("-SNAPSHOT")).getOrElse(false)) Seq(Resolver.sonatypeRepo("snapshots")) | ||
else Seq.empty | ||
} |
14 changes: 14 additions & 0 deletions
14
src/sbt-test/scoverage/bad-coverage-file-branch/project/plugins.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
val pluginVersion = sys.props.getOrElse( | ||
"plugin.version", | ||
throw new RuntimeException( | ||
"""|The system property 'plugin.version' is not defined. | ||
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)) | ||
|
||
addSbtPlugin("org.scoverage" % "sbt-scoverage" % pluginVersion) | ||
|
||
resolvers ++= { | ||
if (pluginVersion.endsWith("-SNAPSHOT")) | ||
Seq(Resolver.sonatypeRepo("snapshots")) | ||
else | ||
Seq.empty | ||
} |
9 changes: 9 additions & 0 deletions
9
src/sbt-test/scoverage/bad-coverage-file-branch/src/main/scala/one/BadCoverage.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package one | ||
|
||
object BadCoverage { | ||
|
||
def sum(num1: Int, num2: Int) = { | ||
if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2 | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/sbt-test/scoverage/bad-coverage-file-branch/src/main/scala/two/BadCoverage.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package two | ||
|
||
object BadCoverage { | ||
|
||
def sum(num1: Int, num2: Int) = { | ||
if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2 | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/sbt-test/scoverage/bad-coverage-file-branch/src/test/scala/BadCoverageSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import munit.FunSuite | ||
|
||
class BadCoverageSpec extends FunSuite { | ||
|
||
test("one.BadCoverage should sum two numbers") { | ||
assertEquals(one.BadCoverage.sum(1, 2), 3) | ||
assertEquals(one.BadCoverage.sum(0, 3), 3) | ||
assertEquals(one.BadCoverage.sum(3, 0), 3) | ||
} | ||
|
||
test("two.BadCoverage should sum two numbers") { | ||
assertEquals(two.BadCoverage.sum(1, 2), 3) | ||
assertEquals(two.BadCoverage.sum(0, 3), 3) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# run scoverage | ||
> clean | ||
> coverage | ||
> test | ||
-> coverageReport |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
version := "0.1" | ||
|
||
scalaVersion := "2.13.5" | ||
|
||
libraryDependencies += "org.scalameta" %% "munit" % "0.7.25" % Test | ||
|
||
coverageMinimumStmtPerFile := 90 | ||
|
||
coverageFailOnMinimum := true | ||
|
||
resolvers ++= { | ||
if (sys.props.get("plugin.version").map(_.endsWith("-SNAPSHOT")).getOrElse(false)) Seq(Resolver.sonatypeRepo("snapshots")) | ||
else Seq.empty | ||
} |
14 changes: 14 additions & 0 deletions
14
src/sbt-test/scoverage/bad-coverage-file-stmt/project/plugins.sbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
val pluginVersion = sys.props.getOrElse( | ||
"plugin.version", | ||
throw new RuntimeException( | ||
"""|The system property 'plugin.version' is not defined. | ||
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)) | ||
|
||
addSbtPlugin("org.scoverage" % "sbt-scoverage" % pluginVersion) | ||
|
||
resolvers ++= { | ||
if (pluginVersion.endsWith("-SNAPSHOT")) | ||
Seq(Resolver.sonatypeRepo("snapshots")) | ||
else | ||
Seq.empty | ||
} |
9 changes: 9 additions & 0 deletions
9
src/sbt-test/scoverage/bad-coverage-file-stmt/src/main/scala/one/BadCoverage.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package one | ||
|
||
object BadCoverage { | ||
|
||
def sum(num1: Int, num2: Int) = { | ||
if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2 | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/sbt-test/scoverage/bad-coverage-file-stmt/src/main/scala/two/BadCoverage.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package two | ||
|
||
object BadCoverage { | ||
|
||
def sum(num1: Int, num2: Int) = { | ||
if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2 | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/sbt-test/scoverage/bad-coverage-file-stmt/src/test/scala/BadCoverageSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import munit.FunSuite | ||
|
||
class BadCoverageSpec extends FunSuite { | ||
|
||
test("one.BadCoverage should sum two numbers") { | ||
assertEquals(one.BadCoverage.sum(1, 2), 3) | ||
assertEquals(one.BadCoverage.sum(0, 3), 3) | ||
assertEquals(one.BadCoverage.sum(3, 0), 3) | ||
} | ||
|
||
test("two.BadCoverage should sum two numbers") { | ||
assertEquals(two.BadCoverage.sum(1, 2), 3) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# run scoverage | ||
> clean | ||
> coverage | ||
> test | ||
-> coverageReport |
Oops, something went wrong.