Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Improving the log messages in the ScoverageSensor #61

Merged
merged 1 commit into from May 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -21,7 +21,7 @@ package scoverage

import java.nio.file.{Path, Paths}

import com.mwz.sonar.scala.util.PathUtils._
import com.mwz.sonar.scala.util.PathUtils

import scala.xml.{Node, XML}

Expand All @@ -38,12 +38,12 @@ trait ScoverageReportParser extends ScoverageReportParserAPI {
scoverageFilename = Paths.get(classNode \@ "filename")
filename <- sourcePrefixes.collectFirst {
case prefix
if cwd
if PathUtils.cwd
.resolve(prefix)
.resolve(stripOutPrefix(prefix, scoverageFilename))
.resolve(PathUtils.stripOutPrefix(prefix, scoverageFilename))
.toFile
.exists =>
prefix.resolve(stripOutPrefix(prefix, scoverageFilename))
prefix.resolve(PathUtils.stripOutPrefix(prefix, scoverageFilename)).toString
}
classScoverage = extractScoverageFromNode(classNode)

Expand All @@ -62,7 +62,7 @@ trait ScoverageReportParser extends ScoverageReportParserAPI {
}

classCoverage = FileCoverage(classScoverage, linesCoverage)
} yield filename.toString -> classCoverage
} yield filename -> classCoverage

// merge the class coverages by filename
val files = classCoverages groupBy {
Expand Down
25 changes: 15 additions & 10 deletions src/main/scala/com/mwz/sonar/scala/scoverage/ScoverageSensor.scala
Expand Up @@ -54,38 +54,43 @@ private[scoverage] abstract class ScoverageSensorInternal extends Sensor {
/** Saves in SonarQube the scoverage information of a module */
override def execute(context: SensorContext): Unit = {
logger.info("[scoverage] Initializing the scoverage sensor.")
logger.debug(s"[scoverage] The current working directory (CWD) is: '${PathUtils.cwd}'")
val settings = context.config
val filesystem = context.fileSystem

val modulePath = getModuleBaseDirectory(filesystem)
val reportPath = modulePath.resolve(getScoverageReportPath(settings))
val sources = Scala.getSourcesPaths(settings)
val sourcePrefixes = sources.map(PathUtils.relativize(PathUtils.cwd, modulePath, _))
logger.debug(s"[scoverage] The source prefixes are: ${sourcePrefixes.mkString("[", ",", "]")}")

logger.info(s"[scoverage] Loading the scoverage report file: '$reportPath'.")
Try(scoverageReportParser.parse(reportPath, sourcePrefixes)) match {
case Success(moduleCoverage) =>
logger.info(s"[scoverage] Successfully loaded the scoverage report file: '$reportPath'.")

logger.debug(
"[scoverage] Saving the overall scoverage information about the module, " +
s"the statement coverage is ${moduleCoverage.moduleScoverage.statementCoverage}%."
logger.info(
"[scoverage] Successfully loaded the scoverage report file, " +
"proceeding to save the module scoverage information"
)
logger.debug(s"[scoverage] Module scoverage information: $moduleCoverage")

saveComponentScoverage(context, context.module(), moduleCoverage.moduleScoverage)

// save the coverage information of each file of the module
getModuleSourceFiles(filesystem).foreach { file =>
getModuleSourceFiles(filesystem) foreach { file =>
// toString returns the project relative path of the file
val filename = file.toString
logger.debug(s"[scoverage] Saving the scoverage information of the file: '$filename'")
moduleCoverage.filesCoverage.get(filename) match {
case Some(fileCoverage) =>
logger.debug(s"[scoverage] File scoverage information: $fileCoverage")

// save the file overall scoverage information
saveComponentScoverage(context, file, fileCoverage.fileScoverage)

// save the coverage of each line of the file
val coverage = context.newCoverage()
coverage.onFile(file)
fileCoverage.linesCoverage.foreach {
fileCoverage.linesCoverage foreach {
case (lineNum, hits) => coverage.lineHits(lineNum, hits)
}
coverage.save()
Expand All @@ -97,9 +102,9 @@ private[scoverage] abstract class ScoverageSensorInternal extends Sensor {
}
case Failure(ex) =>
logger.error(
s"""[scoverage] Aborting the scoverage sensor execution,
|cause: an error occurred while reading the scoverage report file: '$reportPath',
|the error was: ${ex.getMessage}.""".stripMargin
"[scoverage] Aborting the scoverage sensor execution, " +
s"cause: an error occurred while reading the scoverage report file: '$reportPath', " +
s"the error was: ${ex.getMessage}."
)
}
}
Expand Down