-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
gotest2junit.go
211 lines (194 loc) · 4.97 KB
/
gotest2junit.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
package main
import (
"bufio"
"encoding/json"
"encoding/xml"
"flag"
"fmt"
"io"
"os"
"sort"
"strings"
"time"
"github.com/openshift/origin/tools/gotest2junit/pkg/api"
)
type Record struct {
Package string
Test string
Time time.Time
Action string
Output string
Elapsed float64
}
type testSuite struct {
suite *api.TestSuite
tests map[string]*api.TestCase
}
func main() {
summarize := false
verbose := false
flag.BoolVar(&summarize, "summary", true, "display a summary as items are processed")
flag.BoolVar(&verbose, "v", false, "display passing results")
flag.Parse()
if err := process(os.Stdin, summarize, verbose); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func process(r io.Reader, summarize, verbose bool) error {
suites, err := stream(r, summarize, verbose)
if err != nil {
return err
}
obj := newTestSuites(suites)
out, err := xml.MarshalIndent(obj, "", " ")
if err != nil {
return err
}
fmt.Fprintf(os.Stdout, "%s\n", string(out))
return nil
}
func newTestSuites(suites map[string]*testSuite) *api.TestSuites {
all := &api.TestSuites{}
for _, suite := range suites {
for _, test := range suite.suite.TestCases {
suite.suite.NumTests++
if test.SkipMessage != nil {
suite.suite.NumSkipped++
continue
}
if test.FailureOutput != nil {
suite.suite.NumFailed++
continue
}
}
// suites with no tests are usually empty packages, ignore them
if suite.suite.NumTests == 0 {
continue
}
// always return the test cases in consistent order
sort.Slice(suite.suite.TestCases, func(i, j int) bool {
return suite.suite.TestCases[i].Name < suite.suite.TestCases[j].Name
})
all.Suites = append(all.Suites, suite.suite)
}
// always return the test suites in consistent order
sort.Slice(all.Suites, func(i, j int) bool {
return all.Suites[i].Name < all.Suites[j].Name
})
return all
}
func stream(r io.Reader, summarize, verbose bool) (map[string]*testSuite, error) {
suites := make(map[string]*testSuite)
defaultTest := &api.TestCase{
Name: "build and execution",
}
defaultSuite := &testSuite{
suite: &api.TestSuite{Name: "go test", TestCases: []*api.TestCase{defaultTest}},
}
suites[""] = defaultSuite
rdr := bufio.NewReader(r)
for {
// some output from go test -json is not valid JSON - read the line to see whether it
// starts with { - if not, just mirror it to stderr and continue.
line, err := rdr.ReadString('\n')
if err != nil {
if err != io.EOF {
return suites, err
}
break
}
if len(line) == 0 || line[0] != '{' {
defaultTest.SystemOut += line
if strings.HasPrefix(line, "FAIL") {
defaultTest.FailureOutput = &api.FailureOutput{}
}
fmt.Fprint(os.Stderr, line)
continue
}
var r Record
if err := json.Unmarshal([]byte(line), &r); err != nil {
if err == io.EOF {
return suites, nil
}
fmt.Fprintf(os.Stderr, "error: Unable to parse remainder of output %v\n", err)
return suites, nil
}
suite, ok := suites[r.Package]
if !ok {
suite = &testSuite{
suite: &api.TestSuite{
Name: r.Package,
},
tests: make(map[string]*api.TestCase),
}
suites[r.Package] = suite
}
// if this is package level output, we only care about pass/fail duration
if len(r.Test) == 0 {
switch r.Action {
case "pass", "fail":
suite.suite.Duration = r.Elapsed
}
continue
}
test, ok := suite.tests[r.Test]
if !ok {
test = &api.TestCase{
Name: r.Test,
}
suite.suite.TestCases = append(suite.suite.TestCases, test)
suite.tests[r.Test] = test
}
switch r.Action {
case "run":
case "pause":
case "cont":
case "bench":
case "skip":
if summarize {
fmt.Fprintf(os.Stderr, "SKIP: %s %s\n", r.Package, r.Test)
}
test.SkipMessage = &api.SkipMessage{
Message: r.Output,
}
case "pass":
if summarize && verbose {
fmt.Fprintf(os.Stderr, "PASS: %s %s %s\n", r.Package, r.Test, time.Duration(r.Elapsed*float64(time.Second)))
}
test.SystemOut = ""
test.Duration = r.Elapsed
case "fail":
if summarize {
fmt.Fprintf(os.Stderr, "FAIL: %s %s %s\n", r.Package, r.Test, time.Duration(r.Elapsed*float64(time.Second)))
}
test.Duration = r.Elapsed
if len(r.Output) == 0 {
r.Output = test.SystemOut
if len(r.Output) > 50 {
r.Output = r.Output[:50] + " ..."
}
}
test.FailureOutput = &api.FailureOutput{
Message: r.Output,
Output: r.Output,
}
case "output":
test.SystemOut += r.Output
default:
// usually a bug in go test -json
out := fmt.Sprintf("error: Unrecognized go test action %s: %#v\n", r.Action, r)
defaultTest.SystemOut += line
defaultTest.SystemOut += out
defaultTest.FailureOutput = &api.FailureOutput{}
fmt.Fprintf(os.Stderr, out)
}
}
// if we recorded any failure output
if defaultTest.FailureOutput != nil {
defaultTest.FailureOutput.Message = "Some packages failed during test execution"
defaultTest.FailureOutput.Output = defaultTest.SystemOut
defaultTest.SystemOut = ""
}
return suites, nil
}