Update go-junit-report to use new parser

This commit is contained in:
Joël Stemmer 2019-10-08 00:32:03 +01:00
parent 7bc0f1a86b
commit 72cd8b3697
2 changed files with 45 additions and 11 deletions

View File

@ -1,12 +1,13 @@
package main package main
import ( import (
"encoding/xml"
"flag" "flag"
"fmt" "fmt"
"os" "os"
"github.com/jstemmer/go-junit-report/formatter" "github.com/jstemmer/go-junit-report/v2/pkg/gtr"
"github.com/jstemmer/go-junit-report/parser" "github.com/jstemmer/go-junit-report/v2/pkg/parser/gotest"
) )
var ( var (
@ -21,6 +22,9 @@ var (
goVersionFlag = flag.String("go-version", "", "specify the value to use for the go.version property in the generated XML") goVersionFlag = flag.String("go-version", "", "specify the value to use for the go.version property in the generated XML")
setExitCode = flag.Bool("set-exit-code", false, "set exit code to 1 if tests failed") setExitCode = flag.Bool("set-exit-code", false, "set exit code to 1 if tests failed")
version = flag.Bool("version", false, "print version") version = flag.Bool("version", false, "print version")
// debug flags
printEvents = flag.Bool("debug.print-events", false, "print events generated by the go test parser")
) )
func main() { func main() {
@ -38,20 +42,36 @@ func main() {
} }
// Read input // Read input
report, err := parser.Parse(os.Stdin, *packageName) events, err := gotest.Parse(os.Stdin)
if err != nil { if err != nil {
fmt.Printf("Error reading input: %s\n", err) fmt.Fprintf(os.Stderr, "Error reading input: %s\n", err)
os.Exit(1) os.Exit(1)
} }
// Write xml if *printEvents {
err = formatter.JUnitReportXML(report, *noXMLHeader, *goVersionFlag, os.Stdout) for i, ev := range events {
if err != nil { fmt.Printf("%02d: %#v\n", i, ev)
fmt.Printf("Error writing XML: %s\n", err) }
os.Exit(1) }
report := gtr.FromEvents(events)
if !*noXMLHeader {
fmt.Fprintf(os.Stdout, xml.Header)
} }
if *setExitCode && report.Failures() > 0 { enc := xml.NewEncoder(os.Stdout)
enc.Indent("", "\t")
if err := enc.Encode(gtr.JUnit(report)); err != nil {
fmt.Fprintf(os.Stderr, "Error writing XML: %s\n", err)
os.Exit(1)
}
if err := enc.Flush(); err != nil {
fmt.Fprintf(os.Stderr, "Error flusing XML: %s\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "\n")
if *setExitCode && report.HasFailures() {
os.Exit(1) os.Exit(1)
} }
} }

View File

@ -1560,13 +1560,27 @@ var testCases = []TestCase{
}, },
} }
/*
func TestNewOutput(t *testing.T) {
matchRegex := compileMatch(t)
for _, testCase := range testCases {
if !matchRegex.MatchString(testCase.name) {
continue
}
t.Run(testCase.name, func(t *testing.T) {
})
}
}
*/
func TestParser(t *testing.T) { func TestParser(t *testing.T) {
matchRegex := compileMatch(t) matchRegex := compileMatch(t)
for _, testCase := range testCases { for _, testCase := range testCases {
if !matchRegex.MatchString(testCase.name) { if !matchRegex.MatchString(testCase.name) {
continue continue
} }
t.Logf("Test %s", testCase.name)
file, err := os.Open("testdata/" + testCase.name) file, err := os.Open("testdata/" + testCase.name)
if err != nil { if err != nil {