Skip to content

Commit 7204f97

Browse files
Bug Fix: sbt-coveralls: Issue 199: Use relative paths in cobertura.xml file (#497)
* Make the paths in cobertura.xml relative. * (Re)Format * Make the windows build work.
1 parent f59c3a0 commit 7204f97

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

reporter/src/main/scala/scoverage/reporter/CoberturaXmlWriter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class CoberturaXmlWriter(
5151

5252
def klass(klass: MeasuredClass): Node = {
5353
<class name={klass.fullClassName}
54-
filename={klass.source}
54+
filename={relativeSource(klass.source)}
5555
line-rate={DoubleFormat.twoFractionDigits(klass.statementCoverage)}
5656
branch-rate={DoubleFormat.twoFractionDigits(klass.branchCoverage)}
5757
complexity="0">

reporter/src/main/scala/scoverage/reporter/ScoverageXmlWriter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class ScoverageXmlWriter(
9999

100100
private def klass(klass: MeasuredClass): Node = {
101101
<class name={klass.fullClassName}
102-
filename={klass.source}
102+
filename={relativeSource(klass.source)}
103103
statement-count={klass.statementCount.toString}
104104
statements-invoked={klass.invokedStatementCount.toString}
105105
statement-rate={klass.statementCoverageFormatted}

reporter/src/test/scala/scoverage/reporter/CoberturaXmlWriterTest.scala

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,84 @@ class CoberturaXmlWriterTest extends FunSuite {
3434
private def canonicalPath(fileName: String) =
3535
new File(sourceRoot, fileName).getCanonicalPath
3636

37+
test("cobertura output has relative file path") {
38+
39+
val dir = tempDir()
40+
41+
val coverage = Coverage()
42+
coverage.add(
43+
Statement(
44+
Location(
45+
"com.sksamuel.scoverage",
46+
"A",
47+
"com.sksamuel.scoverage.A",
48+
ClassType.Object,
49+
"create",
50+
canonicalPath("a.scala")
51+
),
52+
1,
53+
2,
54+
3,
55+
12,
56+
"",
57+
"",
58+
"",
59+
false,
60+
3
61+
)
62+
)
63+
coverage.add(
64+
Statement(
65+
Location(
66+
"com.sksamuel.scoverage.A",
67+
"B",
68+
"com.sksamuel.scoverage.A.B",
69+
ClassType.Object,
70+
"create",
71+
canonicalPath("a/b.scala")
72+
),
73+
2,
74+
2,
75+
3,
76+
12,
77+
"",
78+
"",
79+
"",
80+
false,
81+
3
82+
)
83+
)
84+
85+
val writer = new CoberturaXmlWriter(sourceRoot, dir, None)
86+
writer.write(coverage)
87+
88+
// Needed to acount for https://github.com/scala/scala-xml/pull/177
89+
val customXML: XMLLoader[Elem] = XML.withSAXParser {
90+
val factory = SAXParserFactory.newInstance()
91+
factory.setFeature(
92+
"http://apache.org/xml/features/nonvalidating/load-external-dtd",
93+
false
94+
)
95+
factory.newSAXParser()
96+
}
97+
98+
val xml = customXML.loadFile(fileIn(dir))
99+
100+
assertEquals(
101+
((xml \\ "coverage" \ "packages" \ "package" \ "classes" \ "class")(
102+
0
103+
) \ "@filename").text,
104+
new File("a.scala").getPath()
105+
)
106+
107+
assertEquals(
108+
((xml \\ "coverage" \ "packages" \ "package" \ "classes" \ "class")(
109+
1
110+
) \ "@filename").text,
111+
new File("a", "b.scala").getPath()
112+
)
113+
}
114+
37115
test("cobertura output validates") {
38116

39117
val dir = tempDir()

0 commit comments

Comments
 (0)