forked from gotestyourself/gotestsum
/
printer.go
200 lines (178 loc) · 4.68 KB
/
printer.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
package testjson
import (
"fmt"
"go/build"
"os"
"path/filepath"
"strings"
"github.com/fatih/color"
)
// EventFormatter is a function which handles an event and returns a string to
// output for the event.
type EventFormatter func(event TestEvent, output *Execution) (string, error)
func debugFormat(event TestEvent, _ *Execution) (string, error) {
return fmt.Sprintf("%s %s %s (%.3f) [%d] %s\n",
event.Package,
event.Test,
event.Action,
event.Elapsed,
event.Time.Unix(),
event.Output), nil
}
// go test -v
func standardVerboseFormat(event TestEvent, _ *Execution) (string, error) {
if event.Action == ActionOutput {
return event.Output, nil
}
return "", nil
}
// go test
func standardQuietFormat(event TestEvent, _ *Execution) (string, error) {
if event.PackageEvent() && event.Output != "PASS\n" {
return event.Output, nil
}
return "", nil
}
func shortVerboseFormat(event TestEvent, exec *Execution) (string, error) {
result := colorEvent(event)(strings.ToUpper(string(event.Action)))
formatTest := func() string {
return fmt.Sprintf("%s %s.%s %s\n",
result,
relativePackagePath(event.Package),
event.Test,
event.ElapsedFormatted())
}
switch {
case isPkgFailureOutput(event):
return event.Output, nil
case event.PackageEvent():
switch event.Action {
case ActionSkip:
result = colorEvent(event)("EMPTY")
fallthrough
case ActionPass, ActionFail:
return fmt.Sprintf("%s %s\n", result, relativePackagePath(event.Package)), nil
}
case event.Action == ActionFail:
return exec.Output(event.Package, event.Test) + formatTest(), nil
case event.Action == ActionPass:
return formatTest(), nil
}
return "", nil
}
// isPkgFailureOutput returns true if the event is package output, and the output
// doesn't match any of the expected framing messages. Events which match this
// pattern should be package-level failures (ex: exit(1) or panic in an init() or
// TestMain).
func isPkgFailureOutput(event TestEvent) bool {
out := event.Output
return all(
event.PackageEvent(),
event.Action == ActionOutput,
out != "PASS\n",
out != "FAIL\n",
!strings.HasPrefix(out, "FAIL\t"+event.Package),
!strings.HasPrefix(out, "ok \t"+event.Package),
!strings.HasPrefix(out, "? \t"+event.Package),
)
}
func all(cond ...bool) bool {
for _, c := range cond {
if !c {
return false
}
}
return true
}
func shortFormat(event TestEvent, _ *Execution) (string, error) {
if !event.PackageEvent() {
return "", nil
}
fmtElapsed := func() string {
d := elapsedDuration(event.Elapsed)
if d == 0 {
return ""
}
return fmt.Sprintf(" (%s)", d)
}
fmtEvent := func(action string) (string, error) {
return fmt.Sprintf("%s %s%s\n",
action, relativePackagePath(event.Package), fmtElapsed()), nil
}
withColor := colorEvent(event)
switch event.Action {
case ActionSkip:
return fmtEvent(withColor("∅"))
case ActionPass:
return fmtEvent(withColor("✓"))
case ActionFail:
return fmtEvent(withColor("✖"))
}
return "", nil
}
func dotsFormat(event TestEvent, exec *Execution) (string, error) {
pkg := exec.Package(event.Package)
withColor := colorEvent(event)
switch {
case event.PackageEvent():
return "", nil
case event.Action == ActionRun && pkg.Total == 1:
return "[" + relativePackagePath(event.Package) + "]", nil
case event.Action == ActionPass:
return withColor("·"), nil
case event.Action == ActionFail:
return withColor("✖"), nil
case event.Action == ActionSkip:
return withColor("↷"), nil
}
return "", nil
}
func colorEvent(event TestEvent) func(format string, a ...interface{}) string {
switch event.Action {
case ActionPass:
return color.GreenString
case ActionFail:
return color.RedString
case ActionSkip:
return color.YellowString
}
return color.WhiteString
}
func relativePackagePath(pkgpath string) string {
if pkgpath == pkgPathPrefix {
return "."
}
return strings.TrimPrefix(pkgpath, pkgPathPrefix+"/")
}
// TODO: might not work on windows
func getPkgPathPrefix() string {
cwd, _ := os.Getwd()
gopaths := strings.Split(build.Default.GOPATH, string(filepath.ListSeparator))
for _, gopath := range gopaths {
gosrcpath := gopath + "/src/"
if strings.HasPrefix(cwd, gosrcpath) {
return strings.TrimPrefix(cwd, gosrcpath)
}
}
return ""
}
var pkgPathPrefix = getPkgPathPrefix()
// NewEventFormatter returns a formatter for printing events.
func NewEventFormatter(format string) EventFormatter {
switch format {
case "debug":
return debugFormat
case "standard-verbose":
return standardVerboseFormat
case "standard-quiet":
return standardQuietFormat
case "dots":
return dotsFormat
case "short-verbose":
return shortVerboseFormat
case "short":
return shortFormat
default:
return nil
}
}