Skip to content

Commit f7abe54

Browse files
authored
chore: generate test targets if needed (#302)
1 parent 84157ab commit f7abe54

File tree

8 files changed

+30
-29
lines changed

8 files changed

+30
-29
lines changed

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/CodegenVisitor.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class CodegenVisitor(context: PluginContext) : ShapeVisitor.Default<Void>() {
9090
println("Walking shapes from " + service.id + " to find shapes to generate")
9191
val serviceShapes: Set<Shape> = Walker(model).walkShapes(service)
9292
serviceShapes.forEach { it.accept(this) }
93-
var generateTestTarget = false
93+
var shouldGenerateTestTarget = false
9494
protocolGenerator?.apply {
9595
val ctx = ProtocolGenerator.GenerationContext(
9696
settings,
@@ -107,9 +107,8 @@ class CodegenVisitor(context: PluginContext) : ShapeVisitor.Default<Void>() {
107107
generateCodableConformanceForNestedTypes(ctx)
108108

109109
LOGGER.info("[${service.id}] Generating unit tests for protocol ${protocolGenerator.protocol}")
110-
generateProtocolUnitTests(ctx)
111-
112-
generateTestTarget = ctx.settings.shouldGenerateUnitTestTarget
110+
val numProtocolUnitTestsGenerated = generateProtocolUnitTests(ctx)
111+
shouldGenerateTestTarget = (numProtocolUnitTestsGenerated > 0)
113112

114113
LOGGER.info("[${service.id}] Generating service client for protocol ${protocolGenerator.protocol}")
115114
generateProtocolClient(ctx)
@@ -120,7 +119,7 @@ class CodegenVisitor(context: PluginContext) : ShapeVisitor.Default<Void>() {
120119
writers.flushWriters()
121120

122121
println("Generating package manifest file")
123-
writePackageManifest(settings, fileManifest, dependencies, generateTestTarget)
122+
writePackageManifest(settings, fileManifest, dependencies, shouldGenerateTestTarget)
124123
}
125124

126125
override fun getDefault(shape: Shape?): Void? {

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/SwiftSettings.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ private const val HOMEPAGE = "homepage"
2626
private const val SDK_ID = "sdkId"
2727
private const val GIT_REPO = "gitRepo"
2828
private const val SWIFT_VERSION = "swiftVersion"
29-
private const val SHOULD_GENERATE_UNIT_TEST_TARGET = "shouldGenerateUnitTestTarget"
3029

3130
class SwiftSettings(
3231
val service: ShapeId,
@@ -37,8 +36,7 @@ class SwiftSettings(
3736
val homepage: String,
3837
val sdkId: String,
3938
val gitRepo: String,
40-
val swiftVersion: String,
41-
val shouldGenerateUnitTestTarget: Boolean
39+
val swiftVersion: String
4240
) {
4341

4442
companion object {
@@ -55,7 +53,7 @@ class SwiftSettings(
5553
* @return Returns the extracted settings
5654
*/
5755
fun from(model: Model, config: ObjectNode): SwiftSettings {
58-
config.warnIfAdditionalProperties(listOf(SERVICE, MODULE_NAME, MODULE_DESCRIPTION, MODULE_VERSION, AUTHOR, SDK_ID, HOMEPAGE, GIT_REPO, SWIFT_VERSION, SHOULD_GENERATE_UNIT_TEST_TARGET))
56+
config.warnIfAdditionalProperties(listOf(SERVICE, MODULE_NAME, MODULE_DESCRIPTION, MODULE_VERSION, AUTHOR, SDK_ID, HOMEPAGE, GIT_REPO, SWIFT_VERSION))
5957

6058
val serviceId = config.getStringMember(SERVICE)
6159
.map(StringNode::expectShapeId)
@@ -69,9 +67,8 @@ class SwiftSettings(
6967
val gitRepo = config.expectStringMember(GIT_REPO).value
7068
val swiftVersion = config.expectStringMember(SWIFT_VERSION).value
7169
val sdkId = sanitizeSdkId(config.getStringMemberOrDefault(SDK_ID, serviceId.name))
72-
val shouldGenerateUnitTestTarget = config.getBooleanMemberOrDefault(SHOULD_GENERATE_UNIT_TEST_TARGET, false)
7370

74-
return SwiftSettings(serviceId, moduleName, version, desc, author, homepage, sdkId, gitRepo, swiftVersion, shouldGenerateUnitTestTarget)
71+
return SwiftSettings(serviceId, moduleName, version, desc, author, homepage, sdkId, gitRepo, swiftVersion)
7572
}
7673

7774
private fun sanitizeSdkId(sdkId: String): String {

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/HttpProtocolTestGenerator.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,19 @@ class HttpProtocolTestGenerator(
3737
/**
3838
* Generates the API HTTP protocol tests defined in the smithy model.
3939
*/
40-
fun generateProtocolTests() {
40+
fun generateProtocolTests(): Int {
4141
val topDownIndex: TopDownIndex = TopDownIndex.of(ctx.model)
4242
val serviceSymbol = ctx.symbolProvider.toSymbol(ctx.service)
43-
43+
var numTests = 0
4444
for (operation in TreeSet(topDownIndex.getContainedOperations(ctx.service).filterNot(::serverOnly))) {
45-
renderRequestTests(operation, serviceSymbol)
46-
renderResponseTests(operation, serviceSymbol)
47-
renderErrorTestCases(operation, serviceSymbol)
45+
numTests += renderRequestTests(operation, serviceSymbol)
46+
numTests += renderResponseTests(operation, serviceSymbol)
47+
numTests += renderErrorTestCases(operation, serviceSymbol)
4848
}
49+
return numTests
4950
}
5051

51-
private fun renderRequestTests(operation: OperationShape, serviceSymbol: Symbol) {
52+
private fun renderRequestTests(operation: OperationShape, serviceSymbol: Symbol): Int {
5253
val tempTestCases = operation.getTrait(HttpRequestTestsTrait::class.java)
5354
.getOrNull()
5455
?.getTestCasesFor(AppliesTo.CLIENT)
@@ -78,9 +79,10 @@ class HttpProtocolTestGenerator(
7879
.renderTestClass(testClassName)
7980
}
8081
}
82+
return requestTestCases.count()
8183
}
8284

83-
private fun renderResponseTests(operation: OperationShape, serviceSymbol: Symbol) {
85+
private fun renderResponseTests(operation: OperationShape, serviceSymbol: Symbol): Int {
8486
val tempResponseTests = operation.getTrait(HttpResponseTestsTrait::class.java)
8587
.getOrNull()
8688
?.getTestCasesFor(AppliesTo.CLIENT)
@@ -110,17 +112,19 @@ class HttpProtocolTestGenerator(
110112
.renderTestClass(testClassName)
111113
}
112114
}
115+
return responseTestCases.count()
113116
}
114117

115-
private fun renderErrorTestCases(operation: OperationShape, serviceSymbol: Symbol) {
118+
private fun renderErrorTestCases(operation: OperationShape, serviceSymbol: Symbol): Int {
116119
val operationIndex: OperationIndex = OperationIndex.of(ctx.model)
117-
120+
var numTestCases = 0
118121
for (error in operationIndex.getErrors(operation).filterNot(::serverOnly)) {
119122
val tempTestCases = error.getTrait(HttpResponseTestsTrait::class.java)
120123
.getOrNull()
121124
?.getTestCasesFor(AppliesTo.CLIENT)
122125
.orEmpty()
123126
val testCases = filterProtocolTestCases(tempTestCases)
127+
numTestCases += testCases.count()
124128
if (testCases.isNotEmpty()) {
125129
// multiple error (tests) may be associated with a single operation,
126130
// use the operation name + error name as the class name
@@ -150,6 +154,7 @@ class HttpProtocolTestGenerator(
150154
}
151155
}
152156
}
157+
return numTestCases
153158
}
154159

155160
private fun <T : HttpMessageTestCase> filterProtocolTestCases(testCases: List<T>): List<T> = testCases.filter {

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/integration/ProtocolGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ interface ProtocolGenerator {
121121
*
122122
* Generate unit tests for the protocol
123123
*/
124-
fun generateProtocolUnitTests(ctx: GenerationContext)
124+
fun generateProtocolUnitTests(ctx: GenerationContext): Int
125125

126126
/**
127127
* Generate an actual client implementation of the service interface

smithy-swift-codegen/src/test/kotlin/mocks/MockHttpAWSQueryProtocolGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ class MockHttpAWSQueryProtocolGenerator : HttpBindingProtocolGenerator() {
101101
return true
102102
}
103103

104-
override fun generateProtocolUnitTests(ctx: ProtocolGenerator.GenerationContext) {
104+
override fun generateProtocolUnitTests(ctx: ProtocolGenerator.GenerationContext): Int {
105105
val requestTestBuilder = HttpProtocolUnitTestRequestGenerator.Builder()
106106
val responseTestBuilder = HttpProtocolUnitTestResponseGenerator.Builder()
107107
val errorTestBuilder = HttpProtocolUnitTestErrorGenerator.Builder()
108108

109-
HttpProtocolTestGenerator(
109+
return HttpProtocolTestGenerator(
110110
ctx,
111111
requestTestBuilder,
112112
responseTestBuilder,

smithy-swift-codegen/src/test/kotlin/mocks/MockHttpEC2QueryProtocolGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ class MockHttpEC2QueryProtocolGenerator : HttpBindingProtocolGenerator() {
100100
return true
101101
}
102102

103-
override fun generateProtocolUnitTests(ctx: ProtocolGenerator.GenerationContext) {
103+
override fun generateProtocolUnitTests(ctx: ProtocolGenerator.GenerationContext): Int {
104104
val requestTestBuilder = HttpProtocolUnitTestRequestGenerator.Builder()
105105
val responseTestBuilder = HttpProtocolUnitTestResponseGenerator.Builder()
106106
val errorTestBuilder = HttpProtocolUnitTestErrorGenerator.Builder()
107107

108-
HttpProtocolTestGenerator(
108+
return HttpProtocolTestGenerator(
109109
ctx,
110110
requestTestBuilder,
111111
responseTestBuilder,

smithy-swift-codegen/src/test/kotlin/mocks/MockHttpRestJsonProtocolGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ class MockHttpRestJsonProtocolGenerator : HttpBindingProtocolGenerator() {
8585
decodeGenerator.render()
8686
}
8787

88-
override fun generateProtocolUnitTests(ctx: ProtocolGenerator.GenerationContext) {
88+
override fun generateProtocolUnitTests(ctx: ProtocolGenerator.GenerationContext): Int {
8989
val requestTestBuilder = HttpProtocolUnitTestRequestGenerator.Builder()
9090
val responseTestBuilder = HttpProtocolUnitTestResponseGenerator.Builder()
9191
val errorTestBuilder = HttpProtocolUnitTestErrorGenerator.Builder()
9292

93-
HttpProtocolTestGenerator(
93+
return HttpProtocolTestGenerator(
9494
ctx,
9595
requestTestBuilder,
9696
responseTestBuilder,

smithy-swift-codegen/src/test/kotlin/mocks/MockHttpRestXMLProtocolGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ class MockHttpRestXMLProtocolGenerator : HttpBindingProtocolGenerator() {
6565
val decodeGenerator = StructDecodeXMLGenerator(ctx, members, mapOf(), writer, defaultTimestampFormat)
6666
decodeGenerator.render()
6767
}
68-
override fun generateProtocolUnitTests(ctx: ProtocolGenerator.GenerationContext) {
68+
override fun generateProtocolUnitTests(ctx: ProtocolGenerator.GenerationContext): Int {
6969
val requestTestBuilder = HttpProtocolUnitTestRequestGenerator.Builder()
7070
val responseTestBuilder = HttpProtocolUnitTestResponseGenerator.Builder()
7171
val errorTestBuilder = HttpProtocolUnitTestErrorGenerator.Builder()
7272

73-
HttpProtocolTestGenerator(
73+
return HttpProtocolTestGenerator(
7474
ctx,
7575
requestTestBuilder,
7676
responseTestBuilder,

0 commit comments

Comments
 (0)