Skip to content

Commit

Permalink
Allow sbt-scoverage to work on Windows
Browse files Browse the repository at this point in the history
The -Xplugin option expects a classpath string with multiple paths
separated by a semicolon or colon depending on the operating system.
However, this currently always uses a colon, which doesn't work on
Windows. This means scalac cannot find the scoverage plugin jars and
leads to errors about "bad options".

This modifies the -Xplugin logic to use File.pathSeparator when building
the Xplugin classpath, allowing it to work regardless of operating
system.

Also modifies excludedPackages when on Windows to replace Unix path
separators with Windows path separators (which are escaped because it is
treated as a regular expression). Modifies tests to remove unnecessary
escaping of Unix separators that results in an invalid regex due to this
replacement on Windows.

Note that two tests currently prevent CI from working on Windows:
coverage-off and preserve-set. The reason for these failures on Windows
is not clear, but these changes at least allow the common uses to work
on Windows.
  • Loading branch information
stevedlawrence authored and ckipp01 committed Oct 20, 2022
1 parent 98c69fb commit 8ba258c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ on:

jobs:
test:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
java: [ '8', '17' ]
os: [ 'ubuntu-latest' ]

steps:
- name: checkout the repo
Expand Down
10 changes: 9 additions & 1 deletion src/main/scala/scoverage/ScoverageSbtPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scoverage

import sbt.Keys._
import sbt._
import sbt.internal.util.Util.isWindows
import sbt.plugins.JvmPlugin
import scoverage.reporter.CoberturaXmlWriter
import scoverage.domain.Constants
Expand Down Expand Up @@ -147,7 +148,7 @@ object ScoverageSbtPlugin extends AutoPlugin {

Seq(
Some(
s"-Xplugin:${pluginPaths.mkString(":")}"
s"-Xplugin:${pluginPaths.mkString(java.io.File.pathSeparator)}"
),
Some(
s"-P:scoverage:dataDir:${coverageDataDir.value.getAbsolutePath}/scoverage-data"
Expand All @@ -160,6 +161,13 @@ object ScoverageSbtPlugin extends AutoPlugin {
.map(v => s"-P:scoverage:excludedPackages:$v"),
Option(coverageExcludedFiles.value.trim)
.filter(_.nonEmpty)
.map(v =>
// On windows, replace unix file separators with windows file
// separators. Note that we need to replace / with \\ because
// the plugin treats this string as a regular expression and
// backslashes must be escaped in regular expressions.
if (isWindows) v.replace("/", """\\""") else v
)
.map(v => s"-P:scoverage:excludedFiles:$v"),
Some("-P:scoverage:reportTestName"),
// rangepos is broken in some releases of scala so option to turn it off
Expand Down
2 changes: 1 addition & 1 deletion src/sbt-test/scoverage/coverage-excluded-files/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ scalaVersion := "2.13.6"

libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test

coverageExcludedFiles := ".*\\/two\\/GoodCoverage"
coverageExcludedFiles := ".*/two/GoodCoverage"

resolvers ++= {
if (sys.props.get("plugin.version").exists(_.endsWith("-SNAPSHOT")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ scalaVersion := "3.2.0-RC1"

libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test

coverageExcludedFiles := ".*\\/two\\/GoodCoverage"
coverageExcludedFiles := ".*/two/GoodCoverage"

resolvers ++= {
if (sys.props.get("plugin.version").exists(_.endsWith("-SNAPSHOT")))
Expand Down

0 comments on commit 8ba258c

Please sign in to comment.