Skip to content

Commit

Permalink
Fix windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
mdedetrich committed Sep 16, 2024
1 parent 3cb10c8 commit db1978c
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 17 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ on:

jobs:
scala:
runs-on: ubuntu-latest
strategy:
fail-fast: false # remove when PR is finished, just to make sure we don't make regression
matrix:
JDK: [ 8, 17 ]
os:
- ubuntu-latest
- windows-latest
runs-on: ${{ matrix.os }}

steps:
- name: Ignore line ending differences in git
if: contains(runner.os, 'windows')
shell: bash
run: git config --global core.autocrlf false

- name: checkout the repo
uses: actions/checkout@v4
with:
Expand Down
36 changes: 32 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,47 @@ name := "sbt-coveralls"

import sbt.ScriptedPlugin.autoImport.scriptedLaunchOpts

import scala.sys.process._
import java.io.File
import scala.sys.process.*
lazy val generateXMLFiles =
taskKey[Unit]("Generate XML files (for test)")
generateXMLFiles := {
val log = streams.value.log
s"./src/test/resources/generate.sh" ! log
val dir = (Test / resourceDirectory).value
val pwd = (run / baseDirectory).value.absolutePath

val template = if (System.getProperty("os.name").startsWith("Windows"))
".xml.windows.template"
else
".xml.template"

dir.listFiles { (_, name) => name.endsWith(template) }.foreach {
templateFile =>
val newFile = dir / templateFile.getName.replace(template, ".xml")
val content = IO.read(templateFile)
IO.write(newFile, content.replace("{{PWD}}", pwd))
}
}

lazy val prepareScripted =
taskKey[Unit]("Update .git files to make scripted work")
prepareScripted := {
val log = streams.value.log
s"./src/sbt-test/prepare.sh" ! log
val pwd = (run / baseDirectory).value.absolutePath

val submodules = "git submodule status" !! log
val submodulePaths = submodules.split('\n').map{x =>
x.split(" ")(2)
}

submodulePaths.foreach{ subModulePath =>
val path = file(pwd) / ".git" / "modules" / subModulePath
val pathFixedForWindows = if (System.getProperty("os.name").startsWith("Windows"))
path.absolutePath.replace(File.separator, "/") // Git under Windows uses / for path separator
else
path.absolutePath
IO.delete(file(subModulePath) / ".git")
IO.write(file(subModulePath) / ".git", s"gitdir: $pathFixedForWindows")
}
}

inThisBuild(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class CoberturaMultiSourceReader(

protected def lineCoverage(sourceFile: String) = {
val filenamePath =
splitPath(new File(sourceFile))._2.replace(File.separator, "/")
splitPath(new File(sourceFile))._2

lineCoverageMap(filenamePath)
}
Expand All @@ -130,9 +130,7 @@ class CoberturaMultiSourceReader(
val lineHitMap = lineCoverage(source)
val fullLineHit = (0 until lineCount).map(i => lineHitMap.get(i + 1))

val sourceNormalized = source.replace(File.separator, "/")

SourceFileReport(sourceNormalized, fullLineHit.toList)
SourceFileReport(source, fullLineHit.toList)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ class CoverallPayloadWriter(
}

def addSourceFile(report: SourceFileReport) = {
val repoRootDirStr =
repoRootDir.getCanonicalPath.replace(File.separator, "/") + "/"
val repoRootDirStr = repoRootDir.getCanonicalPath + File.separator

// create a name relative to the project root (rather than the module root)
// this is needed so that coveralls can find the file in git.
Expand Down
4 changes: 0 additions & 4 deletions src/test/resources/generate.sh

This file was deleted.

43 changes: 43 additions & 0 deletions src/test/resources/test_cobertura.xml.windows.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0"?>
<!DOCTYPE coverage SYSTEM "http://localhost:1/xml/coverage-04.dtd">
<coverage line-rate="0.87">
<sources>
<source>--source</source>
<source>{{PWD}}\src\test\resources\projectA\arc\main\scala</source>
<source>{{PWD}}\src\test\resources\projectA\arc\main\scala-2.12</source>
<source>{{PWD}}\src\test\resources\projectB\arc\main\scala</source>
</sources>
<packages>
<package line-rate="0.87" name="org.scoverage.coveralls">
<classes>
<class line-rate="0.87" name="TestSourceFile" filename="bar\foo\TestSourceFile.scala">
<methods/>
<lines>
<line number="4" hits="1"/>
<line number="5" hits="1"/>
<line number="6" hits="2"/>
</lines>
</class>
<class line-rate="0.87" name="TestSourceFile" filename="bar\foo\TestSourceFile.scala">
<methods/>
<lines>
<line number="9" hits="1"/>
<line number="10" hits="1"/>
</lines>
</class>
<class line-rate="0" name="TestSourceScala212" filename="bar\foo\TestSourceScala212.scala">
<methods/>
<lines/>
</class>
<class line-rate="0.87" name="TestSourceFile" filename="foo\TestSourceFile.scala">
<methods/>
<lines>
<line number="3" hits="1"/>
<line number="4" hits="1"/>
<line number="5" hits="1"/>
</lines>
</class>
</classes>
</package>
</packages>
</coverage>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
corrupted file content
43 changes: 43 additions & 0 deletions src/test/resources/test_cobertura_dtd.xml.windows.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0"?>
<!DOCTYPE coverage SYSTEM "http://localhost:1/xml/coverage-04.dtd">
<coverage line-rate="0.87">
<sources>
<source>--source</source>
<source>{{PWD}}\src\test\resources\projectA\src\main\scala</source>
<source>{{PWD}}\src\test\resources\projectA\src\main\scala-2.12</source>
<source>{{PWD}}\src\test\resources\projectB\src\main\scala</source>
</sources>
<packages>
<package line-rate="0.87" name="org.scoverage.coveralls">
<classes>
<class line-rate="0.87" name="TestSourceFile" filename="bar\foo\TestSourceFile.scala">
<methods/>
<lines>
<line number="4" hits="1"/>
<line number="5" hits="1"/>
<line number="6" hits="2"/>
</lines>
</class>
<class line-rate="0.87" name="TestSourceFile" filename="bar\foo\TestSourceFile.scala">
<methods/>
<lines>
<line number="9" hits="1"/>
<line number="10" hits="1"/>
</lines>
</class>
<class line-rate="0" name="TestSourceScala212" filename="bar\foo\TestSourceScala212.scala">
<methods/>
<lines/>
</class>
<class line-rate="0.87" name="TestSourceFile" filename="foo\TestSourceFile.scala">
<methods/>
<lines>
<line number="3" hits="1"/>
<line number="4" hits="1"/>
<line number="5" hits="1"/>
</lines>
</class>
</classes>
</package>
</packages>
</coverage>
41 changes: 41 additions & 0 deletions src/test/resources/test_cobertura_multisource.xml.windows.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<coverage line-rate="0.87">
<sources>
<source>--source</source>
<source>{{PWD}}\src\test\resources\projectA\src\main\scala</source>
<source>{{PWD}}\src\test\resources\projectA\src\main\scala-2.12</source>
<source>{{PWD}}\src\test\resources\projectB\src\main\scala</source>
</sources>
<packages>
<package line-rate="0.87" name="org.scoverage.coveralls">
<classes>
<class line-rate="0.87" name="TestSourceFile" filename="bar\foo\TestSourceFile.scala">
<methods/>
<lines>
<line number="4" hits="1"/>
<line number="5" hits="1"/>
<line number="6" hits="2"/>
</lines>
</class>
<class line-rate="0.87" name="TestSourceFile" filename="bar\foo\TestSourceFile.scala">
<methods/>
<lines>
<line number="9" hits="1"/>
<line number="10" hits="1"/>
</lines>
</class>
<class line-rate="0" name="TestSourceScala212" filename="bar\foo\TestSourceScala212.scala">
<methods/>
<lines/>
</class>
<class line-rate="0.87" name="TestSourceFile" filename="foo\TestSourceFile.scala">
<methods/>
<lines>
<line number="3" hits="1"/>
<line number="4" hits="1"/>
<line number="5" hits="1"/>
</lines>
</class>
</classes>
</package>
</packages>
</coverage>
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,17 @@ class CoverallPayloadWriterTest
)
payloadWriter.flush()

writer.toString should equal(
"""{"name":"./src/test/resources/projectA/src/main/scala/bar/foo/TestSourceFile.scala","source_digest":"B77361233B09D69968F8C62491A5085F","coverage":[1,null,2]}"""
val output = writer.toString

val separator = if (System.getProperty("os.name").startsWith("Windows"))
s"""${File.separator}\\""" // Backwards slash is a special character in JSON so it needs to be escaped
else
File.separator

val name = List(".","src","test","resources","projectA","src","main","scala","bar","foo","TestSourceFile.scala")
.mkString(separator)
output should equal(
s"""{"name":"$name","source_digest":"B77361233B09D69968F8C62491A5085F","coverage":[1,null,2]}"""
)
}

Expand Down

0 comments on commit db1978c

Please sign in to comment.