forked from gotestyourself/gotestsum
/
summary.go
150 lines (133 loc) · 3.59 KB
/
summary.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
package testjson
import (
"fmt"
"io"
"strings"
"time"
"unicode"
"unicode/utf8"
"github.com/fatih/color"
)
// Summary enumerates the sections which can be printed by PrintSummary
type Summary int
// nolint: golint
const (
SummarizeNone Summary = 1 << (iota * 2)
SummarizeSkipped
SummarizeFailed
SummarizeErrors
SummarizeAll = SummarizeSkipped | SummarizeFailed | SummarizeErrors
)
// PrintSummary of a test Execution. Prints a section for each summary type
// followed by a DONE line.
func PrintSummary(out io.Writer, execution *Execution, opts Summary) error {
if opts&SummarizeSkipped != 0 {
writeTestCaseSummary(out, execution, formatSkipped())
}
if opts&SummarizeFailed != 0 {
writeTestCaseSummary(out, execution, formatFailed())
}
errors := execution.Errors()
if opts&SummarizeErrors != 0 {
writeErrorSummary(out, errors)
}
fmt.Fprintf(out, "\n%s %d tests%s%s%s in %s\n",
"DONE", // TODO: maybe color this?
execution.Total(),
formatTestCount(len(execution.Skipped()), "skipped", ""),
formatTestCount(len(execution.Failed()), "failure", "s"),
formatTestCount(countErrors(errors), "error", "s"),
FormatDurationAsSeconds(execution.Elapsed(), 3))
return nil
}
func formatTestCount(count int, category string, pluralize string) string {
switch count {
case 0:
return ""
case 1:
default:
category += pluralize
}
return fmt.Sprintf(", %d %s", count, category)
}
// FormatDurationAsSeconds formats a time.Duration as a float.
func FormatDurationAsSeconds(d time.Duration, precision int) string {
return fmt.Sprintf("%.[2]*[1]fs", d.Seconds(), precision)
}
func writeErrorSummary(out io.Writer, errors []string) {
if len(errors) > 0 {
fmt.Fprintln(out, color.MagentaString("\n=== Errors"))
}
for _, err := range errors {
fmt.Fprintln(out, err)
}
}
// countErrors in stderr lines. Build errors may include multiple lines where
// subsequent lines are indented.
// FIXME: Panics will include multiple lines, and are still overcounted.
func countErrors(errors []string) int {
var count int
for _, line := range errors {
r, _ := utf8.DecodeRuneInString(line)
if !unicode.IsSpace(r) {
count++
}
}
return count
}
func writeTestCaseSummary(out io.Writer, execution *Execution, conf testCaseFormatConfig) {
testCases := conf.getter(execution)
if len(testCases) == 0 {
return
}
fmt.Fprintln(out, "\n=== "+conf.header)
for _, tc := range testCases {
fmt.Fprintf(out, "=== %s: %s %s (%s)\n",
conf.prefix,
relativePackagePath(tc.Package),
tc.Test,
FormatDurationAsSeconds(tc.Elapsed, 2))
for _, line := range execution.OutputLines(tc.Package, tc.Test) {
if isRunLine(line) || conf.filter(line) {
continue
}
fmt.Fprint(out, line)
}
fmt.Fprintln(out)
}
}
type testCaseFormatConfig struct {
header string
prefix string
filter func(string) bool
getter func(*Execution) []TestCase
}
func formatFailed() testCaseFormatConfig {
withColor := color.RedString
return testCaseFormatConfig{
header: withColor("Failed"),
prefix: withColor("FAIL"),
filter: func(line string) bool {
return strings.HasPrefix(line, "--- FAIL: Test")
},
getter: func(execution *Execution) []TestCase {
return execution.Failed()
},
}
}
func formatSkipped() testCaseFormatConfig {
withColor := color.YellowString
return testCaseFormatConfig{
header: withColor("Skipped"),
prefix: withColor("SKIP"),
filter: func(line string) bool {
return strings.HasPrefix(line, "--- SKIP: Test")
},
getter: func(execution *Execution) []TestCase {
return execution.Skipped()
},
}
}
func isRunLine(line string) bool {
return strings.HasPrefix(line, "=== RUN Test")
}