-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
result.go
379 lines (350 loc) · 8.53 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
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
package runn
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/samber/lo"
)
type result string
const (
resultSuccess result = "success"
resultFailure result = "failure"
resultSkipped result = "skipped"
)
// RunResult is the result of a runbook run.
type RunResult struct {
// runbook ID
ID string
Desc string
Labels []string
Path string
Skipped bool
Err error
StepResults []*StepResult
Elapsed time.Duration
store *store
included bool
}
// StepResult is the result of a step run.
type StepResult struct {
// runbook ID
ID string
Key string
Desc string
Skipped bool
Err error
// Run results of runbook loaded by include runner
IncludedRunResults []*RunResult
Elapsed time.Duration
}
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"`
Elapsed time.Duration `json:"elapsed,omitempty"`
}
type runResultSimplified struct {
ID string `json:"id"`
Labels []string `json:"labels,omitempty"`
Path string `json:"path"`
Result result `json:"result"`
Steps []*stepResultSimplified `json:"steps"`
Elapsed time.Duration `json:"elapsed,omitempty"`
}
type stepResultSimplified struct {
ID string `json:"id"`
Key string `json:"key"`
Result result `json:"result"`
IncludedRunResults []*runResultSimplified `json:"included_run_result,omitempty"`
Elapsed time.Duration `json:"elapsed,omitempty"`
}
func newRunResult(desc string, labels []string, path string, included bool, store *store) *RunResult {
return &RunResult{
Desc: desc,
Labels: labels,
Path: path,
included: included,
store: store,
}
}
// HasFailure returns true if any run result has failure.
func (r *runNResult) HasFailure() bool {
for _, rr := range r.RunResults {
if rr.Err != nil {
return true
}
}
return false
}
func (r *runNResult) Out(out io.Writer) error {
var ts, fs string
_, _ = fmt.Fprintln(out, "")
if 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) Store() map[string]any {
return rr.store.toMap()
}
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 (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, normalizePath(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", normalizePath(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 len(sr.IncludedRunResults) == 0 {
paths = append(paths, []string{rr.Path})
errs = append(errs, sr.Err)
indexes = append(indexes, i)
continue
}
for _, ir := range sr.IncludedRunResults {
ps, is, es := failedRunbookPathsAndErrors(ir)
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
}
np := normalizePath(rr.Path)
switch {
case rr.Err != nil:
return &runResultSimplified{
ID: rr.ID,
Path: np,
Result: resultFailure,
Steps: simplifyStepResults(rr.StepResults),
Elapsed: rr.Elapsed,
}
case rr.Skipped:
return &runResultSimplified{
ID: rr.ID,
Path: np,
Result: resultSkipped,
Steps: simplifyStepResults(rr.StepResults),
Elapsed: rr.Elapsed,
}
default:
return &runResultSimplified{
ID: rr.ID,
Path: np,
Result: resultSuccess,
Steps: simplifyStepResults(rr.StepResults),
Elapsed: rr.Elapsed,
}
}
}
func simplifyStepResults(stepResults []*StepResult) []*stepResultSimplified {
var simplified []*stepResultSimplified
for _, sr := range stepResults {
switch {
case sr.Err != nil:
simplified = append(simplified, &stepResultSimplified{
ID: sr.ID,
Key: sr.Key,
Result: resultFailure,
IncludedRunResults: lo.Map(sr.IncludedRunResults, func(ir *RunResult, _ int) *runResultSimplified {
return simplifyRunResult(ir)
}),
Elapsed: sr.Elapsed,
})
case sr.Skipped:
simplified = append(simplified, &stepResultSimplified{
ID: sr.ID,
Key: sr.Key,
Result: resultSkipped,
IncludedRunResults: lo.Map(sr.IncludedRunResults, func(ir *RunResult, _ int) *runResultSimplified {
return simplifyRunResult(ir)
}),
Elapsed: sr.Elapsed,
})
default:
simplified = append(simplified, &stepResultSimplified{
ID: sr.ID,
Key: sr.Key,
Result: resultSuccess,
IncludedRunResults: lo.Map(sr.IncludedRunResults, func(ir *RunResult, _ int) *runResultSimplified {
return simplifyRunResult(ir)
}),
Elapsed: sr.Elapsed,
})
}
}
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
}
var (
// root = project root path.
root string
once sync.Once
)
func normalizePath(p string) string {
once.Do(func() {
root, _ = projectRoot()
})
if root == "" {
return p
}
abs, err := filepath.Abs(filepath.Clean(p))
if err != nil {
return p
}
rel, err := filepath.Rel(root, abs)
if err != nil {
return p
}
return rel
}
func projectRoot() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", err
}
for {
if dir == filepath.Dir(dir) {
return "", errors.New("failed to find project root")
}
if _, err := os.Stat(filepath.Join(dir, ".git", "config")); err == nil {
return dir, nil
}
dir = filepath.Dir(dir)
}
}