Skip to content

Commit

Permalink
Bug Fix: sbt-coveralls: Issue 199: Use relative paths in cobertura.xm…
Browse files Browse the repository at this point in the history
…l file (#497)

* Make the paths in cobertura.xml relative.

* (Re)Format

* Make the windows build work.
  • Loading branch information
rolandtritsch authored Sep 10, 2022
1 parent f59c3a0 commit 7204f97
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class CoberturaXmlWriter(

def klass(klass: MeasuredClass): Node = {
<class name={klass.fullClassName}
filename={klass.source}
filename={relativeSource(klass.source)}
line-rate={DoubleFormat.twoFractionDigits(klass.statementCoverage)}
branch-rate={DoubleFormat.twoFractionDigits(klass.branchCoverage)}
complexity="0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ScoverageXmlWriter(

private def klass(klass: MeasuredClass): Node = {
<class name={klass.fullClassName}
filename={klass.source}
filename={relativeSource(klass.source)}
statement-count={klass.statementCount.toString}
statements-invoked={klass.invokedStatementCount.toString}
statement-rate={klass.statementCoverageFormatted}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,84 @@ class CoberturaXmlWriterTest extends FunSuite {
private def canonicalPath(fileName: String) =
new File(sourceRoot, fileName).getCanonicalPath

test("cobertura output has relative file path") {

val dir = tempDir()

val coverage = Coverage()
coverage.add(
Statement(
Location(
"com.sksamuel.scoverage",
"A",
"com.sksamuel.scoverage.A",
ClassType.Object,
"create",
canonicalPath("a.scala")
),
1,
2,
3,
12,
"",
"",
"",
false,
3
)
)
coverage.add(
Statement(
Location(
"com.sksamuel.scoverage.A",
"B",
"com.sksamuel.scoverage.A.B",
ClassType.Object,
"create",
canonicalPath("a/b.scala")
),
2,
2,
3,
12,
"",
"",
"",
false,
3
)
)

val writer = new CoberturaXmlWriter(sourceRoot, dir, None)
writer.write(coverage)

// Needed to acount for https://github.com/scala/scala-xml/pull/177
val customXML: XMLLoader[Elem] = XML.withSAXParser {
val factory = SAXParserFactory.newInstance()
factory.setFeature(
"http://apache.org/xml/features/nonvalidating/load-external-dtd",
false
)
factory.newSAXParser()
}

val xml = customXML.loadFile(fileIn(dir))

assertEquals(
((xml \\ "coverage" \ "packages" \ "package" \ "classes" \ "class")(
0
) \ "@filename").text,
new File("a.scala").getPath()
)

assertEquals(
((xml \\ "coverage" \ "packages" \ "package" \ "classes" \ "class")(
1
) \ "@filename").text,
new File("a", "b.scala").getPath()
)
}

test("cobertura output validates") {

val dir = tempDir()
Expand Down

0 comments on commit 7204f97

Please sign in to comment.