forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
270 lines (239 loc) · 7.71 KB
/
parser.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package gotest
import (
"bufio"
"fmt"
"strings"
"github.com/openshift/origin/tools/junitreport/pkg/api"
"github.com/openshift/origin/tools/junitreport/pkg/builder"
"github.com/openshift/origin/tools/junitreport/pkg/parser"
)
// NewParser returns a new parser that's capable of parsing Go unit test output
func NewParser(builder builder.TestSuitesBuilder, stream bool) parser.TestOutputParser {
return &testOutputParser{
builder: builder,
stream: stream,
}
}
type testOutputParser struct {
builder builder.TestSuitesBuilder
stream bool
}
const (
stateBegin = iota
stateOutput
stateResults
stateComplete
)
func log(format string, args ...interface{}) {
//fmt.Printf(format, args...)
}
// Parse parses `go test -v` output into test suites. Test output from `go test -v` is not bookmarked for packages, so
// the parsing strategy is to advance line-by-line, building up a slice of test cases until a package declaration is found,
// at which point all tests cases are added to that package and the process can start again.
func (p *testOutputParser) Parse(input *bufio.Scanner) (*api.TestSuites, error) {
suites := &api.TestSuites{}
var testNameStack []string
var tests map[string]*api.TestCase
var output map[string][]string
var messages map[string][]string
var currentSuite *api.TestSuite
var state int
var count int
var orderedTests []string
for input.Scan() {
line := input.Text()
count++
log("Line %03d: %d: %s\n", count, state, line)
switch state {
case stateBegin:
// this is the first state
name, ok := ExtractRun(line)
if !ok {
// A test that defines a test.M handler can write output prior to test execution. We will drop this because
// we have no place to put it, although the first test case *could* use it in the future.
log(" ignored output outside of suite\n")
continue
}
log(" found run command %s\n", name)
currentSuite = &api.TestSuite{}
tests = make(map[string]*api.TestCase)
output = make(map[string][]string)
messages = make(map[string][]string)
orderedTests = []string{name}
testNameStack = []string{name}
tests[testNameStack[0]] = &api.TestCase{
Name: name,
}
state = stateOutput
case stateOutput:
// open a new test for gathering output
if name, ok := ExtractRun(line); ok {
log(" found run command %s\n", name)
test, ok := tests[name]
if !ok {
test = &api.TestCase{
Name: name,
}
tests[name] = test
}
orderedTests = append(orderedTests, name)
testNameStack = []string{name}
continue
}
// transition to result mode ONLY if it matches a result at the top level
if result, name, depth, duration, ok := ExtractResult(line); ok && tests[name] != nil && depth == 0 {
test := tests[name]
log(" found result %s %s %s\n", result, name, duration)
switch result {
case api.TestResultPass:
case api.TestResultFail:
test.FailureOutput = &api.FailureOutput{}
case api.TestResultSkip:
test.SkipMessage = &api.SkipMessage{}
}
if err := test.SetDuration(duration); err != nil {
return nil, fmt.Errorf("unexpected duration on line %d: %s", count, duration)
}
testNameStack = []string{name}
state = stateResults
continue
}
// in output mode, turn output lines into output on the particular test
if _, _, ok := ExtractOutput(line); ok {
log(" found output\n")
output[testNameStack[0]] = append(output[testNameStack[0]], line)
continue
}
log(" fallthrough\n")
case stateResults:
output, depth, ok := ExtractOutput(line)
if !ok {
return nil, fmt.Errorf("unexpected output on line %d, can't grab results", count)
}
// we're back to the root, we expect either a new RUN, a test suite end, or this is just an
// output line that was chopped up
if depth == 0 {
if name, ok := ExtractRun(line); ok {
log(" found run %s\n", name)
// starting a new set of runs
orderedTests = append(orderedTests, name)
testNameStack = []string{name}
tests[testNameStack[0]] = &api.TestCase{
Name: name,
}
state = stateOutput
continue
}
switch {
case line == "PASS", line == "FAIL":
log(" found end of suite\n")
// at the end of the suite
state = stateComplete
default:
// a broken output line that was not indented
log(" found message\n")
name := testNameStack[len(testNameStack)-1]
test := tests[name]
switch {
case test.FailureOutput != nil, test.SkipMessage != nil:
messages[name] = append(messages[name], output)
}
}
continue
}
// if this is a result AND we have already declared this as a test, parse it
if result, name, _, duration, ok := ExtractResult(output); ok && tests[name] != nil {
log(" found result %s %s (%d)\n", result, name, depth)
test := tests[name]
switch result {
case api.TestResultPass:
case api.TestResultFail:
test.FailureOutput = &api.FailureOutput{}
case api.TestResultSkip:
test.SkipMessage = &api.SkipMessage{}
}
if err := test.SetDuration(duration); err != nil {
return nil, fmt.Errorf("unexpected duration on line %d: %s", count, duration)
}
switch {
case depth >= len(testNameStack):
// we found a new, more deeply nested test
testNameStack = append(testNameStack, name)
default:
if depth < len(testNameStack)-1 {
// the current result is less indented than our current test, so remove the deepest
// items from the stack
testNameStack = testNameStack[:depth]
}
testNameStack[len(testNameStack)-1] = name
}
continue
}
// treat as regular output at the appropriate depth
log(" found message line %d %v\n", depth, testNameStack)
// BUG: in go test, double nested output is double indented for some reason
if depth >= len(testNameStack) {
depth = len(testNameStack) - 1
}
name := testNameStack[depth]
log(" name %s\n", name)
if test, ok := tests[name]; ok {
switch {
case test.FailureOutput != nil, test.SkipMessage != nil:
messages[name] = append(messages[name], output)
}
}
case stateComplete:
// suite exit line
if name, duration, coverage, ok := ExtractPackage(line); ok {
currentSuite.Name = name
if props, ok := ExtractProperties(coverage); ok {
for k, v := range props {
currentSuite.AddProperty(k, v)
}
}
for _, name := range orderedTests {
test := tests[name]
messageLines := messages[name]
var extraOutput []string
for i, s := range messageLines {
if s == "=== OUTPUT" {
log("test %s has OUTPUT section, %d %d\n", name, i, len(messageLines))
if i < len(messageLines) {
log(" test %s add lines: %d\n", name, len(messageLines[i+1:]))
extraOutput = messageLines[i+1:]
}
messageLines = messageLines[:i]
break
}
}
switch {
case test.FailureOutput != nil:
test.FailureOutput.Output = strings.Join(messageLines, "\n")
lines := append(output[name], extraOutput...)
test.SystemOut = strings.Join(lines, "\n")
case test.SkipMessage != nil:
test.SkipMessage.Message = strings.Join(messageLines, "\n")
default:
lines := append(output[name], extraOutput...)
test.SystemOut = strings.Join(lines, "\n")
}
currentSuite.AddTestCase(test)
}
if err := currentSuite.SetDuration(duration); err != nil {
return nil, fmt.Errorf("unexpected duration on line %d: %s", count, duration)
}
suites.Suites = append(suites.Suites, currentSuite)
state = stateBegin
continue
}
// coverage only line
if props, ok := ExtractProperties(line); ok {
for k, v := range props {
currentSuite.AddProperty(k, v)
}
}
}
}
return suites, nil
}