Browse code

Replace default reporter in Ginkgo with SimpleReporter

Michal Fojtik authored on 2015/10/07 19:00:29
Showing 3 changed files
... ...
@@ -110,4 +110,4 @@ fi
110 110
 echo "[INFO] Running extended tests"
111 111
 
112 112
 # Run the tests
113
-TMPDIR=${BASETMPDIR} ginkgo -progress -stream -v "-skip=${SKIP}" "$@" ${OS_OUTPUT_BINPATH}/extended.test
113
+TMPDIR=${BASETMPDIR} ginkgo "-skip=${SKIP}" "$@" ${OS_OUTPUT_BINPATH}/extended.test
114 114
new file mode 100644
... ...
@@ -0,0 +1,126 @@
0
+package util
1
+
2
+import (
3
+	"fmt"
4
+	"io"
5
+	"os"
6
+	"strings"
7
+
8
+	"github.com/onsi/ginkgo/config"
9
+	"github.com/onsi/ginkgo/reporters/stenographer"
10
+	"github.com/onsi/ginkgo/types"
11
+)
12
+
13
+const maxDescriptionLength = 100
14
+
15
+type SimpleReporter struct {
16
+	stenographer stenographer.Stenographer
17
+	Output       io.Writer
18
+}
19
+
20
+func NewSimpleReporter() *SimpleReporter {
21
+	return &SimpleReporter{
22
+		Output:       os.Stdout,
23
+		stenographer: stenographer.New(!config.DefaultReporterConfig.NoColor),
24
+	}
25
+}
26
+
27
+func (r *SimpleReporter) SpecSuiteWillBegin(config config.GinkgoConfigType, summary *types.SuiteSummary) {
28
+	fmt.Fprintf(r.Output, "=== SUITE %s (%d total specs, %d will run):\n", summary.SuiteDescription, summary.NumberOfTotalSpecs, summary.NumberOfSpecsThatWillBeRun)
29
+}
30
+
31
+func (r *SimpleReporter) BeforeSuiteDidRun(*types.SetupSummary) {
32
+}
33
+
34
+func (r *SimpleReporter) SpecWillRun(spec *types.SpecSummary) {
35
+	r.printRunLine(spec)
36
+}
37
+
38
+func (r *SimpleReporter) SpecDidComplete(spec *types.SpecSummary) {
39
+	r.handleSpecFailure(spec)
40
+	r.printStatusLine(spec)
41
+}
42
+
43
+func (r *SimpleReporter) AfterSuiteDidRun(setupSummary *types.SetupSummary) {
44
+}
45
+
46
+func (r *SimpleReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) {
47
+}
48
+
49
+func (r *SimpleReporter) handleSpecFailure(spec *types.SpecSummary) {
50
+	switch spec.State {
51
+	case types.SpecStateFailed:
52
+		r.stenographer.AnnounceSpecFailed(spec, true, false)
53
+	case types.SpecStatePanicked:
54
+		r.stenographer.AnnounceSpecPanicked(spec, true, false)
55
+	case types.SpecStateTimedOut:
56
+		r.stenographer.AnnounceSpecTimedOut(spec, true, false)
57
+	}
58
+}
59
+
60
+func (r *SimpleReporter) printStatusLine(spec *types.SpecSummary) {
61
+	runTime := ""
62
+	if runTime = fmt.Sprintf(" (%v)", spec.RunTime); runTime == " (0)" {
63
+		runTime = ""
64
+	}
65
+	fmt.Fprintf(r.Output, "%4s%-16s %s%s\n", " ", stateToString(spec.State), specDescription(spec), runTime)
66
+}
67
+
68
+func (r *SimpleReporter) printRunLine(spec *types.SpecSummary) {
69
+	fmt.Fprintf(r.Output, "=== RUN %s:\n", trimLocation(spec.ComponentCodeLocations[1]))
70
+}
71
+
72
+func specDescription(spec *types.SpecSummary) string {
73
+	name := ""
74
+	for _, t := range spec.ComponentTexts[1:len(spec.ComponentTexts)] {
75
+		name += strings.TrimSpace(t) + " "
76
+	}
77
+	if len(name) == 0 {
78
+		name = fmt.Sprintf("FIXME: Spec without valid name (%s)", spec.ComponentTexts)
79
+	}
80
+	return short(strings.TrimSpace(name))
81
+}
82
+
83
+func short(s string) string {
84
+	runes := []rune(s)
85
+	if len(runes) > maxDescriptionLength {
86
+		return string(runes[:maxDescriptionLength]) + " ..."
87
+	}
88
+	return s
89
+}
90
+
91
+func bold(v string) string {
92
+	return "\033[1m" + v + "\033[0m"
93
+}
94
+
95
+func red(v string) string {
96
+	return "\033[31m" + v + "\033[0m"
97
+}
98
+
99
+func magenta(v string) string {
100
+	return "\033[35m" + v + "\033[0m"
101
+}
102
+
103
+func stateToString(s types.SpecState) string {
104
+	switch s {
105
+	case types.SpecStatePassed:
106
+		return bold("ok")
107
+	case types.SpecStateSkipped:
108
+		return magenta("skip")
109
+	case types.SpecStateFailed:
110
+		return red("fail")
111
+	case types.SpecStateTimedOut:
112
+		return red("timed")
113
+	case types.SpecStatePanicked:
114
+		return red("panic")
115
+	case types.SpecStatePending:
116
+		return magenta("pending")
117
+	default:
118
+		return bold(fmt.Sprintf("%v", s))
119
+	}
120
+}
121
+
122
+func trimLocation(l types.CodeLocation) string {
123
+	delimiter := "/openshift/origin/"
124
+	return fmt.Sprintf("%q", l.FileName[strings.LastIndex(l.FileName, delimiter)+len(delimiter):])
125
+}
... ...
@@ -65,5 +65,7 @@ func ExecuteTest(t *testing.T, reportDir string) {
65 65
 		r = append(r, reporters.NewJUnitReporter(path.Join(reportDir, fmt.Sprintf("junit_%02d.xml", config.GinkgoConfig.ParallelNode))))
66 66
 	}
67 67
 
68
-	ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "OpenShift extended tests suite", r)
68
+	r = append(r, NewSimpleReporter())
69
+
70
+	ginkgo.RunSpecsWithCustomReporters(t, "OpenShift extended tests suite", r)
69 71
 }