now writing each TestSuite to a separate file

This commit is contained in:
Erik Brakkee 2024-11-20 22:15:44 +01:00
parent d97ffd7fea
commit 220b73dc49
2 changed files with 28 additions and 18 deletions

View File

@ -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 <gotest.json>\n")
fmt.Fprintf(os.Stderr, "Usage: go2junit <outputdir>\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)
}

View File

@ -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"`