-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move converters from constraint violations into ValidationException
to decorators
#2302
Changes from 5 commits
f0aa96b
e0e445d
649dd0c
d1f9294
32fe4d6
f1f3261
40bd7b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rust.codegen.client.testutil | ||
|
||
import software.amazon.smithy.build.PluginContext | ||
import software.amazon.smithy.build.SmithyBuildPlugin | ||
import software.amazon.smithy.model.Model | ||
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext | ||
import software.amazon.smithy.rust.codegen.client.smithy.RustClientCodegenPlugin | ||
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator | ||
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate | ||
import software.amazon.smithy.rust.codegen.core.testutil.IntegrationTestParams | ||
import software.amazon.smithy.rust.codegen.core.testutil.codegenIntegrationTest | ||
import java.nio.file.Path | ||
|
||
fun clientIntegrationTest( | ||
model: Model, | ||
params: IntegrationTestParams = IntegrationTestParams(), | ||
additionalDecorators: List<ClientCodegenDecorator> = listOf(), | ||
test: (ClientCodegenContext, RustCrate) -> Unit = { _, _ -> }, | ||
): Path { | ||
fun invokeRustCodegenPlugin(ctx: PluginContext) { | ||
val codegenDecorator = object : ClientCodegenDecorator { | ||
override val name: String = "Add tests" | ||
override val order: Byte = 0 | ||
|
||
override fun classpathDiscoverable(): Boolean = false | ||
|
||
override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) { | ||
test(codegenContext, rustCrate) | ||
} | ||
} | ||
RustClientCodegenPlugin().executeWithDecorator(ctx, codegenDecorator, *additionalDecorators.toTypedArray()) | ||
} | ||
return codegenIntegrationTest(model, params, invokePlugin = ::invokeRustCodegenPlugin) | ||
} | ||
|
||
/** | ||
* A `SmithyBuildPlugin` that accepts an additional decorator. | ||
* | ||
* This exists to allow tests to easily customize the _real_ build without needing to list out customizations | ||
* or attempt to manually discover them from the path. | ||
*/ | ||
abstract class ClientDecoratableBuildPlugin : SmithyBuildPlugin { | ||
abstract fun executeWithDecorator( | ||
context: PluginContext, | ||
vararg decorator: ClientCodegenDecorator, | ||
) | ||
|
||
override fun execute(context: PluginContext) { | ||
executeWithDecorator(context) | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rust.codegen.core.testutil | ||
|
||
import software.amazon.smithy.build.PluginContext | ||
import software.amazon.smithy.model.Model | ||
import software.amazon.smithy.model.node.ObjectNode | ||
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig | ||
import software.amazon.smithy.rust.codegen.core.util.runCommand | ||
import java.io.File | ||
import java.nio.file.Path | ||
|
||
/** | ||
* A helper class holding common data with defaults that is threaded through several functions, to make their | ||
* signatures shorter. | ||
*/ | ||
data class IntegrationTestParams( | ||
val addModuleToEventStreamAllowList: Boolean = false, | ||
val service: String? = null, | ||
val runtimeConfig: RuntimeConfig? = null, | ||
val additionalSettings: ObjectNode = ObjectNode.builder().build(), | ||
val overrideTestDir: File? = null, | ||
val command: ((Path) -> Unit)? = null, | ||
) | ||
|
||
/** | ||
* Run cargo test on a true, end-to-end, codegen product of a given model. | ||
*/ | ||
fun codegenIntegrationTest(model: Model, params: IntegrationTestParams, invokePlugin: (PluginContext) -> Unit): Path { | ||
val (ctx, testDir) = generatePluginContext( | ||
model, | ||
params.additionalSettings, | ||
params.addModuleToEventStreamAllowList, | ||
params.service, | ||
params.runtimeConfig, | ||
params.overrideTestDir, | ||
) | ||
invokePlugin(ctx) | ||
ctx.fileManifest.printGeneratedFiles() | ||
params.command?.invoke(testDir) ?: "cargo test".runCommand(testDir, environment = mapOf("RUSTFLAGS" to "-D warnings")) | ||
return testDir | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.RustModule | |
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter | ||
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext | ||
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget | ||
import software.amazon.smithy.rust.codegen.core.smithy.DirectedWalker | ||
import software.amazon.smithy.rust.codegen.core.smithy.ErrorsModule | ||
import software.amazon.smithy.rust.codegen.core.smithy.ModelsModule | ||
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType | ||
|
@@ -118,11 +119,15 @@ object EventStreamTestTools { | |
val symbolProvider = codegenContext.symbolProvider | ||
val operationShape = model.expectShape(ShapeId.from("test#TestStreamOp")) as OperationShape | ||
val unionShape = model.expectShape(ShapeId.from("test#TestStream")) as UnionShape | ||
val walker = DirectedWalker(model) | ||
|
||
val project = TestWorkspace.testProject(symbolProvider) | ||
val operationSymbol = symbolProvider.toSymbol(operationShape) | ||
project.withModule(ErrorsModule) { | ||
val errors = model.structureShapes.filter { shape -> shape.hasTrait<ErrorTrait>() } | ||
val errors = model.serviceShapes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jdisanti What do you think about replacing these tests:
by simple There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be great! |
||
.flatMap { walker.walkShapes(it) } | ||
.filterIsInstance<StructureShape>() | ||
.filter { shape -> shape.hasTrait<ErrorTrait>() } | ||
requirements.renderOperationError(this, model, symbolProvider, operationSymbol, errors) | ||
requirements.renderOperationError(this, model, symbolProvider, symbolProvider.toSymbol(unionShape), errors) | ||
for (shape in errors) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!