-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
396 lines (342 loc) 路 12.7 KB
/
Copy pathtypes.go
File metadata and controls
396 lines (342 loc) 路 12.7 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package venom
import (
"context"
"encoding/json"
"encoding/xml"
"fmt"
"maps"
"strings"
"time"
"unicode"
"github.com/fatih/color"
"github.com/spf13/cast"
)
type Status string
const (
StatusRun Status = "RUN"
StatusFail Status = "FAIL"
StatusSkip Status = "SKIP"
StatusPass Status = "PASS"
)
type H map[string]interface{}
func (h H) Clone() H {
return maps.Clone(h)
}
func (h *H) Add(k string, v interface{}) {
if h == nil || *h == nil {
*h = make(map[string]interface{})
}
(*h)[k] = v
}
func (h *H) AddWithPrefix(p, k string, v interface{}) {
(*h)[p+"."+k] = v
}
func (h *H) AddAll(h2 H) {
for k, v := range h2 {
h.Add(k, v)
}
}
func (h *H) AddAllWithPrefix(p string, h2 H) {
if h2 == nil {
return
}
if h == nil {
_h := H{}
*h = _h
}
for k, v := range h2 {
h.AddWithPrefix(p, k, v)
}
}
// Assertion (usually a string, but could be a dictionary when using logical operators)
type Assertion interface{}
// StepAssertions contains step assertions
type StepAssertions struct {
Assertions []Assertion `json:"assertions,omitempty" yaml:"assertions,omitempty"`
}
type TestsXML struct {
XMLName xml.Name `xml:"testsuites" json:"-" yaml:"-"`
TestSuites []TestSuiteXML `xml:"testsuite" json:"test_suites"`
}
type Tests struct {
TestSuites []TestSuite `json:"test_suites" yml:"tests_suites"`
Status Status `json:"status" yml:"status"`
NbTestsuitesFail int `json:"nbTestsuitesFail" yaml:"-"`
NbTestsuitesPass int `json:"nbTestsuitesPass" yaml:"-"`
NbTestsuitesSkip int `json:"nbTestsuitesSkip" yaml:"-"`
Duration float64 `json:"duration" yaml:"-"`
Start time.Time `json:"start" yaml:"-"`
End time.Time `json:"end" yaml:"-"`
}
// TestSuite is a single JUnit test suite which may contain many
// testcases.
type TestSuiteXML struct {
XMLName xml.Name `xml:"testsuite" json:"-" yaml:"-"`
Disabled int `xml:"disabled,attr,omitempty" json:"disabled" yaml:""`
Errors int `xml:"errors,attr,omitempty" json:"errors" yaml:"-"`
Failures int `xml:"failures,attr,omitempty" json:"failures" yaml:"-"`
Hostname string `xml:"hostname,attr,omitempty" json:"hostname" yaml:"-"`
ID string `xml:"id,attr,omitempty" json:"id" yaml:"-"`
Name string `xml:"name,attr" json:"name" yaml:"name"`
Package string `xml:"package,attr,omitempty" json:"package" yaml:"-"`
Skipped int `xml:"skipped,attr,omitempty" json:"skipped" yaml:"skipped,omitempty"`
Total int `xml:"tests,attr" json:"total" yaml:"total,omitempty"`
TestCases []TestCaseXML `xml:"testcase" json:"testcases" yaml:"testcases"`
Version string `xml:"version,omitempty" json:"version" yaml:"version,omitempty"`
Time string `xml:"time,attr,omitempty" json:"time" yaml:"-"`
Timestamp string `xml:"timestamp,attr,omitempty" json:"timestamp" yaml:"-"`
}
type TestSuiteInput struct {
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`
TestCases []TestCaseInput `json:"testcases" yaml:"testcases"`
Vars H `json:"vars" yaml:"vars"`
Secrets []string `json:"secrets" yaml:"secrets"`
}
type TestSuite struct {
Name string `json:"name" yaml:"name"`
Description string `json:"description,omitempty" yaml:"description"`
TestCases []TestCase `json:"testcases" yaml:"testcases"`
Vars H `json:"vars" yaml:"vars"`
Secrets []string `json:"secrets" yaml:"secrets"`
// computed
ShortName string `json:"shortname" yaml:"-"`
Filename string `json:"filename" yaml:"-"`
Filepath string `json:"filepath" yaml:"-"`
ComputedVars H `json:"computed_vars" yaml:"-"`
WorkDir string `json:"workdir" yaml:"_"`
Status Status `json:"status" yaml:"status"`
Duration float64 `json:"duration" yaml:"-"`
Start time.Time `json:"start" yaml:"-"`
End time.Time `json:"end" yaml:"-"`
NbTestcasesFail int `json:"nbTestcasesFail" yaml:"-"`
NbTestcasesPass int `json:"nbTestcasesPass" yaml:"-"`
NbTestcasesSkip int `json:"nbTestcasesSkip" yaml:"-"`
}
// TestCase is a single test case with its result.
type TestCaseXML struct {
XMLName xml.Name `xml:"testcase" json:"-" yaml:"-"`
Classname string `xml:"classname,attr,omitempty" json:"classname" yaml:"-"`
Errors []FailureXML `xml:"error,omitempty" json:"errors" yaml:"errors,omitempty"`
Failures []FailureXML `xml:"failure,omitempty" json:"failures" yaml:"failures,omitempty"`
Name string `xml:"name,attr" json:"name" yaml:"name"`
Skipped []Skipped `xml:"skipped,omitempty" json:"skipped" yaml:"skipped,omitempty"`
Systemout InnerResult `xml:"system-out,omitempty" json:"systemout" yaml:"systemout,omitempty"`
Systemerr InnerResult `xml:"system-err,omitempty" json:"systemerr" yaml:"systemerr,omitempty"`
Time float64 `xml:"time,attr,omitempty" json:"time" yaml:"time,omitempty"`
ID string `xml:"id,attr,omitempty" json:"id" yaml:"id"`
}
type TestCaseInput struct {
Name string `json:"name" yaml:"name"`
Vars H `json:"vars" yaml:"vars"`
Skip []string `json:"skip" yaml:"skip"`
RawTestSteps []json.RawMessage `json:"steps" yaml:"steps"`
ID string `json:"id" yaml:"id"`
}
type TestCase struct {
TestCaseInput
// Computed
originalName string
number int
Skipped []Skipped `json:"skipped" yaml:"-"`
Status Status `json:"status" yaml:"-"`
Duration float64 `json:"duration" yaml:"-"`
Start time.Time `json:"start" yaml:"-"`
End time.Time `json:"end" yaml:"-"`
testSteps []TestStep `json:"-" yaml:"-"`
TestStepResults []TestStepResult `json:"results" yaml:"-"`
TestSuiteVars H `json:"-" yaml:"-"`
computedVars H `json:"-" yaml:"-"`
computedVerbose []string `json:"-" yaml:"-"`
IsExecutor bool `json:"-" yaml:"-"`
IsEvaluated bool `json:"-" yaml:"-"`
// Source line numbers extracted from YAML parsing (1-indexed)
SourceLine int `json:"-" yaml:"-"` // line of the testcase definition
StepSourceLines []int `json:"-" yaml:"-"` // line of each step (indexed by step number)
AssertionSourceLines [][]int `json:"-" yaml:"-"` // [stepIdx][assertIdx] -> line number
}
type TestStepResult struct {
Name string `json:"name"`
Errors []Failure `json:"errors"`
Skipped []Skipped `json:"skipped" yaml:"skipped"`
Status Status `json:"status" yaml:"status"`
Raw interface{} `json:"raw" yaml:"raw"`
Interpolated interface{} `json:"interpolated" yaml:"interpolated"`
Number int `json:"number" yaml:"number"`
RangedIndex int `json:"rangedIndex" yaml:"rangedIndex"`
RangedEnable bool `json:"rangedEnable" yaml:"rangedEnable"`
InputVars map[string]string `json:"inputVars" yaml:"-"`
ComputedVars H `json:"computedVars" yaml:"-"`
ComputedInfo []string `json:"computedInfos" yaml:"-"`
AssertionsApplied AssertionsApplied `json:"assertionsApplied" yaml:"-"`
Retries int `json:"retries" yaml:"retries"`
Systemout string `json:"systemout"`
Systemerr string `json:"systemerr"`
Duration float64 `json:"duration"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
}
func (ts *TestStepResult) appendError(err error) {
ts.Errors = append(ts.Errors, Failure{Value: RemoveNotPrintableChar(err.Error())})
ts.Status = StatusFail
}
// Append an error to a test step and its associated test case
func (ts *TestStepResult) appendFailure(failure ...Failure) {
ts.Errors = append(ts.Errors, failure...)
}
// TestStep represents a testStep
type TestStep map[string]interface{}
func (t TestStep) IntValue(name string) (int, error) {
out, err := cast.ToIntE(t[name])
if err != nil {
return -1, fmt.Errorf("attribute %q is not an integer", name)
}
return out, nil
}
func (t TestStep) StringValue(name string) (string, error) {
out, err := cast.ToStringE(t[name])
if err != nil {
return "", fmt.Errorf("attribute %q is not an string", name)
}
return out, nil
}
func (t TestStep) StringSliceValue(name string) ([]string, error) {
out, err := cast.ToStringE(t[name])
if err != nil {
out, err := cast.ToStringSliceE(t[name])
if err != nil {
return nil, fmt.Errorf("attribute %q is neither a string nor a string array", name)
}
return out, nil
}
// If string is empty, return an empty slice instead
if len(out) == 0 {
return []string{}, nil
}
return []string{out}, nil
}
// Range contains data related to iterable user values
type Range struct {
Enabled bool
Items []RangeData
RawContent interface{} `json:"range"`
}
// RangeData contains a single iterable user value
type RangeData struct {
Key string
Value interface{}
}
// Skipped contains data related to a skipped test.
type Skipped struct {
Value string `xml:",cdata" json:"value" yaml:"value,omitempty"`
}
// Failure contains data related to a failed test.
type Failure struct {
TestcaseClassname string `xml:"-" json:"-" yaml:"-"`
TestcaseName string `xml:"-" json:"-" yaml:"-"`
TestcaseLineNumber int `xml:"-" json:"-" yaml:"-"`
StepNumber int `xml:"-" json:"-" yaml:"-"`
Assertion string `xml:"-" json:"-" yaml:"-"`
AssertionRequired bool `xml:"-" json:"-" yaml:"-"`
Error error `xml:"-" json:"-" yaml:"-"`
Value string `json:"value" yaml:"value,omitempty"`
}
type FailureXML struct {
Value string `xml:",cdata" json:"value" yaml:"value,omitempty"`
Type string `xml:"type,attr,omitempty" json:"type" yaml:"type,omitempty"`
Message string `xml:"message,attr,omitempty" json:"message" yaml:"message,omitempty"`
}
func newFailure(ctx context.Context, tc TestCase, stepNumber int, rangedIndex int, assertionIndex int, assertion string, err error) *Failure {
filepath := StringVarFromCtx(ctx, "venom.testsuite.filepath")
// Try to get line number from stored source lines
lineNumber := tc.findSourceLine(stepNumber, assertionIndex)
var value string
if assertion != "" {
value = fmt.Sprintf(`Testcase %q, step #%d-%d: Assertion %q failed. %s (%v:%d)`,
tc.originalName,
stepNumber,
rangedIndex,
RemoveNotPrintableChar(assertion),
RemoveNotPrintableChar(err.Error()),
filepath,
lineNumber,
)
} else {
value = fmt.Sprintf(`Testcase %q, step #%d-%d: %s (%v:%d)`,
tc.originalName,
stepNumber,
rangedIndex,
RemoveNotPrintableChar(err.Error()),
filepath,
lineNumber,
)
}
failure := Failure{
TestcaseClassname: filepath,
TestcaseName: tc.Name,
TestcaseLineNumber: lineNumber,
StepNumber: stepNumber,
Assertion: assertion,
Error: err,
Value: value,
}
return &failure
}
// findSourceLine returns the best source line number for a given step and assertion.
// It tries the assertion line first, then falls back to the step line.
func (tc TestCase) findSourceLine(stepNumber int, assertionIndex int) int {
// Try assertion-level line number
if assertionIndex >= 0 && stepNumber < len(tc.AssertionSourceLines) {
assertions := tc.AssertionSourceLines[stepNumber]
if assertionIndex < len(assertions) && assertions[assertionIndex] > 0 {
return assertions[assertionIndex]
}
}
// Fall back to step line
if stepNumber < len(tc.StepSourceLines) && tc.StepSourceLines[stepNumber] > 0 {
return tc.StepSourceLines[stepNumber]
}
// Fall back to testcase line
if tc.SourceLine > 0 {
return tc.SourceLine
}
return 0
}
func (f Failure) String() string {
if f.Value != "" {
return color.YellowString(f.Value)
}
if f.Error != nil {
return color.YellowString(f.Error.Error())
}
return ""
}
// InnerResult is used by TestCase
type InnerResult struct {
Value string `xml:",cdata" json:"value" yaml:"value"`
}
type AssignStep struct {
Assignments map[string]Assignment `json:"vars" yaml:"vars" mapstructure:"vars"`
}
type Assignment struct {
From string `json:"from" yaml:"from"`
Regex string `json:"regex" yaml:"regex"`
Default interface{} `json:"default" yaml:"default"`
}
// RemoveNotPrintableChar removes not printable character from a string
func RemoveNotPrintableChar(in string) string {
m := func(r rune) rune {
if unicode.IsPrint(r) || unicode.IsSpace(r) || unicode.IsPunct(r) {
return r
}
return ' '
}
return strings.Map(m, in)
}
var (
Red = color.New(color.FgRed).SprintFunc()
Yellow = color.New(color.FgYellow).SprintFunc()
Green = color.New(color.FgGreen).SprintFunc()
Cyan = color.New(color.FgCyan).SprintFunc()
Gray = color.New(color.Attribute(90)).SprintFunc()
)