Skip to content

Commit 01112a4

Browse files
committed
client-tests work
1 parent e73318e commit 01112a4

File tree

7 files changed

+118
-108
lines changed

7 files changed

+118
-108
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.rust.codegen.client.testutil
7+
8+
import software.amazon.smithy.build.PluginContext
9+
import software.amazon.smithy.build.SmithyBuildPlugin
10+
import software.amazon.smithy.model.Model
11+
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
12+
import software.amazon.smithy.rust.codegen.client.smithy.RustClientCodegenPlugin
13+
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
14+
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
15+
import software.amazon.smithy.rust.codegen.core.testutil.IntegrationTestParams
16+
import software.amazon.smithy.rust.codegen.core.testutil.codegenIntegrationTest
17+
import java.nio.file.Path
18+
19+
fun clientIntegrationTest(
20+
model: Model,
21+
params: IntegrationTestParams = IntegrationTestParams(),
22+
additionalDecorators: List<ClientCodegenDecorator> = listOf(),
23+
test: (ClientCodegenContext, RustCrate) -> Unit = { _, _ -> },
24+
): Path {
25+
fun invokeRustCodegenPlugin(ctx: PluginContext) {
26+
val codegenDecorator = object : ClientCodegenDecorator {
27+
override val name: String = "Add tests"
28+
override val order: Byte = 0
29+
30+
override fun classpathDiscoverable(): Boolean = false
31+
32+
override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {
33+
test(codegenContext, rustCrate)
34+
}
35+
}
36+
RustClientCodegenPlugin().executeWithDecorator(ctx, codegenDecorator, *additionalDecorators.toTypedArray())
37+
}
38+
return codegenIntegrationTest(model, params, invokePlugin = ::invokeRustCodegenPlugin)
39+
}
40+
41+
/**
42+
* A `SmithyBuildPlugin` that accepts an additional decorator.
43+
*
44+
* This exists to allow tests to easily customize the _real_ build without needing to list out customizations
45+
* or attempt to manually discover them from the path.
46+
*/
47+
abstract class DecoratableBuildPlugin : SmithyBuildPlugin {
48+
abstract fun executeWithDecorator(
49+
context: PluginContext,
50+
vararg decorator: ClientCodegenDecorator,
51+
)
52+
53+
override fun execute(context: PluginContext) {
54+
executeWithDecorator(context)
55+
}
56+
}

codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/testutil/CodegenIntegrationTest.kt

Lines changed: 0 additions & 103 deletions
This file was deleted.

codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/customizations/HttpVersionListGeneratorTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
1919
import software.amazon.smithy.rust.codegen.core.rustlang.writable
2020
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
2121
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
22+
import software.amazon.smithy.rust.codegen.core.testutil.IntegrationTestParams
2223
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
2324
import software.amazon.smithy.rust.codegen.core.testutil.integrationTest
2425

@@ -170,8 +171,8 @@ internal class HttpVersionListGeneratorTest {
170171

171172
clientIntegrationTest(
172173
model,
173-
listOf(FakeSigningDecorator()),
174-
addModuleToEventStreamAllowList = true,
174+
IntegrationTestParams(addModuleToEventStreamAllowList = true),
175+
additionalDecorators = listOf(FakeSigningDecorator()),
175176
) { clientCodegenContext, rustCrate ->
176177
val moduleName = clientCodegenContext.moduleUseName()
177178
rustCrate.integrationTest("validate_eventstream_http") {

codegen-client/src/test/kotlin/software/amazon/smithy/rust/codegen/client/endpoint/EndpointsDecoratorTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.junit.jupiter.api.Test
1111
import software.amazon.smithy.rust.codegen.client.testutil.clientIntegrationTest
1212
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
1313
import software.amazon.smithy.rust.codegen.core.rustlang.rust
14+
import software.amazon.smithy.rust.codegen.core.testutil.IntegrationTestParams
1415
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
1516
import software.amazon.smithy.rust.codegen.core.testutil.integrationTest
1617
import software.amazon.smithy.rust.codegen.core.testutil.runWithWarnings
@@ -123,8 +124,8 @@ class EndpointsDecoratorTest {
123124
fun `set an endpoint in the property bag`() {
124125
val testDir = clientIntegrationTest(
125126
model,
126-
// just run integration tests
127-
command = { "cargo test --test *".runWithWarnings(it) },
127+
// Just run integration tests.
128+
IntegrationTestParams(command = { "cargo test --test *".runWithWarnings(it) }),
128129
) { clientCodegenContext, rustCrate ->
129130
rustCrate.integrationTest("endpoint_params_test") {
130131
val moduleName = clientCodegenContext.moduleUseName()

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/rustlang/RustWriter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ class RustWriter private constructor(
481481
* Callers must take care to use [this] when writing to ensure code is written to the right place:
482482
* ```kotlin
483483
* val writer = RustWriter.forModule("model")
484-
* writer.withModule(RustModule.public("nested")) {
484+
* writer.withInlineModule(RustModule.public("nested")) {
485485
* Generator(...).render(this) // GOOD
486486
* Generator(...).render(writer) // WRONG!
487487
* }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.rust.codegen.core.testutil
7+
8+
import software.amazon.smithy.build.PluginContext
9+
import software.amazon.smithy.model.Model
10+
import software.amazon.smithy.model.node.ObjectNode
11+
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
12+
import software.amazon.smithy.rust.codegen.core.util.runCommand
13+
import java.io.File
14+
import java.nio.file.Path
15+
16+
/**
17+
* A helper class holding common data with defaults that is threaded through several functions, to make their
18+
* signatures shorter.
19+
*/
20+
data class IntegrationTestParams(
21+
val addModuleToEventStreamAllowList: Boolean = false,
22+
val service: String? = null,
23+
val runtimeConfig: RuntimeConfig? = null,
24+
val additionalSettings: ObjectNode = ObjectNode.builder().build(),
25+
val overrideTestDir: File? = null,
26+
val command: ((Path) -> Unit)? = null,
27+
)
28+
29+
/**
30+
* Run cargo test on a true, end-to-end, codegen product of a given model.
31+
*/
32+
fun codegenIntegrationTest(model: Model, params: IntegrationTestParams, invokePlugin: (PluginContext) -> Unit): Path {
33+
val (ctx, testDir) = generatePluginContext(
34+
model,
35+
params.additionalSettings,
36+
params.addModuleToEventStreamAllowList,
37+
params.service,
38+
params.runtimeConfig,
39+
params.overrideTestDir,
40+
)
41+
invokePlugin(ctx)
42+
ctx.fileManifest.printGeneratedFiles()
43+
params.command?.invoke(testDir) ?: "cargo test".runCommand(testDir, environment = mapOf("RUSTFLAGS" to "-D warnings"))
44+
return testDir
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package software.amazon.smithy.rust.codegen.server.smithy.customizations
2+
3+
import org.junit.jupiter.api.Assertions.*
4+
import org.junit.jupiter.api.Test
5+
6+
internal class CustomValidationExceptionWithReasonDecoratorTest {
7+
@Test
8+
fun `constrained model with the CustomValidationExceptionWithReasonDecorator applied compiles`() {
9+
}
10+
}

0 commit comments

Comments
 (0)