Skip to content

Commit aa44461

Browse files
committed
PR feedback
1 parent f4fdfe3 commit aa44461

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

runtime/runtime-core/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
2-
31
/*
42
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
53
* SPDX-License-Identifier: Apache-2.0
64
*/
75

6+
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
7+
88
plugins {
99
alias(libs.plugins.kotlinx.serialization)
1010
}

runtime/runtime-core/common/test/aws/smithy/kotlin/runtime/util/SystemPlatformProviderTest.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class SystemPlatformProviderTest {
1818
val ps = PlatformProvider.System
1919

2020
val tempDir = if (ps.osInfo().family == OsFamily.Windows) {
21-
ps.getenv("TEMP") ?: "C:\\Windows\\Temp"
21+
requireNotNull(ps.getenv("TEMP")) { "%TEMP% unexpectedly null" }
2222
} else {
2323
"/tmp"
2424
}
@@ -36,8 +36,9 @@ class SystemPlatformProviderTest {
3636
@Test
3737
fun testGetEnv() = runTest {
3838
val envVarKeys = listOf("PATH", "USERPROFILE") // PATH is not set on Windows CI
39-
val result: String? = envVarKeys.fold(null) { acc, curr -> acc ?: PlatformProvider.System.getenv(curr) }
40-
assertNotNull(result)
39+
assertNotNull(
40+
envVarKeys.firstNotNullOfOrNull { PlatformProvider.System.getenv(it) }
41+
)
4142

4243
assertNull(PlatformProvider.System.getenv("THIS_ENV_VAR_IS_NOT_SET"))
4344
}
@@ -48,12 +49,9 @@ class SystemPlatformProviderTest {
4849
assertTrue(allEnv.isNotEmpty())
4950

5051
val envVarKeys = listOf("PATH", "USERPROFILE") // PATH is not set on Windows CI
51-
52-
var envContainsKey = false
53-
envVarKeys.forEach { key ->
54-
envContainsKey = envContainsKey || allEnv.contains(key)
55-
}
56-
assertTrue(envContainsKey)
52+
assertTrue(
53+
envVarKeys.any { allEnv.contains(it) }
54+
)
5755
}
5856

5957
@Test
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package aws.smithy.kotlin.runtime.util
6+
7+
import kotlinx.coroutines.test.runTest
8+
import kotlin.test.Test
9+
import kotlin.test.assertEquals
10+
11+
class SystemPlatformProviderTestLinux {
12+
@Test
13+
fun testOsInfo() = runTest {
14+
val osInfo = PlatformProvider.System.osInfo()
15+
assertEquals(OsFamily.Linux, osInfo.family)
16+
}
17+
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
package aws.smithy.kotlin.runtime.util
66

77
import aws.smithy.kotlin.runtime.io.IOException
8+
import aws.smithy.kotlin.runtime.io.internal.SdkDispatchers
89
import aws.smithy.platform.posix.get_environ_ptr
910
import kotlinx.cinterop.*
11+
import kotlinx.coroutines.withContext
1012
import platform.posix.*
1113

1214
internal actual object SystemDefaultProvider : PlatformProvider {
@@ -17,7 +19,7 @@ internal actual object SystemDefaultProvider : PlatformProvider {
1719
.takeWhile { it != null }
1820
.associate { env ->
1921
val parts = env?.split("=", limit = 2)
20-
check(parts?.size == 2) { "Environment entry $env is malformed" }
22+
check(parts?.size == 2) { "Environment entry \"$env\" is malformed" }
2123
parts[0] to parts[1]
2224
}
2325
}
@@ -30,9 +32,9 @@ internal actual object SystemDefaultProvider : PlatformProvider {
3032
else -> "/"
3133
}
3234

33-
actual override suspend fun readFileOrNull(path: String): ByteArray? {
34-
return try {
35-
val file = fopen(path, "rb") ?: return null
35+
actual override suspend fun readFileOrNull(path: String): ByteArray? = withContext(SdkDispatchers.IO) {
36+
try {
37+
val file = fopen(path, "rb") ?: return@withContext null
3638

3739
try {
3840
// Get file size
@@ -42,7 +44,7 @@ internal actual object SystemDefaultProvider : PlatformProvider {
4244

4345
// Read file content
4446
val buffer = ByteArray(size.toInt()).pin()
45-
val rc = fread(buffer.addressOf(0), 1.toULong(), size.toULong(), file)
47+
val rc = fread(buffer.addressOf(0), 1uL, size.toULong(), file)
4648
if (rc == size.toULong()) buffer.get() else null
4749
} finally {
4850
fclose(file)
@@ -52,10 +54,10 @@ internal actual object SystemDefaultProvider : PlatformProvider {
5254
}
5355
}
5456

55-
actual override suspend fun writeFile(path: String, data: ByteArray) {
57+
actual override suspend fun writeFile(path: String, data: ByteArray) = withContext(SdkDispatchers.IO) {
5658
val file = fopen(path, "wb") ?: throw IOException("Cannot open file for writing: $path")
5759
try {
58-
val wc = fwrite(data.refTo(0), 1.toULong(), data.size.toULong(), file)
60+
val wc = fwrite(data.refTo(0), 1uL, data.size.toULong(), file)
5961
if (wc != data.size.toULong()) {
6062
throw IOException("Failed to write all bytes to file $path, expected ${data.size.toLong()}, wrote $wc")
6163
}

0 commit comments

Comments
 (0)