Skip to content

Allow sbt-scoverage to work on windows #465

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
Oct 20, 2022
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
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"
Copy link
Member

Choose a reason for hiding this comment

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

How come this changes?

Copy link
Contributor Author

@stevedlawrence stevedlawrence Oct 20, 2022

Choose a reason for hiding this comment

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

These escapes aren't needed on linux, and cause issues on windows.

Forward slashes aren't a special character in regular expressions so they don't need to be escaped. The only reason the test works now is because this string becomes the regex .*\/two\/GoodCoverage, and while you don't need to escape forward slashes, an escaped forward slash is just a forward slash and works as expected.

However, on windows our excludedFiles regex needs to look for a backslash instead of a forward slash due to windows using backslashes for paths. However, unlike forward slashes, backslashes do need to be escaped in regular expressions. So this commit adds a change to replace / with \\ (i.e. replace a single forward slash with two literal backslashes).

If we do this replacement, with the original regex in the test, the regex on windows becomes .*\\\two\\\GoodCoverage with three literal backslashes. This means the regex contains an escaped backslash followed by an escaped t, and another escaped backslash followed by an escaped G. The escaped t and G break the regex.

With these changes in the test and the new string replace stuff, on linux the regex is .*/two/GoodCoverage, and on windows the regex is .*\\two\\GoodCoverage.

Copy link
Member

Choose a reason for hiding this comment

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

Gotcha thanks for the explanation.

coverageExcludedFiles := ".*/two/GoodCoverage"

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