Skip to content

Commit 6457d05

Browse files
Make protocol test generation a part of protocols
This updates the way protocol tests are generated. Instead of always generating http protocol tests, it now relies on protocols to initiate test generation. This is important for non-http protocols. It's also important for being able to customize the generation for http protocols. There is an unfortunate implication - a test file will always be generated now. This shouldn't *really* matter since that won't be part of the uploaded artifacts anyway.
1 parent 0c541c0 commit 6457d05

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenVisitor.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Comparator;
2121
import java.util.HashMap;
2222
import java.util.List;
23+
import java.util.Locale;
2324
import java.util.Map;
2425
import java.util.Objects;
2526
import java.util.ServiceLoader;
@@ -214,8 +215,20 @@ void execute() {
214215
// Generate protocol tests IFF found in the model.
215216
if (protocolGenerator != null) {
216217
ShapeId protocol = protocolGenerator.getProtocol();
217-
new HttpProtocolTestGenerator(
218-
settings, model, protocol, symbolProvider, writers, protocolGenerator).run();
218+
ProtocolGenerator.GenerationContext context = new ProtocolGenerator.GenerationContext();
219+
context.setProtocolName(protocolGenerator.getName());
220+
context.setIntegrations(integrations);
221+
context.setModel(model);
222+
context.setService(service);
223+
context.setSettings(settings);
224+
context.setSymbolProvider(symbolProvider);
225+
String baseName = protocol.getName().toLowerCase(Locale.US)
226+
.replace("-", "_")
227+
.replace(".", "_");
228+
writers.useFileWriter(String.format("tests/functional/%s.spec.ts", baseName), writer -> {
229+
context.setWriter(writer);
230+
protocolGenerator.generateProtocolTests(context);
231+
});
219232
}
220233

221234
// Write each pending writer.

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/HttpProtocolTestGenerator.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import software.amazon.smithy.protocoltests.traits.HttpResponseTestCase;
6464
import software.amazon.smithy.protocoltests.traits.HttpResponseTestsTrait;
6565
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
66+
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator.GenerationContext;
6667
import software.amazon.smithy.utils.IoUtils;
6768
import software.amazon.smithy.utils.MapUtils;
6869
import software.amazon.smithy.utils.Pair;
@@ -81,7 +82,7 @@
8182
* TODO: try/catch and if/else are still cumbersome with TypeScriptWriter.
8283
*/
8384
@SmithyInternalApi
84-
final class HttpProtocolTestGenerator implements Runnable {
85+
public final class HttpProtocolTestGenerator implements Runnable {
8586

8687
private static final Logger LOGGER = Logger.getLogger(HttpProtocolTestGenerator.class.getName());
8788
private static final String TEST_CASE_FILE_TEMPLATE = "tests/functional/%s.spec.ts";
@@ -96,42 +97,30 @@ final class HttpProtocolTestGenerator implements Runnable {
9697
private final ProtocolGenerator protocolGenerator;
9798
private final TestFilter testFilter;
9899

99-
/** Vends a TypeScript IFF it's needed. */
100-
private final TypeScriptDelegator delegator;
101-
102-
/** The TypeScript writer that's only allocated once if needed. */
103100
private TypeScriptWriter writer;
101+
private boolean writerInitialized = false;
104102

105103
HttpProtocolTestGenerator(
106-
TypeScriptSettings settings,
107-
Model model,
108-
ShapeId protocol,
109-
SymbolProvider symbolProvider,
110-
TypeScriptDelegator delegator,
104+
GenerationContext context,
111105
ProtocolGenerator protocolGenerator,
112106
TestFilter testFilter
113107
) {
114-
this.settings = settings;
115-
this.model = model;
116-
this.protocol = protocol;
108+
this.settings = context.getSettings();
109+
this.model = context.getModel();
110+
this.protocol = context.getSettings().getProtocol();
117111
this.service = settings.getService(model);
118-
this.symbolProvider = symbolProvider;
119-
this.delegator = delegator;
112+
this.symbolProvider = context.getSymbolProvider();
113+
this.writer = context.getWriter();
120114
this.protocolGenerator = protocolGenerator;
121115
serviceSymbol = symbolProvider.toSymbol(service);
122116
this.testFilter = testFilter;
123117
}
124118

125119
HttpProtocolTestGenerator(
126-
TypeScriptSettings settings,
127-
Model model,
128-
ShapeId protocol,
129-
SymbolProvider symbolProvider,
130-
TypeScriptDelegator delegator,
120+
GenerationContext context,
131121
ProtocolGenerator protocolGenerator
132122
) {
133-
this(settings, model, protocol, symbolProvider, delegator, protocolGenerator,
134-
(service, operation, testCase, typeScriptSettings) -> false);
123+
this(context, protocolGenerator, (service, operation, testCase, typeScriptSettings) -> false);
135124
}
136125

137126
@Override
@@ -215,18 +204,18 @@ private void generateServerOperationTests(OperationShape operation, OperationInd
215204
private <T extends HttpMessageTestCase> void onlyIfProtocolMatches(T testCase, Runnable runnable) {
216205
if (testCase.getProtocol().equals(protocol)) {
217206
LOGGER.fine(() -> format("Generating protocol test case for %s.%s", service.getId(), testCase.getId()));
218-
allocateWriterIfNeeded();
207+
initializeWriterIfNeeded();
219208
runnable.run();
220209
}
221210
}
222211

223-
private void allocateWriterIfNeeded() {
224-
if (writer == null) {
225-
delegator.useFileWriter(createTestCaseFilename(), writer -> this.writer = writer);
212+
private void initializeWriterIfNeeded() {
213+
if (!writerInitialized) {
226214
writer.addDependency(TypeScriptDependency.AWS_SDK_TYPES);
227215
writer.addDependency(TypeScriptDependency.AWS_SDK_PROTOCOL_HTTP);
228216
// Add the template to each generated test.
229217
writer.write(IoUtils.readUtf8Resource(getClass(), "protocol-test-stub.ts"));
218+
writerInitialized = true;
230219
}
231220
}
232221

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/ProtocolGenerator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ default void generateSharedComponents(GenerationContext context) {
183183
*/
184184
void generateResponseDeserializers(GenerationContext context);
185185

186+
/**
187+
* Generates protocol tests to assert the protocol works properly.
188+
*
189+
* @param context Generation context.
190+
*/
191+
void generateProtocolTests(GenerationContext context);
192+
186193
/**
187194
* Generates the name of a serializer function for shapes of a service.
188195
*

0 commit comments

Comments
 (0)