diff --git a/go-junit-report.go b/go-junit-report.go index 117ab5e..c44e4a3 100644 --- a/go-junit-report.go +++ b/go-junit-report.go @@ -7,16 +7,18 @@ import ( ) var noXmlHeader bool +var packageName string func init() { flag.BoolVar(&noXmlHeader, "no-xml-header", false, "do not print xml header") + flag.StringVar(&packageName, "package-name", "", "specify a package name (compiled test have no package name in output)") } func main() { flag.Parse() // Read input - report, err := Parse(os.Stdin) + report, err := Parse(os.Stdin, packageName) if err != nil { fmt.Printf("Error reading input: %s\n", err) os.Exit(1) diff --git a/parser.go b/parser.go index 3d8213e..87ef254 100644 --- a/parser.go +++ b/parser.go @@ -38,7 +38,7 @@ var ( regexResult = regexp.MustCompile(`^(ok|FAIL)\s+(.+)\s(\d+\.\d+)s$`) ) -func Parse(r io.Reader) (*Report, error) { +func Parse(r io.Reader, specifiedPackageName string) (*Report, error) { reader := bufio.NewReader(r) report := &Report{make([]Package, 0)} @@ -46,6 +46,9 @@ func Parse(r io.Reader) (*Report, error) { // keep track of tests we find tests := make([]Test, 0) + // sum of tests' time, use this if current test has no result line (when it is compiled test) + testsTime := 0 + // current test var test *Test @@ -85,6 +88,7 @@ func Parse(r io.Reader) (*Report, error) { }) tests = make([]Test, 0) + testsTime = 0 } else if test != nil { if matches := regexStatus.FindStringSubmatch(line); len(matches) == 4 { // test status @@ -97,7 +101,9 @@ func Parse(r io.Reader) (*Report, error) { } test.Name = matches[2] - test.Time = parseTime(matches[3]) * 10 + testTime := parseTime(matches[3]) * 10 + test.Time = testTime + testsTime += testTime } else if strings.HasPrefix(line, "\t") { // test output test.Output = append(test.Output, line[1:]) @@ -105,6 +111,18 @@ func Parse(r io.Reader) (*Report, error) { } } + if test != nil { + tests = append(tests, *test) + } + + if len(tests) > 0 { //no result line found + report.Packages = append(report.Packages, Package{ + Name: specifiedPackageName, + Time: testsTime, + Tests: tests, + }) + } + return report, nil }