Skip to content

Restored the ability to enable coverage with coverageEnabled #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ turn it back off when you're done running reports, use the `coverageOff` command

Sample project with scoverage in both sbt and maven - [the scoverage samples project](https://github.com/scoverage/sbt-scoverage-samples).

### Disabling recording of coverage data at run-time

Coverage instrumentation assumes the code will be run on the same machine
where it was compiled: absolute destination directories are compiled into the class files.
To run the code on a file system where those directories do not exist, for example when
testing in a VM, you may either recompile without coverage or disable the recording by
setting the JVM property `scoverage.recording.enabled` to `false`.

## Notes on upgrading to version 1.3.0

* The object containing the keys has changed from nested to top level so you might need to adjust the import. It's also an auto plugin now, so you might not need the import at all.
Expand Down
32 changes: 15 additions & 17 deletions src/main/scala/scoverage/ScoverageSbtPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object ScoverageSbtPlugin extends AutoPlugin {
// this should match the version defined in build.sbt
val DefaultScoverageVersion = "1.2.0-SNAPSHOT"
val autoImport = ScoverageKeys
lazy val ScoveragePluginConfig = config("scoveragePlugin").hide

import autoImport._

Expand All @@ -28,6 +29,13 @@ object ScoverageSbtPlugin extends AutoPlugin {
commands += Command.command("coverageOff", "disable compiled code with instrumentation", "")(toggleCoverage(false)),
coverageReport <<= coverageReport0,
coverageAggregate <<= coverageAggregate0,
ivyConfigurations := ivyConfigurations.value :+ ScoveragePluginConfig,
libraryDependencies ++= {
if (coverageEnabled.value) Seq(
OrgScoverage % (ScalacRuntimeArtifact + "_" + scalaBinaryVersion.value) % DefaultScoverageVersion,
OrgScoverage % (ScalacPluginArtifact + "_" + scalaBinaryVersion.value) % DefaultScoverageVersion % "scoveragePlugin->default(compile)"
) else Nil
},
scalacOptions in(Compile, compile) ++= scoverageScalacOptions.value,
aggregate in coverageAggregate := false,
coverageExcludedPackages := "",
Expand All @@ -49,17 +57,8 @@ object ScoverageSbtPlugin extends AutoPlugin {
*/
private def toggleCoverage(status: Boolean): State => State = { state =>
val extracted = Project.extract(state)
val newSettings = extracted.structure.allProjectRefs flatMap { proj =>
Seq(
coverageEnabled in proj := status,
libraryDependencies in proj ++= {
if (status) Seq(
OrgScoverage % (ScalacRuntimeArtifact + "_" + scalaBinaryVersion.value) % DefaultScoverageVersion % "provided" intransitive(),
OrgScoverage % (ScalacPluginArtifact + "_" + scalaBinaryVersion.value) % DefaultScoverageVersion % "provided" intransitive()
) else Nil
}
)
}
val newSettings = extracted.structure.allProjectRefs.flatMap(proj =>
Seq(coverageEnabled in proj := status))
extracted.append(newSettings, state)
}

Expand Down Expand Up @@ -118,17 +117,16 @@ object ScoverageSbtPlugin extends AutoPlugin {
}

private lazy val scoverageScalacOptions = Def.task {
val scoverageDeps: Seq[File] = update.value matching configurationFilter("provided")
scoverageDeps.find(_.getAbsolutePath.contains(ScalacPluginArtifact)) match {
case None => throw new Exception(s"Fatal: $ScalacPluginArtifact not in libraryDependencies")
case Some(pluginPath) =>
update.value
.matching(configurationFilter(ScoveragePluginConfig.name))
.find(_.getAbsolutePath.contains(ScalacPluginArtifact))
.fold[Seq[String]](Nil)(pluginPath =>
scalaArgs(coverageEnabled.value,
pluginPath,
crossTarget.value,
coverageExcludedPackages.value,
coverageExcludedFiles.value,
coverageHighlighting.value)
}
coverageHighlighting.value))
}

private def scalaArgs(coverageEnabled: Boolean,
Expand Down