forked from k1LoW/runn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.go
212 lines (193 loc) · 4.55 KB
/
result.go
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
package runn
import (
"encoding/json"
"fmt"
"io"
"strings"
"sync"
"sync/atomic"
"github.com/fatih/color"
)
type result string
const (
resultSuccess result = "success"
resultFailure result = "failure"
resultSkipped result = "skipped"
)
type RunResult struct {
ID string
Desc string
Path string
Skipped bool
Err error
StepResults []*StepResult
Store map[string]any
}
type StepResult struct {
Key string
Desc string
Skipped bool
Err error
}
type runNResult struct {
Total atomic.Int64
RunResults []*RunResult
mu sync.Mutex
}
type runNResultSimplified struct {
Total int64 `json:"total"`
Success int64 `json:"success"`
Failure int64 `json:"failure"`
Skipped int64 `json:"skipped"`
Results []runResultSimplified `json:"results"`
}
type runResultSimplified struct {
ID string `json:"id"`
Path string `json:"path"`
Result result `json:"result"`
Steps []stepResultSimplified `json:"steps"`
}
type stepResultSimplified struct {
Key string `json:"key"`
Result result `json:"result"`
}
func newRunResult(desc, path string) *RunResult {
return &RunResult{
Desc: desc,
Path: path,
}
}
func (r *runNResult) HasFailure() bool {
for _, rr := range r.RunResults {
if rr.Err != nil {
return true
}
}
return false
}
func (r *runNResult) Simplify() runNResultSimplified {
s := runNResultSimplified{
Total: r.Total.Load(),
}
for _, rr := range r.RunResults {
switch {
case rr.Err != nil:
s.Failure += 1
s.Results = append(s.Results, runResultSimplified{
ID: rr.ID,
Path: rr.Path,
Result: resultFailure,
Steps: simplifyStepResults(rr.StepResults),
})
case rr.Skipped:
s.Skipped += 1
s.Results = append(s.Results, runResultSimplified{
ID: rr.ID,
Path: rr.Path,
Result: resultSkipped,
Steps: simplifyStepResults(rr.StepResults),
})
default:
s.Success += 1
s.Results = append(s.Results, runResultSimplified{
ID: rr.ID,
Path: rr.Path,
Result: resultSuccess,
Steps: simplifyStepResults(rr.StepResults),
})
}
}
return s
}
func (r *runNResult) Out(out io.Writer, verbose bool) error {
var ts, fs string
green := color.New(color.FgGreen).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
cyan := color.New(color.FgCyan).SprintFunc()
_, _ = fmt.Fprintln(out, "")
if !verbose && r.HasFailure() {
_, _ = fmt.Fprintln(out, "")
i := 1
for _, r := range r.RunResults {
if r.Err == nil {
continue
}
_, _ = fmt.Fprintf(out, "%d) %s %s\n", i, ShortenPath(r.Path), cyan(r.ID))
for _, sr := range r.StepResults {
if sr.Err == nil {
continue
}
_, _ = fmt.Fprint(out, SprintMultilinef(" %s\n", "%v", red(fmt.Sprintf("Failure/Error: %s", strings.TrimRight(sr.Err.Error(), "\n")))))
}
i++
}
}
_, _ = fmt.Fprintln(out, "")
rs := r.Simplify()
if rs.Total == 1 {
ts = fmt.Sprintf("%d scenario", rs.Total)
} else {
ts = fmt.Sprintf("%d scenarios", rs.Total)
}
ss := fmt.Sprintf("%d skipped", rs.Skipped)
if rs.Failure == 1 {
fs = fmt.Sprintf("%d failure", rs.Failure)
} else {
fs = fmt.Sprintf("%d failures", rs.Failure)
}
if r.HasFailure() {
if _, err := fmt.Fprintf(out, red("%s, %s, %s\n"), ts, ss, fs); err != nil {
return err
}
} else {
if _, err := fmt.Fprintf(out, green("%s, %s, %s\n"), ts, ss, fs); err != nil {
return err
}
}
return nil
}
func (r *runNResult) OutJSON(out io.Writer) error {
s := r.Simplify()
b, err := json.MarshalIndent(s, "", " ")
if err != nil {
return err
}
if _, err := out.Write(b); err != nil {
return err
}
if _, err := fmt.Fprint(out, "\n"); err != nil {
return err
}
return nil
}
func simplifyStepResults(stepResults []*StepResult) []stepResultSimplified {
simplified := []stepResultSimplified{}
for _, sr := range stepResults {
switch {
case sr.Err != nil:
simplified = append(simplified, stepResultSimplified{
Key: sr.Key,
Result: resultFailure,
})
case sr.Skipped:
simplified = append(simplified, stepResultSimplified{
Key: sr.Key,
Result: resultSkipped,
})
default:
simplified = append(simplified, stepResultSimplified{
Key: sr.Key,
Result: resultSuccess,
})
}
}
return simplified
}
func SprintMultilinef(lineformat, format string, a ...any) string {
lines := strings.Split(fmt.Sprintf(format, a...), "\n")
var formatted string
for _, l := range lines {
formatted += fmt.Sprintf(lineformat, l)
}
return formatted
}