|
| 1 | +/* |
| 2 | + * Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.smithy.go.codegen.integration; |
| 17 | + |
| 18 | +import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; |
| 19 | + |
| 20 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 21 | +import software.amazon.smithy.go.codegen.GoDelegator; |
| 22 | +import software.amazon.smithy.go.codegen.GoSettings; |
| 23 | +import software.amazon.smithy.go.codegen.GoStdlibTypes; |
| 24 | +import software.amazon.smithy.go.codegen.GoWriter; |
| 25 | +import software.amazon.smithy.go.codegen.SmithyGoDependency; |
| 26 | +import software.amazon.smithy.go.codegen.SmithyGoTypes; |
| 27 | +import software.amazon.smithy.model.Model; |
| 28 | +import software.amazon.smithy.model.knowledge.TopDownIndex; |
| 29 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 30 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 31 | +import software.amazon.smithy.utils.MapUtils; |
| 32 | + |
| 33 | +public class MiddlewareStackSnapshotTests implements GoIntegration { |
| 34 | + @Override |
| 35 | + public void writeAdditionalFiles( |
| 36 | + GoSettings settings, Model model, SymbolProvider symbolProvider, GoDelegator goDelegator |
| 37 | + ) { |
| 38 | + goDelegator.useFileWriter("snapshot_test.go", settings.getModuleName(), writer -> { |
| 39 | + writer.addBuildTag("snapshot"); |
| 40 | + writer.write(commonTestSource()); |
| 41 | + writer.write(snapshotTests(model, settings.getService(model), symbolProvider)); |
| 42 | + writer.write(snapshotUpdaters(model, settings.getService(model), symbolProvider)); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + private GoWriter.Writable commonTestSource() { |
| 47 | + return goTemplate(""" |
| 48 | + $os:D $fs:D $io:D $errors:D $fmt:D $middleware:D |
| 49 | +
|
| 50 | + const ssprefix = "snapshot" |
| 51 | +
|
| 52 | + type snapshotOK struct{} |
| 53 | +
|
| 54 | + func (snapshotOK) Error() string { return "error: success" } |
| 55 | +
|
| 56 | + func createp(path string) (*os.File, error) { |
| 57 | + if err := os.Mkdir(ssprefix, 0700); err != nil && !errors.Is(err, fs.ErrExist) { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + return os.Create(path) |
| 61 | + } |
| 62 | +
|
| 63 | + func sspath(op string) string { |
| 64 | + return fmt.Sprintf("%s/api_op_%s.go.snap", ssprefix, op) |
| 65 | + } |
| 66 | +
|
| 67 | + func updateSnapshot(stack *middleware.Stack, operation string) error { |
| 68 | + f, err := createp(sspath(operation)) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + defer f.Close() |
| 73 | + if _, err := f.Write([]byte(stack.String())); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + return snapshotOK{} |
| 77 | + } |
| 78 | +
|
| 79 | + func testSnapshot(stack *middleware.Stack, operation string) error { |
| 80 | + f, err := os.Open(sspath(operation)) |
| 81 | + if errors.Is(err, fs.ErrNotExist) { |
| 82 | + return snapshotOK{} |
| 83 | + } |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + defer f.Close() |
| 88 | + expected, err := io.ReadAll(f) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + if actual := stack.String(); actual != string(expected) { |
| 93 | + return fmt.Errorf("%s != %s", expected, actual) |
| 94 | + } |
| 95 | + return snapshotOK{} |
| 96 | + } |
| 97 | + """, |
| 98 | + MapUtils.of( |
| 99 | + "errors", SmithyGoDependency.ERRORS, "fmt", SmithyGoDependency.FMT, |
| 100 | + "fs", SmithyGoDependency.FS, "io", SmithyGoDependency.IO, |
| 101 | + "middleware", SmithyGoDependency.SMITHY_MIDDLEWARE, "os", SmithyGoDependency.OS |
| 102 | + )); |
| 103 | + } |
| 104 | + |
| 105 | + private GoWriter.Writable snapshotUpdaters(Model model, ServiceShape service, SymbolProvider symbolProvider) { |
| 106 | + return GoWriter.ChainWritable.of( |
| 107 | + TopDownIndex.of(model).getContainedOperations(service).stream() |
| 108 | + .map(it -> testUpdateSnapshot(it, symbolProvider)) |
| 109 | + .toList() |
| 110 | + ).compose(); |
| 111 | + } |
| 112 | + |
| 113 | + private GoWriter.Writable snapshotTests(Model model, ServiceShape service, SymbolProvider symbolProvider) { |
| 114 | + return GoWriter.ChainWritable.of( |
| 115 | + TopDownIndex.of(model).getContainedOperations(service).stream() |
| 116 | + .map(it -> testCheckSnapshot(it, symbolProvider)) |
| 117 | + .toList() |
| 118 | + ).compose(); |
| 119 | + } |
| 120 | + |
| 121 | + private GoWriter.Writable testUpdateSnapshot(OperationShape operation, SymbolProvider symbolProvider) { |
| 122 | + return goTemplate(""" |
| 123 | + func TestUpdateSnapshot_$operation:L(t $testingT:P) { |
| 124 | + svc := New(Options{}) |
| 125 | + _, err := svc.$operation:L($contextBackground:T(), nil, func(o *Options) { |
| 126 | + o.APIOptions = append(o.APIOptions, func(stack $middlewareStack:P) error { |
| 127 | + return updateSnapshot(stack, $operation:S) |
| 128 | + }) |
| 129 | + }) |
| 130 | + if _, ok := err.(snapshotOK); !ok && err != nil { |
| 131 | + t.Fatal(err) |
| 132 | + } |
| 133 | + } |
| 134 | + """, |
| 135 | + MapUtils.of( |
| 136 | + "testingT", GoStdlibTypes.Testing.T, |
| 137 | + "contextBackground", GoStdlibTypes.Context.Background, |
| 138 | + "middlewareStack", SmithyGoTypes.Middleware.Stack, |
| 139 | + "operation", symbolProvider.toSymbol(operation).getName() |
| 140 | + )); |
| 141 | + } |
| 142 | + |
| 143 | + private GoWriter.Writable testCheckSnapshot(OperationShape operation, SymbolProvider symbolProvider) { |
| 144 | + return goTemplate(""" |
| 145 | + func TestCheckSnapshot_$operation:L(t $testingT:P) { |
| 146 | + svc := New(Options{}) |
| 147 | + _, err := svc.$operation:L($contextBackground:T(), nil, func(o *Options) { |
| 148 | + o.APIOptions = append(o.APIOptions, func(stack $middlewareStack:P) error { |
| 149 | + return testSnapshot(stack, $operation:S) |
| 150 | + }) |
| 151 | + }) |
| 152 | + if _, ok := err.(snapshotOK); !ok && err != nil { |
| 153 | + t.Fatal(err) |
| 154 | + } |
| 155 | + } |
| 156 | + """, |
| 157 | + MapUtils.of( |
| 158 | + "testingT", GoStdlibTypes.Testing.T, |
| 159 | + "contextBackground", GoStdlibTypes.Context.Background, |
| 160 | + "middlewareStack", SmithyGoTypes.Middleware.Stack, |
| 161 | + "operation", symbolProvider.toSymbol(operation).getName() |
| 162 | + )); |
| 163 | + } |
| 164 | +} |
0 commit comments