Skip to content

Commit

Permalink
Merge pull request #114 from gslowikowski/reporting-base-class
Browse files Browse the repository at this point in the history
#109 introduced...
  • Loading branch information
sksamuel committed Mar 29, 2015
2 parents adacc60 + caf662e commit 274d5e3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 40 deletions.
19 changes: 0 additions & 19 deletions scalac-scoverage-plugin/src/main/scala/scoverage/IOUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,4 @@ object IOUtils {
acc
}

/**
* Converts absolute path to relative one if any of the source directories is it's parent.
* If there is no parent directory, the path is returned unchanged (absolute).
*
* @param src absolute file path in canonical form
* @param sourcePaths absolute source paths in canonical form WITH trailing file separators
*/
def relativeSource(src: String, sourcePaths: Seq[String]): String = {
val sourceRoot: Option[String] = sourcePaths.find(
sourcePath => src.startsWith(sourcePath)
)
sourceRoot match {
case Some(path: String) => src.replace(path, "")
case _ =>
val fmtSourcePaths: String = sourcePaths.mkString("'", "', '", "'")
throw new RuntimeException(s"No source root found for '$src' (source roots: $fmtSourcePaths)");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package scoverage.report

import java.io.File

class BaseReportWriter(sourceDirectories: Seq[File], outputDir: File) {

// Source paths in canonical form WITH trailing file separator
private val formattedSourcePaths: Seq[String] = sourceDirectories filter ( _.isDirectory ) map ( _.getCanonicalPath + File.separator )

/**
* Converts absolute path to relative one if any of the source directories is it's parent.
* If there is no parent directory, the path is returned unchanged (absolute).
*
* @param src absolute file path in canonical form
*/
def relativeSource(src: String): String = relativeSource(src, formattedSourcePaths)

private def relativeSource(src: String, sourcePaths: Seq[String]): String = {
val sourceRoot: Option[String] = sourcePaths.find(
sourcePath => src.startsWith(sourcePath)
)
sourceRoot match {
case Some(path: String) => src.replace(path, "")
case _ =>
val fmtSourcePaths: String = sourcePaths.mkString("'", "', '", "'")
throw new RuntimeException(s"No source root found for '$src' (source roots: $fmtSourcePaths)");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ import scoverage._
import scala.xml.{Node, PrettyPrinter}

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

def this (baseDir: File, outputDir: File) {
this(Seq(baseDir), outputDir);
}

// Source paths in canonical form WITH trailing file separator
val formattedSourcePaths: Seq[String] = sourceDirectories filter ( _.isDirectory ) map ( _.getCanonicalPath + File.separator )

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

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

private def relativeSource(src: String): String = IOUtils.relativeSource(src, formattedSourcePaths)

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ import scoverage._
import scala.xml.Node

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

def this (sourceDirectory: File, outputDir: File) {
this(Seq(sourceDirectory), outputDir);
}

// Source paths in canonical form WITH trailing file separator
val formattedSourcePaths: Seq[String] = sourceDirectories filter ( _.isDirectory ) map ( _.getCanonicalPath + File.separator )

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

private def relativeSource(src: String): String = IOUtils.relativeSource(src, formattedSourcePaths)

}

Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ package scoverage.report

import java.io.File

import _root_.scoverage._
import scoverage._

import scala.xml.{Node, PrettyPrinter}

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

def this (sourceDir: File, outputDir: File, debug: Boolean) {
this(Seq(sourceDir), outputDir, debug);
}

// Source paths in canonical form WITH trailing file separator
val formattedSourcePaths: Seq[String] = sourceDirectories filter ( _.isDirectory ) map ( _.getCanonicalPath + File.separator )

def write(coverage: Coverage): Unit = {
val file = IOUtils.reportFile(outputDir, debug)
IOUtils.writeToFile(file, new PrettyPrinter(120, 4).format(xml(coverage)))
Expand Down Expand Up @@ -103,7 +100,4 @@ class ScoverageXmlWriter(sourceDirectories: Seq[File], outputDir: File, debug: B
</package>
}

private def relativeSource(src: String): String = IOUtils.relativeSource(src, formattedSourcePaths)

}

0 comments on commit 274d5e3

Please sign in to comment.