forked from leonid-shevtsov/split_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
junit.go
58 lines (50 loc) · 1.32 KB
/
junit.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
package main
import (
"encoding/xml"
"io"
"os"
"path"
"github.com/bmatcuk/doublestar"
)
type junitXML struct {
TestCases []struct {
File string `xml:"file,attr"`
Time float64 `xml:"time,attr"`
} `xml:"testcase"`
}
func loadJUnitXML(reader io.Reader) *junitXML {
var junitXML junitXML
decoder := xml.NewDecoder(reader)
err := decoder.Decode(&junitXML)
if err != nil {
fatalMsg("failed to parse junit xml: %v\n", err)
}
return &junitXML
}
func addFileTimesFromIOReader(fileTimes map[string]float64, reader io.Reader) {
junitXML := loadJUnitXML(reader)
for _, testCase := range junitXML.TestCases {
filePath := path.Clean(testCase.File)
fileTimes[filePath] += testCase.Time
}
}
func getFileTimesFromJUnitXML(fileTimes map[string]float64) {
if junitXMLPath != "" {
filenames, err := doublestar.Glob(junitXMLPath)
if err != nil {
fatalMsg("failed to match jUnit filename pattern: %v", err)
}
for _, junitFilename := range filenames {
file, err := os.Open(junitFilename)
if err != nil {
fatalMsg("failed to open junit xml: %v\n", err)
}
printMsg("using test times from JUnit report %s\n", junitFilename)
addFileTimesFromIOReader(fileTimes, file)
file.Close()
}
} else {
printMsg("using test times from JUnit report at stdin\n")
addFileTimesFromIOReader(fileTimes, os.Stdin)
}
}