diff --git a/README.md b/README.md index b4780f94..ebc88679 100644 --- a/README.md +++ b/README.md @@ -89,8 +89,13 @@ coverage report. Based on minimum coverage, you can fail the build with the following keys: ```scala -coverageMinimum := 80 coverageFailOnMinimum := true +coverageMinimumStmtTotal := 90 +coverageMinimumBranchTotal := 90 +coverageMinimumStmtPerPackage := 90 +coverageMinimumBranchPerPackage := 85 +coverageMinimumStmtPerFile := 85 +coverageMinimumBranchPerFile := 80 ``` These settings will be enforced when the reports are generated. If you generate diff --git a/src/main/scala/scoverage/CoverageMinimum.scala b/src/main/scala/scoverage/CoverageMinimum.scala new file mode 100644 index 00000000..be05c872 --- /dev/null +++ b/src/main/scala/scoverage/CoverageMinimum.scala @@ -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 + ) + ) + } + +} diff --git a/src/main/scala/scoverage/ScoverageKeys.scala b/src/main/scala/scoverage/ScoverageKeys.scala index 3a6cd203..f846b277 100644 --- a/src/main/scala/scoverage/ScoverageKeys.scala +++ b/src/main/scala/scoverage/ScoverageKeys.scala @@ -11,8 +11,6 @@ object ScoverageKeys { lazy val coverageAggregate = taskKey[Unit]("aggregate reports from subprojects") lazy val coverageExcludedPackages = settingKey[String]("regex for excluded packages") lazy val coverageExcludedFiles = settingKey[String]("regex for excluded file paths") - lazy val coverageMinimum = settingKey[Double]("scoverage-minimum-coverage") - lazy val coverageFailOnMinimum = settingKey[Boolean]("if coverage is less than this value then fail build") lazy val coverageHighlighting = settingKey[Boolean]("enables range positioning for highlighting") lazy val coverageOutputCobertura = settingKey[Boolean]("enables cobertura XML report generation") lazy val coverageOutputXML = settingKey[Boolean]("enables xml report generation") @@ -21,4 +19,22 @@ object ScoverageKeys { lazy val coverageOutputTeamCity = settingKey[Boolean]("turn on teamcity reporting") lazy val coverageScalacPluginVersion = settingKey[String]("version of scalac-scoverage-plugin to use") // format: on + + @deprecated("Use coverageMinimumStmtTotal instead", "v1.8.0") + lazy val coverageMinimum = + settingKey[Double]("see coverageMinimumStmtTotal") + lazy val coverageMinimumStmtTotal = + settingKey[Double]("scoverage minimum coverage: statement total") + lazy val coverageMinimumBranchTotal = + settingKey[Double]("scoverage minimum coverage: branch total") + lazy val coverageMinimumStmtPerPackage = + settingKey[Double]("scoverage minimum coverage: statement per package") + lazy val coverageMinimumBranchPerPackage = + settingKey[Double]("scoverage minimum coverage: branch per package") + lazy val coverageMinimumStmtPerFile = + settingKey[Double]("scoverage minimum coverage: statement per file") + lazy val coverageMinimumBranchPerFile = + settingKey[Double]("scoverage minimum coverage: branch per file") + lazy val coverageFailOnMinimum = + settingKey[Boolean]("if coverage is less than minimum then fail build") } diff --git a/src/main/scala/scoverage/ScoverageSbtPlugin.scala b/src/main/scala/scoverage/ScoverageSbtPlugin.scala index 9e74868a..07abe634 100644 --- a/src/main/scala/scoverage/ScoverageSbtPlugin.scala +++ b/src/main/scala/scoverage/ScoverageSbtPlugin.scala @@ -35,6 +35,12 @@ object ScoverageSbtPlugin extends AutoPlugin { coverageExcludedPackages := "", coverageExcludedFiles := "", coverageMinimum := 0, // default is no minimum + coverageMinimumStmtTotal := 0, + coverageMinimumBranchTotal := 0, + coverageMinimumStmtPerPackage := 0, + coverageMinimumBranchPerPackage := 0, + coverageMinimumStmtPerFile := 0, + coverageMinimumBranchPerFile := 0, coverageFailOnMinimum := false, coverageHighlighting := true, coverageOutputXML := true, @@ -131,7 +137,7 @@ object ScoverageSbtPlugin extends AutoPlugin { private lazy val coverageReport0 = Def.task { val target = crossTarget.value - val log = streams.value.log + implicit val log = streams.value.log log.info(s"Waiting for measurement data to sync...") Thread.sleep( @@ -153,18 +159,14 @@ object ScoverageSbtPlugin extends AutoPlugin { log ) - checkCoverage( - cov, - log, - coverageMinimum.value, - coverageFailOnMinimum.value - ) + CoverageMinimum.all.value + .checkCoverage(cov, coverageFailOnMinimum.value) case None => log.warn("No coverage data, skipping reports") } } private lazy val coverageAggregate0 = Def.task { - val log = streams.value.log + implicit val log = streams.value.log log.info(s"Aggregating coverage from subprojects...") val dataDirs = crossTarget @@ -187,12 +189,8 @@ object ScoverageSbtPlugin extends AutoPlugin { val cfmt = cov.statementCoverageFormatted log.info(s"Aggregation complete. Coverage was [$cfmt]") - checkCoverage( - cov, - log, - coverageMinimum.value, - coverageFailOnMinimum.value - ) + CoverageMinimum.all.value + .checkCoverage(cov, coverageFailOnMinimum.value) case None => log.info("No subproject data to aggregate, skipping reports") } @@ -326,34 +324,6 @@ object ScoverageSbtPlugin extends AutoPlugin { } } - private def checkCoverage( - coverage: Coverage, - log: Logger, - min: Double, - failOnMin: Boolean - ): Unit = { - - val cper = coverage.statementCoveragePercent - val cfmt = coverage.statementCoverageFormatted - - // check for default minimum - if (min > 0) { - def is100(d: Double) = Math.abs(100 - d) <= 0.00001 - - if (is100(min) && is100(cper)) { - log.info(s"100% Coverage !") - } else if (min > cper) { - log.error(s"Coverage is below minimum [$cfmt% < $min%]") - if (failOnMin) - throw new RuntimeException("Coverage minimum was not reached") - } else { - log.info(s"Coverage is above minimum [$cfmt% > $min%]") - } - } - - log.info(s"All done. Coverage was [$cfmt%]") - } - private def sourceEncoding(scalacOptions: Seq[String]): Option[String] = { val i = scalacOptions.indexOf("-encoding") + 1 if (i > 0 && i < scalacOptions.length) Some(scalacOptions(i)) else None diff --git a/src/sbt-test/scoverage/bad-coverage-file-branch/build.sbt b/src/sbt-test/scoverage/bad-coverage-file-branch/build.sbt new file mode 100644 index 00000000..36d96ac6 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-branch/build.sbt @@ -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 +} diff --git a/src/sbt-test/scoverage/bad-coverage-file-branch/project/plugins.sbt b/src/sbt-test/scoverage/bad-coverage-file-branch/project/plugins.sbt new file mode 100644 index 00000000..8f2837b3 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-branch/project/plugins.sbt @@ -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 +} diff --git a/src/sbt-test/scoverage/bad-coverage-file-branch/src/main/scala/one/BadCoverage.scala b/src/sbt-test/scoverage/bad-coverage-file-branch/src/main/scala/one/BadCoverage.scala new file mode 100644 index 00000000..300ec10c --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-branch/src/main/scala/one/BadCoverage.scala @@ -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 + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-file-branch/src/main/scala/two/BadCoverage.scala b/src/sbt-test/scoverage/bad-coverage-file-branch/src/main/scala/two/BadCoverage.scala new file mode 100644 index 00000000..3ebc31da --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-branch/src/main/scala/two/BadCoverage.scala @@ -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 + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-file-branch/src/test/scala/BadCoverageSpec.scala b/src/sbt-test/scoverage/bad-coverage-file-branch/src/test/scala/BadCoverageSpec.scala new file mode 100644 index 00000000..801c7b43 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-branch/src/test/scala/BadCoverageSpec.scala @@ -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) + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-file-branch/test b/src/sbt-test/scoverage/bad-coverage-file-branch/test new file mode 100644 index 00000000..45cbbb60 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-branch/test @@ -0,0 +1,5 @@ +# run scoverage +> clean +> coverage +> test +-> coverageReport diff --git a/src/sbt-test/scoverage/bad-coverage-file-stmt/build.sbt b/src/sbt-test/scoverage/bad-coverage-file-stmt/build.sbt new file mode 100644 index 00000000..291ad6c8 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-stmt/build.sbt @@ -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 +} diff --git a/src/sbt-test/scoverage/bad-coverage-file-stmt/project/plugins.sbt b/src/sbt-test/scoverage/bad-coverage-file-stmt/project/plugins.sbt new file mode 100644 index 00000000..8f2837b3 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-stmt/project/plugins.sbt @@ -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 +} diff --git a/src/sbt-test/scoverage/bad-coverage-file-stmt/src/main/scala/one/BadCoverage.scala b/src/sbt-test/scoverage/bad-coverage-file-stmt/src/main/scala/one/BadCoverage.scala new file mode 100644 index 00000000..300ec10c --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-stmt/src/main/scala/one/BadCoverage.scala @@ -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 + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-file-stmt/src/main/scala/two/BadCoverage.scala b/src/sbt-test/scoverage/bad-coverage-file-stmt/src/main/scala/two/BadCoverage.scala new file mode 100644 index 00000000..3ebc31da --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-stmt/src/main/scala/two/BadCoverage.scala @@ -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 + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-file-stmt/src/test/scala/BadCoverageSpec.scala b/src/sbt-test/scoverage/bad-coverage-file-stmt/src/test/scala/BadCoverageSpec.scala new file mode 100644 index 00000000..e6f4c197 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-stmt/src/test/scala/BadCoverageSpec.scala @@ -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) + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-file-stmt/test b/src/sbt-test/scoverage/bad-coverage-file-stmt/test new file mode 100644 index 00000000..45cbbb60 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-stmt/test @@ -0,0 +1,5 @@ +# run scoverage +> clean +> coverage +> test +-> coverageReport diff --git a/src/sbt-test/scoverage/bad-coverage-package-branch/build.sbt b/src/sbt-test/scoverage/bad-coverage-package-branch/build.sbt new file mode 100644 index 00000000..333d81eb --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-branch/build.sbt @@ -0,0 +1,14 @@ +version := "0.1" + +scalaVersion := "2.13.5" + +libraryDependencies += "org.scalameta" %% "munit" % "0.7.25" % Test + +coverageMinimumBranchPerPackage := 80 + +coverageFailOnMinimum := true + +resolvers ++= { + if (sys.props.get("plugin.version").map(_.endsWith("-SNAPSHOT")).getOrElse(false)) Seq(Resolver.sonatypeRepo("snapshots")) + else Seq.empty +} diff --git a/src/sbt-test/scoverage/bad-coverage-package-branch/project/plugins.sbt b/src/sbt-test/scoverage/bad-coverage-package-branch/project/plugins.sbt new file mode 100644 index 00000000..8f2837b3 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-branch/project/plugins.sbt @@ -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 +} diff --git a/src/sbt-test/scoverage/bad-coverage-package-branch/src/main/scala/one/BadCoverage.scala b/src/sbt-test/scoverage/bad-coverage-package-branch/src/main/scala/one/BadCoverage.scala new file mode 100644 index 00000000..300ec10c --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-branch/src/main/scala/one/BadCoverage.scala @@ -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 + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-package-branch/src/main/scala/two/BadCoverage.scala b/src/sbt-test/scoverage/bad-coverage-package-branch/src/main/scala/two/BadCoverage.scala new file mode 100644 index 00000000..3ebc31da --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-branch/src/main/scala/two/BadCoverage.scala @@ -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 + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-package-branch/src/test/scala/BadCoverageSpec.scala b/src/sbt-test/scoverage/bad-coverage-package-branch/src/test/scala/BadCoverageSpec.scala new file mode 100644 index 00000000..801c7b43 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-branch/src/test/scala/BadCoverageSpec.scala @@ -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) + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-package-branch/test b/src/sbt-test/scoverage/bad-coverage-package-branch/test new file mode 100644 index 00000000..45cbbb60 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-branch/test @@ -0,0 +1,5 @@ +# run scoverage +> clean +> coverage +> test +-> coverageReport diff --git a/src/sbt-test/scoverage/bad-coverage-package-stmt/build.sbt b/src/sbt-test/scoverage/bad-coverage-package-stmt/build.sbt new file mode 100644 index 00000000..c9f662cc --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-stmt/build.sbt @@ -0,0 +1,14 @@ +version := "0.1" + +scalaVersion := "2.13.5" + +libraryDependencies += "org.scalameta" %% "munit" % "0.7.25" % Test + +coverageMinimumStmtPerPackage := 80 + +coverageFailOnMinimum := true + +resolvers ++= { + if (sys.props.get("plugin.version").map(_.endsWith("-SNAPSHOT")).getOrElse(false)) Seq(Resolver.sonatypeRepo("snapshots")) + else Seq.empty +} diff --git a/src/sbt-test/scoverage/bad-coverage-package-stmt/project/plugins.sbt b/src/sbt-test/scoverage/bad-coverage-package-stmt/project/plugins.sbt new file mode 100644 index 00000000..8f2837b3 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-stmt/project/plugins.sbt @@ -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 +} diff --git a/src/sbt-test/scoverage/bad-coverage-package-stmt/src/main/scala/one/BadCoverage.scala b/src/sbt-test/scoverage/bad-coverage-package-stmt/src/main/scala/one/BadCoverage.scala new file mode 100644 index 00000000..300ec10c --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-stmt/src/main/scala/one/BadCoverage.scala @@ -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 + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-package-stmt/src/main/scala/two/BadCoverage.scala b/src/sbt-test/scoverage/bad-coverage-package-stmt/src/main/scala/two/BadCoverage.scala new file mode 100644 index 00000000..3ebc31da --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-stmt/src/main/scala/two/BadCoverage.scala @@ -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 + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-package-stmt/src/test/scala/BadCoverageSpec.scala b/src/sbt-test/scoverage/bad-coverage-package-stmt/src/test/scala/BadCoverageSpec.scala new file mode 100644 index 00000000..e6f4c197 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-stmt/src/test/scala/BadCoverageSpec.scala @@ -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) + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-package-stmt/test b/src/sbt-test/scoverage/bad-coverage-package-stmt/test new file mode 100644 index 00000000..45cbbb60 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-stmt/test @@ -0,0 +1,5 @@ +# run scoverage +> clean +> coverage +> test +-> coverageReport diff --git a/src/sbt-test/scoverage/bad-coverage-total-branch/build.sbt b/src/sbt-test/scoverage/bad-coverage-total-branch/build.sbt new file mode 100644 index 00000000..8b61d1bb --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-total-branch/build.sbt @@ -0,0 +1,14 @@ +version := "0.1" + +scalaVersion := "2.13.5" + +libraryDependencies += "org.scalameta" %% "munit" % "0.7.25" % Test + +coverageMinimumBranchTotal := 80 + +coverageFailOnMinimum := true + +resolvers ++= { + if (sys.props.get("plugin.version").map(_.endsWith("-SNAPSHOT")).getOrElse(false)) Seq(Resolver.sonatypeRepo("snapshots")) + else Seq.empty +} diff --git a/src/sbt-test/scoverage/bad-coverage-total-branch/project/plugins.sbt b/src/sbt-test/scoverage/bad-coverage-total-branch/project/plugins.sbt new file mode 100644 index 00000000..8f2837b3 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-total-branch/project/plugins.sbt @@ -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 +} diff --git a/src/sbt-test/scoverage/bad-coverage-total-branch/src/main/scala/BadCoverage.scala b/src/sbt-test/scoverage/bad-coverage-total-branch/src/main/scala/BadCoverage.scala new file mode 100644 index 00000000..fd6887a7 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-total-branch/src/main/scala/BadCoverage.scala @@ -0,0 +1,7 @@ +object BadCoverage { + + def sum(num1: Int, num2: Int) = { + if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2 + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-total-branch/src/test/scala/BadCoverageSpec.scala b/src/sbt-test/scoverage/bad-coverage-total-branch/src/test/scala/BadCoverageSpec.scala new file mode 100644 index 00000000..eb4b4ec8 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-total-branch/src/test/scala/BadCoverageSpec.scala @@ -0,0 +1,10 @@ +import munit.FunSuite + +class BadCoverageSpec extends FunSuite { + + test("BadCoverage should sum two numbers") { + assertEquals(BadCoverage.sum(1, 2), 3) + assertEquals(BadCoverage.sum(0, 3), 3) + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-total-branch/test b/src/sbt-test/scoverage/bad-coverage-total-branch/test new file mode 100644 index 00000000..45cbbb60 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-total-branch/test @@ -0,0 +1,5 @@ +# run scoverage +> clean +> coverage +> test +-> coverageReport diff --git a/src/sbt-test/scoverage/bad-coverage-total-stmt/build.sbt b/src/sbt-test/scoverage/bad-coverage-total-stmt/build.sbt new file mode 100644 index 00000000..e6522c5b --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-total-stmt/build.sbt @@ -0,0 +1,14 @@ +version := "0.1" + +scalaVersion := "2.13.5" + +libraryDependencies += "org.scalameta" %% "munit" % "0.7.25" % Test + +coverageMinimumStmtTotal := 80 + +coverageFailOnMinimum := true + +resolvers ++= { + if (sys.props.get("plugin.version").map(_.endsWith("-SNAPSHOT")).getOrElse(false)) Seq(Resolver.sonatypeRepo("snapshots")) + else Seq.empty +} diff --git a/src/sbt-test/scoverage/bad-coverage-total-stmt/project/plugins.sbt b/src/sbt-test/scoverage/bad-coverage-total-stmt/project/plugins.sbt new file mode 100644 index 00000000..8f2837b3 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-total-stmt/project/plugins.sbt @@ -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 +} diff --git a/src/sbt-test/scoverage/bad-coverage-total-stmt/src/main/scala/one/BadCoverage.scala b/src/sbt-test/scoverage/bad-coverage-total-stmt/src/main/scala/one/BadCoverage.scala new file mode 100644 index 00000000..300ec10c --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-total-stmt/src/main/scala/one/BadCoverage.scala @@ -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 + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-total-stmt/src/main/scala/two/BadCoverage.scala b/src/sbt-test/scoverage/bad-coverage-total-stmt/src/main/scala/two/BadCoverage.scala new file mode 100644 index 00000000..3ebc31da --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-total-stmt/src/main/scala/two/BadCoverage.scala @@ -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 + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-total-stmt/src/test/scala/BadCoverageSpec.scala b/src/sbt-test/scoverage/bad-coverage-total-stmt/src/test/scala/BadCoverageSpec.scala new file mode 100644 index 00000000..e487ec12 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-total-stmt/src/test/scala/BadCoverageSpec.scala @@ -0,0 +1,11 @@ +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) + } + +} diff --git a/src/sbt-test/scoverage/bad-coverage-total-stmt/test b/src/sbt-test/scoverage/bad-coverage-total-stmt/test new file mode 100644 index 00000000..45cbbb60 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-total-stmt/test @@ -0,0 +1,5 @@ +# run scoverage +> clean +> coverage +> test +-> coverageReport diff --git a/src/sbt-test/scoverage/coverage-off/build.sbt b/src/sbt-test/scoverage/coverage-off/build.sbt index de77b640..0ab1d3ef 100644 --- a/src/sbt-test/scoverage/coverage-off/build.sbt +++ b/src/sbt-test/scoverage/coverage-off/build.sbt @@ -5,6 +5,12 @@ scalaVersion := "2.13.5" libraryDependencies += "org.scalameta" %% "munit" % "0.7.25" % Test coverageMinimum := 80 +coverageMinimumStmtTotal := 100 +coverageMinimumBranchTotal := 100 +coverageMinimumStmtPerPackage := 100 +coverageMinimumBranchPerPackage := 100 +coverageMinimumStmtPerFile := 100 +coverageMinimumBranchPerFile := 100 coverageFailOnMinimum := true diff --git a/src/sbt-test/scoverage/coverage-off/src/main/scala/GoodCoverage.scala b/src/sbt-test/scoverage/coverage-off/src/main/scala/GoodCoverage.scala index f4f3ecae..ac6582bb 100644 --- a/src/sbt-test/scoverage/coverage-off/src/main/scala/GoodCoverage.scala +++ b/src/sbt-test/scoverage/coverage-off/src/main/scala/GoodCoverage.scala @@ -1,6 +1,7 @@ object GoodCoverage { def sum(num1: Int, num2: Int) = { - num1 + num2 + if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2 } -} \ No newline at end of file + +} diff --git a/src/sbt-test/scoverage/coverage-off/src/main/scala/two/GoodCoverage.scala b/src/sbt-test/scoverage/coverage-off/src/main/scala/two/GoodCoverage.scala new file mode 100644 index 00000000..587e11e1 --- /dev/null +++ b/src/sbt-test/scoverage/coverage-off/src/main/scala/two/GoodCoverage.scala @@ -0,0 +1,9 @@ +package two + +object GoodCoverage { + + def sum(num1: Int, num2: Int) = { + if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2 + } + +} diff --git a/src/sbt-test/scoverage/coverage-off/src/test/scala/GoodCoverageSpec.scala b/src/sbt-test/scoverage/coverage-off/src/test/scala/GoodCoverageSpec.scala index b1d4ddfc..44eec1c9 100644 --- a/src/sbt-test/scoverage/coverage-off/src/test/scala/GoodCoverageSpec.scala +++ b/src/sbt-test/scoverage/coverage-off/src/test/scala/GoodCoverageSpec.scala @@ -4,7 +4,16 @@ import munit.FunSuite */ class GoodCoverageSpec extends FunSuite { - test("GoodCoverage should sum two numvers") { + test("GoodCoverage should sum two numbers") { assertEquals(GoodCoverage.sum(1, 2), 3) + assertEquals(GoodCoverage.sum(0, 3), 3) + assertEquals(GoodCoverage.sum(3, 0), 3) } + + test("two.GoodCoverage should sum two numbers") { + assertEquals(two.GoodCoverage.sum(1, 2), 3) + assertEquals(two.GoodCoverage.sum(0, 3), 3) + assertEquals(two.GoodCoverage.sum(3, 0), 3) + } + } diff --git a/src/sbt-test/scoverage/good-coverage/build.sbt b/src/sbt-test/scoverage/good-coverage/build.sbt index de77b640..0ab1d3ef 100644 --- a/src/sbt-test/scoverage/good-coverage/build.sbt +++ b/src/sbt-test/scoverage/good-coverage/build.sbt @@ -5,6 +5,12 @@ scalaVersion := "2.13.5" libraryDependencies += "org.scalameta" %% "munit" % "0.7.25" % Test coverageMinimum := 80 +coverageMinimumStmtTotal := 100 +coverageMinimumBranchTotal := 100 +coverageMinimumStmtPerPackage := 100 +coverageMinimumBranchPerPackage := 100 +coverageMinimumStmtPerFile := 100 +coverageMinimumBranchPerFile := 100 coverageFailOnMinimum := true diff --git a/src/sbt-test/scoverage/good-coverage/src/main/scala/GoodCoverage.scala b/src/sbt-test/scoverage/good-coverage/src/main/scala/GoodCoverage.scala index f4f3ecae..ac6582bb 100644 --- a/src/sbt-test/scoverage/good-coverage/src/main/scala/GoodCoverage.scala +++ b/src/sbt-test/scoverage/good-coverage/src/main/scala/GoodCoverage.scala @@ -1,6 +1,7 @@ object GoodCoverage { def sum(num1: Int, num2: Int) = { - num1 + num2 + if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2 } -} \ No newline at end of file + +} diff --git a/src/sbt-test/scoverage/good-coverage/src/main/scala/two/GoodCoverage.scala b/src/sbt-test/scoverage/good-coverage/src/main/scala/two/GoodCoverage.scala new file mode 100644 index 00000000..587e11e1 --- /dev/null +++ b/src/sbt-test/scoverage/good-coverage/src/main/scala/two/GoodCoverage.scala @@ -0,0 +1,9 @@ +package two + +object GoodCoverage { + + def sum(num1: Int, num2: Int) = { + if (0 == num1) num2 else if (0 == num2) num1 else num1 + num2 + } + +} diff --git a/src/sbt-test/scoverage/good-coverage/src/test/scala/GoodCoverageSpec.scala b/src/sbt-test/scoverage/good-coverage/src/test/scala/GoodCoverageSpec.scala index 4e88b703..44eec1c9 100644 --- a/src/sbt-test/scoverage/good-coverage/src/test/scala/GoodCoverageSpec.scala +++ b/src/sbt-test/scoverage/good-coverage/src/test/scala/GoodCoverageSpec.scala @@ -6,5 +6,14 @@ class GoodCoverageSpec extends FunSuite { test("GoodCoverage should sum two numbers") { assertEquals(GoodCoverage.sum(1, 2), 3) + assertEquals(GoodCoverage.sum(0, 3), 3) + assertEquals(GoodCoverage.sum(3, 0), 3) } + + test("two.GoodCoverage should sum two numbers") { + assertEquals(two.GoodCoverage.sum(1, 2), 3) + assertEquals(two.GoodCoverage.sum(0, 3), 3) + assertEquals(two.GoodCoverage.sum(3, 0), 3) + } + }