Skip to content

Commit

Permalink
fix for relativeTo with paths containing dots
Browse files Browse the repository at this point in the history
  • Loading branch information
oldergod committed Apr 15, 2024
1 parent a647246 commit dcb0fea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions okio/src/commonMain/kotlin/okio/internal/Path.kt
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ internal inline fun Path.commonRelativeTo(other: Path): Path {
"Impossible relative path to resolve: $this and $other"
}

if (other.segmentsBytes.size == 1 && other.segmentsBytes.first() == DOT) {
// Anything relative to "." is itself!
return this
}

val buffer = Buffer()
val slash = other.slash ?: slash ?: Path.DIRECTORY_SEPARATOR.toSlash()
for (i in firstNewSegmentIndex until otherSegments.size) {
Expand Down
13 changes: 13 additions & 0 deletions okio/src/commonTest/kotlin/okio/PathTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,19 @@ class PathTest {
assertRelativeTo(b, a, "../a...n".toPath())
}

@Test
fun relativeToRelativeWithDot() {
val a = "Desktop/documents/a".toPath()
val b = ".".toPath()
assertRelativeTo(a, b, "../../..".toPath())
assertRelativeTo(b, a, "Desktop/documents/a".toPath())

val c = "Desktop/documents/c".toPath()
val d = "Desktop/documents/./d".toPath()
assertRelativeTo(c, d, "../d".toPath())
assertRelativeTo(d, c, "../c".toPath())
}

@Test
fun relativeToRelativeWithMiddleDotsInCommonPrefix() {
val a = "Desktop/documents/a...n/red".toPath()
Expand Down

0 comments on commit dcb0fea

Please sign in to comment.