-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathscenarios.go
More file actions
302 lines (266 loc) · 8.65 KB
/
scenarios.go
File metadata and controls
302 lines (266 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package codegen
import (
"fmt"
"maps"
"os"
"path/filepath"
"slices"
"goa.design/goa/v3/codegen"
"goa.design/goa/v3/codegen/service"
"goa.design/goa/v3/expr"
"gopkg.in/yaml.v3"
)
// generateScenarios generates the scenario runner for a service.
func generateScenarios(genpkg string, svcData *service.Data, root *expr.RootExpr, svc *expr.ServiceExpr) []*codegen.File {
if svcData == nil {
return nil
}
data := buildScenariosData(svcData, root, svc)
files := make([]*codegen.File, 0, 2)
// Generate main scenarios runner file
path := filepath.Join(testingPath(genpkg, svc), "scenarios.go")
specs := []*codegen.ImportSpec{
{Path: "context"},
{Path: "encoding/json"},
{Path: "fmt"},
{Path: "os"},
{Path: "strings"},
{Path: "testing"},
{Path: "time"},
{Path: "gopkg.in/yaml.v3", Name: "yaml"},
{Path: filepath.Join(genpkg, codegen.SnakeCase(svc.Name)), Name: svcData.PkgName},
}
// Add validator package import if specified in YAML
// Only add if it's different from the current package
currentPkgPath := filepath.Join(genpkg, codegen.SnakeCase(svc.Name), codegen.SnakeCase(svc.Name)+"test")
validatorInfo := ExtractValidatorsFromYAML()
if validatorInfo.Path != "" && validatorInfo.Package != "" && validatorInfo.Path != currentPkgPath {
specs = append(specs, &codegen.ImportSpec{
Path: validatorInfo.Path,
Name: validatorInfo.Package,
})
// Set the package name for use in template
data.ValidatorPkg = validatorInfo.Package
}
sections := []*codegen.SectionTemplate{
codegen.Header(fmt.Sprintf("Scenario runner for %s service", svc.Name), codegen.SnakeCase(svc.Name)+"test", specs),
{
Name: "scenario-types",
Source: testingTemplates.Read("scenario_types"),
Data: data,
},
{
Name: "scenario-runner",
Source: testingTemplates.Read("scenario_runner"),
Data: data,
},
}
files = append(files, &codegen.File{
Path: path,
SectionTemplates: sections,
})
return files
}
type (
// scenariosData contains the data used to generate scenario runner code.
// It embeds Goa's service.Data to leverage existing codegen structures.
scenariosData struct {
// Embed the base service data from Goa's codegen
*service.Data
// Service expression for additional metadata
ServiceExpr *expr.ServiceExpr
// Methods with scenario-specific extensions
Methods []*scenarioMethodData
// HasHTTP indicates if service has HTTP transport
HasHTTP bool
// HasGRPC indicates if service has gRPC transport
HasGRPC bool
// HasJSONRPC indicates if service has JSON-RPC transport
HasJSONRPC bool
// ValidTransports is the list of all valid transports
ValidTransports []string
// Validators found in scenarios.yaml
Validators map[string][]string // method -> validator names
// ValidatorPkg is the package name for validators
ValidatorPkg string
// ValidatorPath is the import path for the validator package
ValidatorPath string
}
// scenarioMethodData extends Goa's MethodData for scenario generation.
scenarioMethodData struct {
// Embed the base method data from Goa's codegen
*service.MethodData
// Transports lists valid transport strings for YAML
Transports []string
// ResultTypeRef is the fully qualified Go reference to the result type
// as seen from the generated test package (e.g. "*svc.Foo", "[]*svc.Bar").
// This is used in generated type assertions for custom validators.
ResultTypeRef string
}
)
// generateExampleScenarios generates an example scenarios.yaml file for a service.
func generateExampleScenarios(_ string, root *expr.RootExpr, svc *expr.ServiceExpr) *codegen.File {
path := filepath.Join(codegen.Gendir, "..", "scenarios.yaml")
svcData := service.NewServicesData(root).Get(svc.Name)
if svcData == nil {
return nil
}
data := buildScenariosData(svcData, root, svc)
// For YAML files, we need to read the template directly since it's not a .go.tpl file
tmplContent, err := templateFS.ReadFile("templates/example_scenarios.yaml.tpl")
if err != nil {
panic(fmt.Sprintf("failed to read example_scenarios.yaml.tpl: %v", err))
}
sections := []*codegen.SectionTemplate{
{
Name: "example-scenarios",
Source: string(tmplContent),
Data: data,
},
}
return &codegen.File{
Path: path,
SectionTemplates: sections,
SkipExist: true, // Don't overwrite existing scenarios file
}
}
func buildScenariosData(svcData *service.Data, root *expr.RootExpr, svc *expr.ServiceExpr) *scenariosData {
// Extract validator info from YAML
validatorInfo := ExtractValidatorsFromYAML()
data := &scenariosData{
Data: svcData,
ServiceExpr: svc,
Methods: make([]*scenarioMethodData, 0),
HasHTTP: hasHTTPTransport(root, svc),
HasGRPC: hasGRPCTransport(root, svc),
HasJSONRPC: hasJSONRPCTransport(root, svc),
ValidTransports: make([]string, 0),
Validators: validatorInfo.Validators,
ValidatorPkg: "", // Will be set below if it's a different package
ValidatorPath: validatorInfo.Path,
}
// Build list of valid transports
transportSet := make(map[string]bool)
transportSet["auto"] = true
if data.HasHTTP {
transportSet["http"] = true
transportSet["http-sse"] = true
transportSet["http-ws"] = true
}
if data.HasGRPC {
transportSet["grpc"] = true
}
if data.HasJSONRPC {
transportSet["jsonrpc"] = true
transportSet["jsonrpc-sse"] = true
transportSet["jsonrpc-ws"] = true
}
data.ValidTransports = slices.Sorted(maps.Keys(transportSet))
// Build method data with available transports
for i, m := range svc.Methods {
methodData := svcData.Methods[i]
// Build targets for this method using shared function
targets := buildMethodTargets(root, svc, m, methodData)
md := &scenarioMethodData{
MethodData: methodData,
Transports: make([]string, 0),
}
// Compute fully qualified result type reference for type assertions.
// This properly handles composite types like ArrayOf(...) without producing
// invalid Go like "svc.[]T" (see issue #234).
if m.Result != nil && m.Result.Type != expr.Empty {
md.ResultTypeRef = svcData.Scope.GoFullTypeRef(m.Result, svcData.PkgName)
}
// Build list of valid transport strings based on targets
transportSet := make(map[string]bool)
for _, target := range targets {
switch {
case target.IsGRPC:
transportSet["grpc"] = true
case target.IsHTTPPlain:
transportSet["http"] = true
case target.IsHTTPServerSent:
transportSet["http-sse"] = true
case target.IsHTTPWebSocket:
transportSet["http-ws"] = true
case target.IsJSONRPCPlain:
transportSet["jsonrpc"] = true
case target.IsJSONRPCSSE:
transportSet["jsonrpc-sse"] = true
case target.IsJSONRPCWS:
transportSet["jsonrpc-ws"] = true
}
}
// Convert set to sorted list
md.Transports = slices.Sorted(maps.Keys(transportSet))
data.Methods = append(data.Methods, md)
}
return data
}
// ValidatorInfo holds validator configuration
type ValidatorInfo struct {
Validators map[string][]string // method -> validator names
Package string // package name containing validators
Path string // import path for the package
}
// ExtractValidatorsFromYAML reads scenarios.yaml if it exists and extracts validator names and package
func ExtractValidatorsFromYAML() ValidatorInfo {
info := ValidatorInfo{
Validators: make(map[string][]string),
Package: "", // Empty means use current package
}
// Try to read scenarios.yaml from current directory
data, err := os.ReadFile("scenarios.yaml")
if err != nil {
// File doesn't exist or can't be read, that's OK
return info
}
if len(data) == 0 {
// Empty file
return info
}
var config struct {
Validators struct {
Package string `yaml:"package"`
Path string `yaml:"path"`
} `yaml:"validators"`
Scenarios []struct {
Steps []struct {
Method string `yaml:"method"`
Expect struct {
Validator string `yaml:"validator"`
} `yaml:"expect"`
} `yaml:"steps"`
} `yaml:"scenarios"`
}
if err := yaml.Unmarshal(data, &config); err != nil {
// Invalid YAML, skip
return info
}
// Extract package info
info.Package = config.Validators.Package
info.Path = config.Validators.Path
// Extract unique validators per method
for _, scenario := range config.Scenarios {
if scenario.Steps == nil {
continue
}
for _, step := range scenario.Steps {
if step.Method == "" || step.Expect.Validator == "" {
continue
}
// Add to the list if not already there
found := false
for _, v := range info.Validators[step.Method] {
if v == step.Expect.Validator {
found = true
break
}
}
if !found {
info.Validators[step.Method] = append(info.Validators[step.Method], step.Expect.Validator)
}
}
}
return info
}