/
testParser.go
161 lines (136 loc) · 3.92 KB
/
testParser.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
package parser
import (
"encoding/json"
"fmt"
"github.com/smartystreets/goconvey/reporting"
"github.com/smartystreets/goconvey/web/server/results"
"strconv"
"strings"
)
func parseTestOutput(test *results.TestResult) *results.TestResult {
parser := newTestParser(test)
parser.parseTestFunctionOutput()
return test
}
func newTestParser(test *results.TestResult) *testParser {
self := &testParser{}
self.test = test
return self
}
func (self *testParser) parseTestFunctionOutput() {
if len(self.test.RawLines) > 0 {
self.processLines()
self.deserializeJson()
self.composeCapturedOutput()
}
}
func (self *testParser) processLines() {
for self.index, self.line = range self.test.RawLines {
if !self.processLine() {
break
}
}
}
func (self *testParser) processLine() bool {
if self.line == reporting.OpenJson {
self.inJson = true
} else if self.line == reporting.CloseJson {
self.inJson = false
} else if self.inJson {
self.jsonLines = append(self.jsonLines, self.line)
} else if isPanic(self.line) {
self.parsePanicOutput()
return false
} else if isGoTestLogOutput(self.line) {
self.parseLogLocation()
} else {
self.otherLines = append(self.otherLines, self.line)
}
return true
}
func (self *testParser) deserializeJson() {
formatted := createArrayForJsonItems(self.jsonLines)
var scopes []reporting.ScopeResult
err := json.Unmarshal(formatted, &scopes)
if err != nil {
panic(fmt.Sprintf(bugReportRequest, err, formatted))
}
self.test.Stories = scopes
}
func (self *testParser) parsePanicOutput() {
for index, line := range self.test.RawLines[self.index:] {
self.parsePanicLocation(index, line)
self.preserveStackTraceIndentation(index, line)
}
self.test.Error = strings.Join(self.test.RawLines, "\n")
}
func (self *testParser) parsePanicLocation(index int, line string) {
if !panicLineHasMetadata(line) {
return
}
metaLine := self.test.RawLines[index+4]
fields := strings.Split(metaLine, " ")
fileAndLine := strings.Split(fields[0], ":")
self.test.File = fileAndLine[0]
self.test.Line, _ = strconv.Atoi(fileAndLine[1])
}
func (self *testParser) preserveStackTraceIndentation(index int, line string) {
if panicLineShouldBeIndented(index, line) {
self.test.RawLines[index] = "\t" + line
}
}
func (self *testParser) parseLogLocation() {
self.otherLines = append(self.otherLines, self.line)
lineFields := self.line
fields := strings.Split(lineFields, ":")
self.test.File = strings.TrimSpace(fields[0])
self.test.Line, _ = strconv.Atoi(fields[1])
}
func (self *testParser) composeCapturedOutput() {
self.test.Message = strings.Join(self.otherLines, "\n")
}
type testParser struct {
test *results.TestResult
line string
index int
inJson bool
jsonLines []string
otherLines []string
}
func isJson(line string) bool {
return strings.HasPrefix(line, "{")
}
func createArrayForJsonItems(lines []string) []byte {
jsonArrayItems := strings.Join(lines, "")
jsonArrayItems = removeTrailingComma(jsonArrayItems)
return []byte(fmt.Sprintf("[%s]\n", jsonArrayItems))
}
func removeTrailingComma(rawJson string) string {
if trailingComma(rawJson) {
return rawJson[:len(rawJson)-1]
}
return rawJson
}
func trailingComma(value string) bool {
return strings.HasSuffix(value, ",")
}
func isGoTestLogOutput(line string) bool {
return strings.Count(line, ":") == 2
}
func isPanic(line string) bool {
return strings.HasPrefix(line, "panic: ")
}
func panicLineHasMetadata(line string) bool {
return strings.HasPrefix(line, "goroutine") && strings.Contains(line, "[running]")
}
func panicLineShouldBeIndented(index int, line string) bool {
return strings.Contains(line, "+") || (index > 0 && strings.Contains(line, "panic: "))
}
const bugReportRequest = `
Uh-oh! Looks like something went wrong. Please copy the following text and file a bug report at:
https://github.com/smartystreets/goconvey/issues?state=open
======= BEGIN BUG REPORT =======
ERROR: %v
OUTPUT: %s
======= END BUG REPORT =======
`