Skip to content

Commit 69ecdd9

Browse files
author
archinamon
committed
rename separator property; fix tests for mingw adding transform assignment of filePath
1 parent db811bf commit 69ecdd9

File tree

6 files changed

+33
-28
lines changed

6 files changed

+33
-28
lines changed

src/commonMain/kotlin/File.common.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ expect class File(pathname: String) {
2525
fun delete(): Boolean
2626
}
2727

28-
expect val File.fileSeparator: Char
28+
expect val filePathSeparator: Char
2929

3030
val File.nameWithoutExtension: String
3131
get() = getName().substringBeforeLast(".")
@@ -48,7 +48,7 @@ fun File.deleteRecursively(): Boolean = walkBottomUp()
4848
fun File.getParentFileUnsafe(): File {
4949
return getParentFile()
5050
?: getAbsolutePath()
51-
.substringBeforeLast(fileSeparator)
51+
.substringBeforeLast(filePathSeparator)
5252
.run(::File)
5353
}
5454

src/commonTest/kotlin/FileTests.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import kotlin.test.*
44

55
class FileTests {
66

7+
private val localSeparator = '/'
8+
79
@Test
810
fun testNonexistentRootFile() {
911
val testFile = File("testNonexistentRootFile.txt")
@@ -19,7 +21,8 @@ class FileTests {
1921

2022
@Test
2123
fun testExistentRootFile() {
22-
val testFile = File("build/testFileRoot/testExistentRootFile.txt")
24+
val testFile = File("testFileRoot/testExistentRootFile.txt")
25+
println(testFile.getAbsolutePath())
2326

2427
assertFalse(testFile.exists(), "file should not exist")
2528
assertFalse(testFile.getParentFile()?.exists() == true, "file should not have parent file")

src/jsMain/kotlin/File.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ actual class File constructor(jsfile: JsFile) {
1515
actual fun getParent(): String? {
1616
return when {
1717
!exists() -> null
18-
fileSeparator in innerFile.name -> innerFile.name
19-
.split(fileSeparator)
18+
filePathSeparator in innerFile.name -> innerFile.name
19+
.split(filePathSeparator)
2020
.let { if (it.size > 1) it[it.lastIndex - 1] else it.last() }
2121
else -> innerFile.name
2222
}
@@ -79,7 +79,7 @@ actual class File constructor(jsfile: JsFile) {
7979
}
8080
}
8181

82-
actual val File.fileSeparator by lazy { '/' }
82+
actual val filePathSeparator by lazy { '/' }
8383

8484
actual val File.mimeType: String
8585
get() = innerFile.type

src/jvmMain/kotlin/File.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlin.io.writeBytes as kWriteBytes
88

99
actual typealias File = java.io.File
1010

11-
actual val File.fileSeparator by lazy { File.separatorChar }
11+
actual val filePathSeparator by lazy { File.separatorChar }
1212

1313
actual val File.mimeType: String
1414
get() = URLConnection.guessContentTypeFromName(name)

src/mingwX64Main/kotlin/File.kt

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@ import platform.windows.*
77
private const val EPOCH_DIFF = 11644473600000
88

99

10-
actual class File actual constructor(
11-
private val pathname: String
12-
) {
10+
actual class File {
11+
12+
private val pathname: String = pathname.replace('/', filePathSeparator)
13+
14+
actual constructor(pathname: String)
1315

1416
actual fun getParent(): String? {
15-
return if (exists()) getAbsolutePath().substringBeforeLast(fileSeparator) else null
17+
return if (exists()) getAbsolutePath().substringBeforeLast(filePathSeparator) else null
1618
}
1719

1820
actual fun getParentFile(): File? {
1921
return getParent()?.run(::File)
2022
}
2123

2224
actual fun getName(): String {
23-
return if (fileSeparator in pathname) {
24-
pathname.split(fileSeparator).last(String::isNotBlank)
25+
return if (filePathSeparator in pathname) {
26+
pathname.split(filePathSeparator).last(String::isNotBlank)
2527
} else {
2628
pathname
2729
}
@@ -102,7 +104,7 @@ actual class File actual constructor(
102104
}
103105

104106
actual fun getAbsolutePath(): String {
105-
return if (pathname.startsWith(fileSeparator) || pathname.getOrNull(1) == ':') {
107+
return if (pathname.startsWith(filePathSeparator) || pathname.getOrNull(1) == ':') {
106108
pathname
107109
} else {
108110
memScoped {
@@ -117,7 +119,7 @@ actual class File actual constructor(
117119
retryBuf.toKString()
118120
} else {
119121
buf.toKString()
120-
} + fileSeparator + pathname
122+
} + filePathSeparator + pathname
121123
}
122124
}
123125
}
@@ -167,10 +169,10 @@ actual class File actual constructor(
167169
if (isFile()) return emptyArray()
168170

169171
val findData = alloc<WIN32_FIND_DATAA>()
170-
val searchPath = if (pathname.endsWith(fileSeparator)) {
172+
val searchPath = if (pathname.endsWith(filePathSeparator)) {
171173
pathname
172174
} else {
173-
"$pathname${fileSeparator}"
175+
"$pathname${filePathSeparator}"
174176
} + "*"
175177
val find = FindFirstFileA(searchPath, findData.ptr)
176178
if (find == INVALID_HANDLE_VALUE) {
@@ -195,8 +197,8 @@ actual class File actual constructor(
195197
actual fun listFiles(): Array<File> {
196198
if (isFile()) return emptyArray()
197199
val thisPath = getAbsolutePath().let { path ->
198-
if (!path.endsWith(fileSeparator)) {
199-
path + fileSeparator
200+
if (!path.endsWith(filePathSeparator)) {
201+
path + filePathSeparator
200202
} else path
201203
}
202204
return list()
@@ -266,7 +268,7 @@ actual class File actual constructor(
266268
}
267269
}
268270

269-
actual val File.fileSeparator by lazy { if (Platform.osFamily == OsFamily.WINDOWS) '\\' else '/' }
271+
actual val filePathSeparator by lazy { if (Platform.osFamily == OsFamily.WINDOWS) '\\' else '/' }
270272

271273
actual val File.mimeType: String
272274
get() = ""

src/posixMain/kotlin/File.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,26 @@ actual class File actual constructor(
5050
private val modeRewrite = "w"
5151

5252
actual fun getParent(): String? {
53-
return if (exists()) getAbsolutePath().substringBeforeLast(fileSeparator) else null
53+
return if (exists()) getAbsolutePath().substringBeforeLast(filePathSeparator) else null
5454
}
5555

5656
actual fun getParentFile(): File? {
5757
return getParent()?.run(::File)
5858
}
5959

6060
actual fun getName(): String {
61-
return if (fileSeparator in pathname) {
62-
pathname.split(fileSeparator).last(String::isNotBlank)
61+
return if (filePathSeparator in pathname) {
62+
pathname.split(filePathSeparator).last(String::isNotBlank)
6363
} else {
6464
pathname
6565
}
6666
}
6767

6868
actual fun getAbsolutePath(): String {
69-
return if (!pathname.startsWith(fileSeparator)) {
69+
return if (!pathname.startsWith(filePathSeparator)) {
7070
memScoped {
7171
getcwd(allocArray(FILENAME_MAX), FILENAME_MAX.convert())
72-
?.toKString() + fileSeparator + pathname
72+
?.toKString() + filePathSeparator + pathname
7373
}
7474
} else pathname
7575
}
@@ -153,8 +153,8 @@ actual class File actual constructor(
153153

154154
actual fun listFiles(): Array<File> {
155155
val thisPath = getAbsolutePath().let { path ->
156-
if (!path.endsWith(fileSeparator)) {
157-
path + fileSeparator
156+
if (!path.endsWith(filePathSeparator)) {
157+
path + filePathSeparator
158158
} else path
159159
}
160160
return list()
@@ -224,7 +224,7 @@ internal expect fun readdir(dir: CPointer<out CPointed>): CPointer<dirent>?
224224

225225
internal expect fun closedir(dir: CPointer<out CPointed>): Int
226226

227-
actual val File.fileSeparator by lazy { if (Platform.osFamily == OsFamily.WINDOWS) '\\' else '/' }
227+
actual val filePathSeparator by lazy { if (Platform.osFamily == OsFamily.WINDOWS) '\\' else '/' }
228228

229229
//todo determine mimeType on file extension; see jdk mappings
230230
actual val File.mimeType: String

0 commit comments

Comments
 (0)