Erik Brakkee
e20795e7b9
Parsing the test tree and deciding in the marsheller whether it is a testcase (leaf) or a suite (non-leaf)
173 lines
3.6 KiB
Go
173 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"encoding/xml"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func (t *Test) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
|
if len(t.Tests) == 0 {
|
|
start.Name = xml.Name{Local: "testcase"}
|
|
classname := ""
|
|
if t.Parent != nil {
|
|
classname = t.Parent.Name
|
|
}
|
|
var skipped *Result
|
|
if t.Skipped > 0 {
|
|
skipped = &Result{
|
|
Message: "skipped",
|
|
}
|
|
}
|
|
var failed *Result
|
|
if t.Failures > 0 {
|
|
failed = &Result{
|
|
Message: "failed",
|
|
}
|
|
}
|
|
tc := Testcase{
|
|
Name: t.Name,
|
|
// 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)
|
|
}
|
|
|
|
func main() {
|
|
/*
|
|
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 <outputdir> \n")
|
|
os.Exit(1)
|
|
}
|
|
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
|
|
|
|
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.Printf("%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()
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
}
|