-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.go
125 lines (102 loc) · 2.63 KB
/
xml.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
package junit
import (
"encoding/xml"
"fmt"
"os"
"sync"
"time"
)
// A nice JUnit XML file format document can be found here:
// http://llg.cubic.org/docs/junit/
type testSuites struct {
suiteLock sync.Mutex
XMLName xml.Name `xml:"testsuites"`
Suites []*testSuite `xml:"testsuite"`
}
func newTestSuites() *testSuites {
return &testSuites{
Suites: make([]*testSuite, 0),
}
}
func (s *testSuites) GetSuite(name string) *testSuite {
s.suiteLock.Lock()
defer s.suiteLock.Unlock()
for _, suite := range s.Suites {
if suite.Name == name {
return suite
}
}
hostname, err := os.Hostname()
if err != nil {
hostname = "localhost"
}
newSuite := &testSuite{
Name: name,
ID: len(s.Suites),
Package: name,
Timestamp: time.Now().Format("2006-01-02T15:04:05"),
Hostname: hostname,
}
s.Suites = append(s.Suites, newSuite)
return newSuite
}
type testSuite struct {
// These must be at the top of the struct due to how 64 bit values are
// handled by the atomic package in 32 bit Go builds
Tests int64 `xml:"tests,attr"`
Failures int64 `xml:"failures,attr"`
Errors int64 `xml:"errors,attr"`
Name string `xml:"name,attr"`
ID int `xml:"id,attr"`
Package string `xml:"package,attr"`
Timestamp string `xml:"timestamp,attr"`
Hostname string `xml:"hostname,attr"`
Time float64 `xml:"time,attr"`
Properties *suiteProperties `xml:"properties,omitempty"`
testCaseLock sync.Mutex
TestCases []*testCase `xml:"testcase"`
}
func (s *testSuite) AddProperty(name, value string) {
if s.Properties == nil {
s.Properties = &suiteProperties{
Properties: make([]*suiteProperty, 1),
}
}
s.Properties.Properties = append(s.Properties.Properties, &suiteProperty{
Name: name,
Value: value,
})
}
func (s *testSuite) AddTestCase(newCase *testCase) {
s.testCaseLock.Lock()
defer s.testCaseLock.Unlock()
if s.TestCases == nil {
s.TestCases = make([]*testCase, 1)
}
s.TestCases = append(s.TestCases, newCase)
}
type suiteProperties struct {
Properties []*suiteProperty `xml:"property"`
}
type suiteProperty struct {
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}
type testCase struct {
ClassName string `xml:"classname,attr"`
Name string `xml:"name,attr"`
Time float64 `xml:"time,attr"`
Failure *caseFailure `xml:"failure"`
}
func (c *testCase) SetFailure(file string, line int, message string) {
c.Failure = &caseFailure{
Message: fmt.Sprintf("%s:%d", file, line),
Type: "assertion",
Text: message,
}
}
type caseFailure struct {
Message string `xml:"message,attr"`
Type string `xml:"type,attr"`
Text string `xml:",chardata"`
}