forked from SemanticallyNull/golandreporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgolandreporter.go
150 lines (130 loc) · 3.83 KB
/
golandreporter.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 golandreporter
import (
"fmt"
"github.com/onsi/ginkgo/config"
"github.com/onsi/ginkgo/reporters"
"github.com/onsi/ginkgo/reporters/stenographer"
"github.com/onsi/ginkgo/types"
"os"
"strings"
"testing"
"time"
)
type node struct {
parent *node
description string
failure *types.SpecFailure
time time.Duration
testResult string
children []*node
}
var root *node
type GolandReporter struct{}
func NewGolandReporter() reporters.Reporter {
return GolandReporter{}
}
func NewAutoGolandReporter() reporters.Reporter {
if strings.Contains(strings.ToLower(os.Getenv("OLDPWD")), "goland") ||
strings.Contains(strings.ToLower(os.Getenv("SNAP_NAME")), "goland") {
return NewGolandReporter()
} else {
stenographer := stenographer.New(!config.DefaultReporterConfig.NoColor, config.GinkgoConfig.FlakeAttempts > 1, os.Stdout)
return reporters.NewDefaultReporter(config.DefaultReporterConfig, stenographer)
}
}
func (g GolandReporter) SpecSuiteWillBegin(config config.GinkgoConfigType, summary *types.SuiteSummary) {
root = &node{nil, "[Top Level]", nil, 0, "", []*node{}}
}
func (g GolandReporter) BeforeSuiteDidRun(setupSummary *types.SetupSummary) {
root.print()
}
func (g GolandReporter) SpecWillRun(specSummary *types.SpecSummary) {
insertNode(root, specSummary.ComponentTexts[1:])
}
func (g GolandReporter) SpecDidComplete(specSummary *types.SpecSummary) {
if specSummary.Passed() {
updateResult(root, specSummary, "PASS")
} else if specSummary.HasFailureState() {
updateResult(root, specSummary, "FAIL")
spec, ok := findNode(root, specSummary.ComponentTexts[1:])
if ok {
spec.failure = &specSummary.Failure
}
} else if specSummary.Skipped() {
updateResult(root, specSummary, "SKIP")
} else if specSummary.Pending() {
updateResult(root, specSummary, "SKIP")
} else {
panic("Unknown test output")
}
}
func updateResult(node *node, specSummary *types.SpecSummary, result string) {
for i := 1; i < len(specSummary.ComponentTexts); i++ {
target, ok := findNode(node, specSummary.ComponentTexts[1:i+1])
target.time = target.time + specSummary.RunTime
if !ok {
panic(strings.Join(specSummary.ComponentTexts, "/"))
}
if !strings.Contains(target.testResult, "FAIL") {
target.testResult = result
}
}
}
func findNode(node *node, components []string) (*node, bool) {
if len(components) == 0 {
return node, true
}
component := strings.ReplaceAll(components[0], " ", "_")
child, ok := getChild(node.children, component)
if !ok {
return nil, false
}
return findNode(child, components[1:])
}
func (g GolandReporter) AfterSuiteDidRun(setupSummary *types.SetupSummary) {
root.print()
}
func (g GolandReporter) SpecSuiteDidEnd(summary *types.SuiteSummary) {
root.print()
}
func (n node) print() {
fmt.Printf("=== RUN %s\n", getSpecName(n))
for _, node := range n.children {
node.print()
}
if n.failure != nil {
fmt.Printf("%v\n", n.failure.Location.String())
fmt.Printf("%s\n\n", n.failure.Message)
if testing.Verbose() {
fmt.Printf("%s\n\n", n.failure.Location.FullStackTrace)
}
}
fmt.Printf("--- %s: %s (%.3fs)\n", n.testResult, getSpecName(n), n.time.Seconds())
}
func getSpecName(n node) string {
if n.description == "[Top Level]" {
return ""
}
name := fmt.Sprintf("%v/%v", getSpecName(*n.parent), n.description)
return strings.TrimPrefix(name, "/")
}
func insertNode(current *node, components []string) {
if len(components) < 1 {
return
}
component := strings.ReplaceAll(components[0], " ", "_")
child, ok := getChild(current.children, component)
if !ok {
child = &node{current, component, nil, 0,"", []*node{}}
current.children = append(current.children, child)
}
insertNode(child, components[1:])
}
func getChild(children []*node, c string) (*node, bool) {
for _, node := range children {
if node.description == c {
return node, true
}
}
return nil, false
}