Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f269c95ce4 | ||
|
|
3ce73b8db9 | ||
|
|
fe733f96d6 | ||
|
|
7cf5827f22 | ||
|
|
09fd2cb2a4 | ||
|
|
74383ddcc0 | ||
|
|
e20795e7b9 | ||
|
|
36563f55fb | ||
|
|
bfbf24a6c4 | ||
|
|
e0207bab2a | ||
|
|
37f4359dcb | ||
|
|
befec17e06 | ||
|
|
220b73dc49 | ||
|
|
d97ffd7fea |
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import "strings"
|
||||
|
||||
// copied from go-junit-report for fixing issues with chars that
|
||||
// are out of range.
|
||||
|
||||
// from encoding/xml/xml.go, replace chars by unknown char
|
||||
func isInCharacterRange(r rune) (inrange bool) {
|
||||
return r == 0x09 ||
|
||||
r == 0x0A ||
|
||||
r == 0x0D ||
|
||||
r >= 0x20 && r <= 0xD7FF ||
|
||||
r >= 0xE000 && r <= 0xFFFD ||
|
||||
r >= 0x10000 && r <= 0x10FFFF
|
||||
}
|
||||
|
||||
func escapeIllegalChars(str string) string {
|
||||
return strings.Map(func(r rune) rune {
|
||||
if isInCharacterRange(r) {
|
||||
return r
|
||||
}
|
||||
return '\uFFFD'
|
||||
}, str)
|
||||
}
|
||||
+82
-77
@@ -5,79 +5,69 @@ import (
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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",
|
||||
},
|
||||
},
|
||||
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",
|
||||
}
|
||||
}
|
||||
log.Printf("TIME %f", t.Time)
|
||||
parts := strings.Split(t.Name, "/")
|
||||
tc := Testcase{
|
||||
Name: parts[len(parts)-1],
|
||||
// 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{}
|
||||
|
||||
if len(os.Args) != 2 {
|
||||
fmt.Fprintf(os.Stderr, "Usage: go2junit <gotest.json>\n")
|
||||
if len(os.Args) != 2 && len(os.Args) != 3 {
|
||||
fmt.Fprintf(os.Stderr, "Usage: go2junit <outputdir> [<prefix>]\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
filename := os.Args[1]
|
||||
var file *os.File
|
||||
if filename != "-" {
|
||||
file, err := os.Open(filename)
|
||||
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)
|
||||
}
|
||||
prefix := ""
|
||||
if len(os.Args) == 3 {
|
||||
prefix = os.Args[2]
|
||||
}
|
||||
|
||||
var file = os.Stdin
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
lineno := 0
|
||||
@@ -94,33 +84,48 @@ func main() {
|
||||
|
||||
//.fmt.Printf("Parsed %d:\n%v\n\n", lineno, item)
|
||||
|
||||
pkg := prefix + item.Package
|
||||
switch item.Action {
|
||||
case "start":
|
||||
testsuites.Suite(item.Time, item.Package)
|
||||
testsuites.Start(item.Time, pkg)
|
||||
case "run":
|
||||
testsuites.Test(item.Time, item.Package, item.Test)
|
||||
fmt.Println()
|
||||
testsuites.Test(item.Time, pkg, item.Test)
|
||||
case "output":
|
||||
testsuites.Output(item.Time, item.Package, item.Test, item.Output)
|
||||
fmt.Fprintf(os.Stderr, "%s", item.Output)
|
||||
testsuites.Output(item.Time, pkg, item.Test, escapeIllegalChars(item.Output))
|
||||
fmt.Printf("%s", item.Output)
|
||||
case "pause":
|
||||
testsuites.Output(item.Time, item.Package, item.Test, "PAUSED")
|
||||
testsuites.Output(item.Time, pkg, item.Test, "PAUSED")
|
||||
case "cont":
|
||||
testsuites.Output(item.Time, item.Package, item.Test, "CONTINUED")
|
||||
testsuites.Output(item.Time, pkg, item.Test, "CONTINUED")
|
||||
case "pass":
|
||||
testsuites.Pass(item.Time, item.Package, item.Test, item.Elapsed)
|
||||
testsuites.Pass(item.Time, pkg, item.Test)
|
||||
case "bench":
|
||||
testsuites.Bench(item.Time, item.Package, item.Test, item.Output, item.Elapsed)
|
||||
testsuites.Bench(item.Time, pkg, item.Test, escapeIllegalChars(item.Output))
|
||||
case "fail":
|
||||
testsuites.Fail(item.Time, item.Package, item.Test, item.Elapsed)
|
||||
testsuites.Fail(item.Time, pkg, item.Test)
|
||||
case "skip":
|
||||
testsuites.Skip(item.Time, item.Package, item.Test)
|
||||
testsuites.Skip(item.Time, pkg, item.Test)
|
||||
}
|
||||
}
|
||||
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)
|
||||
|
||||
}
|
||||
|
||||
+61
-80
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -18,26 +17,30 @@ type Testsuites struct {
|
||||
|
||||
Time float64 `xml:"time,attr"`
|
||||
|
||||
Suites []*Testsuite `xml:"testsuite,omitempty"`
|
||||
Suites []*Test `xml:"testsuite,omitempty"`
|
||||
}
|
||||
|
||||
type Testsuite struct {
|
||||
type Test struct {
|
||||
XMLName xml.Name `xml:"testsuite"`
|
||||
|
||||
// required attributes
|
||||
Name string `xml:"name,attr"`
|
||||
Tests int `xml:"tests,attr"`
|
||||
Failures int `xml:"failures,attr"`
|
||||
Errors int `xml:"errors,attr"`
|
||||
Name string `xml:"name,attr"`
|
||||
TestCount int `xml:"tests,attr"`
|
||||
Failures int `xml:"failures,attr"`
|
||||
Errors int `xml:"errors,attr"`
|
||||
|
||||
// optional attributes
|
||||
Disabled int `xml:"disabled,attr,omitempty"`
|
||||
Package string `xml:"package,attr,omitempty"`
|
||||
Skipped int `xml:"skipped,attr,omitempty"`
|
||||
Time string `xml:"time,attr"`
|
||||
Time float64 `xml:"time,attr"`
|
||||
Timestamp time.Time `xml:"timestamp,attr,omitempty"`
|
||||
|
||||
Testsuites []*Testsuite `xml:"testsuite,omitempty"`
|
||||
Testcases []*Testcase `xml:"testcase,omitempty"`
|
||||
SystemOut string `xml:"system-out,omitempty"`
|
||||
Tests []*Test `xml:"testsuite,omitempty"`
|
||||
SystemOut string `xml:"system-out,omitempty"`
|
||||
|
||||
parent *Test
|
||||
t0 time.Time
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
@@ -58,137 +61,115 @@ type Testcase struct {
|
||||
SystemOut string `xml:"system-out,omitempty"`
|
||||
}
|
||||
|
||||
func (testsuites *Testsuites) getSuite(t time.Time, pkg string) *Testsuite {
|
||||
// Looks for a test in root
|
||||
func (testsuites *Testsuites) getRootSuite(t time.Time, pkg string, create bool) *Test {
|
||||
for _, suite := range testsuites.Suites {
|
||||
if suite.Name == pkg {
|
||||
return suite
|
||||
}
|
||||
}
|
||||
suite := Testsuite{
|
||||
if !create {
|
||||
return nil
|
||||
}
|
||||
suite := Test{
|
||||
Name: pkg,
|
||||
Skipped: 0,
|
||||
Timestamp: t,
|
||||
t0: t,
|
||||
}
|
||||
log.Printf("Adding suite %s", pkg)
|
||||
testsuites.Suites = append(testsuites.Suites, &suite)
|
||||
return &suite
|
||||
}
|
||||
|
||||
func (suite *Testsuite) getSuite(t time.Time, name string) *Testsuite {
|
||||
for _, s := range suite.Testsuites {
|
||||
if s.Name == name {
|
||||
func (suite *Test) getSuite(t time.Time, name string) *Test {
|
||||
for _, s := range suite.Tests {
|
||||
if s.Name == suite.Name+"/"+name {
|
||||
return s
|
||||
}
|
||||
}
|
||||
s := Testsuite{
|
||||
Name: name,
|
||||
s := Test{
|
||||
Name: suite.Name + "/" + name,
|
||||
Timestamp: t,
|
||||
t0: t,
|
||||
}
|
||||
suite.Testsuites = append(suite.Testsuites, &s)
|
||||
suite.Tests = append(suite.Tests, &s)
|
||||
return &s
|
||||
}
|
||||
|
||||
func (suite *Testsuite) getTest(t time.Time, testname string) *Testcase {
|
||||
func (suite *Test) getTest(t time.Time, testname string) *Test {
|
||||
if testname == "" {
|
||||
return suite
|
||||
}
|
||||
suitename := suite.Name
|
||||
path := strings.Split(testname, "/")
|
||||
for i := 0; i < len(path)-1; i++ {
|
||||
for i := 0; i < len(path); i++ {
|
||||
suite = suite.getSuite(t, path[i])
|
||||
suitename = suitename + "/" + path[i]
|
||||
}
|
||||
for _, test := range suite.Testcases {
|
||||
if test.Name == path[len(path)-1] {
|
||||
return test
|
||||
}
|
||||
}
|
||||
if path[len(path)-1] == "" {
|
||||
panic("Empty testcase")
|
||||
}
|
||||
test := Testcase{
|
||||
Name: path[len(path)-1],
|
||||
}
|
||||
suite.Testcases = append(suite.Testcases, &test)
|
||||
return &test
|
||||
return suite
|
||||
}
|
||||
|
||||
func (testsuites *Testsuites) getTest(t time.Time, pkg string, testname string) *Testcase {
|
||||
suite := testsuites.getSuite(t, pkg)
|
||||
func (testsuites *Testsuites) getTest(t time.Time, pkg string, testname string) *Test {
|
||||
suite := testsuites.getRootSuite(t, pkg, true)
|
||||
test := suite.getTest(t, testname)
|
||||
return test
|
||||
}
|
||||
|
||||
func (testsuites *Testsuites) Suite(t time.Time, pkg string) {
|
||||
testsuites.getSuite(t, pkg)
|
||||
func (testsuites *Testsuites) Start(t time.Time, pkg string) {
|
||||
testsuites.getRootSuite(t, pkg, true)
|
||||
}
|
||||
|
||||
func (testsuites *Testsuites) Test(t time.Time, pkg string, test string) {
|
||||
// This can be a test suite as well
|
||||
testsuites.getTest(t, pkg, test)
|
||||
}
|
||||
|
||||
func (testsuites *Testsuites) Output(t time.Time, pkg string, test string, output string) {
|
||||
if test == "" {
|
||||
ts := testsuites.getSuite(t, pkg)
|
||||
ts.SystemOut = ts.SystemOut + output
|
||||
return
|
||||
}
|
||||
tc := testsuites.getTest(t, pkg, test)
|
||||
tc.SystemOut = tc.SystemOut + output
|
||||
}
|
||||
|
||||
func (testsuites *Testsuites) Pass(t time.Time, pkg string, test string, elapsed float64) {
|
||||
if test == "" {
|
||||
return
|
||||
}
|
||||
testsuites.getTest(t, pkg, test)
|
||||
func (testsuites *Testsuites) Pass(t time.Time, pkg string, test string) {
|
||||
tc := testsuites.getTest(t, pkg, test)
|
||||
tc.Time = float64(t.Sub(tc.t0).Nanoseconds()) / 1000_000_000.0
|
||||
}
|
||||
|
||||
func (testsuites *Testsuites) Bench(t time.Time, pkg string, test string, output string, elapsed float64) {
|
||||
func (testsuites *Testsuites) Bench(t time.Time, pkg string, test string, output string) {
|
||||
tc := testsuites.getTest(t, pkg, test)
|
||||
tc.SystemOut = tc.SystemOut + output + "\n"
|
||||
}
|
||||
|
||||
func (testsuites *Testsuites) Fail(t time.Time, pkg string, test string, elapsed float64) {
|
||||
if test == "" {
|
||||
return
|
||||
}
|
||||
func (testsuites *Testsuites) Fail(t time.Time, pkg string, test string) {
|
||||
tc := testsuites.getTest(t, pkg, test)
|
||||
tc.Time = elapsed
|
||||
tc.Failure = &Result{
|
||||
Message: "failed",
|
||||
}
|
||||
tc.Time = float64(t.Sub(tc.t0).Nanoseconds()) / 1000_000_000.0
|
||||
tc.Failures = 1
|
||||
}
|
||||
|
||||
func (testsuites *Testsuites) Skip(t time.Time, pkg string, test string) {
|
||||
if test == "" {
|
||||
return
|
||||
}
|
||||
tc := testsuites.getTest(t, pkg, test)
|
||||
tc.Failure = &Result{
|
||||
Message: "skipped",
|
||||
}
|
||||
tc.Time = float64(t.Sub(tc.t0).Nanoseconds()) / 1000_000_000.0
|
||||
tc.Skipped = 1
|
||||
}
|
||||
|
||||
func (suite *Testsuite) Complete() {
|
||||
suite.Tests = 0
|
||||
func (suite *Test) Complete() {
|
||||
suite.TestCount = 0
|
||||
suite.Failures = 0
|
||||
suite.Errors = 0
|
||||
suite.Disabled = 0
|
||||
suite.Skipped = 0
|
||||
for _, ts := range suite.Testsuites {
|
||||
|
||||
if len(suite.Tests) == 0 {
|
||||
suite.TestCount = 1
|
||||
}
|
||||
|
||||
for _, ts := range suite.Tests {
|
||||
ts.Complete()
|
||||
suite.Tests += ts.Tests
|
||||
suite.TestCount += ts.TestCount
|
||||
suite.Failures += ts.Failures
|
||||
suite.Errors += ts.Errors
|
||||
suite.Disabled += ts.Disabled
|
||||
suite.Skipped += ts.Skipped
|
||||
}
|
||||
for _, tc := range suite.Testcases {
|
||||
suite.Tests += 1
|
||||
if tc.Failure != nil {
|
||||
switch tc.Failure.Message {
|
||||
case "skipped":
|
||||
suite.Skipped++
|
||||
default:
|
||||
suite.Failures++
|
||||
}
|
||||
}
|
||||
ts.parent = suite
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,7 +181,7 @@ func (testsuites *Testsuites) Complete() {
|
||||
testsuites.Skipped = 0
|
||||
for _, ts := range testsuites.Suites {
|
||||
ts.Complete()
|
||||
testsuites.Tests += ts.Tests
|
||||
testsuites.Tests += ts.TestCount
|
||||
testsuites.Failures += ts.Failures
|
||||
testsuites.Errors += ts.Errors
|
||||
testsuites.Disabled += ts.Disabled
|
||||
|
||||
Reference in New Issue
Block a user