forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
junitmerge.go
155 lines (137 loc) · 3.23 KB
/
junitmerge.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
package main
import (
"encoding/xml"
"log"
"os"
"fmt"
"github.com/openshift/origin/tools/junitreport/pkg/api"
"sort"
)
type uniqueSuites map[string]*suiteRuns
func (s uniqueSuites) Merge(namePrefix string, suite *api.TestSuite) {
name := suite.Name
if len(namePrefix) > 0 {
name = namePrefix + "/"
}
existing, ok := s[name]
if !ok {
existing = newSuiteRuns(suite)
s[name] = existing
}
existing.Merge(suite.TestCases)
for _, suite := range suite.Children {
s.Merge(name, suite)
}
}
type suiteRuns struct {
suite *api.TestSuite
runs map[string]*api.TestCase
}
func newSuiteRuns(suite *api.TestSuite) *suiteRuns {
return &suiteRuns{
suite: suite,
runs: make(map[string]*api.TestCase),
}
}
func (r *suiteRuns) Merge(testCases []*api.TestCase) {
for _, testCase := range testCases {
existing, ok := r.runs[testCase.Name]
if !ok {
r.runs[testCase.Name] = testCase
continue
}
switch {
case testCase.SkipMessage != nil:
// if the new test is a skip, ignore it
case existing.SkipMessage != nil && testCase.SkipMessage == nil:
// always replace a skip with a non-skip
r.runs[testCase.Name] = testCase
case existing.FailureOutput == nil && testCase.FailureOutput != nil:
// replace a passing test with a failing test
r.runs[testCase.Name] = testCase
}
}
}
func main() {
log.SetFlags(0)
suites := make(uniqueSuites)
for _, arg := range os.Args[1:] {
f, err := os.Open(arg)
if err != nil {
log.Fatal(err)
}
defer f.Close()
d := xml.NewDecoder(f)
for {
t, err := d.Token()
if err != nil {
log.Fatal(err)
}
if t == nil {
log.Fatalf("input file %s does not appear to be a JUnit XML file", arg)
}
// Inspect the top level DOM element and perform the appropriate action
switch se := t.(type) {
case xml.StartElement:
switch se.Name.Local {
case "testsuites":
input := &api.TestSuites{}
if err := d.DecodeElement(input, &se); err != nil {
log.Fatal(err)
}
for _, suite := range input.Suites {
suites.Merge("", suite)
}
case "testsuite":
input := &api.TestSuite{}
if err := d.DecodeElement(input, &se); err != nil {
log.Fatal(err)
}
suites.Merge("", input)
default:
log.Fatal(fmt.Errorf("unexpected top level element in %s: %s", arg, se.Name.Local))
}
default:
continue
}
break
}
}
var suiteNames []string
for k := range suites {
suiteNames = append(suiteNames, k)
}
sort.Sort(sort.StringSlice(suiteNames))
output := &api.TestSuites{}
for _, name := range suiteNames {
suite := suites[name]
out := &api.TestSuite{
Name: name,
NumTests: uint(len(suite.runs)),
}
var keys []string
for k := range suite.runs {
keys = append(keys, k)
}
sort.Sort(sort.StringSlice(keys))
for _, k := range keys {
testCase := suite.runs[k]
out.TestCases = append(out.TestCases, testCase)
switch {
case testCase.SkipMessage != nil:
out.NumSkipped++
case testCase.FailureOutput != nil:
out.NumFailed++
}
out.Duration += testCase.Duration
}
output.Suites = append(output.Suites, out)
}
e := xml.NewEncoder(os.Stdout)
e.Indent("", "\t")
if err := e.Encode(output); err != nil {
log.Fatal(err)
}
e.Flush()
fmt.Fprintln(os.Stdout)
}