Skip to content

Commit 57e251a

Browse files
authored
misc: add fileExists API (#1009)
1 parent 8460072 commit 57e251a

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

runtime/runtime-core/api/runtime-core.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,7 @@ public abstract interface class aws/smithy/kotlin/runtime/util/EnvironmentProvid
15831583

15841584
public abstract interface class aws/smithy/kotlin/runtime/util/Filesystem {
15851585
public static final field Companion Laws/smithy/kotlin/runtime/util/Filesystem$Companion;
1586+
public abstract fun fileExists (Ljava/lang/String;)Z
15861587
public abstract fun getFilePathSeparator ()Ljava/lang/String;
15871588
public abstract fun readFileOrNull (Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
15881589
public abstract fun writeFile (Ljava/lang/String;[BLkotlin/coroutines/Continuation;)Ljava/lang/Object;

runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/util/Filesystem.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public interface Filesystem {
3131
*/
3232
public suspend fun writeFile(path: String, data: ByteArray)
3333

34+
/**
35+
* Check if a file exists at the [path].
36+
* @param path fully qualified path encoded specifically to the target platform's filesystem
37+
*/
38+
public fun fileExists(path: String): Boolean
39+
3440
public companion object {
3541
/**
3642
* Construct a fake filesystem from a mapping of paths to contents
@@ -48,4 +54,5 @@ internal class MapFilesystem(
4854
override suspend fun writeFile(path: String, data: ByteArray) {
4955
memFs[path] = data
5056
}
57+
override fun fileExists(path: String): Boolean = memFs[path] != null
5158
}

runtime/runtime-core/jvm/src/aws/smithy/kotlin/runtime/util/PlatformJVM.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ internal actual object SystemDefaultProvider : PlatformProvider {
4848
}
4949
}
5050

51+
override fun fileExists(path: String): Boolean = File(path).exists()
52+
5153
public suspend fun readFileOrNull(path: Path): ByteArray? = readFileOrNull(path.toAbsolutePath().toString())
5254
public suspend fun readFileOrNull(file: File): ByteArray? = readFileOrNull(file.absolutePath)
5355

runtime/runtime-core/jvm/test/aws/smithy/kotlin/runtime/util/PlatformJVMTest.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
*/
55
package aws.smithy.kotlin.runtime.util
66

7-
import kotlinx.coroutines.runBlocking
7+
import kotlinx.coroutines.test.runTest
88
import java.nio.file.Files
99
import java.nio.file.Path
1010
import kotlin.io.path.absolutePathString
1111
import kotlin.io.path.writeText
12-
import kotlin.test.BeforeTest
13-
import kotlin.test.Test
14-
import kotlin.test.assertEquals
15-
import kotlin.test.assertNotNull
12+
import kotlin.test.*
1613

1714
class PlatformJVMTest {
1815

@@ -26,7 +23,7 @@ class PlatformJVMTest {
2623
}
2724

2825
@Test
29-
fun itReadsFiles() = runBlocking {
26+
fun itReadsFiles() = runTest {
3027
val actual = PlatformProvider.System.readFileOrNull(tempFile.absolutePathString())
3128

3229
assertNotNull(actual)
@@ -49,4 +46,9 @@ class PlatformJVMTest {
4946
val allPropertiesFromPlatform = PlatformProvider.System.getAllProperties()
5047
assertEquals(allPropertiesFromSystem, allPropertiesFromPlatform)
5148
}
49+
50+
@Test
51+
fun testFileExists() = runTest {
52+
assertTrue(PlatformProvider.System.fileExists(tempFile.absolutePathString()))
53+
}
5254
}

runtime/runtime-core/native/src/aws/smithy/kotlin/runtime/util/PlatformNative.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ internal actual object SystemDefaultProvider : PlatformProvider {
2424
TODO("Not yet implemented")
2525
}
2626

27+
override fun fileExists(path: String): Boolean {
28+
TODO("Not yet implemented")
29+
}
30+
2731
override fun osInfo(): OperatingSystem {
2832
TODO("Not yet implemented")
2933
}

0 commit comments

Comments
 (0)