Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e20795e7b9 | ||
|
|
36563f55fb | ||
|
|
bfbf24a6c4 | ||
|
|
e0207bab2a | ||
|
|
37f4359dcb | ||
|
|
befec17e06 | ||
|
|
220b73dc49 | ||
|
|
d97ffd7fea | ||
|
|
18d362c926 |
@@ -6,10 +6,46 @@ import (
|
|||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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{
|
testsuites := Testsuites{
|
||||||
Time: 1.23,
|
Time: 1.23,
|
||||||
@@ -64,13 +100,17 @@ func main() {
|
|||||||
testsuites := Testsuites{}
|
testsuites := Testsuites{}
|
||||||
|
|
||||||
if len(os.Args) != 2 {
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
filename := os.Args[1]
|
path := os.Args[1]
|
||||||
|
path = filepath.Clean(path)
|
||||||
|
err := os.MkdirAll(path, 0755)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
file, _ := os.Open(filename)
|
var file = os.Stdin
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
lineno := 0
|
lineno := 0
|
||||||
@@ -94,7 +134,7 @@ func main() {
|
|||||||
testsuites.Test(item.Time, item.Package, item.Test)
|
testsuites.Test(item.Time, item.Package, item.Test)
|
||||||
case "output":
|
case "output":
|
||||||
testsuites.Output(item.Time, item.Package, item.Test, item.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":
|
case "pause":
|
||||||
testsuites.Output(item.Time, item.Package, item.Test, "PAUSED")
|
testsuites.Output(item.Time, item.Package, item.Test, "PAUSED")
|
||||||
case "cont":
|
case "cont":
|
||||||
@@ -111,9 +151,22 @@ func main() {
|
|||||||
}
|
}
|
||||||
testsuites.Complete()
|
testsuites.Complete()
|
||||||
|
|
||||||
xml, err := xml.MarshalIndent(testsuites, "", " ")
|
for _, suite := range testsuites.Suites {
|
||||||
|
xml, err := xml.MarshalIndent(suite, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Printf("%s", xml)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+58
-56
@@ -18,13 +18,15 @@ type Testsuites struct {
|
|||||||
|
|
||||||
Time float64 `xml:"time,attr"`
|
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
|
// required attributes
|
||||||
Name string `xml:"name,attr"`
|
Name string `xml:"name,attr"`
|
||||||
Tests int `xml:"tests,attr"`
|
TestCount int `xml:"tests,attr"`
|
||||||
Failures int `xml:"failures,attr"`
|
Failures int `xml:"failures,attr"`
|
||||||
Errors int `xml:"errors,attr"`
|
Errors int `xml:"errors,attr"`
|
||||||
|
|
||||||
@@ -32,12 +34,13 @@ type Testsuite struct {
|
|||||||
Disabled int `xml:"disabled,attr,omitempty"`
|
Disabled int `xml:"disabled,attr,omitempty"`
|
||||||
Package string `xml:"package,attr,omitempty"`
|
Package string `xml:"package,attr,omitempty"`
|
||||||
Skipped int `xml:"skipped,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"`
|
Timestamp time.Time `xml:"timestamp,attr,omitempty"`
|
||||||
|
|
||||||
Testsuites []*Testsuite `xml:"testsuite,omitempty"`
|
Tests []*Test `xml:"testsuite,omitempty"`
|
||||||
Testcases []*Testcase `xml:"testcase,omitempty"`
|
|
||||||
SystemOut string `xml:"system-out,omitempty"`
|
SystemOut string `xml:"system-out,omitempty"`
|
||||||
|
|
||||||
|
Parent *Test `xml:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Result struct {
|
type Result struct {
|
||||||
@@ -58,13 +61,17 @@ type Testcase struct {
|
|||||||
SystemOut string `xml:"system-out,omitempty"`
|
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 {
|
for _, suite := range testsuites.Suites {
|
||||||
if suite.Name == pkg {
|
if suite.Name == pkg {
|
||||||
return suite
|
return suite
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
suite := Testsuite{
|
if !create {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
suite := Test{
|
||||||
Name: pkg,
|
Name: pkg,
|
||||||
Skipped: 0,
|
Skipped: 0,
|
||||||
Timestamp: t,
|
Timestamp: t,
|
||||||
@@ -74,57 +81,53 @@ func (testsuites *Testsuites) getSuite(t time.Time, pkg string) *Testsuite {
|
|||||||
return &suite
|
return &suite
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *Testsuite) getSuite(t time.Time, name string) *Testsuite {
|
func (suite *Test) getSuite(t time.Time, name string) *Test {
|
||||||
for _, s := range suite.Testsuites {
|
for _, s := range suite.Tests {
|
||||||
if s.Name == name {
|
if s.Name == suite.Name+"/"+name {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s := Testsuite{
|
s := Test{
|
||||||
Name: name,
|
Name: suite.Name + "/" + name,
|
||||||
Timestamp: t,
|
Timestamp: t,
|
||||||
}
|
}
|
||||||
suite.Testsuites = append(suite.Testsuites, &s)
|
suite.Tests = append(suite.Tests, &s)
|
||||||
return &s
|
return &s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *Testsuite) getTest(t time.Time, testname string) *Testcase {
|
func (suite *Test) getTest(t time.Time, testname string) *Test {
|
||||||
|
suitename := suite.Name
|
||||||
path := strings.Split(testname, "/")
|
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])
|
suite = suite.getSuite(t, path[i])
|
||||||
|
suitename = suitename + "/" + path[i]
|
||||||
}
|
}
|
||||||
for _, test := range suite.Testcases {
|
return suite
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (testsuites *Testsuites) getTest(t time.Time, pkg string, testname string) *Testcase {
|
func (testsuites *Testsuites) getTest(t time.Time, pkg string, testname string) *Test {
|
||||||
suite := testsuites.getSuite(t, pkg)
|
suite := testsuites.getRootSuite(t, pkg, true)
|
||||||
test := suite.getTest(t, testname)
|
test := suite.getTest(t, testname)
|
||||||
return test
|
return test
|
||||||
}
|
}
|
||||||
|
|
||||||
func (testsuites *Testsuites) Suite(t time.Time, pkg string) {
|
func (testsuites *Testsuites) Suite(t time.Time, pkg string) {
|
||||||
testsuites.getSuite(t, pkg)
|
testsuites.getRootSuite(t, pkg, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (testsuites *Testsuites) Test(t time.Time, pkg string, test string) {
|
func (testsuites *Testsuites) Test(t time.Time, pkg string, test string) {
|
||||||
|
// This can be a test suite as well
|
||||||
testsuites.getTest(t, pkg, test)
|
testsuites.getTest(t, pkg, test)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (testsuites *Testsuites) Output(t time.Time, pkg string, test string, output string) {
|
func (testsuites *Testsuites) Output(t time.Time, pkg string, test string, output string) {
|
||||||
if test == "" {
|
if test == "" {
|
||||||
ts := testsuites.getSuite(t, pkg)
|
ts := testsuites.getRootSuite(t, pkg, true)
|
||||||
|
ts.SystemOut = ts.SystemOut + output
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ts := testsuites.getRootSuite(t, pkg+"/"+test, false)
|
||||||
|
if ts != nil {
|
||||||
ts.SystemOut = ts.SystemOut + output
|
ts.SystemOut = ts.SystemOut + output
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -136,7 +139,9 @@ func (testsuites *Testsuites) Pass(t time.Time, pkg string, test string, elapsed
|
|||||||
if test == "" {
|
if test == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
testsuites.getTest(t, pkg, test)
|
if testsuites.getRootSuite(t, pkg+"/"+test, false) != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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, elapsed float64) {
|
||||||
@@ -148,47 +153,44 @@ func (testsuites *Testsuites) Fail(t time.Time, pkg string, test string, elapsed
|
|||||||
if test == "" {
|
if test == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if testsuites.getRootSuite(t, pkg+"/"+test, false) != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
tc := testsuites.getTest(t, pkg, test)
|
tc := testsuites.getTest(t, pkg, test)
|
||||||
tc.Time = elapsed
|
tc.Time = elapsed
|
||||||
tc.Failure = &Result{
|
tc.Failures = 1
|
||||||
Message: "failed",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (testsuites *Testsuites) Skip(t time.Time, pkg string, test string) {
|
func (testsuites *Testsuites) Skip(t time.Time, pkg string, test string) {
|
||||||
if test == "" {
|
if test == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tc := testsuites.getTest(t, pkg, test)
|
if testsuites.getRootSuite(t, pkg+"/"+test, false) != nil {
|
||||||
tc.Failure = &Result{
|
return
|
||||||
Message: "skipped",
|
|
||||||
}
|
}
|
||||||
|
tc := testsuites.getTest(t, pkg, test)
|
||||||
|
tc.Skipped = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *Testsuite) Complete() {
|
func (suite *Test) Complete() {
|
||||||
suite.Tests = 0
|
suite.TestCount = 0
|
||||||
suite.Failures = 0
|
suite.Failures = 0
|
||||||
suite.Errors = 0
|
suite.Errors = 0
|
||||||
suite.Disabled = 0
|
suite.Disabled = 0
|
||||||
suite.Skipped = 0
|
suite.Skipped = 0
|
||||||
for _, ts := range suite.Testsuites {
|
|
||||||
|
if len(suite.Tests) == 0 {
|
||||||
|
suite.TestCount = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, ts := range suite.Tests {
|
||||||
ts.Complete()
|
ts.Complete()
|
||||||
suite.Tests += ts.Tests
|
suite.TestCount += ts.TestCount
|
||||||
suite.Failures += ts.Failures
|
suite.Failures += ts.Failures
|
||||||
suite.Errors += ts.Errors
|
suite.Errors += ts.Errors
|
||||||
suite.Disabled += ts.Disabled
|
suite.Disabled += ts.Disabled
|
||||||
suite.Skipped += ts.Skipped
|
suite.Skipped += ts.Skipped
|
||||||
}
|
ts.Parent = suite
|
||||||
for _, tc := range suite.Testcases {
|
|
||||||
suite.Tests += 1
|
|
||||||
if tc.Failure != nil {
|
|
||||||
switch tc.Failure.Message {
|
|
||||||
case "skipped":
|
|
||||||
suite.Skipped++
|
|
||||||
default:
|
|
||||||
suite.Failures++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,7 +202,7 @@ func (testsuites *Testsuites) Complete() {
|
|||||||
testsuites.Skipped = 0
|
testsuites.Skipped = 0
|
||||||
for _, ts := range testsuites.Suites {
|
for _, ts := range testsuites.Suites {
|
||||||
ts.Complete()
|
ts.Complete()
|
||||||
testsuites.Tests += ts.Tests
|
testsuites.Tests += ts.TestCount
|
||||||
testsuites.Failures += ts.Failures
|
testsuites.Failures += ts.Failures
|
||||||
testsuites.Errors += ts.Errors
|
testsuites.Errors += ts.Errors
|
||||||
testsuites.Disabled += ts.Disabled
|
testsuites.Disabled += ts.Disabled
|
||||||
|
|||||||
Reference in New Issue
Block a user