diff --git a/README.md b/README.md index 67ef9623..2ef55bbc 100644 --- a/README.md +++ b/README.md @@ -102,8 +102,13 @@ Any code between two such comments will not be instrumented or included in the c Based on minimum coverage, you can fail the build with the following keys ```scala -coverageMinimum := 80 +coverageMinimum := 95 coverageFailOnMinimum := true +coverageMinimumBranchTotal := 90 +coverageMinimumStmtPerPackage := 90 +coverageMinimumBranchPerPackage := 85 +coverageMinimumStmtPerFile := 85 +coverageMinimumBranchPerFile := 80 ``` These settings will be enforced when the reports are generated. diff --git a/src/main/scala/scoverage/ScoverageKeys.scala b/src/main/scala/scoverage/ScoverageKeys.scala index 52b0b262..b572f311 100644 --- a/src/main/scala/scoverage/ScoverageKeys.scala +++ b/src/main/scala/scoverage/ScoverageKeys.scala @@ -8,7 +8,12 @@ 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 coverageMinimum = settingKey[Double]("scoverage-minimum-coverage-stmt-total") + lazy val coverageMinimumBranchTotal = settingKey[Double]("scoverage-minimum-coverage-branch-total") + lazy val coverageMinimumStmtPerPackage = settingKey[Double]("scoverage-minimum-coverage-stmt-per-package") + lazy val coverageMinimumBranchPerPackage = settingKey[Double]("scoverage-minimum-coverage-branch-per-package") + lazy val coverageMinimumStmtPerFile = settingKey[Double]("scoverage-minimum-coverage-stmt-per-file") + lazy val coverageMinimumBranchPerFile = settingKey[Double]("scoverage-minimum-coverage-branch-per-file") 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") @@ -18,4 +23,32 @@ object ScoverageKeys { lazy val coverageCleanSubprojectFiles = settingKey[Boolean]("removes subproject data after an aggregation") lazy val coverageOutputTeamCity = settingKey[Boolean]("turn on teamcity reporting") lazy val coverageScalacPluginVersion = settingKey[String]("version of scalac-scoverage-plugin to use") + + def coverageMinima = Def.setting { + CoverageMinima( + total = CoverageMinimum( + statement = coverageMinimum.value, + branch = coverageMinimumBranchTotal.value), + perPackage = CoverageMinimum( + statement = coverageMinimumStmtPerPackage.value, + branch = coverageMinimumBranchPerPackage.value + ), + perFile = CoverageMinimum( + statement = coverageMinimumStmtPerFile.value, + branch = coverageMinimumBranchPerFile.value + ) + ) + } + + case class CoverageMinimum( + statement: Double, + branch: Double + ) + + case class CoverageMinima( + total: CoverageMinimum, + perPackage: CoverageMinimum, + perFile: CoverageMinimum + ) + } diff --git a/src/main/scala/scoverage/ScoverageSbtPlugin.scala b/src/main/scala/scoverage/ScoverageSbtPlugin.scala index b919c257..e515c97f 100644 --- a/src/main/scala/scoverage/ScoverageSbtPlugin.scala +++ b/src/main/scala/scoverage/ScoverageSbtPlugin.scala @@ -28,6 +28,11 @@ object ScoverageSbtPlugin extends AutoPlugin { coverageExcludedPackages := "", coverageExcludedFiles := "", coverageMinimum := 0, // default is no minimum + coverageMinimumBranchTotal := 0, + coverageMinimumStmtPerPackage := 0, + coverageMinimumBranchPerPackage := 0, + coverageMinimumStmtPerFile := 0, + coverageMinimumBranchPerFile := 0, coverageFailOnMinimum := false, coverageHighlighting := true, coverageOutputXML := true, @@ -123,7 +128,7 @@ object ScoverageSbtPlugin extends AutoPlugin { sourceEncoding((scalacOptions in (Compile)).value), log) - checkCoverage(cov, log, coverageMinimum.value, coverageFailOnMinimum.value) + checkCoverage(cov, log, coverageMinima.value, coverageFailOnMinimum.value) case None => log.warn("No coverage data, skipping reports") } } @@ -150,7 +155,7 @@ object ScoverageSbtPlugin extends AutoPlugin { val cfmt = cov.statementCoverageFormatted log.info(s"Aggregation complete. Coverage was [$cfmt]") - checkCoverage(cov, log, coverageMinimum.value, coverageFailOnMinimum.value) + checkCoverage(cov, log, coverageMinima.value, coverageFailOnMinimum.value) case None => log.info("No subproject data to aggregate, skipping reports") } @@ -244,28 +249,51 @@ object ScoverageSbtPlugin extends AutoPlugin { private def checkCoverage(coverage: Coverage, log: Logger, - min: Double, + min: CoverageMinima, failOnMin: Boolean): Unit = { + val ok: Boolean = checkCoverage(coverage, "Total", log, min.total) && + coverage.packages.forall(x => checkCoverage(x, s"Package:${x.name}", log, min.perPackage)) && + coverage.files.forall(x => checkCoverage(x, s"File:${x.filename}", log, min.perFile)) + + if (!ok && failOnMin) + throw new RuntimeException("Coverage minimum was not reached") + + log.info(s"All done. Coverage was [${coverage.statementCoverageFormatted}%]") + } - val cper = coverage.statementCoveragePercent - val cfmt = coverage.statementCoverageFormatted + private def checkCoverage(metrics: CoverageMetrics, + metric: String, + log: Logger, + min: CoverageMinimum): Boolean = { + checkCoverage(s"Branch:$metric", log, min.branch, metrics.branchCoveragePercent) && + checkCoverage(s"Stmt:$metric", log, min.statement, metrics.statementCoveragePercent) + } + private def checkCoverage(metric: String, + log: Logger, + min: Double, + cper: Double): Boolean = { // check for default minimum - if (min > 0) { + if (min <= 0) { + true + } else { 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") + log.info(s"100% Coverage: $metric") + true } else { - log.info(s"Coverage is above minimum [$cfmt% > $min%]") + val ok: Boolean = min <= cper + val minfmt = scoverage.DoubleFormat.twoFractionDigits(min) + val cfmt = scoverage.DoubleFormat.twoFractionDigits(cper) + if (ok) { + log.info(s"Coverage is above minimum [$cfmt% > $minfmt%]: $metric") + } else { + log.error(s"Coverage is below minimum [$cfmt% < $minfmt%]: $metric") + } + ok } } - - log.info(s"All done. Coverage was [$cfmt%]") } private def sourceEncoding(scalacOptions: Seq[String]): Option[String] = { 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..7eb70b2c --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-branch/build.sbt @@ -0,0 +1,14 @@ +version := "0.1" + +scalaVersion := "2.10.4" + +libraryDependencies += "org.specs2" %% "specs2" % "2.3.13" % "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..30f7c47b --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-branch/src/test/scala/BadCoverageSpec.scala @@ -0,0 +1,17 @@ +import org.specs2.mutable._ + +/** + * Created by tbarke001c on 7/8/14. + */ +class BadCoverageSpec extends Specification { + + "BadCoverage" should { + "sum two numbers" in { + one.BadCoverage.sum(1, 2) mustEqual 3 + one.BadCoverage.sum(0, 3) mustEqual 3 + one.BadCoverage.sum(3, 0) mustEqual 3 + two.BadCoverage.sum(1, 2) mustEqual 3 + two.BadCoverage.sum(3, 0) mustEqual 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..90eac526 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-stmt/build.sbt @@ -0,0 +1,14 @@ +version := "0.1" + +scalaVersion := "2.10.4" + +libraryDependencies += "org.specs2" %% "specs2" % "2.3.13" % "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..c8467e19 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-file-stmt/src/test/scala/BadCoverageSpec.scala @@ -0,0 +1,16 @@ +import org.specs2.mutable._ + +/** + * Created by tbarke001c on 7/8/14. + */ +class BadCoverageSpec extends Specification { + + "BadCoverage" should { + "sum two numbers" in { + one.BadCoverage.sum(1, 2) mustEqual 3 + one.BadCoverage.sum(0, 3) mustEqual 3 + one.BadCoverage.sum(3, 0) mustEqual 3 + two.BadCoverage.sum(1, 2) mustEqual 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..04863300 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-branch/build.sbt @@ -0,0 +1,14 @@ +version := "0.1" + +scalaVersion := "2.10.4" + +libraryDependencies += "org.specs2" %% "specs2" % "2.3.13" % "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..12ebc82a --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-branch/src/test/scala/BadCoverageSpec.scala @@ -0,0 +1,17 @@ +import org.specs2.mutable._ + +/** + * Created by tbarke001c on 7/8/14. + */ +class BadCoverageSpec extends Specification { + + "BadCoverage" should { + "sum two numbers" in { + one.BadCoverage.sum(1, 2) mustEqual 3 + one.BadCoverage.sum(0, 3) mustEqual 3 + one.BadCoverage.sum(3, 0) mustEqual 3 + two.BadCoverage.sum(1, 2) mustEqual 3 + two.BadCoverage.sum(0, 3) mustEqual 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..2561e173 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-stmt/build.sbt @@ -0,0 +1,14 @@ +version := "0.1" + +scalaVersion := "2.10.4" + +libraryDependencies += "org.specs2" %% "specs2" % "2.3.13" % "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..c8467e19 --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-package-stmt/src/test/scala/BadCoverageSpec.scala @@ -0,0 +1,16 @@ +import org.specs2.mutable._ + +/** + * Created by tbarke001c on 7/8/14. + */ +class BadCoverageSpec extends Specification { + + "BadCoverage" should { + "sum two numbers" in { + one.BadCoverage.sum(1, 2) mustEqual 3 + one.BadCoverage.sum(0, 3) mustEqual 3 + one.BadCoverage.sum(3, 0) mustEqual 3 + two.BadCoverage.sum(1, 2) mustEqual 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..52ec12be --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-total-branch/build.sbt @@ -0,0 +1,14 @@ +version := "0.1" + +scalaVersion := "2.10.4" + +libraryDependencies += "org.specs2" %% "specs2" % "2.3.13" % "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..7efcf3fd --- /dev/null +++ b/src/sbt-test/scoverage/bad-coverage-total-branch/src/test/scala/BadCoverageSpec.scala @@ -0,0 +1,14 @@ +import org.specs2.mutable._ + +/** + * Created by tbarke001c on 7/8/14. + */ +class BadCoverageSpec extends Specification { + + "BadCoverage" should { + "sum two numbers" in { + BadCoverage.sum(1, 2) mustEqual 3 + BadCoverage.sum(0, 3) mustEqual 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/coverage-off/build.sbt b/src/sbt-test/scoverage/coverage-off/build.sbt index 4908e4f4..b252a9ff 100644 --- a/src/sbt-test/scoverage/coverage-off/build.sbt +++ b/src/sbt-test/scoverage/coverage-off/build.sbt @@ -5,6 +5,11 @@ scalaVersion := "2.10.4" libraryDependencies += "org.specs2" %% "specs2" % "2.3.13" % "test" coverageMinimum := 80 +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 cbf6a70d..53e14f9f 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 @@ -8,6 +8,11 @@ class GoodCoverageSpec extends Specification { "GoodCoverage" should { "sum two numbers" in { GoodCoverage.sum(1, 2) mustEqual 3 + GoodCoverage.sum(0, 3) mustEqual 3 + GoodCoverage.sum(3, 0) mustEqual 3 + two.GoodCoverage.sum(1, 2) mustEqual 3 + two.GoodCoverage.sum(0, 3) mustEqual 3 + two.GoodCoverage.sum(3, 0) mustEqual 3 } } } diff --git a/src/sbt-test/scoverage/good-coverage/build.sbt b/src/sbt-test/scoverage/good-coverage/build.sbt index 4908e4f4..b252a9ff 100644 --- a/src/sbt-test/scoverage/good-coverage/build.sbt +++ b/src/sbt-test/scoverage/good-coverage/build.sbt @@ -5,6 +5,11 @@ scalaVersion := "2.10.4" libraryDependencies += "org.specs2" %% "specs2" % "2.3.13" % "test" coverageMinimum := 80 +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 cbf6a70d..53e14f9f 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 @@ -8,6 +8,11 @@ class GoodCoverageSpec extends Specification { "GoodCoverage" should { "sum two numbers" in { GoodCoverage.sum(1, 2) mustEqual 3 + GoodCoverage.sum(0, 3) mustEqual 3 + GoodCoverage.sum(3, 0) mustEqual 3 + two.GoodCoverage.sum(1, 2) mustEqual 3 + two.GoodCoverage.sum(0, 3) mustEqual 3 + two.GoodCoverage.sum(3, 0) mustEqual 3 } } }