|
| 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 ClientDecoratableBuildPlugin : 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 | +} |
0 commit comments