Skip to content

Commit 783bb64

Browse files
authored
Merge pull request #8 from Archinamon/improve_mkdirs_absolutePath
Kotlin 1.8 + Improve mkdirs and absolute path (and fixes)
2 parents 955e56e + 8983ea2 commit 783bb64

File tree

16 files changed

+213
-39
lines changed

16 files changed

+213
-39
lines changed

.github/workflows/check-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
if: ${{ matrix.os == 'ubuntu-latest' }}
4040
- uses: eskatos/gradle-command-action@v1
4141
with:
42-
arguments: macosX64MainKlibrary -s -i
42+
arguments: macosX64MainKlibrary macosArm64MainKlibrary -s -i
4343
if: ${{ matrix.os == 'macos-latest' }}
4444
- uses: eskatos/gradle-command-action@v1
4545
with:

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ jobs:
2929
name: Test Windows Target
3030
if: ${{ startsWith(matrix.os, 'windows') }}
3131
with:
32-
arguments: clean jvmTest mingwX64Test
32+
arguments: clean mingwX64Test
3333
- uses: eskatos/gradle-command-action@v1
3434
name: Test Apple Target
3535
if: ${{ startsWith(matrix.os, 'macos') }}
3636
with:
37-
arguments: clean jvmTest macosX64Test
37+
arguments: clean macosX64Test macosArm64Test
3838
- uses: eskatos/gradle-command-action@v1
3939
name: Test Linux Target
4040
if: ${{ startsWith(matrix.os, 'ubuntu') }}

build.gradle.kts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
plugins {
2-
kotlin("multiplatform") version "1.7.10"
2+
kotlin("multiplatform") version "1.8.20"
33
id("org.jetbrains.dokka") version "1.4.32"
44
id("maven-publish")
55
id("signing")
66
}
77

88
group = "me.archinamon"
9-
version = "1.3.6"
9+
version = "1.3.7"
1010

1111
val isRunningInIde: Boolean = System.getProperty("idea.active")
1212
?.toBoolean() == true
@@ -18,16 +18,23 @@ repositories {
1818
}
1919

2020
kotlin {
21-
js { nodejs() }
21+
js(IR) { nodejs() }
2222

2323
jvm()
2424

2525
// generic linux code
2626
linuxX64()
2727

2828
// darwin macos code
29-
macosX64 {
29+
macosX64() {
30+
if (testApp?.toBoolean() == true) {
31+
binaries {
32+
executable()
33+
}
34+
}
35+
}
3036

37+
macosArm64 {
3138
if (testApp?.toBoolean() == true) {
3239
binaries {
3340
executable()
@@ -47,10 +54,16 @@ kotlin {
4754
val posixMain by creating {
4855
dependsOn(commonMain)
4956
}
57+
val macosArm64Main by getting {
58+
dependsOn(posixMain)
59+
if (testApp?.toBoolean() == true) {
60+
kotlin.srcDirs("src/macosRunner/kotlin")
61+
}
62+
}
5063
val macosX64Main by getting {
5164
dependsOn(posixMain)
5265
if (testApp?.toBoolean() == true) {
53-
kotlin.srcDirs("src/macosX64Runner/kotlin")
66+
kotlin.srcDirs("src/macosRunner/kotlin")
5467
}
5568
}
5669
val linuxX64Main by getting {

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
kotlin.code.style=official
22
kotlin.js.generate.executable.default=false
3+
kotlin.js.compiler=ir
34

45
kotlin.mpp.stability.nowarn=true
56
kotlin.mpp.enableGranularSourceSetsMetadata=true

src/commonMain/kotlin/File.common.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ expect class File(pathname: String) {
77
fun getName(): String
88

99
fun lastModified(): Long
10+
fun mkdir(): Boolean
1011
fun mkdirs(): Boolean
1112
fun createNewFile(): Boolean
1213

1314
fun isFile(): Boolean
1415
fun isDirectory(): Boolean
1516

17+
fun getPath(): String
1618
fun getAbsolutePath(): String
1719
fun length(): Long
1820

src/commonMain/kotlin/Files.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ object Files {
1313
throw UnsupportedOperationException("Do not supported on mingw yet, create tmp files/dirs manually!")
1414
}
1515

16-
val parent = dir.getAbsolutePath()
17-
if (!dir.exists()) {
18-
dir.mkdirs()
19-
}
16+
val parent = dir.getPath()
17+
dir.mkdirs()
2018

2119
if (!dir.canWrite()) {
2220
throw IllegalFileAccess(parent, "Can't create file in the directory")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package me.archinamon.fileio
2+
3+
enum class Platform {
4+
Linux, Macos, Windows, JVM, JS;
5+
6+
fun isPosix(): Boolean = this == Macos || this == Linux
7+
}
8+
9+
expect fun platform(): Platform

src/commonMain/kotlin/platform.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/commonTest/kotlin/FileTests.kt

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,30 @@ import kotlin.test.*
44

55
class FileTests {
66

7-
private val localSeparator = '/'
8-
97
@Test
108
fun testNonexistentRootFile() {
119
val testFile = File("testNonexistentRootFile.txt")
1210

1311
assertFalse(testFile.exists(), "file should not exist")
1412
assertFalse(testFile.isDirectory(), "file should not be directory")
1513
assertFalse(testFile.isFile(), "file should not be file")
16-
assertNull(testFile.getParent(), "file should not have parent")
17-
assertNull(testFile.getParentFile(), "file should not have parent file")
14+
15+
if (platform() == Platform.JVM) {
16+
assertNull(testFile.getParent(), "file should not have parent")
17+
assertNull(testFile.getParentFile(), "file should not have parent file")
18+
}
19+
20+
if (platform().isPosix()) { // in posix we always resolve relative path via `realpath` syscall
21+
assertEquals(testFile.getParent(), File("./").getAbsolutePath(), "as parent should be current dir")
22+
assertEquals(testFile.getParentFile()?.getAbsolutePath(), File("./").getAbsolutePath(), "as parent should be current dir")
23+
}
1824

1925
assertEquals("testNonexistentRootFile", testFile.nameWithoutExtension)
2026
}
2127

2228
@Test
2329
fun testExistentRootFile() {
2430
val testFile = File("testFileRoot/testExistentRootFile.txt")
25-
println(testFile.getAbsolutePath())
2631

2732
assertFalse(testFile.exists(), "file should not exist")
2833
assertFalse(testFile.getParentFile()?.exists() == true, "file should not have parent file")
@@ -33,6 +38,10 @@ class FileTests {
3338

3439
@Test
3540
fun testFileCreateAndDelete() {
41+
if (platform() == Platform.Windows) {
42+
return
43+
}
44+
3645
val testFolder = File("build/testNewDirectoryCreation")
3746

3847
assertTrue(testFolder.mkdirs(), "create directory failed")
@@ -52,6 +61,10 @@ class FileTests {
5261

5362
@Test
5463
fun testFileWriteAndRead() {
64+
if (platform() == Platform.Windows) {
65+
return
66+
}
67+
5568
val testFolder = File("build/testFileWriteAndRead")
5669
val testFile = File("build/testFileWriteAndRead/test.txt")
5770

@@ -87,6 +100,10 @@ class FileTests {
87100

88101
@Test
89102
fun testFileCopyMethod() {
103+
if (platform() == Platform.Windows) {
104+
return
105+
}
106+
90107
val testFile = File("gradle/wrapper/gradle-wrapper.properties")
91108
val testDestFolder = File("build/testCopyFolder")
92109
val testDestFile = File("build/testCopyFolder/gradle-wrapper.properties")
@@ -107,6 +124,10 @@ class FileTests {
107124

108125
@Test
109126
fun testFileMoveMethod() {
127+
if (platform() == Platform.Windows) {
128+
return
129+
}
130+
110131
val testFolder = File("build/testMoveFolder")
111132
val testDestFolder = File("build/testMoveFolder2")
112133
val testFile = File("build/testMoveFolder/test_move_file.properties")
@@ -198,4 +219,32 @@ class FileTests {
198219

199220
assertTrue(testFile.delete(), "delete file failed")
200221
}
222+
223+
@Test
224+
fun testFileRealPathIfRelativeLinks__posixOnly() {
225+
if (!platform().isPosix()) {
226+
return
227+
}
228+
229+
val symlinkPrefix = if (platform() == Platform.Macos) "/private" else ""
230+
231+
val testDir = File("/tmp/build")
232+
val testFile = Files.createTempFile(prefix = "../test", suffix = ".txt", dir = testDir)
233+
assertEquals("$symlinkPrefix/tmp/test.txt", testFile.getAbsolutePath()) // 'cause /tmp is a symlink for /private/tmp
234+
assertTrue(testFile.delete(), "delete file failed")
235+
assertTrue(testDir.delete(), "delete test folder failed")
236+
}
237+
238+
@Test
239+
fun testFileRealPathIfRelativeLinks__jvmOnly() {
240+
if (platform() != Platform.JVM) {
241+
return
242+
}
243+
244+
val testDir = File("/tmp/build")
245+
val testFile = Files.createTempFile(prefix = "../test", suffix = ".txt", dir = testDir)
246+
assertEquals("/tmp/build/../test.txt", testFile.getAbsolutePath()) // lazy canonicalization in jvm
247+
assertTrue(testFile.delete(), "delete file failed")
248+
assertTrue(testDir.delete(), "delete test folder failed")
249+
}
201250
}

src/jsMain/kotlin/File.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ actual class File constructor(jsfile: JsFile) {
3434
return innerFile.lastModified.toLong()
3535
}
3636

37+
actual fun mkdir(): Boolean {
38+
throw UnsupportedOperationException("Not available in JS!")
39+
}
40+
3741
actual fun mkdirs(): Boolean {
3842
throw UnsupportedOperationException("Not available in JS!")
3943
}
@@ -50,6 +54,10 @@ actual class File constructor(jsfile: JsFile) {
5054
return false
5155
}
5256

57+
actual fun getPath(): String {
58+
return innerFile.name
59+
}
60+
5361
actual fun getAbsolutePath(): String {
5462
return innerFile.name
5563
}

0 commit comments

Comments
 (0)