Skip to content

Commit 6d1afc9

Browse files
rewrote PathUtil to work with file.getParentFile
Justification: relying on getParentFile gets the path splitting working under windows and unix - adapted the tests to the new behavior - added a windows / unix switch in the tests to get them working on both systems PathUtils now converts: - the empty path to an empty List - an absolute path to a list folders (not including the drive name under windows) - a relative path to a list of folders
1 parent 8b78a99 commit 6d1afc9

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

plugin/src/main/scala/com/buransky/plugins/scoverage/util/PathUtil.scala

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,24 @@
2020
package com.buransky.plugins.scoverage.util
2121

2222
import java.io.File
23+
import scala.Iterator
2324
/**
2425
* File path helper.
2526
*
2627
* @author Rado Buransky
2728
*/
2829
object PathUtil {
29-
def splitPath(filePath: String, separator: String = File.separator): List[String] =
30-
filePath.split(separator.replaceAllLiterally("\\", "\\\\")).toList match {
31-
case "" :: tail if tail.nonEmpty => separator :: tail
32-
case other => other
33-
}
34-
}
30+
31+
def splitPath(filePath: String): List[String] = {
32+
new FileParentIterator(new File(filePath)).toList.reverse
33+
}
34+
35+
class FileParentIterator(private var f: File) extends Iterator[String] {
36+
def hasNext: Boolean = f != null && !f.getName().isEmpty()
37+
def next(): String = {
38+
val name = f.getName()
39+
f = f.getParentFile
40+
name
41+
}
42+
}
43+
}

plugin/src/test/scala/com/buransky/plugins/scoverage/util/PathUtilSpec.scala

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,30 @@ import org.junit.runner.RunWith
2424
import org.scalatest.junit.JUnitRunner
2525

2626
@RunWith(classOf[JUnitRunner])
27-
class UnixPathUtilSpec extends ParamPathUtilSpec("Unix", "/")
28-
29-
@RunWith(classOf[JUnitRunner])
30-
class WindowsPathUtilSpec extends ParamPathUtilSpec("Windows", "\\")
31-
32-
abstract class ParamPathUtilSpec(osName: String, separator: String) extends FlatSpec with Matchers {
27+
class PathUtilSpec extends FlatSpec with Matchers {
28+
29+
val osName = System.getProperty("os.name")
30+
val separator = System.getProperty("file.separator")
31+
3332
behavior of s"splitPath for $osName"
34-
35-
it should "work for empty path" in {
36-
PathUtil.splitPath("", separator) should equal(List(""))
33+
34+
it should "ignore the empty path" in {
35+
PathUtil.splitPath("") should equal(List.empty[String])
3736
}
3837

39-
it should "work with separator at the beginning" in {
40-
PathUtil.splitPath(s"${separator}a", separator) should equal(List(separator, "a"))
38+
it should "ignore a separator at the beginning" in {
39+
PathUtil.splitPath(s"${separator}a") should equal(List("a"))
4140
}
4241

4342
it should "work with separator in the middle" in {
44-
PathUtil.splitPath(s"a${separator}b", separator) should equal(List("a", "b"))
43+
PathUtil.splitPath(s"a${separator}b") should equal(List("a", "b"))
44+
}
45+
46+
it should "work with an OS dependent absolute path" in {
47+
if (osName.startsWith("Windows")) {
48+
PathUtil.splitPath("C:\\test\\2") should equal(List("test", "2"))
49+
} else {
50+
PathUtil.splitPath("/test/2") should equal(List("test", "2"))
51+
}
4552
}
4653
}

0 commit comments

Comments
 (0)