2024-11-20 20:13:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"log"
|
2024-11-20 22:35:05 +00:00
|
|
|
"strings"
|
2024-11-20 20:13:17 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Testsuites struct {
|
2024-11-20 21:15:44 +00:00
|
|
|
XMLName xml.Name `xml:"testsuites"`
|
2024-11-20 20:13:17 +00:00
|
|
|
|
|
|
|
Tests int `xml:"tests,attr"`
|
|
|
|
Failures int `xml:"failures,attr"`
|
|
|
|
Errors int `xml:"errors,attr"`
|
|
|
|
Disabled int `xml:"disabled,attr,omitempty"`
|
|
|
|
Skipped int `xml:"skipped,attr,omitempty"`
|
|
|
|
|
|
|
|
Time float64 `xml:"time,attr"`
|
|
|
|
|
|
|
|
Suites []*Testsuite `xml:"testsuite,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Testsuite struct {
|
2024-11-20 21:32:49 +00:00
|
|
|
XMLName xml.Name `xml:"testsuite"`
|
|
|
|
|
2024-11-20 20:13:17 +00:00
|
|
|
// required attributes
|
|
|
|
Name string `xml:"name,attr"`
|
|
|
|
Tests 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"`
|
|
|
|
Timestamp time.Time `xml:"timestamp,attr,omitempty"`
|
|
|
|
|
|
|
|
Testsuites []*Testsuite `xml:"testsuite,omitempty"`
|
|
|
|
Testcases []*Testcase `xml:"testcase,omitempty"`
|
|
|
|
SystemOut string `xml:"system-out,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Result struct {
|
|
|
|
Message string `xml:"message,attr"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Testcase struct {
|
|
|
|
// required attributes
|
|
|
|
Name string `xml:"name,attr"`
|
|
|
|
Classname string `xml:"classname,attr"`
|
|
|
|
|
|
|
|
// optional attributes
|
|
|
|
Time float64 `xml:"time,attr,omitempty"`
|
|
|
|
|
|
|
|
Skipped *Result `xml:"skipped,omitempty"`
|
|
|
|
Error *Result `xml:"error,omitempty"`
|
|
|
|
Failure *Result `xml:"failure,omitempty"`
|
|
|
|
SystemOut string `xml:"system-out,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (testsuites *Testsuites) getSuite(t time.Time, pkg string) *Testsuite {
|
|
|
|
for _, suite := range testsuites.Suites {
|
|
|
|
if suite.Name == pkg {
|
|
|
|
return suite
|
|
|
|
}
|
|
|
|
}
|
|
|
|
suite := Testsuite{
|
|
|
|
Name: pkg,
|
|
|
|
Skipped: 0,
|
|
|
|
Timestamp: 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 {
|
2024-11-20 22:42:06 +00:00
|
|
|
if s.Name == suite.Name+"/"+name {
|
2024-11-20 20:13:17 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s := Testsuite{
|
2024-11-20 22:35:05 +00:00
|
|
|
Name: suite.Name + "/" + name,
|
2024-11-20 20:13:17 +00:00
|
|
|
Timestamp: t,
|
|
|
|
}
|
|
|
|
suite.Testsuites = append(suite.Testsuites, &s)
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *Testsuite) getTest(t time.Time, testname string) *Testcase {
|
2024-11-20 22:35:05 +00:00
|
|
|
suitename := suite.Name
|
|
|
|
path := strings.Split(testname, "/")
|
|
|
|
for i := 0; i < len(path)-1; i++ {
|
|
|
|
suite = suite.getSuite(t, path[i])
|
|
|
|
suitename = suitename + "/" + path[i]
|
|
|
|
}
|
|
|
|
testname = path[len(path)-1]
|
|
|
|
|
2024-11-20 20:13:17 +00:00
|
|
|
for _, test := range suite.Testcases {
|
2024-11-20 22:25:27 +00:00
|
|
|
if test.Name == testname {
|
2024-11-20 20:13:17 +00:00
|
|
|
return test
|
|
|
|
}
|
|
|
|
}
|
2024-11-20 22:25:27 +00:00
|
|
|
if testname == "" {
|
2024-11-20 20:13:17 +00:00
|
|
|
panic("Empty testcase")
|
|
|
|
}
|
|
|
|
test := Testcase{
|
2024-11-20 22:25:27 +00:00
|
|
|
Name: testname,
|
2024-11-20 22:35:05 +00:00
|
|
|
Classname: suitename,
|
2024-11-20 20:13:17 +00:00
|
|
|
}
|
|
|
|
suite.Testcases = append(suite.Testcases, &test)
|
|
|
|
return &test
|
|
|
|
}
|
|
|
|
|
|
|
|
func (testsuites *Testsuites) getTest(t time.Time, pkg string, testname string) *Testcase {
|
|
|
|
suite := testsuites.getSuite(t, pkg)
|
|
|
|
test := suite.getTest(t, testname)
|
|
|
|
return test
|
|
|
|
}
|
|
|
|
|
|
|
|
func (testsuites *Testsuites) Suite(t time.Time, pkg string) {
|
|
|
|
testsuites.getSuite(t, pkg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (testsuites *Testsuites) Test(t time.Time, pkg string, test string) {
|
|
|
|
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) Bench(t time.Time, pkg string, test string, output string, elapsed float64) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
tc := testsuites.getTest(t, pkg, test)
|
|
|
|
tc.Time = elapsed
|
|
|
|
tc.Failure = &Result{
|
|
|
|
Message: "failed",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *Testsuite) Complete() {
|
|
|
|
suite.Tests = 0
|
|
|
|
suite.Failures = 0
|
|
|
|
suite.Errors = 0
|
|
|
|
suite.Disabled = 0
|
|
|
|
suite.Skipped = 0
|
|
|
|
for _, ts := range suite.Testsuites {
|
|
|
|
ts.Complete()
|
|
|
|
suite.Tests += ts.Tests
|
|
|
|
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++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (testsuites *Testsuites) Complete() {
|
|
|
|
testsuites.Tests = 0
|
|
|
|
testsuites.Failures = 0
|
|
|
|
testsuites.Errors = 0
|
|
|
|
testsuites.Disabled = 0
|
|
|
|
testsuites.Skipped = 0
|
|
|
|
for _, ts := range testsuites.Suites {
|
|
|
|
ts.Complete()
|
|
|
|
testsuites.Tests += ts.Tests
|
|
|
|
testsuites.Failures += ts.Failures
|
|
|
|
testsuites.Errors += ts.Errors
|
|
|
|
testsuites.Disabled += ts.Disabled
|
|
|
|
testsuites.Skipped += ts.Skipped
|
|
|
|
}
|
|
|
|
}
|