Skip to content

Commit

Permalink
Use FileSystem.hasPath for absolute paths
Browse files Browse the repository at this point in the history
  • Loading branch information
RadoBuransky committed May 3, 2015
1 parent 809163c commit a6d2a83
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 25 deletions.
2 changes: 1 addition & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</parent>

<artifactId>sonar-scoverage-plugin</artifactId>
<version>5.1.0-SNAPSHOT</version>
<version>5.1.1-SNAPSHOT</version>
<packaging>sonar-plugin</packaging>
<name>Sonar Scoverage Plugin</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package com.buransky.plugins.scoverage.sensor

import java.io

import com.buransky.plugins.scoverage.language.Scala
import com.buransky.plugins.scoverage.measure.ScalaMetrics
import com.buransky.plugins.scoverage.util.LogUtil
Expand Down Expand Up @@ -143,25 +145,31 @@ class ScoverageSensor(settings: Settings, pathResolver: PathResolver, fileSystem
}

private def processFile(fileCoverage: FileStatementCoverage, context: SensorContext, directory: String) {
val relativePath = appendFilePath(directory, fileCoverage.name)

val path = appendFilePath(directory, fileCoverage.name)
val p = fileSystem.predicates()
val files = fileSystem.inputFiles(p.and(p.matchesPathPattern("**/" + relativePath),
p.hasLanguage(scala.getKey), p.hasType(InputFile.Type.MAIN)))

val pathPredicate = if (new io.File(path).isAbsolute) p.hasAbsolutePath(path) else p.matchesPathPattern("**/" + path)
val files = fileSystem.inputFiles(p.and(
pathPredicate,
p.hasLanguage(scala.getKey),
p.hasType(InputFile.Type.MAIN)))

files.headOption match {
case Some(file) => {
//val scalaSourceFile = new ScalaFile(file.relativePath(), scala)
case Some(file) =>
val scalaSourceFile = File.create(file.relativePath())

// Save measures
saveMeasures(context, scalaSourceFile, fileCoverage)

// Save line coverage. This is needed just for source code highlighting.
saveLineCoverage(fileCoverage.statements, scalaSourceFile, context)
}

case None => log.warn(s"File not found in file system! [$directory, ${fileCoverage.name}]")
case None => {
fileSystem.inputFiles(p.all()).foreach { inputFile =>
log.debug(inputFile.absolutePath())
}
log.warn(s"File not found in file system! [$pathPredicate]")
}
}
}

Expand Down Expand Up @@ -210,7 +218,12 @@ class ScoverageSensor(settings: Settings, pathResolver: PathResolver, fileSystem
new Measure(ScalaMetrics.coveredStatements, coveredStatements);

private def appendFilePath(src: String, name: String) = {
val result = if (!src.isEmpty) src + java.io.File.separator else ""
val result = src match {
case java.io.File.separator => java.io.File.separator
case empty if empty.isEmpty => ""
case other => other + java.io.File.separator
}

result + name
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ import java.io.File
*/
object PathUtil {
def splitPath(filePath: String, separator: String = File.separator): List[String] =
filePath.split(separator.replaceAllLiterally("\\", "\\\\")).toList
filePath.split(separator.replaceAllLiterally("\\", "\\\\")).toList match {
case "" :: tail if tail.nonEmpty => separator :: tail
case other => other
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@
*/
package com.buransky.plugins.scoverage.sensor

import java.io.File
import java.util

import com.buransky.plugins.scoverage.language.Scala
import com.buransky.plugins.scoverage.{ProjectStatementCoverage, ScoverageReportParser}
import com.buransky.plugins.scoverage.{FileStatementCoverage, DirectoryStatementCoverage, ProjectStatementCoverage, ScoverageReportParser}
import org.junit.runner.RunWith
import org.mockito.Mockito._
import org.scalatest.junit.JUnitRunner
import org.scalatest.mock.MockitoSugar
import org.scalatest.{FlatSpec, Matchers}
import org.sonar.api.batch.fs.FileSystem
import org.sonar.api.batch.fs.{FilePredicate, FilePredicates, FileSystem}
import org.sonar.api.config.Settings
import org.sonar.api.resources.Project
import org.sonar.api.resources.Project.AnalysisType
Expand Down Expand Up @@ -75,19 +76,35 @@ class ScoverageSensorSpec extends FlatSpec with Matchers with MockitoSugar {
// Setup
val pathToScoverageReport = "#path-to-scoverage-report#"
val reportAbsolutePath = "#report-absolute-path#"
val projectStatementCoverage = ProjectStatementCoverage("project-name", Nil)
val projectStatementCoverage =
ProjectStatementCoverage("project-name", List(
DirectoryStatementCoverage(File.separator, List(
DirectoryStatementCoverage("home", List(
FileStatementCoverage("a.scala", 3, 2, Nil)
))
)),
DirectoryStatementCoverage("x", List(
FileStatementCoverage("b.scala", 1, 0, Nil)
))
))
val reportFile = mock[java.io.File]
val moduleBaseDir = mock[java.io.File]
val filePredicates = mock[FilePredicates]
when(reportFile.exists).thenReturn(true)
when(reportFile.isFile).thenReturn(true)
when(reportFile.getAbsolutePath).thenReturn(reportAbsolutePath)
when(settings.getString(SCOVERAGE_REPORT_PATH_PROPERTY)).thenReturn(pathToScoverageReport)
when(fileSystem.baseDir).thenReturn(moduleBaseDir)
when(fileSystem.predicates).thenReturn(filePredicates)
when(fileSystem.inputFiles(org.mockito.Matchers.any[FilePredicate]())).thenReturn(Nil)
when(pathResolver.relativeFile(moduleBaseDir, pathToScoverageReport)).thenReturn(reportFile)
when(scoverageReportParser.parse(reportAbsolutePath)).thenReturn(projectStatementCoverage)

// Execute
analyse(project, context)

verify(filePredicates).hasAbsolutePath("/home/a.scala")
verify(filePredicates).matchesPathPattern("**/x/b.scala")
}

class AnalyseScoverageSensorScope extends ScoverageSensorScope {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class UnixPathUtilSpec extends ParamPathUtilSpec("Unix", "/")
class WindowsPathUtilSpec extends ParamPathUtilSpec("Windows", "\\")

abstract class ParamPathUtilSpec(osName: String, separator: String) extends FlatSpec with Matchers {
behavior of s"splitPath for ${osName}"
behavior of s"splitPath for $osName"

it should "work for empty path" in {
PathUtil.splitPath("", separator) should equal(List(""))
}

it should "work with separator at the beginning" in {
PathUtil.splitPath(s"${separator}a", separator) should equal(List("", "a"))
PathUtil.splitPath(s"${separator}a", separator) should equal(List(separator, "a"))
}

it should "work with separator in the middle" in {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.buransky.plugins.scoverage.{ProjectStatementCoverage, FileStatementCo
class XmlScoverageReportConstructingParserSpec extends FlatSpec with Matchers {
behavior of "parse source"

ignore must "parse old broken Scoverage 0.95 file correctly" in {
it must "parse old broken Scoverage 0.95 file correctly" in {
assertReportFile(XmlReportFile1.scoverage095Data, 24.53)(assertScoverage095Data)
}

Expand All @@ -40,14 +40,19 @@ class XmlScoverageReportConstructingParserSpec extends FlatSpec with Matchers {
assert(projectCoverage.name === "")
assert(projectCoverage.children.size.toInt === 1)
projectCoverage.children.head match {
case mainClass: FileStatementCoverage =>
assert(mainClass.name == "/home/rado/workspace/sonar-test/src/main/scala/com/rr/test/sonar/MainClass.scala")
case other => fail(s"This is not a file statement coverage! [$other]")
case rootDir: DirectoryStatementCoverage =>
assert(rootDir.name == "/")
rootDir.children.head match {
case homeDir: DirectoryStatementCoverage =>
assert(homeDir.name == "home")
case other => fail(s"This is not a home statement coverage! [$other]")
}
case other => fail(s"This is not a directory statement coverage! [$other]")
}
}
}

ignore must "parse file1 correctly even without XML declaration" in {
it must "parse file1 correctly even without XML declaration" in {
assertReportFile(XmlReportFile1.dataWithoutDeclaration, 24.53)(assertScoverage095Data)
}

Expand All @@ -67,9 +72,9 @@ class XmlScoverageReportConstructingParserSpec extends FlatSpec with Matchers {

val projectChildren = projectCoverage.children.toList
projectChildren.length should equal(1)
projectChildren(0) shouldBe a [DirectoryStatementCoverage]
projectChildren.head shouldBe a [DirectoryStatementCoverage]

val aaa = projectChildren(0).asInstanceOf[DirectoryStatementCoverage]
val aaa = projectChildren.head.asInstanceOf[DirectoryStatementCoverage]
aaa.name should equal("aaa")
checkRate(24.53, aaa.rate)

Expand All @@ -82,8 +87,8 @@ class XmlScoverageReportConstructingParserSpec extends FlatSpec with Matchers {
errorCode.statementCount should equal (46)
errorCode.coveredStatementsCount should equal (13)

aaaChildren(0) shouldBe a [FileStatementCoverage]
val graph = aaaChildren(0).asInstanceOf[FileStatementCoverage]
aaaChildren.head shouldBe a [FileStatementCoverage]
val graph = aaaChildren.head.asInstanceOf[FileStatementCoverage]
graph.name should equal("Graph.scala")
graph.statementCount should equal (7)
graph.coveredStatementsCount should equal (0)
Expand Down

0 comments on commit a6d2a83

Please sign in to comment.