forked from k1LoW/runn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdout.go
164 lines (154 loc) · 5.33 KB
/
cmdout.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
package runn
import (
"fmt"
"io"
"net/http"
"strings"
"google.golang.org/grpc/status"
)
var _ Capturer = (*cmdOut)(nil)
type cmdOut struct {
out io.Writer
verbose bool
errs error
}
func NewCmdOut(out io.Writer, verbose bool) *cmdOut {
return &cmdOut{
out: out,
verbose: verbose,
}
}
func (d *cmdOut) CaptureStart(trs Trails, bookPath, desc string) {
if !d.verbose {
return
}
_, _ = fmt.Fprintf(d.out, "=== %s (%s)\n", desc, bookPath)
}
func (d *cmdOut) CaptureResult(trs Trails, result *RunResult) {
if d.verbose {
return
}
switch {
case result.Err != nil:
_, _ = fmt.Fprint(d.out, red("F"))
case result.Skipped:
_, _ = fmt.Fprint(d.out, yellow("S"))
default:
_, _ = fmt.Fprint(d.out, green("."))
}
}
func (d *cmdOut) CaptureEnd(trs Trails, bookPath, desc string) {}
func (d *cmdOut) CaptureResultByStep(trs Trails, result *RunResult) {
if !d.verbose {
return
}
if result.included {
return
}
d.verboseOutResult(result, 0)
}
func (d *cmdOut) CaptureHTTPRequest(name string, req *http.Request) {}
func (d *cmdOut) CaptureHTTPResponse(name string, res *http.Response) {}
func (d *cmdOut) CaptureGRPCStart(name string, typ GRPCType, service, method string) {}
func (d *cmdOut) CaptureGRPCRequestHeaders(h map[string][]string) {}
func (d *cmdOut) CaptureGRPCRequestMessage(m map[string]any) {}
func (d *cmdOut) CaptureGRPCResponseStatus(s *status.Status) {}
func (d *cmdOut) CaptureGRPCResponseHeaders(h map[string][]string) {}
func (d *cmdOut) CaptureGRPCResponseMessage(m map[string]any) {}
func (d *cmdOut) CaptureGRPCResponseTrailers(t map[string][]string) {}
func (d *cmdOut) CaptureGRPCClientClose() {}
func (d *cmdOut) CaptureGRPCEnd(name string, typ GRPCType, service, method string) {}
func (d *cmdOut) CaptureCDPStart(name string) {}
func (d *cmdOut) CaptureCDPAction(a CDPAction) {}
func (d *cmdOut) CaptureCDPResponse(a CDPAction, res map[string]any) {}
func (d *cmdOut) CaptureCDPEnd(name string) {}
func (d *cmdOut) CaptureSSHCommand(command string) {}
func (d *cmdOut) CaptureSSHStdout(stdout string) {}
func (d *cmdOut) CaptureSSHStderr(stderr string) {}
func (d *cmdOut) CaptureDBStatement(name string, stmt string) {}
func (d *cmdOut) CaptureDBResponse(name string, res *DBResponse) {}
func (d *cmdOut) CaptureExecCommand(command, shell string, background bool) {}
func (d *cmdOut) CaptureExecStdin(stdin string) {}
func (d *cmdOut) CaptureExecStdout(stdout string) {}
func (d *cmdOut) CaptureExecStderr(stderr string) {}
func (d *cmdOut) SetCurrentTrails(trs Trails) {}
func (d *cmdOut) Errs() error {
return d.errs
}
func (d *cmdOut) verboseOutResult(r *RunResult, nest int) {
if nest == 0 {
idx := len(r.StepResults) - 1
for i, sr := range r.StepResults {
if sr == nil {
idx = i - 1
break
}
}
if idx < 0 {
return
}
sr := r.StepResults[idx]
d.verboseOutResultForStep(idx, sr, r.Path, nest)
} else {
for idx, sr := range r.StepResults {
if sr == nil {
continue
}
d.verboseOutResultForStep(idx, sr, r.Path, nest)
}
}
}
func (d *cmdOut) verboseOutResultForStep(idx int, sr *StepResult, path string, nest int) {
indent := strings.Repeat(" ", nest)
desc := ""
if sr.Desc != "" {
desc = fmt.Sprintf("%s ", sr.Desc)
}
switch {
case sr.Err != nil:
// fail
lineformat := indent + " %s\n"
_, _ = fmt.Fprintf(d.out, "%s --- %s(%s) ... %s\n%s", indent, desc, sr.Key, red("fail"), red(SprintMultilinef(lineformat, "Failure/Error: %s", strings.TrimRight(sr.Err.Error(), "\n"))))
if len(sr.IncludedRunResults) > 0 {
for _, ir := range sr.IncludedRunResults {
_, _ = fmt.Fprintf(d.out, "%s === %s (%s)\n", indent, ir.Desc, ir.Path)
ir.included = false
d.verboseOutResult(ir, nest+1)
}
return
}
b, err := readFile(path)
if err != nil {
return
}
picked, err := pickStepYAML(string(b), idx)
if err != nil {
return
}
_, _ = fmt.Fprintf(d.out, "%s Failure step (%s):\n", indent, path)
_, _ = fmt.Fprint(d.out, SprintMultilinef(lineformat, "%v", picked))
_, _ = fmt.Fprintln(d.out, "")
case sr.Skipped:
// skip
_, _ = fmt.Fprintf(d.out, "%s --- %s(%s) ... %s\n", indent, desc, sr.Key, yellow("skip"))
if len(sr.IncludedRunResults) > 0 {
for _, ir := range sr.IncludedRunResults {
_, _ = fmt.Fprintf(d.out, "%s === %s (%s)\n", indent, ir.Desc, ir.Path)
ir.included = false
d.verboseOutResult(ir, nest+1)
}
return
}
default:
// ok
_, _ = fmt.Fprintf(d.out, "%s --- %s(%s) ... %s\n", indent, desc, sr.Key, green("ok"))
if len(sr.IncludedRunResults) > 0 {
for _, ir := range sr.IncludedRunResults {
_, _ = fmt.Fprintf(d.out, "%s === %s (%s)\n", indent, ir.Desc, ir.Path)
ir.included = false
d.verboseOutResult(ir, nest+1)
}
return
}
}
}