Skip to content

Commit 30d0bf1

Browse files
committed
Don't create test file in build directory
1 parent 2be053f commit 30d0bf1

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

core/common/test/files/SmokeFileTest.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,21 @@ import kotlinx.io.*
99
import kotlin.test.*
1010

1111
class SmokeFileTest {
12+
private var tempFile: String? = null
13+
14+
@BeforeTest
15+
fun setup() {
16+
tempFile = createTempFile()
17+
}
18+
19+
@AfterTest
20+
fun cleanup() {
21+
deleteFile(tempFile!!)
22+
}
1223

1324
@Test
1425
fun testBasicFile() {
15-
val path = Path("test.txt")
26+
val path = Path(tempFile!!)
1627
path.sink().use {
1728
it.writeUtf8("example")
1829
}

core/common/test/util.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ fun bufferWithSegments(vararg segments: String): Buffer {
102102
// b: Path,
103103
// sameAsNio: Boolean = true,
104104
//): IllegalArgumentException
105+
106+
expect fun createTempFile(): String
107+
expect fun deleteFile(path: String)

core/jvm/test/ultil.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright 2017-2023 JetBrains s.r.o. and respective authors and developers.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
4+
*/
5+
package kotlinx.io
6+
7+
import java.nio.file.*
8+
9+
actual fun createTempFile(): String = Files.createTempFile(null, null).toString()
10+
11+
actual fun deleteFile(path: String) {
12+
if (!Files.isRegularFile(Paths.get(path))) {
13+
throw IllegalArgumentException("Path is not a file: $path.")
14+
}
15+
Files.delete(Paths.get(path))
16+
}

core/native/src/files/PathsNative.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ public actual fun Path(path: String): Path = Path(path, null)
2121

2222
public actual fun Path.source(): Source {
2323
val openFile: CPointer<FILE> = fopen(path, "r")
24-
?: throw IOException("Failed to open $path with $errno")
24+
?: throw IOException("Failed to open $path with ${strerror(errno)?.toKString()}")
2525
return FileSource(openFile).buffer()
2626
}
2727

2828
public actual fun Path.sink(): Sink {
29-
val openFile: CPointer<FILE> = fopen(path, "wx")
30-
?: throw IOException("Failed to open $path with $errno")
29+
val openFile: CPointer<FILE> = fopen(path, "w")
30+
?: throw IOException("Failed to open $path with ${strerror(errno)?.toKString()}")
3131
return FileSink(openFile).buffer()
3232
}
3333

core/native/test/util.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2017-2023 JetBrains s.r.o. and respective authors and developers.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
4+
*/
5+
package kotlinx.io
6+
7+
import kotlinx.cinterop.*
8+
import platform.posix.*
9+
10+
actual fun createTempFile(): String {
11+
val template = "tmp-XXXXXX"
12+
val path = mktemp(template.cstr) ?: throw IOException("Filed to create temp file: ${strerror(errno)}")
13+
return path.toKString()
14+
}
15+
16+
actual fun deleteFile(path: String) {
17+
if (access(path, F_OK) != 0) throw IOException("File does not exist: $path")
18+
if (remove(path) != 0) {
19+
throw IOException("Failed to delete file $path: ${strerror(errno)}")
20+
}
21+
}

0 commit comments

Comments
 (0)