Skip to content

Commit 0a48a7d

Browse files
committed
Fixed path tests on macos-arm64
1 parent 30d0bf1 commit 0a48a7d

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

core/native/test/util.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,28 @@ package kotlinx.io
66

77
import kotlinx.cinterop.*
88
import platform.posix.*
9+
import kotlin.random.Random
10+
import kotlin.system.getTimeMillis
911

1012
actual fun createTempFile(): String {
1113
val template = "tmp-XXXXXX"
1214
val path = mktemp(template.cstr) ?: throw IOException("Filed to create temp file: ${strerror(errno)}")
15+
// mktemp don't work on MacOS 13+ (as well as mkstemp), at least the way it's expected.
16+
if (path.toKString() == "") {
17+
val tmpDir = getenv("TMPDIR")?.toKString() ?: getenv("TMP")?.toKString() ?: ""
18+
val rnd = Random(getTimeMillis())
19+
var path: String
20+
do {
21+
path = "$tmpDir/tmp-${rnd.nextInt()}"
22+
} while (access(path, F_OK) == 0)
23+
return path
24+
}
1325
return path.toKString()
1426
}
1527

1628
actual fun deleteFile(path: String) {
1729
if (access(path, F_OK) != 0) throw IOException("File does not exist: $path")
1830
if (remove(path) != 0) {
19-
throw IOException("Failed to delete file $path: ${strerror(errno)}")
31+
throw IOException("Failed to delete file $path: ${strerror(errno)?.toKString()}")
2032
}
2133
}

0 commit comments

Comments
 (0)