Skip to content
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

Relativize relative paths #108

Merged
merged 1 commit into from Dec 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions io/src/main/scala/sbt/io/IO.scala
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
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