-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.go
More file actions
344 lines (284 loc) · 8.82 KB
/
report.go
File metadata and controls
344 lines (284 loc) · 8.82 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
package arp
import (
"bufio"
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"time"
)
const (
MaxResultMsgLength = 92
MaxResponseLines = 80
)
type ReportOptions struct {
ShortErrors bool
Short bool
Tiny bool
Micro bool
AlwaysPrintHeaders bool
ErrorsOnly bool
TestsPath string
Colors Colorizer
// Any failures while report is printed are suppresed and and indication
// is provided that the result data may be incomplete
InProgress bool
}
type Colorizer struct {
Enabled bool
}
func (c *Colorizer) Underline(input string) string {
return c.colorizeStr(input, "\033[4m")
}
func (c *Colorizer) BrightGrey(input string) string {
return c.colorizeStr(input, "\033[37;1m")
}
func (c *Colorizer) BrightRed(input string) string {
return c.colorizeStr(input, "\033[31;1m")
}
func (c *Colorizer) BrightWhite(input string) string {
return c.colorizeStr(input, "\033[37;1m")
}
func (c *Colorizer) BrightYellow(input string) string {
return c.colorizeStr(input, "\033[33;1m")
}
func (c *Colorizer) BrightCyan(input string) string {
return c.colorizeStr(input, "\033[36;1m")
}
func (c *Colorizer) BrightBlue(input string) string {
return c.colorizeStr(input, "\033[34;1m")
}
func (c *Colorizer) Cyan(input string) string {
return c.colorizeStr(input, "\033[36m")
}
func (c *Colorizer) Red(input string) string {
return c.colorizeStr(input, "\033[31m")
}
func (c *Colorizer) Green(input string) string {
return c.colorizeStr(input, "\033[32m")
}
func (c *Colorizer) Yellow(input string) string {
return c.colorizeStr(input, "\033[33m")
}
func (c *Colorizer) colorizeStr(input string, color string) string {
if !c.Enabled {
return input
}
return fmt.Sprintf("%v%v%v", color, input, "\033[0m")
}
func IndentStr(level int) string {
indents := ""
for i := 0; i < level; i++ {
indents += " "
}
return indents
}
func PrintIndentedLn(indentLevel int, format string, args ...interface{}) {
indentFmt := "%[1]v"
for i := 0; i < len(format); i++ {
indentFmt += string(format[i])
// if we reach a newline character and there are more characters after it, indent
// the next line to the same level
if format[i] == '\n' && i+1 < len(format) {
indentFmt += "%[1]v"
}
}
var newArgs []interface{}
newArgs = append(newArgs, IndentStr(indentLevel))
newArgs = append(newArgs, args...)
fmt.Printf(indentFmt, newArgs...)
}
// PageText will show the first numLines of text in a string and dump
// the entire input string to a temporary file for later review.
func PageText(input string, numLines int) string {
scanner := bufio.NewScanner(strings.NewReader(input))
curLine := 0
output := ""
for curLine < numLines && scanner.Scan() {
output += scanner.Text() + "\n"
curLine++
}
if curLine >= numLines {
output += "..."
if f, err := os.CreateTemp("", "response-dump-*.json"); err == nil {
f.WriteString(input)
f.Close()
output += fmt.Sprintf("\nRemaining response data has been saved to: %v\n", f.Name())
}
return output
}
return output
}
func separator(c Colorizer) string {
sep := ""
for i := 0; i < 80; i++ {
sep += "-"
}
return c.BrightWhite(sep)
}
func getSuccessString(c Colorizer, status bool, style string) string {
switch style {
default:
fallthrough
case "test":
if status {
return c.Green("Passed")
}
return c.Red("Failed")
case "validation":
if status {
return c.Green("*")
}
return c.Red("x")
case "skipped":
return c.BrightGrey("Skipped")
case "in_progress":
return c.BrightYellow("In Progress")
case "partial_validation":
if status {
return c.Green("*")
}
return c.BrightYellow("o")
}
}
func ShouldShowReport(opts ReportOptions, test *TestResult) bool {
return (opts.ErrorsOnly && !test.Passed) || !opts.ErrorsOnly
}
func PrintSingleTestReport(opts ReportOptions, test *TestResult) {
showErrors := false
if !test.Passed {
showErrors = !opts.ShortErrors && !opts.InProgress
}
if !ShouldShowReport(opts, test) {
return
}
showExtendedReport := !opts.Short || showErrors
showFieldValidations := showExtendedReport || !opts.Tiny
details := test.TestCase
routeStr := fmt.Sprintf("[%v] %v", opts.Colors.BrightCyan(details.Config.Method), opts.Colors.BrightWhite(details.Config.Route))
statusStyle := ""
if test.TestCase.Config.Skip {
statusStyle = "skipped"
}
if opts.InProgress {
statusStyle = "in_progress"
}
delta := test.EndTime.Sub(test.StartTime)
timeStr := fmt.Sprintf("%v: %v", opts.Colors.BrightWhite("Test Duration"), delta)
PrintIndentedLn(1, "[%v] %v - %v\n", getSuccessString(opts.Colors, test.Passed, statusStyle),
opts.Colors.BrightWhite(details.Config.Name), details.Config.Description)
PrintIndentedLn(2, "%v\n", timeStr)
PrintIndentedLn(1, "%v\n", routeStr)
if showFieldValidations {
sort.Slice(test.Fields, func(i, j int) bool {
a := test.Fields[i].ObjectKeyPath
b := test.Fields[j].ObjectKeyPath
if a[0] == b[0] || (a[0] != '.' && b[0] != '.') {
return a < b
} else if a[0] != '.' {
return true
} else {
return false
}
})
for _, f := range test.Fields {
if f.IgnoreResult {
continue
}
fieldStr := f.ObjectKeyPath
suffix := "..."
maxLength := MaxResultMsgLength
if len(f.Error) < maxLength {
maxLength = len(f.Error)
suffix = ""
}
shortStr := ""
charCounter := 0
for _, c := range f.Error {
if charCounter >= maxLength {
shortStr += suffix
break
}
shortStr += string(c)
charCounter++
}
shortStr = fmt.Sprintf("%q", shortStr)
if !f.Status {
fieldStr = opts.Colors.Cyan(fieldStr)
shortStr = opts.Colors.BrightYellow(shortStr)
} else {
fieldStr = opts.Colors.BrightBlue(fieldStr)
}
style := "validation"
if opts.InProgress && f.Error == ReceivedNullErrFmt {
style = "partial_validation"
shortStr = opts.Colors.BrightYellow("Pending next websocket message...")
}
PrintIndentedLn(2, "[%v] %v: %v\n", getSuccessString(opts.Colors, f.Status, style),
fieldStr, shortStr)
}
}
fmt.Printf("\n")
if showExtendedReport {
PrintIndentedLn(2, "Route: %v\n", test.ResolvedRoute)
PrintIndentedLn(2, "Status Code: %v\n", test.StatusCode)
if len(test.TestCase.Config.Headers) > 0 || opts.AlwaysPrintHeaders {
requestHeadersJson, _ := json.MarshalIndent(test.RequestHeaders, IndentStr(2), " ")
PrintIndentedLn(2, "Request Headers: %v\n", string(requestHeadersJson))
}
if len(test.TestCase.ResponseHeaderMatcher.Config) > 0 || opts.AlwaysPrintHeaders {
// only print headers long output if the test case is validating any of them
headerJson, _ := json.MarshalIndent(test.ResponseHeaders, IndentStr(2), " ")
PrintIndentedLn(2, "Response Headers: %v\n", string(headerJson))
}
input := YamlToJson(test.TestCase.Config.Input)
inputJson, _ := json.MarshalIndent(input, IndentStr(2), " ")
PrintIndentedLn(2, "Input: %v\n", string(inputJson))
data, _ := json.MarshalIndent(test.Response, IndentStr(2), " ")
responsePage := PageText(string(data), MaxResponseLines)
PrintIndentedLn(2, "Response: %v\n\n", responsePage)
PrintIndentedLn(2, "Extended Output:\n")
for _, f := range test.Fields {
if f.ShowExtendedMsg {
PrintIndentedLn(3, fmt.Sprintf("%v", f.ObjectKeyPath))
PrintIndentedLn(5, fmt.Sprintf("%v:\n", f.Error))
}
}
fmt.Print(opts.Colors.BrightWhite("---\n"))
}
}
func PrintReport(opts ReportOptions, passed bool, testingDuration time.Duration, results []MultiSuiteResult) {
globalFailed := 0
globalPassed := 0
var globalTestDuration time.Duration
fmt.Printf("\n\n")
for _, r := range results {
globalFailed += r.TestResults.Failed
globalPassed += r.TestResults.Passed
globalTestDuration += r.TestResults.Duration
if !opts.Micro {
PrintIndentedLn(0, "[%v] %v\n", getSuccessString(opts.Colors, r.Passed, ""),
opts.Colors.Underline(opts.Colors.BrightWhite(r.TestFile)))
PrintIndentedLn(1, "Suite Duration: %v\n", r.TestResults.Duration)
PrintIndentedLn(1, "Passed: %v, Failed: %v, Total:%v\n", r.TestResults.Passed,
r.TestResults.Failed, r.TestResults.Total)
fmt.Printf("%v\n", separator(opts.Colors))
for _, test := range r.TestResults.Results {
if ShouldShowReport(opts, test) {
PrintSingleTestReport(opts, test)
}
}
if r.Error != nil {
PrintIndentedLn(1, opts.Colors.BrightRed("One or more tests failed within execution and the test suite could not be completed:\n"))
PrintIndentedLn(1, "%q\n\n", r.Error)
}
}
}
fmt.Printf("%v\n", separator(opts.Colors))
path := opts.TestsPath
PrintIndentedLn(0, "[%v] %v\n", getSuccessString(opts.Colors, passed, ""), opts.Colors.BrightWhite(path))
PrintIndentedLn(0, "%-6[2]d:Total Tests\n%-6[3]d:Passed\n%-6[4]d:Failed\n", globalPassed+globalFailed, globalPassed, globalFailed)
PrintIndentedLn(0, "\nTotal Execution Time: %v (CPU Time: %v)\n", testingDuration, globalTestDuration)
fmt.Printf("%v\n", separator(opts.Colors))
}