diff --git a/README.md b/README.md index ebc88679..f1ae2d31 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,21 @@ These settings will be enforced when the reports are generated. If you generate an aggregate report using `coverageAggregate` then these settings will apply to that report. +### Override Location for Coverage Data And Report + +If desired, one could override the default location for generating the sbt report and data through setting `coverageDataDir`: + +Example in data-dir test: +```scala +coverageDataDir := target.value / "custom-test" +``` + +Can also be set through the sbt set directive +```scala +set coverageDataDir := file("/tmp") +``` + + ## Trouble-shooting failing tests scoverage does a lot of file writing behind the scenes in order to track which diff --git a/src/main/scala/scoverage/ScoverageKeys.scala b/src/main/scala/scoverage/ScoverageKeys.scala index f846b277..add12d0c 100644 --- a/src/main/scala/scoverage/ScoverageKeys.scala +++ b/src/main/scala/scoverage/ScoverageKeys.scala @@ -18,6 +18,7 @@ object ScoverageKeys { lazy val coverageOutputDebug = settingKey[Boolean]("turn on the debug report") lazy val coverageOutputTeamCity = settingKey[Boolean]("turn on teamcity reporting") lazy val coverageScalacPluginVersion = settingKey[String]("version of scalac-scoverage-plugin to use") + lazy val coverageDataDir = settingKey[File]("directory where the measurements and report files will be stored") // format: on @deprecated("Use coverageMinimumStmtTotal instead", "v1.8.0") diff --git a/src/main/scala/scoverage/ScoverageSbtPlugin.scala b/src/main/scala/scoverage/ScoverageSbtPlugin.scala index 183b6868..1e243a75 100644 --- a/src/main/scala/scoverage/ScoverageSbtPlugin.scala +++ b/src/main/scala/scoverage/ScoverageSbtPlugin.scala @@ -60,7 +60,8 @@ object ScoverageSbtPlugin extends AutoPlugin { ivyConfigurations += ScoveragePluginConfig, coverageReport := coverageReport0.value, coverageAggregate := coverageAggregate0.value, - coverageAggregate / aggregate := false + coverageAggregate / aggregate := false, + coverageDataDir := crossTarget.value ) ++ coverageSettings ++ scalacSettings private lazy val coverageSettings = Seq( @@ -99,7 +100,7 @@ object ScoverageSbtPlugin extends AutoPlugin { Seq( Some(s"-Xplugin:${pluginPath.getAbsolutePath}"), Some( - s"-P:scoverage:dataDir:${crossTarget.value.getAbsolutePath}/scoverage-data" + s"-P:scoverage:dataDir:${coverageDataDir.value.getAbsolutePath}/scoverage-data" ), Option(coverageExcludedPackages.value.trim) .filter(_.nonEmpty) @@ -136,7 +137,7 @@ object ScoverageSbtPlugin extends AutoPlugin { } private lazy val coverageReport0 = Def.task { - val target = crossTarget.value + val target = coverageDataDir.value implicit val log = streams.value.log log.info(s"Waiting for measurement data to sync...") @@ -169,13 +170,13 @@ object ScoverageSbtPlugin extends AutoPlugin { implicit val log = streams.value.log log.info(s"Aggregating coverage from subprojects...") - val dataDirs = crossTarget + val dataDirs = coverageDataDir .all(aggregateFilter) .value map (_ / Constants.DataDir) filter (_.isDirectory) CoverageAggregator.aggregate(dataDirs) match { case Some(cov) => writeReports( - crossTarget.value, + coverageDataDir.value, sourceDirectories.all(aggregateFilter).value.flatten, cov, coverageOutputCobertura.value, diff --git a/src/sbt-test/scoverage/data-dir/build.sbt b/src/sbt-test/scoverage/data-dir/build.sbt new file mode 100644 index 00000000..c18a8d52 --- /dev/null +++ b/src/sbt-test/scoverage/data-dir/build.sbt @@ -0,0 +1,17 @@ +version := "0.1" + +scalaVersion := "2.13.6" + +libraryDependencies += "org.specs2" %% "specs2-core" % "4.12.10" % "test" + +coverageDataDir := target.value / "custom-test" + +coverageMinimum := 80 + +coverageFailOnMinimum := true + +resolvers ++= { + if (sys.props.get("plugin.version").exists(_.endsWith("-SNAPSHOT"))) + Seq(Resolver.sonatypeRepo("snapshots")) + else Seq.empty +} diff --git a/src/sbt-test/scoverage/data-dir/project/build.properties b/src/sbt-test/scoverage/data-dir/project/build.properties new file mode 100644 index 00000000..67d27a1d --- /dev/null +++ b/src/sbt-test/scoverage/data-dir/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.5.3 diff --git a/src/sbt-test/scoverage/data-dir/project/plugins.sbt b/src/sbt-test/scoverage/data-dir/project/plugins.sbt new file mode 100644 index 00000000..8d349239 --- /dev/null +++ b/src/sbt-test/scoverage/data-dir/project/plugins.sbt @@ -0,0 +1,16 @@ +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/data-dir/src/main/scala/GoodCoverage.scala b/src/sbt-test/scoverage/data-dir/src/main/scala/GoodCoverage.scala new file mode 100644 index 00000000..f4f3ecae --- /dev/null +++ b/src/sbt-test/scoverage/data-dir/src/main/scala/GoodCoverage.scala @@ -0,0 +1,6 @@ +object GoodCoverage { + + def sum(num1: Int, num2: Int) = { + num1 + num2 + } +} \ No newline at end of file diff --git a/src/sbt-test/scoverage/data-dir/src/test/scala/GoodCoverageSpec.scala b/src/sbt-test/scoverage/data-dir/src/test/scala/GoodCoverageSpec.scala new file mode 100644 index 00000000..cbf6a70d --- /dev/null +++ b/src/sbt-test/scoverage/data-dir/src/test/scala/GoodCoverageSpec.scala @@ -0,0 +1,13 @@ +import org.specs2.mutable._ + +/** + * Created by tbarke001c on 7/8/14. + */ +class GoodCoverageSpec extends Specification { + + "GoodCoverage" should { + "sum two numbers" in { + GoodCoverage.sum(1, 2) mustEqual 3 + } + } +} diff --git a/src/sbt-test/scoverage/data-dir/test b/src/sbt-test/scoverage/data-dir/test new file mode 100644 index 00000000..a211a6f7 --- /dev/null +++ b/src/sbt-test/scoverage/data-dir/test @@ -0,0 +1,7 @@ +# run scoverage +> clean +> coverage +> test +> coverageReport +$ exists target/custom-test/scoverage-data +$ exists target/custom-test/scoverage-report \ No newline at end of file