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"
|
2024-11-22 21:03:23 +00:00
|
|
|
"log"
|
2024-11-20 20:13:17 +00:00
|
|
|
"os"
|
2024-11-20 21:15:44 +00:00
|
|
|
"path/filepath"
|
2024-11-22 20:50:28 +00:00
|
|
|
"strings"
|
2024-11-20 20:13:17 +00:00
|
|
|
)
|
2024-11-20 17:47:02 +00:00
|
|
|
|
2024-11-22 19:59:23 +00:00
|
|
|
func (t *Test) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
2024-11-22 21:45:58 +00:00
|
|
|
if len(t.Tests) == 0 {
|
2024-11-22 19:59:23 +00:00
|
|
|
start.Name = xml.Name{Local: "testcase"}
|
|
|
|
classname := ""
|
2024-11-22 21:42:02 +00:00
|
|
|
if t.parent != nil {
|
|
|
|
classname = t.parent.Name
|
2024-11-22 19:59:23 +00:00
|
|
|
}
|
|
|
|
var skipped *Result
|
|
|
|
if t.Skipped > 0 {
|
|
|
|
skipped = &Result{
|
|
|
|
Message: "skipped",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var failed *Result
|
|
|
|
if t.Failures > 0 {
|
|
|
|
failed = &Result{
|
|
|
|
Message: "failed",
|
|
|
|
}
|
|
|
|
}
|
2024-11-22 21:03:23 +00:00
|
|
|
log.Printf("TIME %f", t.Time)
|
2024-11-22 20:50:28 +00:00
|
|
|
parts := strings.Split(t.Name, "/")
|
2024-11-22 19:59:23 +00:00
|
|
|
tc := Testcase{
|
2024-11-22 20:50:28 +00:00
|
|
|
Name: parts[len(parts)-1],
|
2024-11-22 19:59:23 +00:00
|
|
|
// parent class name
|
|
|
|
Classname: classname,
|
|
|
|
Time: t.Time,
|
|
|
|
Skipped: skipped,
|
|
|
|
Error: nil,
|
|
|
|
Failure: failed,
|
|
|
|
SystemOut: t.SystemOut,
|
|
|
|
}
|
|
|
|
return e.EncodeElement(tc, start)
|
|
|
|
}
|
|
|
|
type Alias Test
|
|
|
|
start.Name = xml.Name{Local: "testsuite"}
|
|
|
|
return e.EncodeElement(Alias(*t), start)
|
|
|
|
}
|
2024-11-20 20:13:17 +00:00
|
|
|
|
2024-11-22 19:59:23 +00:00
|
|
|
func main() {
|
2024-11-20 20:13:17 +00:00
|
|
|
testsuites := Testsuites{}
|
|
|
|
|
2024-11-22 20:34:52 +00:00
|
|
|
if len(os.Args) != 2 && len(os.Args) != 3 {
|
|
|
|
fmt.Fprintf(os.Stderr, "Usage: go2junit <outputdir> [<prefix>]\n")
|
2024-11-20 20:13:17 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-11-20 21:15:44 +00:00
|
|
|
path := os.Args[1]
|
|
|
|
path = filepath.Clean(path)
|
|
|
|
err := os.MkdirAll(path, 0755)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2024-11-20 20:29:35 +00:00
|
|
|
}
|
2024-11-22 20:34:52 +00:00
|
|
|
prefix := ""
|
|
|
|
if len(os.Args) == 3 {
|
|
|
|
prefix = os.Args[2]
|
|
|
|
}
|
2024-11-20 20:13:17 +00:00
|
|
|
|
2024-11-20 21:15:44 +00:00
|
|
|
var 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)
|
|
|
|
|
2024-11-22 20:34:52 +00:00
|
|
|
pkg := prefix + item.Package
|
2024-11-20 20:13:17 +00:00
|
|
|
switch item.Action {
|
|
|
|
case "start":
|
2024-11-22 21:42:02 +00:00
|
|
|
testsuites.Start(item.Time, pkg)
|
2024-11-20 20:13:17 +00:00
|
|
|
case "run":
|
2024-11-22 20:34:52 +00:00
|
|
|
fmt.Println()
|
|
|
|
testsuites.Test(item.Time, pkg, item.Test)
|
2024-11-20 20:13:17 +00:00
|
|
|
case "output":
|
2024-11-22 21:56:30 +00:00
|
|
|
testsuites.Output(item.Time, pkg, item.Test, escapeIllegalChars(item.Output))
|
2024-11-20 21:15:44 +00:00
|
|
|
fmt.Printf("%s", item.Output)
|
2024-11-20 20:13:17 +00:00
|
|
|
case "pause":
|
2024-11-22 20:34:52 +00:00
|
|
|
testsuites.Output(item.Time, pkg, item.Test, "PAUSED")
|
2024-11-20 20:13:17 +00:00
|
|
|
case "cont":
|
2024-11-22 20:34:52 +00:00
|
|
|
testsuites.Output(item.Time, pkg, item.Test, "CONTINUED")
|
2024-11-20 20:13:17 +00:00
|
|
|
case "pass":
|
2024-11-22 21:42:02 +00:00
|
|
|
testsuites.Pass(item.Time, pkg, item.Test)
|
2024-11-20 20:13:17 +00:00
|
|
|
case "bench":
|
2024-11-22 21:56:30 +00:00
|
|
|
testsuites.Bench(item.Time, pkg, item.Test, escapeIllegalChars(item.Output))
|
2024-11-20 20:13:17 +00:00
|
|
|
case "fail":
|
2024-11-22 21:42:02 +00:00
|
|
|
testsuites.Fail(item.Time, pkg, item.Test)
|
2024-11-20 20:13:17 +00:00
|
|
|
case "skip":
|
2024-11-22 20:34:52 +00:00
|
|
|
testsuites.Skip(item.Time, pkg, item.Test)
|
2024-11-20 20:13:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
testsuites.Complete()
|
|
|
|
|
2024-11-20 21:15:44 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-11-20 20:13:17 +00:00
|
|
|
}
|
2024-11-20 21:15:44 +00:00
|
|
|
|
2024-11-20 17:47:02 +00:00
|
|
|
}
|