Skip to content

Commit 1af7bdb

Browse files
committed
Tackle some Windows issues, again
1 parent efb5f12 commit 1af7bdb

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

core/common/test/files/SmokeFileTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ class SmokeFileTest {
168168
@Test
169169
fun trailingSeparatorsTrimming() {
170170
assertEquals(Path(".").toString(), Path(".///").toString())
171-
assertEquals(Path("/").toString(), Path("///").toString())
172171
assertEquals(Path("/").toString(), Path("/").toString())
173172
assertEquals(Path("/..").toString(), Path("/../").toString())
174173
assertEquals(Path("/a/b/c").toString(), Path("/a/b/c").toString())

core/js/src/files/PathsJs.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,27 @@ internal class FileSink(private val path: Path, private var append: Boolean) : R
156156
}
157157
}
158158

159+
private fun isUnc(path: String): Boolean {
160+
if (path.length < 2) return false
161+
if (path.startsWith("$WindowsPathSeparator$WindowsPathSeparator")) return true
162+
if (path.startsWith("$UnixPathSeparator$UnixPathSeparator")) return true
163+
return false
164+
}
165+
159166
internal fun removeTrailingSeparators(path: String): String {
160167
if (isWindows) {
161168
// don't trim the path separator right after the drive name
162-
val limit = if (path.length > 1 && path[1] == ':') 3 else 1
169+
val limit = if (path.length > 1) {
170+
if (path[1] == ':') {
171+
3
172+
} else if (isUnc(path)) {
173+
2
174+
} else {
175+
1
176+
}
177+
} else {
178+
1
179+
}
163180
return removeTrailingSeparators(limit, path, UnixPathSeparator, WindowsPathSeparator)
164181
}
165182
return removeTrailingSeparators(path, SystemPathSeparator)

core/mingw/src/files/FileSystemMingw.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,23 @@ internal actual fun realpathImpl(path: String): String {
5858
}
5959
}
6060

61+
private fun isUnc(path: String): Boolean {
62+
if (path.length < 2) return false
63+
if (path.startsWith("$WindowsPathSeparator$WindowsPathSeparator")) return true
64+
if (path.startsWith("$SystemPathSeparator$SystemPathSeparator")) return true
65+
return false
66+
}
67+
6168
internal actual fun removeTrailingSeparators(path: String): String {
6269
// Don't trim path separator following the drive name
63-
val limit = if (path.length > 1 && path[1] == ':') {
64-
3
70+
val limit = if (path.length > 1) {
71+
if (path[1] == ':') {
72+
3
73+
} else if (isUnc(path)) {
74+
2
75+
} else {
76+
1
77+
}
6578
} else {
6679
1
6780
}

core/mingw/test/files/SmokeFileTestWindows.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class SmokeFileTestWindows {
3333
assertEquals(".", Path(".\\").toString())
3434
assertEquals("C:\\", Path("C:\\").toString())
3535
assertEquals("C:\\", Path("C:\\\\").toString())
36+
assertEquals("\\\\", Path("\\\\").toString())
37+
assertEquals("//", Path("//").toString())
3638
assertEquals(".\\a", Path(".\\a\\//\\//\\\\////").toString())
3739
}
3840
}

0 commit comments

Comments
 (0)