|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +import aws.sdk.kotlin.gradle.codegen.dsl.generateSmithyProjections |
| 6 | +import aws.sdk.kotlin.gradle.codegen.dsl.smithyKotlinPlugin |
| 7 | +import aws.sdk.kotlin.gradle.codegen.smithyKotlinProjectionPath |
| 8 | +import aws.sdk.kotlin.gradle.dsl.skipPublishing |
| 9 | + |
| 10 | +plugins { |
| 11 | + kotlin("jvm") |
| 12 | + alias(libs.plugins.aws.kotlin.repo.tools.smithybuild) |
| 13 | +} |
| 14 | + |
| 15 | +description = "Smithy protocol test suite" |
| 16 | + |
| 17 | +skipPublishing() |
| 18 | + |
| 19 | +data class ProtocolTest(val projectionName: String, val serviceShapeId: String, val sdkId: String? = null) { |
| 20 | + val packageName: String = projectionName.lowercase().filter { it.isLetterOrDigit() } |
| 21 | +} |
| 22 | + |
| 23 | +// The following section exposes Smithy protocol test suites as gradle test targets |
| 24 | +// for the configured protocols in [enabledProtocols]. |
| 25 | +val enabledProtocols = listOf( |
| 26 | + ProtocolTest("aws-ec2-query", "aws.protocoltests.ec2#AwsEc2"), |
| 27 | + ProtocolTest("aws-json-10", "aws.protocoltests.json10#JsonRpc10"), |
| 28 | + ProtocolTest("aws-json-11", "aws.protocoltests.json#JsonProtocol"), |
| 29 | + ProtocolTest("aws-restjson", "aws.protocoltests.restjson#RestJson"), |
| 30 | + ProtocolTest("aws-restxml", "aws.protocoltests.restxml#RestXml"), |
| 31 | + ProtocolTest("aws-restxml-xmlns", "aws.protocoltests.restxml.xmlns#RestXmlWithNamespace"), |
| 32 | + ProtocolTest("aws-query", "aws.protocoltests.query#AwsQuery"), |
| 33 | + |
| 34 | + // Custom hand written tests |
| 35 | + ProtocolTest("error-correction-json", "aws.protocoltests.errorcorrection#RequiredValueJson"), |
| 36 | + ProtocolTest("error-correction-xml", "aws.protocoltests.errorcorrection#RequiredValueXml"), |
| 37 | +) |
| 38 | + |
| 39 | +smithyBuild { |
| 40 | + enabledProtocols.forEach { test -> |
| 41 | + projections.register(test.projectionName) { |
| 42 | + imports = listOf(file("model").absolutePath) |
| 43 | + |
| 44 | + transforms = listOf( |
| 45 | + """ |
| 46 | + { |
| 47 | + "name": "includeServices", |
| 48 | + "args": { |
| 49 | + "services": ["${test.serviceShapeId}"] |
| 50 | + } |
| 51 | + } |
| 52 | + """, |
| 53 | + ) |
| 54 | + |
| 55 | + smithyKotlinPlugin { |
| 56 | + serviceShapeId = test.serviceShapeId |
| 57 | + packageName = "aws.sdk.kotlin.services.${test.packageName}" |
| 58 | + packageVersion = "1.0" |
| 59 | + sdkId = test.sdkId |
| 60 | + buildSettings { |
| 61 | + generateFullProject = true |
| 62 | + optInAnnotations = listOf( |
| 63 | + "aws.smithy.kotlin.runtime.InternalApi", |
| 64 | + "aws.sdk.kotlin.runtime.InternalSdkApi", |
| 65 | + ) |
| 66 | + } |
| 67 | + apiSettings { |
| 68 | + defaultValueSerializationMode = "always" |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +val codegen by configurations.getting |
| 76 | +dependencies { |
| 77 | + codegen(project(":codegen:smithy-aws-kotlin-codegen")) |
| 78 | + codegen(libs.smithy.cli) |
| 79 | + codegen(libs.smithy.model) |
| 80 | + |
| 81 | + // NOTE: The protocol tests are published to maven as a jar, this ensures that |
| 82 | + // the aws-protocol-tests dependency is found when generating code such that the `includeServices` transform |
| 83 | + // actually works |
| 84 | + codegen(libs.smithy.aws.protocol.tests) |
| 85 | +} |
| 86 | + |
| 87 | +tasks.generateSmithyProjections { |
| 88 | + // ensure the generated clients use the same version of the runtime as the aws aws-runtime |
| 89 | + val sdkVersion: String by project |
| 90 | + val smithyKotlinRuntimeVersion = sdkVersion |
| 91 | + doFirst { |
| 92 | + System.setProperty("smithy.kotlin.codegen.clientRuntimeVersion", smithyKotlinRuntimeVersion) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +abstract class ProtocolTestTask @Inject constructor(private val project: Project) : DefaultTask() { |
| 97 | + /** |
| 98 | + * The projection |
| 99 | + */ |
| 100 | + @get:Input |
| 101 | + abstract val projectionName: Property<String> |
| 102 | + |
| 103 | + /** |
| 104 | + * The projection root directory |
| 105 | + */ |
| 106 | + @get:Input |
| 107 | + abstract val projectionRootDirectory: Property<String> |
| 108 | + |
| 109 | + @TaskAction |
| 110 | + fun runTests() { |
| 111 | + val projectionRootDir = project.file(projectionRootDirectory.get()) |
| 112 | + println("[$projectionName] buildDir: $projectionRootDir") |
| 113 | + if (!projectionRootDir.exists()) { |
| 114 | + throw GradleException("$projectionRootDir does not exist") |
| 115 | + } |
| 116 | + val wrapper = if (System.getProperty("os.name").lowercase().contains("windows")) "gradlew.bat" else "gradlew" |
| 117 | + val gradlew = project.rootProject.file(wrapper).absolutePath |
| 118 | + |
| 119 | + // NOTE - this still requires us to publish to maven local. |
| 120 | + project.exec { |
| 121 | + workingDir = projectionRootDir |
| 122 | + executable = gradlew |
| 123 | + args = listOf("test") |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +smithyBuild.projections.forEach { |
| 129 | + val protocolName = it.name |
| 130 | + |
| 131 | + tasks.register<ProtocolTestTask>("testProtocol-$protocolName") { |
| 132 | + dependsOn(tasks.generateSmithyProjections) |
| 133 | + group = "Verification" |
| 134 | + projectionName.set(it.name) |
| 135 | + projectionRootDirectory.set(smithyBuild.smithyKotlinProjectionPath(it.name).map { it.toString() }) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +tasks.register("testAllProtocols") { |
| 140 | + group = "Verification" |
| 141 | + val allTests = tasks.withType<ProtocolTestTask>() |
| 142 | + dependsOn(allTests) |
| 143 | +} |
0 commit comments