-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathjunit.go
More file actions
80 lines (66 loc) · 1.79 KB
/
junit.go
File metadata and controls
80 lines (66 loc) · 1.79 KB
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
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
}
}
// loadJUnitTimingsFromGlob loads test timings from JUnit XML files matching a glob pattern
func loadJUnitTimingsFromGlob(globPattern string) map[string]float64 {
fileTimes := make(map[string]float64)
if globPattern == "" {
return fileTimes
}
filenames, err := doublestar.Glob(globPattern)
if err != nil {
fatalMsg("failed to match jUnit filename pattern: %v", err)
}
if len(filenames) == 0 {
printMsg("warning: no files matched pattern %s\n", globPattern)
return fileTimes
}
for _, junitFilename := range filenames {
file, err := os.Open(junitFilename)
if err != nil {
fatalMsg("failed to open junit xml: %v\n", err)
}
printMsg("loaded test times from %s\n", junitFilename)
addFileTimesFromIOReader(fileTimes, file)
file.Close()
}
return fileTimes
}
func getFileTimesFromJUnitXML(fileTimes map[string]float64) {
if junitXMLPath != "" {
loadedTimes := loadJUnitTimingsFromGlob(junitXMLPath)
for file, time := range loadedTimes {
fileTimes[file] += time
}
} else {
printMsg("using test times from JUnit report at stdin\n")
addFileTimesFromIOReader(fileTimes, os.Stdin)
}
}