forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.go
126 lines (105 loc) · 3.2 KB
/
reporter.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
package util
import (
"fmt"
"io"
"os"
"strings"
"github.com/onsi/ginkgo/config"
"github.com/onsi/ginkgo/reporters/stenographer"
"github.com/onsi/ginkgo/types"
)
const maxDescriptionLength = 100
type SimpleReporter struct {
stenographer stenographer.Stenographer
Output io.Writer
}
func NewSimpleReporter() *SimpleReporter {
return &SimpleReporter{
Output: os.Stdout,
stenographer: stenographer.New(!config.DefaultReporterConfig.NoColor, false),
}
}
func (r *SimpleReporter) SpecSuiteWillBegin(config config.GinkgoConfigType, summary *types.SuiteSummary) {
fmt.Fprintf(r.Output, "=== SUITE %s (%d total specs, %d will run):\n", summary.SuiteDescription, summary.NumberOfTotalSpecs, summary.NumberOfSpecsThatWillBeRun)
}
func (r *SimpleReporter) BeforeSuiteDidRun(*types.SetupSummary) {
}
func (r *SimpleReporter) SpecWillRun(spec *types.SpecSummary) {
r.printRunLine(spec)
}
func (r *SimpleReporter) SpecDidComplete(spec *types.SpecSummary) {
r.handleSpecFailure(spec)
r.printStatusLine(spec)
}
func (r *SimpleReporter) AfterSuiteDidRun(setupSummary *types.SetupSummary) {
}
func (r *SimpleReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) {
}
func (r *SimpleReporter) handleSpecFailure(spec *types.SpecSummary) {
switch spec.State {
case types.SpecStateFailed:
r.stenographer.AnnounceSpecFailed(spec, true, false)
case types.SpecStatePanicked:
r.stenographer.AnnounceSpecPanicked(spec, true, false)
case types.SpecStateTimedOut:
r.stenographer.AnnounceSpecTimedOut(spec, true, false)
}
}
func (r *SimpleReporter) printStatusLine(spec *types.SpecSummary) {
runTime := ""
if runTime = fmt.Sprintf(" (%v)", spec.RunTime); runTime == " (0)" {
runTime = ""
}
fmt.Fprintf(r.Output, "%4s%-16s %s%s\n", " ", stateToString(spec.State), specDescription(spec), runTime)
}
func (r *SimpleReporter) printRunLine(spec *types.SpecSummary) {
fmt.Fprintf(r.Output, "=== RUN %s:\n", trimLocation(spec.ComponentCodeLocations[1]))
}
func specDescription(spec *types.SpecSummary) string {
name := ""
for _, t := range spec.ComponentTexts[1:len(spec.ComponentTexts)] {
name += strings.TrimSpace(t) + " "
}
if len(name) == 0 {
name = fmt.Sprintf("FIXME: Spec without valid name (%s)", spec.ComponentTexts)
}
return short(strings.TrimSpace(name))
}
func short(s string) string {
runes := []rune(s)
if len(runes) > maxDescriptionLength {
return string(runes[:maxDescriptionLength]) + " ..."
}
return s
}
func bold(v string) string {
return "\033[1m" + v + "\033[0m"
}
func red(v string) string {
return "\033[31m" + v + "\033[0m"
}
func magenta(v string) string {
return "\033[35m" + v + "\033[0m"
}
func stateToString(s types.SpecState) string {
switch s {
case types.SpecStatePassed:
return bold("ok")
case types.SpecStateSkipped:
return magenta("skip")
case types.SpecStateFailed:
return red("fail")
case types.SpecStateTimedOut:
return red("timed")
case types.SpecStatePanicked:
return red("panic")
case types.SpecStatePending:
return magenta("pending")
default:
return bold(fmt.Sprintf("%v", s))
}
}
func trimLocation(l types.CodeLocation) string {
delimiter := "/openshift/origin/"
return fmt.Sprintf("%q", l.FileName[strings.LastIndex(l.FileName, delimiter)+len(delimiter):])
}