Skip to content
Open
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
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ jobs:
if: "${{ matrix.java != 8 }}"
run: sbt "++ 3.x" test

# "++ 3.x" selects the sbt 2 plugin build (pluginCrossBuild / sbtVersion), so scripted
# launches the test projects with sbt 2. Only tests that support sbt 2 are listed.
- name: Run scripted tests sbt 2
if: "${{ matrix.java != 8 }}"
run: sbt "++ 3.x" "scripted scoverage/cached-compile"

formatting:
runs-on: ubuntu-latest

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ 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`:
sbt report and data through setting `coverageDataDir`. It defaults to
`crossTarget` on sbt 1 and to `Compile / classDirectory` on sbt 2. On sbt 2,
`compile` is cached, and a cache hit only restores the class directory, so the
scoverage data must live inside it. If you override it on sbt 2, keep it inside
`Compile / classDirectory`.

Example in data-dir test:
```scala
Expand Down
7 changes: 7 additions & 0 deletions src/main/scala-2/scoverage/ScoverageSbtPluginCompat.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package scoverage

import sbt.Keys._
import sbt._

private[scoverage] object ScoverageSbtPluginCompat {
implicit class DefOps(private val self: sbt.Def.type) extends AnyVal {
def uncached[A](a: A): A = a
}

/** Default for `coverageDataDir` on sbt 1. */
val defaultCoverageDataDir: Def.Initialize[File] =
Def.setting(crossTarget.value)
}
21 changes: 20 additions & 1 deletion src/main/scala-3/scoverage/ScoverageSbtPluginCompat.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
package scoverage

private[scoverage] object ScoverageSbtPluginCompat
import sbt.Keys.*
import sbt.*

private[scoverage] object ScoverageSbtPluginCompat {

/** Default for `coverageDataDir` on sbt 2.
*
* sbt 2 caches `compile` and, on a cache hit, restores only its declared
* outputs, i.e. the class directory. The scoverage-data directory
* (`scoverage.coverage`, plus the directory the instrumented classes write
* measurements into) is a side effect of compilation. Under `crossTarget` it
* is not restored, so tests fail with a FileNotFoundException or
* coverageReport finds no data. Keeping it inside the class directory makes
* it part of the cached compile output.
*
* See https://github.com/scoverage/sbt-scoverage/issues/696
*/
val defaultCoverageDataDir: Def.Initialize[File] =
Def.setting((Compile / classDirectory).value)
}
2 changes: 1 addition & 1 deletion src/main/scala/scoverage/ScoverageSbtPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object ScoverageSbtPlugin extends AutoPlugin {
coverageReport := coverageReport0.value,
coverageAggregate := coverageAggregate0.value,
coverageAggregate / aggregate := false,
coverageDataDir := crossTarget.value,
coverageDataDir := defaultCoverageDataDir.value,
coverageScalacPluginVersion := {
scalaVersion.value match {
case "2.13.11" => "2.3.0"
Expand Down
16 changes: 16 additions & 0 deletions src/sbt-test/scoverage/cached-compile/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version := "0.1"
scalaVersion := "3.8.4"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we can't run it on JDK 8, but since we are actually testing sbt 2, we can just use Scala 2.13 here.

Suggested change
scalaVersion := "3.8.4"
scalaVersion := "2.13.18"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we can check the java version and switch between Scala versions.

libraryDependencies += "org.scalameta" %% "munit" % "1.1.1" % Test
Test / fork := true
coverageMinimumStmtTotal := 100
coverageFailOnMinimum := true

// coverageReport only logs "No coverage data" when the instrumentation file is
// missing, so assert on it explicitly. A command rather than a task: sbt 2
// caches tasks, and a cached check would not look at the file again.
commands += Command.command("checkCoverageData") { state =>
val dir = Project.extract(state).get(coverageDataDir)
val f = dir / "scoverage-data" / "scoverage.coverage"
if (!f.exists) sys.error(s"missing $f")
state
}
9 changes: 9 additions & 0 deletions src/sbt-test/scoverage/cached-compile/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Calc {
def add(a: Int, b: Int): Int = a + b
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class CalcSuite extends munit.FunSuite {
test("add")(assertEquals(Calc.add(1, 2), 3))
}
14 changes: 14 additions & 0 deletions src/sbt-test/scoverage/cached-compile/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# First round compiles with coverage and populates the build cache.
> clean
> coverage
> testOnly CalcSuite
> checkCoverageData
> coverageReport

# Second round: on sbt 2, compile is restored from the cache after clean.
# The scoverage data must come back with it.
> clean
> coverage
> testOnly CalcSuite
> checkCoverageData
> coverageReport
Loading