Skip to content

converting source path from absolute to relative #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 17 additions & 2 deletions scalac-scoverage-plugin/src/main/scala/scoverage/Serializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ object Serializer {
}

def serialize(coverage: Coverage, writer: Writer): Unit = {
// Used for getting the relative filepath for [stmt.location.sourcePath]
// instead of the canonical path. This is required to make it work with
// remoting or in a distributed environment.
def getRelativePath(filePath: String): String = {
val base = new File(".").getCanonicalPath + File.separator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filePath.replace(base, "")
}

def writeHeader(writer: Writer): Unit = {
writer.write(s"""# Coverage data, format version: 2.0
|# Statement data:
Expand All @@ -48,7 +56,7 @@ object Serializer {

def writeStatement(stmt: Statement, writer: Writer): Unit = {
writer.write(s"""${stmt.id}
|${stmt.location.sourcePath}
|${getRelativePath(stmt.location.sourcePath)}
|${stmt.location.packageName}
|${stmt.location.className}
|${stmt.location.classType}
Expand Down Expand Up @@ -79,6 +87,13 @@ object Serializer {
}

def deserialize(lines: Iterator[String]): Coverage = {
// To integrate it smoothly with rest of the report writers,
// it is necessary to again convert [sourcePath] into a
// canonical one.
def getAbsolutePath(filePath: String): String = {
(new File(filePath)).getCanonicalPath
}

def toStatement(lines: Iterator[String]): Statement = {
val id: Int = lines.next.toInt
val sourcePath = lines.next
Expand All @@ -87,7 +102,7 @@ object Serializer {
val classType = lines.next
val fullClassName = lines.next
val method = lines.next
val loc = Location(packageName, className, fullClassName, ClassType.fromString(classType), method, sourcePath)
val loc = Location(packageName, className, fullClassName, ClassType.fromString(classType), method, getAbsolutePath(sourcePath))
val start: Int = lines.next.toInt
val end: Int = lines.next.toInt
val lineNo: Int = lines.next.toInt
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scoverage

import java.io.StringWriter
import java.io.File

import org.scalatest.{OneInstancePerTest, FunSuite}

Expand Down Expand Up @@ -97,7 +98,7 @@ class SerializerTest extends FunSuite with OneInstancePerTest {
|\f
|""".stripMargin.split("\n").iterator
val statements = List(Statement(
Location("org.scoverage", "test", "org.scoverage.test", ClassType.Trait, "mymethod", "mypath"),
Location("org.scoverage", "test", "org.scoverage.test", ClassType.Trait, "mymethod", new File("mypath").getCanonicalPath),
14, 100, 200, 4, "def test : String", "test", "DefDef", true, 1
))
val coverage = Serializer.deserialize(input)
Expand Down