Skip to content

Commit caf662e

Browse files
committed
#109 introduced code redundance in report writer classes. Base class created and common code moved there. "IOUtils.relativeSource" moved there too, because it's reports specific and works on paths as strings, not real files.
1 parent 16dcfbd commit caf662e

File tree

5 files changed

+34
-40
lines changed

5 files changed

+34
-40
lines changed

scalac-scoverage-plugin/src/main/scala/scoverage/IOUtils.scala

-19
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,4 @@ object IOUtils {
8282
acc
8383
}
8484

85-
/**
86-
* Converts absolute path to relative one if any of the source directories is it's parent.
87-
* If there is no parent directory, the path is returned unchanged (absolute).
88-
*
89-
* @param src absolute file path in canonical form
90-
* @param sourcePaths absolute source paths in canonical form WITH trailing file separators
91-
*/
92-
def relativeSource(src: String, sourcePaths: Seq[String]): String = {
93-
val sourceRoot: Option[String] = sourcePaths.find(
94-
sourcePath => src.startsWith(sourcePath)
95-
)
96-
sourceRoot match {
97-
case Some(path: String) => src.replace(path, "")
98-
case _ =>
99-
val fmtSourcePaths: String = sourcePaths.mkString("'", "', '", "'")
100-
throw new RuntimeException(s"No source root found for '$src' (source roots: $fmtSourcePaths)");
101-
}
102-
}
103-
10485
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package scoverage.report
2+
3+
import java.io.File
4+
5+
class BaseReportWriter(sourceDirectories: Seq[File], outputDir: File) {
6+
7+
// Source paths in canonical form WITH trailing file separator
8+
private val formattedSourcePaths: Seq[String] = sourceDirectories filter ( _.isDirectory ) map ( _.getCanonicalPath + File.separator )
9+
10+
/**
11+
* Converts absolute path to relative one if any of the source directories is it's parent.
12+
* If there is no parent directory, the path is returned unchanged (absolute).
13+
*
14+
* @param src absolute file path in canonical form
15+
*/
16+
def relativeSource(src: String): String = relativeSource(src, formattedSourcePaths)
17+
18+
private def relativeSource(src: String, sourcePaths: Seq[String]): String = {
19+
val sourceRoot: Option[String] = sourcePaths.find(
20+
sourcePath => src.startsWith(sourcePath)
21+
)
22+
sourceRoot match {
23+
case Some(path: String) => src.replace(path, "")
24+
case _ =>
25+
val fmtSourcePaths: String = sourcePaths.mkString("'", "', '", "'")
26+
throw new RuntimeException(s"No source root found for '$src' (source roots: $fmtSourcePaths)");
27+
}
28+
}
29+
30+
}

scalac-scoverage-plugin/src/main/scala/scoverage/report/CoberturaXmlWriter.scala

+1-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ import scoverage._
77
import scala.xml.{Node, PrettyPrinter}
88

99
/** @author Stephen Samuel */
10-
class CoberturaXmlWriter(sourceDirectories: Seq[File], outputDir: File) {
10+
class CoberturaXmlWriter(sourceDirectories: Seq[File], outputDir: File) extends BaseReportWriter(sourceDirectories, outputDir) {
1111

1212
def this (baseDir: File, outputDir: File) {
1313
this(Seq(baseDir), outputDir);
1414
}
15-
16-
// Source paths in canonical form WITH trailing file separator
17-
val formattedSourcePaths: Seq[String] = sourceDirectories filter ( _.isDirectory ) map ( _.getCanonicalPath + File.separator )
1815

1916
def format(double: Double): String = "%.2f".format(double)
2017

@@ -95,6 +92,4 @@ class CoberturaXmlWriter(sourceDirectories: Seq[File], outputDir: File) {
9592
</coverage>
9693
}
9794

98-
private def relativeSource(src: String): String = IOUtils.relativeSource(src, formattedSourcePaths)
99-
10095
}

scalac-scoverage-plugin/src/main/scala/scoverage/report/ScoverageHtmlWriter.scala

+1-7
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ import scoverage._
88
import scala.xml.Node
99

1010
/** @author Stephen Samuel */
11-
class ScoverageHtmlWriter(sourceDirectories: Seq[File], outputDir: File) {
11+
class ScoverageHtmlWriter(sourceDirectories: Seq[File], outputDir: File) extends BaseReportWriter(sourceDirectories, outputDir) {
1212

1313
def this (sourceDirectory: File, outputDir: File) {
1414
this(Seq(sourceDirectory), outputDir);
1515
}
16-
17-
// Source paths in canonical form WITH trailing file separator
18-
val formattedSourcePaths: Seq[String] = sourceDirectories filter ( _.isDirectory ) map ( _.getCanonicalPath + File.separator )
1916

2017
def write(coverage: Coverage): Unit = {
2118
val indexFile = new File(outputDir.getAbsolutePath + "/index.html")
@@ -537,7 +534,4 @@ class ScoverageHtmlWriter(sourceDirectories: Seq[File], outputDir: File) {
537534
</script>
538535
}
539536

540-
private def relativeSource(src: String): String = IOUtils.relativeSource(src, formattedSourcePaths)
541-
542537
}
543-

scalac-scoverage-plugin/src/main/scala/scoverage/report/ScoverageXmlWriter.scala

+2-8
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@ package scoverage.report
22

33
import java.io.File
44

5-
import _root_.scoverage._
5+
import scoverage._
66

77
import scala.xml.{Node, PrettyPrinter}
88

99
/** @author Stephen Samuel */
10-
class ScoverageXmlWriter(sourceDirectories: Seq[File], outputDir: File, debug: Boolean) {
10+
class ScoverageXmlWriter(sourceDirectories: Seq[File], outputDir: File, debug: Boolean) extends BaseReportWriter(sourceDirectories, outputDir) {
1111

1212
def this (sourceDir: File, outputDir: File, debug: Boolean) {
1313
this(Seq(sourceDir), outputDir, debug);
1414
}
1515

16-
// Source paths in canonical form WITH trailing file separator
17-
val formattedSourcePaths: Seq[String] = sourceDirectories filter ( _.isDirectory ) map ( _.getCanonicalPath + File.separator )
18-
1916
def write(coverage: Coverage): Unit = {
2017
val file = IOUtils.reportFile(outputDir, debug)
2118
IOUtils.writeToFile(file, new PrettyPrinter(120, 4).format(xml(coverage)))
@@ -103,7 +100,4 @@ class ScoverageXmlWriter(sourceDirectories: Seq[File], outputDir: File, debug: B
103100
</package>
104101
}
105102

106-
private def relativeSource(src: String): String = IOUtils.relativeSource(src, formattedSourcePaths)
107-
108103
}
109-

0 commit comments

Comments
 (0)