diff --git a/cmd/go2junit/go2junit.go b/cmd/go2junit/go2junit.go index d2c3092..7dfe1ef 100644 --- a/cmd/go2junit/go2junit.go +++ b/cmd/go2junit/go2junit.go @@ -6,6 +6,7 @@ import ( "encoding/xml" "fmt" "os" + "path/filepath" ) func main() { @@ -64,22 +65,18 @@ func main() { testsuites := Testsuites{} if len(os.Args) != 2 { - fmt.Fprintf(os.Stderr, "Usage: go2junit \n") + fmt.Fprintf(os.Stderr, "Usage: go2junit \n") os.Exit(1) } - filename := os.Args[1] - var file *os.File - if filename != "-" { - file2, err := os.Open(filename) - file = file2 - if err != nil { - panic(err) - } - defer file.Close() - } else { - file = os.Stdin + path := os.Args[1] + path = filepath.Clean(path) + err := os.MkdirAll(path, 0755) + if err != nil { + panic(err) } + var file = os.Stdin + scanner := bufio.NewScanner(file) lineno := 0 @@ -102,7 +99,7 @@ func main() { testsuites.Test(item.Time, item.Package, item.Test) case "output": testsuites.Output(item.Time, item.Package, item.Test, item.Output) - fmt.Fprintf(os.Stderr, "%s", item.Output) + fmt.Printf("%s", item.Output) case "pause": testsuites.Output(item.Time, item.Package, item.Test, "PAUSED") case "cont": @@ -119,9 +116,22 @@ func main() { } testsuites.Complete() - xml, err := xml.MarshalIndent(testsuites, "", " ") - if err != nil { - panic(err) + for _, suite := range testsuites.Suites { + xml, err := xml.MarshalIndent(suite, "", " ") + if err != nil { + panic(err) + } + fname := path + "/" + suite.Name + ".xml" + dir := filepath.Dir(fname) + err = os.MkdirAll(dir, 0755) + if err != nil { + panic(err) + } + fmt.Fprintf(os.Stderr, "Writing %s\n", fname) + err = os.WriteFile(fname, xml, 0644) + if err != nil { + panic(err) + } } - fmt.Printf("%s", xml) + } diff --git a/cmd/go2junit/output.go b/cmd/go2junit/output.go index 5f27ecd..7b140c8 100644 --- a/cmd/go2junit/output.go +++ b/cmd/go2junit/output.go @@ -8,7 +8,7 @@ import ( ) type Testsuites struct { - XMLName xml.Name `xml:"testsuite"` + XMLName xml.Name `xml:"testsuites"` Tests int `xml:"tests,attr"` Failures int `xml:"failures,attr"`