Skip to content

Commit

Permalink
Merge pull request #108 from dwijnand/relativize-relative
Browse files Browse the repository at this point in the history
Relativize relative paths
  • Loading branch information
eed3si9n authored Dec 19, 2017
2 parents 1bfd89a + 5901b4c commit 70aed88
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions io/src/main/scala/sbt/io/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,8 @@ object IO {
* If `file` or `base` are not absolute, they are first resolved against the current working directory.
*/
def relativize(base: File, file: File): Option[String] = {
val basePath = base.toPath
val filePath = file.toPath
val basePath = (if (base.isAbsolute) base else base.getCanonicalFile).toPath
val filePath = (if (file.isAbsolute) file else file.getCanonicalFile).toPath
if ((filePath startsWith basePath) || (filePath.normalize() startsWith basePath.normalize())) {
val relativePath = catching(classOf[IllegalArgumentException]) opt (basePath relativize filePath)
relativePath map (_.toString)
Expand Down
6 changes: 6 additions & 0 deletions io/src/test/scala/sbt/io/IOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class IOSpec extends FlatSpec with Matchers {
IO.relativize(base, file3) shouldBe Some(".git")
}

it should "relativize relative paths" in {
val base = new File(".").getCanonicalFile
val file = new File("build.sbt")
IO.relativize(base, file) shouldBe Some("build.sbt")
}

"toURI" should "make URI" in {
val u = IO.toURI(file("/etc/hosts"))
assert(u.toString == "file:///etc/hosts")
Expand Down

0 comments on commit 70aed88

Please sign in to comment.