Skip to content

Commit 1f0c7bb

Browse files
luigi617luigi liu
andauthored
Replace junit with kotlin test (#1857)
* easy replace from junit to kotlin-test * ktlint * Replace all JUnit API usages with kotlin-test and smithy-kotlin testing utilities * ktlint * update smithy-kotlin-version * fix indentation * fix indentation --------- Co-authored-by: luigi liu <luigiliu@amazon.com>
1 parent a9980de commit 1f0c7bb

32 files changed

Lines changed: 382 additions & 365 deletions

File tree

aws-runtime/aws-config/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ kotlin {
5151
dependencies {
5252
implementation(libs.kotlinx.coroutines.test)
5353
implementation(libs.smithy.kotlin.http.test)
54+
implementation(libs.smithy.kotlin.testing)
5455
implementation(libs.kotlinx.serialization.json)
5556
}
5657
}

aws-runtime/aws-config/jvm/test/aws/sdk/kotlin/runtime/config/AbstractAwsSdkClientFactoryTest.kt

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,23 @@ import aws.sdk.kotlin.runtime.region.DefaultRegionProviderChain
1212
import aws.smithy.kotlin.runtime.client.*
1313
import aws.smithy.kotlin.runtime.client.region.RegionProvider
1414
import aws.smithy.kotlin.runtime.retries.StandardRetryStrategy
15+
import aws.smithy.kotlin.runtime.testing.withTempDir
1516
import aws.smithy.kotlin.runtime.util.PlatformProvider
1617
import aws.smithy.kotlin.runtime.util.asyncLazy
1718
import io.kotest.extensions.system.withEnvironment
1819
import io.kotest.extensions.system.withSystemProperties
1920
import kotlinx.coroutines.test.runTest
20-
import org.junit.jupiter.api.io.TempDir
21-
import java.nio.file.Path
22-
import kotlin.io.path.absolutePathString
23-
import kotlin.io.path.deleteIfExists
24-
import kotlin.io.path.writeText
21+
import kotlinx.io.buffered
22+
import kotlinx.io.files.Path
23+
import kotlinx.io.files.SystemFileSystem
24+
import kotlinx.io.writeString
2525
import kotlin.test.Ignore
2626
import kotlin.test.Test
2727
import kotlin.test.assertEquals
2828
import kotlin.test.assertIs
2929
import kotlin.time.Duration.Companion.seconds
3030

3131
class AbstractAwsSdkClientFactoryTest {
32-
@JvmField
33-
@TempDir
34-
var tempDir: Path? = null
3532

3633
@Test
3734
fun testFromEnvironmentFavorsExplicitConfig() = runTest {
@@ -66,27 +63,26 @@ class AbstractAwsSdkClientFactoryTest {
6663
fun testFromEnvironmentResolvesAppId() = runTest(
6764
timeout = 20.seconds,
6865
) {
69-
val credentialsFile = tempDir!!.resolve("credentials")
70-
val configFile = tempDir!!.resolve("config")
66+
withTempDir { dir ->
67+
val credentialsFile = Path(dir, "credentials")
68+
val configFile = Path(dir, "config")
7169

72-
configFile.writeText("[profile foo]\nsdk_ua_app_id = profile-app-id")
70+
SystemFileSystem.sink(configFile).buffered().use { it.writeString("[profile foo]\nsdk_ua_app_id = profile-app-id") }
7371

74-
val testPlatform = mockPlatform(
75-
pathSegment = PlatformProvider.System.filePathSeparator,
76-
awsProfileEnv = "foo",
77-
homeEnv = "/home/user",
78-
awsConfigFileEnv = configFile.absolutePathString(),
79-
awsSharedCredentialsFileEnv = credentialsFile.absolutePathString(),
80-
os = PlatformProvider.System.osInfo(),
81-
)
72+
val testPlatform = mockPlatform(
73+
pathSegment = PlatformProvider.System.filePathSeparator,
74+
awsProfileEnv = "foo",
75+
homeEnv = "/home/user",
76+
awsConfigFileEnv = configFile.toString(),
77+
awsSharedCredentialsFileEnv = credentialsFile.toString(),
78+
os = PlatformProvider.System.osInfo(),
79+
)
8280

83-
val sharedConfig = asyncLazy { loadAwsSharedConfig(testPlatform) }
84-
val profile = asyncLazy { sharedConfig.get().activeProfile }
81+
val sharedConfig = asyncLazy { loadAwsSharedConfig(testPlatform) }
82+
val profile = asyncLazy { sharedConfig.get().activeProfile }
8583

86-
assertEquals("profile-app-id", resolveUserAgentAppId(testPlatform, profile))
87-
88-
configFile.deleteIfExists()
89-
credentialsFile.deleteIfExists()
84+
assertEquals("profile-app-id", resolveUserAgentAppId(testPlatform, profile))
85+
}
9086

9187
withEnvironment(
9288
mapOf(

aws-runtime/aws-config/jvm/test/aws/sdk/kotlin/runtime/config/profile/AWSConfigLoaderFilesystemTest.kt

Lines changed: 48 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,68 @@
66
package aws.sdk.kotlin.runtime.config.profile
77

88
import aws.sdk.kotlin.runtime.config.utils.mockPlatform
9+
import aws.smithy.kotlin.runtime.testing.withTempDir
910
import aws.smithy.kotlin.runtime.util.PlatformProvider
1011
import kotlinx.coroutines.test.runTest
11-
import org.junit.jupiter.api.Test
12-
import org.junit.jupiter.api.io.TempDir
13-
import java.nio.file.Path
14-
import kotlin.io.path.absolutePathString
15-
import kotlin.io.path.deleteIfExists
16-
import kotlin.io.path.writeText
12+
import kotlinx.io.buffered
13+
import kotlinx.io.files.Path
14+
import kotlinx.io.files.SystemFileSystem
15+
import kotlinx.io.writeString
16+
import kotlin.test.Test
1717
import kotlin.test.assertEquals
1818

1919
/**
2020
* Tests that exercise logic associated with the filesystem
2121
*/
2222
class AWSConfigLoaderFilesystemTest {
2323

24-
@TempDir
25-
@JvmField
26-
var tempDir: Path? = null
27-
2824
@Test
2925
fun itLoadsConfigFileFromFilesystem() = runTest {
30-
val configFile = tempDir!!.resolve("config")
31-
val credentialsFile = tempDir!!.resolve("credentials")
32-
33-
configFile.writeText("[profile foo]\nname = value")
34-
35-
val testPlatform = mockPlatform(
36-
pathSegment = PlatformProvider.System.filePathSeparator, // Use actual value from Platform in mock
37-
awsProfileEnv = "foo",
38-
homeEnv = "/home/user",
39-
awsConfigFileEnv = configFile.absolutePathString(),
40-
awsSharedCredentialsFileEnv = credentialsFile.absolutePathString(),
41-
os = PlatformProvider.System.osInfo(), // Actual value
42-
)
43-
44-
val actual = loadAwsSharedConfig(testPlatform).activeProfile
45-
46-
assertEquals("foo", actual.name)
47-
assertEquals("value", actual.getOrNull("name"))
48-
49-
configFile.deleteIfExists()
50-
credentialsFile.deleteIfExists()
26+
withTempDir { dir ->
27+
val configFile = Path(dir, "config")
28+
val credentialsFile = Path(dir, "credentials")
29+
30+
SystemFileSystem.sink(configFile).buffered().use { it.writeString("[profile foo]\nname = value") }
31+
32+
val testPlatform = mockPlatform(
33+
pathSegment = PlatformProvider.System.filePathSeparator, // Use actual value from Platform in mock
34+
awsProfileEnv = "foo",
35+
homeEnv = "/home/user",
36+
awsConfigFileEnv = configFile.toString(),
37+
awsSharedCredentialsFileEnv = credentialsFile.toString(),
38+
os = PlatformProvider.System.osInfo(), // Actual value
39+
)
40+
41+
val actual = loadAwsSharedConfig(testPlatform).activeProfile
42+
43+
assertEquals("foo", actual.name)
44+
assertEquals("value", actual.getOrNull("name"))
45+
}
5146
}
5247

5348
@Test
5449
fun itLoadsConfigAndCredsFileFromFilesystem() = runTest {
55-
val configFile = tempDir!!.resolve("config")
56-
val credentialsFile = tempDir!!.resolve("credentials")
57-
58-
configFile.writeText("[profile default]\nname = value\n[default]\nname2 = value2\n[profile default]\nname3 = value3")
59-
credentialsFile.writeText("[default]\nsecret=foo")
60-
61-
val testPlatform = mockPlatform(
62-
pathSegment = PlatformProvider.System.filePathSeparator, // Use actual value from Platform in mock
63-
homeEnv = "/home/user",
64-
awsConfigFileEnv = configFile.absolutePathString(),
65-
awsSharedCredentialsFileEnv = credentialsFile.absolutePathString(),
66-
os = PlatformProvider.System.osInfo(), // Actual value
67-
)
68-
69-
val actual = loadAwsSharedConfig(testPlatform).activeProfile
70-
71-
assertEquals("default", actual.name)
72-
assertEquals("value", actual.getOrNull("name"))
73-
assertEquals("value3", actual.getOrNull("name3"))
74-
assertEquals("foo", actual.getOrNull("secret"))
75-
76-
configFile.deleteIfExists()
77-
credentialsFile.deleteIfExists()
50+
withTempDir { dir ->
51+
val configFile = Path(dir, "config")
52+
val credentialsFile = Path(dir, "credentials")
53+
54+
SystemFileSystem.sink(configFile).buffered().use { it.writeString("[profile default]\nname = value\n[default]\nname2 = value2\n[profile default]\nname3 = value3") }
55+
SystemFileSystem.sink(credentialsFile).buffered().use { it.writeString("[default]\nsecret=foo") }
56+
57+
val testPlatform = mockPlatform(
58+
pathSegment = PlatformProvider.System.filePathSeparator, // Use actual value from Platform in mock
59+
homeEnv = "/home/user",
60+
awsConfigFileEnv = configFile.toString(),
61+
awsSharedCredentialsFileEnv = credentialsFile.toString(),
62+
os = PlatformProvider.System.osInfo(), // Actual value
63+
)
64+
65+
val actual = loadAwsSharedConfig(testPlatform).activeProfile
66+
67+
assertEquals("default", actual.name)
68+
assertEquals("value", actual.getOrNull("name"))
69+
assertEquals("value3", actual.getOrNull("name3"))
70+
assertEquals("foo", actual.getOrNull("secret"))
71+
}
7872
}
7973
}

build-support/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ dependencies {
2525
implementation(libs.smithy.protocol.traits)
2626
implementation(libs.kotlinx.serialization.json)
2727

28-
testImplementation(libs.junit.jupiter)
29-
testImplementation(libs.junit.jupiter.params)
3028
testImplementation(libs.kotlin.test.junit5)
29+
testImplementation(libs.smithy.kotlin.testing)
30+
testImplementation(libs.kotlinx.coroutines.core)
3131
}
3232

3333
gradlePlugin {

build-support/src/test/kotlin/aws/sdk/kotlin/gradle/sdk/AwsServiceTest.kt

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
*/
55
package aws.sdk.kotlin.gradle.sdk
66

7+
import aws.smithy.kotlin.runtime.testing.withTempDir
8+
import kotlinx.coroutines.runBlocking
79
import org.gradle.kotlin.dsl.extra
810
import org.gradle.testfixtures.ProjectBuilder
9-
import org.junit.jupiter.api.io.TempDir
1011
import java.io.File
1112
import kotlin.test.*
1213

@@ -64,60 +65,69 @@ class AwsServiceTest {
6465
}
6566

6667
@Test
67-
fun testFileToService(@TempDir tempDir: File) {
68-
val tests = listOf(
69-
BootstrapConfig.ALL,
70-
// filename
71-
BootstrapConfig("+test-gradle"),
72-
BootstrapConfig("test-gradle"),
73-
// artifact name
74-
BootstrapConfig("+testgradle"),
75-
BootstrapConfig("testgradle"),
76-
// protocol
77-
BootstrapConfig(null, "awsJson1_0"),
78-
)
79-
80-
tests.forEach { bootstrap ->
81-
val result = testWith(tempDir, bootstrap)
82-
val expected = AwsService(
83-
"gradle.test#TestService",
84-
"aws.sdk.kotlin.services.testgradle2",
85-
"1.2.3",
86-
result.model,
87-
"test-gradle",
88-
"Test Gradle",
89-
"1-alpha",
90-
"test-gradle",
91-
"The AWS SDK for Kotlin client for Test Gradle",
68+
fun testFileToService() = runBlocking {
69+
withTempDir { dir ->
70+
val tempDir = File(dir.toString())
71+
val tests = listOf(
72+
BootstrapConfig.ALL,
73+
// filename
74+
BootstrapConfig("+test-gradle"),
75+
BootstrapConfig("test-gradle"),
76+
// artifact name
77+
BootstrapConfig("+testgradle"),
78+
BootstrapConfig("testgradle"),
79+
// protocol
80+
BootstrapConfig(null, "awsJson1_0"),
9281
)
93-
assertEquals(expected, result.actual)
82+
83+
tests.forEach { bootstrap ->
84+
val result = testWith(tempDir, bootstrap)
85+
val expected = AwsService(
86+
"gradle.test#TestService",
87+
"aws.sdk.kotlin.services.testgradle2",
88+
"1.2.3",
89+
result.model,
90+
"test-gradle",
91+
"Test Gradle",
92+
"1-alpha",
93+
"test-gradle",
94+
"The AWS SDK for Kotlin client for Test Gradle",
95+
)
96+
assertEquals(expected, result.actual)
97+
}
9498
}
9599
}
96100

97101
@Test
98-
fun testFileToServiceExclude(@TempDir tempDir: File) {
99-
val tests = listOf(
100-
// explicit exclude
101-
BootstrapConfig("-test-gradle"),
102-
BootstrapConfig("-testgradle"),
103-
// explicit include without service under test
104-
BootstrapConfig("other"),
105-
// protocol exclude
106-
BootstrapConfig(null, "-awsJson1_0"),
107-
)
102+
fun testFileToServiceExclude() = runBlocking {
103+
withTempDir { dir ->
104+
val tempDir = File(dir.toString())
105+
val tests = listOf(
106+
// explicit exclude
107+
BootstrapConfig("-test-gradle"),
108+
BootstrapConfig("-testgradle"),
109+
// explicit include without service under test
110+
BootstrapConfig("other"),
111+
// protocol exclude
112+
BootstrapConfig(null, "-awsJson1_0"),
113+
)
108114

109-
tests.forEach { bootstrap ->
110-
val result = testWith(tempDir, bootstrap)
111-
assertNull(result.actual, "expected null for bootstrap with $bootstrap")
115+
tests.forEach { bootstrap ->
116+
val result = testWith(tempDir, bootstrap)
117+
assertNull(result.actual, "expected null for bootstrap with $bootstrap")
118+
}
112119
}
113120
}
114121

115122
// FIXME - re-enable after migration
116123
// @Test
117-
// fun testFileToServiceMissingPackageMetadata(@TempDir tempDir: File) {
118-
// val ex = assertFailsWith<IllegalStateException> {
119-
// testWith(tempDir, BootstrapConfig.ALL, PackageManifest(emptyList()))
124+
// fun testFileToServiceMissingPackageMetadata() = runBlocking {
125+
// withTempDir { dir ->
126+
// val tempDir = File(dir.toString())
127+
// val ex = assertFailsWith<IllegalStateException> {
128+
// testWith(tempDir, BootstrapConfig.ALL, PackageManifest(emptyList()))
129+
// }
130+
// assertContains(ex.message!!, "unable to find package metadata for sdkId: Test Gradle")
120131
// }
121-
// assertContains(ex.message!!, "unable to find package metadata for sdkId: Test Gradle")
122132
// }
123133
}

0 commit comments

Comments
 (0)