Skip to content

Commit 27d4132

Browse files
committed
Fixed relative path detection on Windows, split parent path test to platform specific tests as NodeJS handles UNC path differently
1 parent 72b18d9 commit 27d4132

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

core/common/test/files/SmokeFileTestWindows.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ class SmokeFileTestWindows {
1515
assertTrue(Path("C:\\").isAbsolute)
1616
assertTrue(Path("C:/").isAbsolute)
1717
assertTrue(Path("C:/../").isAbsolute)
18-
// Well, it's a relative path, but Win32's PathIsRelative don't think so.
19-
assertTrue(Path("C:file").isAbsolute)
18+
assertFalse(Path("C:file").isAbsolute)
2019
assertFalse(Path("bla\\bla\\bla").isAbsolute)
2120
assertTrue(Path("\\\\server\\share").isAbsolute)
2221
}
@@ -26,7 +25,6 @@ class SmokeFileTestWindows {
2625
if (!isWindows) return
2726
assertNull(Path("C:\\").parent)
2827
assertNull(Path("a\\b").parent?.parent)
29-
assertEquals(Path("\\\\server"), Path("\\\\server\\share").parent)
3028
assertEquals(Path("C:\\"), Path("C:\\Program Files").parent)
3129
assertEquals(Path("C:\\Program Files"), Path("C:\\Program Files/Java").parent)
3230
}
@@ -38,7 +36,9 @@ class SmokeFileTestWindows {
3836
assertEquals("C:\\", Path("C:\\").toString())
3937
assertEquals("C:\\", Path("C:\\\\").toString())
4038
assertEquals("\\\\", Path("\\\\").toString())
41-
assertEquals("//", Path("//").toString())
4239
assertEquals(".\\a", Path(".\\a\\//\\//\\\\////").toString())
40+
41+
// this path could be transformed to use canonical separator on JVM
42+
assertEquals(Path("//").toString(), Path("//").toString())
4343
}
4444
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.io.files
7+
8+
import kotlinx.io.isWindows
9+
import kotlin.test.Test
10+
import kotlin.test.assertEquals
11+
import kotlin.test.assertNull
12+
13+
class SmokeFileTestWindowMinGW {
14+
@Test
15+
fun uncParent() {
16+
if (!isWindows) return
17+
// NodeJS, correctly, assume that it's a root path, that's where behavior diverge
18+
assertNull(Path("\\\\server\\share").parent)
19+
assertEquals(Path("\\\\server\\share"), Path("\\\\server\\share\\dir").parent)
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.io.files
7+
8+
import kotlinx.io.isWindows
9+
import kotlin.test.Test
10+
import kotlin.test.assertEquals
11+
12+
class SmokeFileTestWindowsJVM {
13+
@Test
14+
fun uncParent() {
15+
if (!isWindows) return
16+
assertEquals(Path("\\\\server"), Path("\\\\server\\share").parent)
17+
assertEquals(Path("\\\\server\\share"), Path("\\\\server\\share\\dir").parent)
18+
}
19+
}

core/mingw/src/files/FileSystemMingw.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ internal actual fun basenameImpl(path: String): String {
3838

3939
internal actual fun isAbsoluteImpl(path: String): Boolean {
4040
if (path.startsWith(SystemPathSeparator)) return true
41+
if (path.length > 1 && path[1] == ':') {
42+
if (path.length == 1) return false
43+
val next = path[2]
44+
return next == WindowsPathSeparator || next == SystemPathSeparator
45+
}
4146
return PathIsRelativeA(path) == 0
4247
}
4348

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.io.files
7+
8+
import kotlin.test.Test
9+
import kotlin.test.assertEquals
10+
11+
class SmokeFileTestWindowsMinGW {
12+
@Test
13+
fun uncParent() {
14+
assertEquals(Path("\\\\server"), Path("\\\\server\\share").parent)
15+
assertEquals(Path("\\\\server\\share"), Path("\\\\server\\share\\dir").parent)
16+
}
17+
}

0 commit comments

Comments
 (0)