-
Notifications
You must be signed in to change notification settings - Fork 21
/
ingest.go
126 lines (108 loc) Β· 2.87 KB
/
ingest.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
// Copyright Josh Komoroske. All rights reserved.
// Use of this source code is governed by the MIT license,
// a copy of which can be found in the LICENSE.txt file.
// SPDX-License-Identifier: MIT
package junit
import (
"strconv"
"strings"
"time"
)
// findSuites performs a depth-first search through the XML document, and
// attempts to ingest any "testsuite" tags that are encountered.
func findSuites(nodes []xmlNode, suites chan Suite) {
for _, node := range nodes {
switch node.XMLName.Local {
case "testsuite":
suites <- ingestSuite(node)
default:
findSuites(node.Nodes, suites)
}
}
}
func ingestSuite(root xmlNode) Suite {
suite := Suite{
Name: root.Attr("name"),
Package: root.Attr("package"),
Properties: root.Attrs,
}
for _, node := range root.Nodes {
switch node.XMLName.Local {
case "testsuite":
testsuite := ingestSuite(node)
suite.Suites = append(suite.Suites, testsuite)
case "testcase":
testcase := ingestTestcase(node)
suite.Tests = append(suite.Tests, testcase)
case "properties":
props := ingestProperties(node)
suite.Properties = props
case "system-out":
suite.SystemOut = string(node.Content)
case "system-err":
suite.SystemErr = string(node.Content)
}
}
suite.Aggregate()
return suite
}
func ingestProperties(root xmlNode) map[string]string {
props := make(map[string]string, len(root.Nodes))
for _, node := range root.Nodes {
if node.XMLName.Local == "property" {
name := node.Attr("name")
value := node.Attr("value")
props[name] = value
}
}
return props
}
func ingestTestcase(root xmlNode) Test {
test := Test{
Name: root.Attr("name"),
Classname: root.Attr("classname"),
Duration: duration(root.Attr("time")),
Status: StatusPassed,
Properties: root.Attrs,
}
for _, node := range root.Nodes {
switch node.XMLName.Local {
case "skipped":
test.Status = StatusSkipped
test.Message = node.Attr("message")
case "failure":
test.Status = StatusFailed
test.Message = node.Attr("message")
test.Error = ingestError(node)
case "error":
test.Status = StatusError
test.Message = node.Attr("message")
test.Error = ingestError(node)
case "system-out":
test.SystemOut = string(node.Content)
case "system-err":
test.SystemErr = string(node.Content)
}
}
return test
}
func ingestError(root xmlNode) Error {
return Error{
Body: string(root.Content),
Type: root.Attr("type"),
Message: root.Attr("message"),
}
}
func duration(timespec string) time.Duration {
// Remove commas for larger durations
timespec = strings.ReplaceAll(timespec, ",", "")
// Check if there was a valid decimal value
if s, err := strconv.ParseFloat(timespec, 64); err == nil {
return time.Duration(s * float64(time.Second))
}
// Check if there was a valid duration string
if d, err := time.ParseDuration(timespec); err == nil {
return d
}
return 0
}