Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
package com.buransky.plugins.scoverage.pathcleaner

import java.io.File

import org.apache.commons.io.FileUtils
import BruteForceSequenceMatcher._
import com.buransky.plugins.scoverage.util.PathUtil

import scala.collection.JavaConversions._
import org.sonar.api.utils.log.Loggers

import scala.annotation.tailrec

object BruteForceSequenceMatcher {

val extensions = Array[String]("java", "scala")
Expand All @@ -48,40 +52,53 @@ object BruteForceSequenceMatcher {
*/
class BruteForceSequenceMatcher(baseDir: File, sourcePath: String) extends PathSanitizer {

private val sourceDir = initSourceDir()
require(sourceDir.isAbsolute)
require(sourceDir.isDirectory)
private val sourceDirs = initSourceDir()

private val log = Loggers.get(classOf[BruteForceSequenceMatcher])
private val sourcePathLength = PathUtil.splitPath(sourceDir.getAbsolutePath).size
//private val sourcePathLength =
private val filesMap = initFilesMap()


def getSourceRelativePath(reportPath: PathSeq): Option[PathSeq] = {
// match with file system map of files
val relPathOption = for {
absPathCandidates <- filesMap.get(reportPath.last)
path <- absPathCandidates.find(absPath => absPath.endsWith(reportPath))
} yield path.drop(sourcePathLength)
getSourceRelativePathInner(filesMap, reportPath)
}

relPathOption
@tailrec
final def getSourceRelativePathInner(elements:Seq[(Int, Map[String, Seq[PathSeq]])], reportPath: PathSeq): Option[PathSeq]= {
elements match {
case Nil => None
case (sourcePathLength, head) :: rest =>
val relPathOption = for {
absPathCandidates <- head.get(reportPath.last)
path <- absPathCandidates.find(absPath => absPath.endsWith(reportPath))
} yield {
path.drop(sourcePathLength)
}
relPathOption match {
case None => getSourceRelativePathInner(rest, reportPath)
case v => v
}
}
}

// mock able helpers that allow us to remove the dependency to the real file system during tests

private[pathcleaner] def initSourceDir(): File = {
sourcePath.split(",").headOption.map { first =>
private[pathcleaner] def initSourceDir(): Seq[File] = {
sourcePath.split(",").map { first =>
val sourceDir = new File(baseDir, first)
require(sourceDir.isAbsolute)
require(sourceDir.isDirectory)
sourceDir
}.getOrElse(null)
}
}

private[pathcleaner] def initFilesMap(): Map[String, Seq[PathSeq]] = {
val srcFiles = FileUtils.iterateFiles(sourceDir, extensions, true)
val paths = srcFiles.map(file => PathUtil.splitPath(file.getAbsolutePath)).toSeq

// group them by filename, in case multiple files have the same name
paths.groupBy(path => path.last)
private[pathcleaner] def initFilesMap(): List[(Int, Map[String, Seq[PathSeq]])] = {
sourceDirs.map { sourceDir =>
val srcFiles = FileUtils.iterateFiles(sourceDir, extensions, true)
val paths = srcFiles.map(file => PathUtil.splitPath(file.getAbsolutePath)).toSeq
// group them by filename, in case multiple files have the same name
(PathUtil.splitPath(sourceDir.getAbsolutePath).size, paths.groupBy(path => path.last))
}.toList
}

}
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.File

import com.buransky.plugins.scoverage.language.Scala
import com.buransky.plugins.scoverage.measure.ScalaMetrics
import com.buransky.plugins.scoverage.pathcleaner.{BruteForceSequenceMatcher, PathSanitizer}
Expand Down Expand Up @@ -248,12 +250,27 @@ class ScoverageSensor(settings: Settings, pathResolver: PathResolver, fileSystem
new Measure(ScalaMetrics.coveredStatements, coveredStatements.toDouble, 0)

private def appendFilePath(src: String, name: String) = {
val result = src match {
def whyDoWeMakeThisAssumption(elems:List[String]):Option[String] = {
elems match {
case Nil => None
case elem :: rest =>
val f = new File(elem)
val n = new File(f, name)
n.exists match {
case true => Some(elem + java.io.File.separator + name)
case false => whyDoWeMakeThisAssumption(rest)
}
}
}
src match {
case java.io.File.separator => java.io.File.separator
case empty if empty.isEmpty => ""
case other => other + java.io.File.separator
case other =>
val omg = src.split(",").toList
whyDoWeMakeThisAssumption(omg) match {
case None => other + java.io.File.separator + name
case Some(real) => real
}
}

result + name
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import org.scalatest.FlatSpec
import com.buransky.plugins.scoverage.pathcleaner.BruteForceSequenceMatcher.PathSeq
import org.scalatest.Matchers
import java.io.File

import com.buransky.plugins.scoverage.util.PathUtil
import org.mockito.Mockito._

@RunWith(classOf[JUnitRunner])
Expand Down Expand Up @@ -111,7 +113,7 @@ class BruteForceSequenceMatcherSpec extends FlatSpec with Matchers with MockitoS
dir
}

override private[pathcleaner] def initSourceDir(): File = srcDir
override private[pathcleaner] def initFilesMap(): Map[String, Seq[PathSeq]] = filesMap
override private[pathcleaner] def initSourceDir(): Seq[File] = List(srcDir)
override private[pathcleaner] def initFilesMap(): List[(Int, Map[String, Seq[PathSeq]])] = List((PathUtil.splitPath(srcDir.getAbsolutePath).size, filesMap))
}
}