forked from k1LoW/runn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
293 lines (269 loc) · 6.58 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
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
package runn
import (
"encoding/json"
"fmt"
"io"
"strings"
"sync"
"sync/atomic"
)
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
// Run result of runbook loaded by include runner
IncludedRunResult *RunResult
}
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"`
IncludedRunResult *runResultSimplified `json:"included_run_result,omitempty"`
}
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
case rr.Skipped:
s.Skipped += 1
default:
s.Success += 1
}
s.Results = append(s.Results, simplifyRunResult(rr))
}
return s
}
func (r *runNResult) Out(out io.Writer, verbose bool) error {
var ts, fs string
_, _ = fmt.Fprintln(out, "")
if verbose && r.HasFailure() {
_, _ = fmt.Fprintln(out, "")
i := 1
var err error
for _, rr := range r.RunResults {
i, err = rr.outFailure(out, i)
if err != nil {
return err
}
}
}
_, _ = 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 (rr *RunResult) OutFailure(out io.Writer) error {
_, err := rr.outFailure(out, 1)
return err
}
func (rr *RunResult) outFailure(out io.Writer, index int) (int, error) {
const tr = "└──"
if rr.Err == nil {
return index, nil
}
paths, indexes, errs := failedRunbookPathsAndErrors(rr)
for ii, p := range paths {
_, _ = fmt.Fprintf(out, "%d) %s %s\n", index, p[0], cyan(rr.ID))
for iii, pp := range p[1:] {
_, _ = fmt.Fprintf(out, " %s%s %s\n", strings.Repeat(" ", iii), tr, pp)
}
_, _ = fmt.Fprint(out, SprintMultilinef(" %s\n", "%v", red(fmt.Sprintf("Failure/Error: %s", strings.TrimRight(errs[ii].Error(), "\n")))))
last := p[len(p)-1]
b, err := readFile(last)
if err != nil {
return index, err
}
idx := indexes[ii]
if idx >= 0 {
picked, err := pickStepYAML(string(b), idx)
if err != nil {
return index, err
}
_, _ = fmt.Fprintf(out, " Failure step (%s):\n", last)
_, _ = fmt.Fprint(out, SprintMultilinef(" %s\n", "%v", picked))
_, _ = fmt.Fprintln(out, "")
}
index++
}
return index, nil
}
func failedRunbookPathsAndErrors(rr *RunResult) ([][]string, []int, []error) {
var (
paths [][]string
indexes []int
errs []error
)
if rr.Err == nil {
return paths, indexes, errs
}
for i, sr := range rr.StepResults {
if sr.Err == nil {
continue
}
if sr.IncludedRunResult == nil {
paths = append(paths, []string{rr.Path})
errs = append(errs, sr.Err)
indexes = append(indexes, i)
continue
}
ps, is, es := failedRunbookPathsAndErrors(sr.IncludedRunResult)
for _, p := range ps {
p = append([]string{rr.Path}, p...)
paths = append(paths, p)
}
indexes = append(indexes, is...)
errs = append(errs, es...)
}
if len(paths) == 0 {
paths = append(paths, []string{rr.Path})
errs = append(errs, rr.Err)
indexes = append(indexes, -1)
}
return paths, indexes, errs
}
func simplifyRunResult(rr *RunResult) *runResultSimplified {
if rr == nil {
return nil
}
switch {
case rr.Err != nil:
return &runResultSimplified{
ID: rr.ID,
Path: rr.Path,
Result: resultFailure,
Steps: simplifyStepResults(rr.StepResults),
}
case rr.Skipped:
return &runResultSimplified{
ID: rr.ID,
Path: rr.Path,
Result: resultSkipped,
Steps: simplifyStepResults(rr.StepResults),
}
default:
return &runResultSimplified{
ID: rr.ID,
Path: rr.Path,
Result: resultSuccess,
Steps: simplifyStepResults(rr.StepResults),
}
}
}
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,
IncludedRunResult: simplifyRunResult(sr.IncludedRunResult),
})
case sr.Skipped:
simplified = append(simplified, &stepResultSimplified{
Key: sr.Key,
Result: resultSkipped,
IncludedRunResult: simplifyRunResult(sr.IncludedRunResult),
})
default:
simplified = append(simplified, &stepResultSimplified{
Key: sr.Key,
Result: resultSuccess,
IncludedRunResult: simplifyRunResult(sr.IncludedRunResult),
})
}
}
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
}