forked from k1LoW/runn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.go
136 lines (124 loc) · 2.51 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
package runn
import (
"encoding/json"
"fmt"
"io"
"sync"
"sync/atomic"
"github.com/fatih/color"
)
type result string
const (
resultSuccess result = "success"
resultFailure result = "failure"
resultSkipped result = "skipped"
)
type RunResult struct {
Desc string
Path string
Skipped bool
Err error
Store map[string]interface{}
}
type runNResult struct {
Total atomic.Int64
RunResults sync.Map
}
type runNResultSimplified struct {
Total int64 `json:"total"`
Success int64 `json:"success"`
Failure int64 `json:"failure"`
Skipped int64 `json:"skipped"`
Results map[string]result `json:"results"`
}
func newRunResult(desc, path string) *RunResult {
return &RunResult{
Desc: desc,
Path: path,
}
}
func (r *runNResult) HasFailure() bool {
f := false
r.RunResults.Range(func(k, v any) bool {
rr, ok := v.(*RunResult)
if !ok {
return false
}
if rr.Err != nil {
f = true
}
return true
})
return f
}
func (r *runNResult) Simplify() runNResultSimplified {
s := runNResultSimplified{
Total: r.Total.Load(),
Results: map[string]result{},
}
r.RunResults.Range(func(k, v any) bool {
rr, ok := v.(*RunResult)
if !ok {
return false
}
kk, ok := k.(string)
if !ok {
return false
}
if rr.Err != nil {
s.Failure += 1
s.Results[kk] = resultFailure
return true
}
if rr.Skipped {
s.Skipped += 1
s.Results[kk] = resultSkipped
return true
}
s.Success += 1
s.Results[kk] = resultSuccess
return true
})
return s
}
func (r *runNResult) Out(out io.Writer) error {
var ts, fs string
green := color.New(color.FgGreen).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
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
}