Skip to content

Commit

Permalink
enable passing a glob for jUnit XML
Browse files Browse the repository at this point in the history
  • Loading branch information
leonid-shevtsov committed May 19, 2020
1 parent 86da608 commit 2ae9631
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
34 changes: 22 additions & 12 deletions junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io"
"os"
"path"

"github.com/bmatcuk/doublestar"
)

type junitXML struct {
Expand All @@ -26,23 +28,31 @@ func loadJUnitXML(reader io.Reader) *junitXML {
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) {
var source io.Reader
if junitXMLPath != "" {
file, err := os.Open(junitXMLPath)
filenames, err := doublestar.Glob(junitXMLPath)
if err != nil {
fatalMsg("failed to open junit xml: %v\n", err)
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", junitXMLPath)
addFileTimesFromIOReader(fileTimes, file)
file.Close()
}
defer file.Close()
printMsg("using test times from JUnit report %s\n", junitXMLPath)
source = file
} else {
printMsg("using test times from JUnit report at stdin\n")
source = os.Stdin
}
junitXML := loadJUnitXML(source)
for _, testCase := range junitXML.TestCases {
filePath := path.Clean(testCase.File)
fileTimes[filePath] += testCase.Time
addFileTimesFromIOReader(fileTimes, os.Stdin)
}
}
2 changes: 1 addition & 1 deletion split_tests.go → main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func parseFlags() {
flag.StringVar(&circleCIBranchName, "circleci-branch", "", "Current branch for CircleCI (or set CIRCLE_BRANCH) - required to use CircleCI")

flag.BoolVar(&useJUnitXML, "junit", false, "Use a JUnit XML report for test times")
flag.StringVar(&junitXMLPath, "junit-path", "", "Path to a JUnit XML report (leave empty to read from stdin)")
flag.StringVar(&junitXMLPath, "junit-path", "", "Path to a JUnit XML report (leave empty to read from stdin; use glob pattern to load multiple files)")

flag.BoolVar(&useLineCount, "line-count", false, "Use line count to estimate test times")

Expand Down

0 comments on commit 2ae9631

Please sign in to comment.