-
Notifications
You must be signed in to change notification settings - Fork 115
/
format.go
253 lines (226 loc) · 6.02 KB
/
format.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
package testjson
import (
"fmt"
"io"
"strings"
"github.com/fatih/color"
)
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() {
return "", nil
}
if event.Output == "PASS\n" || isCoverageOutput(event.Output) {
return "", nil
}
if isWarningNoTestsToRunOutput(event.Output) {
return "", nil
}
return event.Output, nil
}
func testNameFormat(event TestEvent, exec *Execution) (string, error) {
result := colorEvent(event)(strings.ToUpper(string(event.Action)))
formatTest := func() string {
pkgPath := RelativePackagePath(event.Package)
return fmt.Sprintf("%s %s%s %s\n",
result,
joinPkgToTestName(pkgPath, event.Test),
formatRunID(event.RunID),
event.ElapsedFormatted())
}
switch {
case isPkgFailureOutput(event):
return event.Output, nil
case event.PackageEvent():
if !event.Action.IsTerminal() {
return "", nil
}
pkg := exec.Package(event.Package)
if event.Action == ActionSkip || (event.Action == ActionPass && pkg.Total == 0) {
result = colorEvent(event)("EMPTY")
}
var cached string
if pkg.cached {
cached = cachedMessage
}
return fmt.Sprintf("%s %s%s\n",
result,
RelativePackagePath(event.Package),
cached), nil
case event.Action == ActionFail:
pkg := exec.Package(event.Package)
tc := pkg.LastFailedByName(event.Test)
return pkg.Output(tc.ID) + formatTest(), nil
case event.Action == ActionPass:
return formatTest(), nil
}
return "", nil
}
// joinPkgToTestName for formatting.
// If the package path isn't the current directory, we add a period to separate
// the test name and the package path. If it is the current directory, we don't
// show it at all. This prevents output like ..MyTest when the test is in the
// current directory.
func joinPkgToTestName(pkg string, test string) string {
if pkg == "." {
return test
}
return pkg + "." + test
}
// formatRunID returns a formatted string of the runID.
func formatRunID(runID int) string {
if runID <= 0 {
return ""
}
return fmt.Sprintf(" (re-run %d)", runID)
}
// 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",
!isWarningNoTestsToRunOutput(out),
!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
}
const cachedMessage = " (cached)"
func pkgNameFormat(event TestEvent, exec *Execution) (string, error) {
if !event.PackageEvent() {
return "", nil
}
return shortFormatPackageEvent(event, exec)
}
func shortFormatPackageEvent(event TestEvent, exec *Execution) (string, error) {
pkg := exec.Package(event.Package)
fmtElapsed := func() string {
if pkg.cached {
return cachedMessage
}
d := elapsedDuration(event.Elapsed)
if d == 0 {
return ""
}
return fmt.Sprintf(" (%s)", d)
}
fmtCoverage := func() string {
if pkg.coverage == "" {
return ""
}
return " (" + pkg.coverage + ")"
}
fmtEvent := func(action string) (string, error) {
return fmt.Sprintf("%s %s%s%s\n",
action,
RelativePackagePath(event.Package),
fmtElapsed(),
fmtCoverage(),
), nil
}
withColor := colorEvent(event)
switch event.Action {
case ActionSkip:
return fmtEvent(withColor("∅"))
case ActionPass:
if pkg.Total == 0 {
return fmtEvent(withColor("∅"))
}
return fmtEvent(withColor("✓"))
case ActionFail:
return fmtEvent(withColor("✖"))
}
return "", nil
}
func pkgNameWithFailuresFormat(event TestEvent, exec *Execution) (string, error) {
if !event.PackageEvent() {
if event.Action == ActionFail {
pkg := exec.Package(event.Package)
tc := pkg.LastFailedByName(event.Test)
return pkg.Output(tc.ID), nil
}
return "", nil
}
return shortFormatPackageEvent(event, exec)
}
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
}
// EventFormatter is a function which handles an event and returns a string to
// output for the event.
type EventFormatter interface {
Format(event TestEvent, output *Execution) error
}
// NewEventFormatter returns a formatter for printing events.
func NewEventFormatter(out io.Writer, format string) EventFormatter {
switch format {
case "debug":
return &formatAdapter{out, debugFormat}
case "standard-verbose":
return &formatAdapter{out, standardVerboseFormat}
case "standard-quiet":
return &formatAdapter{out, standardQuietFormat}
case "dots", "dots-v1":
return &formatAdapter{out, dotsFormatV1}
case "dots-v2":
return newDotFormatter(out)
case "testname", "short-verbose":
return &formatAdapter{out, testNameFormat}
case "pkgname", "short":
return &formatAdapter{out, pkgNameFormat}
case "pkgname-and-test-fails", "short-with-failures":
return &formatAdapter{out, pkgNameWithFailuresFormat}
default:
return nil
}
}
type formatAdapter struct {
out io.Writer
format func(TestEvent, *Execution) (string, error)
}
func (f *formatAdapter) Format(event TestEvent, exec *Execution) error {
o, err := f.format(event, exec)
if err != nil {
return err
}
_, err = f.out.Write([]byte(o))
return err
}