gotools/cmd/go2junit/go2junit.go

127 lines
2.6 KiB
Go
Raw Permalink Normal View History

2024-11-20 17:47:02 +00:00
package main
2024-11-20 20:13:17 +00:00
import (
"bufio"
"encoding/json"
"encoding/xml"
"fmt"
"os"
)
2024-11-20 17:47:02 +00:00
func main() {
2024-11-20 20:13:17 +00:00
/*
testsuites := Testsuites{
Time: 1.23,
TImestamp: time.Now(),
Suites: []*Testsuite{
{
Name: "hello",
Tests: 0,
Failures: 0,
Errors: 0,
Disabled: 0,
Package: "",
Skipped: 0,
Time: "",
Timestamp: time.Now(),
Testsuites: []*Testsuite{
{
Name: "abc",
Tests: 0,
Failures: 0,
Errors: 0,
Disabled: 0,
Package: "",
Skipped: 0,
Time: "",
Timestamp: time.Time{},
Testsuites: nil,
Testcases: []*Testcase{
{
Name: "test",
Classname: "",
Time: "",
Skipped: nil,
Error: &Result{
Message: "error",
},
Failure: nil,
SystemOut: "",
},
},
SystemOut: "ddd",
},
},
Testcases: nil,
SystemOut: "hello",
},
},
}
*/
testsuites := Testsuites{}
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: go2junit <gotest.json>\n")
os.Exit(1)
}
filename := os.Args[1]
2024-11-20 20:29:35 +00:00
var file *os.File
if filename != "-" {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
defer file.Close()
} else {
file = os.Stdin
}
2024-11-20 20:13:17 +00:00
scanner := bufio.NewScanner(file)
lineno := 0
testsuites = Testsuites{}
for scanner.Scan() {
lineno++
var item TestEvent
line := scanner.Bytes()
if err := json.Unmarshal(line, &item); err != nil {
fmt.Fprintf(os.Stderr, "%d: %s: %v", lineno, line, err)
continue
}
//.fmt.Printf("Parsed %d:\n%v\n\n", lineno, item)
switch item.Action {
case "start":
testsuites.Suite(item.Time, item.Package)
case "run":
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)
case "pause":
testsuites.Output(item.Time, item.Package, item.Test, "PAUSED")
case "cont":
testsuites.Output(item.Time, item.Package, item.Test, "CONTINUED")
case "pass":
testsuites.Pass(item.Time, item.Package, item.Test, item.Elapsed)
case "bench":
testsuites.Bench(item.Time, item.Package, item.Test, item.Output, item.Elapsed)
case "fail":
testsuites.Fail(item.Time, item.Package, item.Test, item.Elapsed)
case "skip":
testsuites.Skip(item.Time, item.Package, item.Test)
}
}
testsuites.Complete()
xml, err := xml.MarshalIndent(testsuites, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s", xml)
2024-11-20 17:47:02 +00:00
}