14 Commits
Author SHA1 Message Date
erik 09fd2cb2a4 simple testcase names 2024-11-22 21:50:28 +01:00
erik 74383ddcc0 added a prefix option to separate test results 2024-11-22 21:34:52 +01:00
erik e20795e7b9 no more duplication of test suites.
Parsing the test tree and deciding in the marsheller whether it is a
testcase (leaf) or a suite (non-leaf)
2024-11-22 20:59:23 +01:00
erik 36563f55fb avoid duplicate testsuites for everyt line of output 2024-11-20 23:42:06 +01:00
erik bfbf24a6c4 reintroducing nesting. 2024-11-20 23:35:05 +01:00
erik e0207bab2a now using classname in testcases to refer to the testsuite 2024-11-20 23:25:27 +01:00
erik 37f4359dcb now forcing the orot name of the output to be correct 2024-11-20 22:32:49 +01:00
erik befec17e06 testing autoupdate 2024-11-20 22:19:24 +01:00
erik 220b73dc49 now writing each TestSuite to a separate file 2024-11-20 22:15:44 +01:00
erik d97ffd7fea root element rename to testsuite 2024-11-20 21:54:52 +01:00
erik 18d362c926 now can also read from stdin 2024-11-20 21:29:35 +01:00
erik 8a2c4757b9 first working version. 2024-11-20 21:13:17 +01:00
erik 7643f37b73 initial version of CLI 2024-11-20 18:47:02 +01:00
root 991739b1ea change 2024-11-20 18:37:40 +01:00
7 changed files with 432 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
bin/
.env
*_templ.go
.idea/
+23
View File
@@ -0,0 +1,23 @@
.DEFAULT_GOAL := all
# seems superfluous
#.PHONY: fmt vet build clean
fmt:
go fmt ./...
vet: fmt
go vet ./...
build: vet
mkdir -p bin
go build -o bin ./cmd/...
test: build
go test -count=1 -coverprofile=testout/coverage.out ${TESTFLAGS} ./...
clean:
rm -rf bin
all: build
+1
View File
@@ -1,2 +1,3 @@
initial version of tools initial version of tools
xxx
+180
View File
@@ -0,0 +1,180 @@
package main
import (
"bufio"
"encoding/json"
"encoding/xml"
"fmt"
"os"
"path/filepath"
"strings"
)
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",
}
}
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{
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 && len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "Usage: go2junit <outputdir> [<prefix>]\n")
os.Exit(1)
}
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
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)
pkg := prefix + item.Package
switch item.Action {
case "start":
testsuites.Suite(item.Time, pkg)
case "run":
fmt.Println()
testsuites.Test(item.Time, pkg, item.Test)
case "output":
testsuites.Output(item.Time, pkg, item.Test, item.Output)
fmt.Printf("%s", item.Output)
case "pause":
testsuites.Output(item.Time, pkg, item.Test, "PAUSED")
case "cont":
testsuites.Output(item.Time, pkg, item.Test, "CONTINUED")
case "pass":
testsuites.Pass(item.Time, pkg, item.Test, item.Elapsed)
case "bench":
testsuites.Bench(item.Time, pkg, item.Test, item.Output, item.Elapsed)
case "fail":
testsuites.Fail(item.Time, pkg, item.Test, item.Elapsed)
case "skip":
testsuites.Skip(item.Time, pkg, 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)
}
}
}
+12
View File
@@ -0,0 +1,12 @@
package main
import "time"
type TestEvent struct {
Time time.Time
Action string
Package string
Test string
Elapsed float64
Output string
}
+209
View File
@@ -0,0 +1,209 @@
package main
import (
"encoding/xml"
"strings"
"time"
)
type Testsuites struct {
XMLName xml.Name `xml:"testsuites"`
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 []*Test `xml:"testsuite,omitempty"`
}
type Test struct {
XMLName xml.Name `xml:"testsuite"`
// required attributes
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 float64 `xml:"time,attr"`
Timestamp time.Time `xml:"timestamp,attr,omitempty"`
Tests []*Test `xml:"testsuite,omitempty"`
SystemOut string `xml:"system-out,omitempty"`
Parent *Test `xml:"-"`
}
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"`
}
// 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
}
}
if !create {
return nil
}
suite := Test{
Name: pkg,
Skipped: 0,
Timestamp: t,
}
testsuites.Suites = append(testsuites.Suites, &suite)
return &suite
}
func (suite *Test) getSuite(t time.Time, name string) *Test {
for _, s := range suite.Tests {
if s.Name == suite.Name+"/"+name {
return s
}
}
s := Test{
Name: suite.Name + "/" + name,
Timestamp: t,
}
suite.Tests = append(suite.Tests, &s)
return &s
}
func (suite *Test) getTest(t time.Time, testname string) *Test {
suitename := suite.Name
path := strings.Split(testname, "/")
for i := 0; i < len(path); i++ {
suite = suite.getSuite(t, path[i])
suitename = suitename + "/" + path[i]
}
return suite
}
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.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.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
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
}
if testsuites.getRootSuite(t, pkg+"/"+test, false) != nil {
return
}
}
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
}
if testsuites.getRootSuite(t, pkg+"/"+test, false) != nil {
return
}
tc := testsuites.getTest(t, pkg, test)
tc.Time = elapsed
tc.Failures = 1
}
func (testsuites *Testsuites) Skip(t time.Time, pkg string, test string) {
if test == "" {
return
}
if testsuites.getRootSuite(t, pkg+"/"+test, false) != nil {
return
}
tc := testsuites.getTest(t, pkg, test)
tc.Skipped = 1
}
func (suite *Test) Complete() {
suite.TestCount = 0
suite.Failures = 0
suite.Errors = 0
suite.Disabled = 0
suite.Skipped = 0
if len(suite.Tests) == 0 {
suite.TestCount = 1
}
for _, ts := range suite.Tests {
ts.Complete()
suite.TestCount += ts.TestCount
suite.Failures += ts.Failures
suite.Errors += ts.Errors
suite.Disabled += ts.Disabled
suite.Skipped += ts.Skipped
ts.Parent = suite
}
}
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.TestCount
testsuites.Failures += ts.Failures
testsuites.Errors += ts.Errors
testsuites.Disabled += ts.Disabled
testsuites.Skipped += ts.Skipped
}
}
+3
View File
@@ -0,0 +1,3 @@
module git.wamblee.org/public/gotools
go 1.23.3