From 8ba258cff5f44973b74c45ff4792719d8e551279 Mon Sep 17 00:00:00 2001 From: Steve Lawrence Date: Thu, 20 Oct 2022 08:43:59 -0400 Subject: [PATCH] Allow sbt-scoverage to work on Windows 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. --- .github/workflows/ci.yml | 3 ++- src/main/scala/scoverage/ScoverageSbtPlugin.scala | 10 +++++++++- .../scoverage/coverage-excluded-files/build.sbt | 2 +- .../scoverage/scala3-coverage-excluded-files/build.sbt | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 294414cb..ed16eedd 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/src/main/scala/scoverage/ScoverageSbtPlugin.scala b/src/main/scala/scoverage/ScoverageSbtPlugin.scala index 57ca108c..d59dbf91 100644 --- a/src/main/scala/scoverage/ScoverageSbtPlugin.scala +++ b/src/main/scala/scoverage/ScoverageSbtPlugin.scala @@ -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 @@ -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" @@ -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 diff --git a/src/sbt-test/scoverage/coverage-excluded-files/build.sbt b/src/sbt-test/scoverage/coverage-excluded-files/build.sbt index 22856a91..4fcc343e 100644 --- a/src/sbt-test/scoverage/coverage-excluded-files/build.sbt +++ b/src/sbt-test/scoverage/coverage-excluded-files/build.sbt @@ -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"))) diff --git a/src/sbt-test/scoverage/scala3-coverage-excluded-files/build.sbt b/src/sbt-test/scoverage/scala3-coverage-excluded-files/build.sbt index b6fed517..939f7489 100644 --- a/src/sbt-test/scoverage/scala3-coverage-excluded-files/build.sbt +++ b/src/sbt-test/scoverage/scala3-coverage-excluded-files/build.sbt @@ -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")))