From 72cd8b369770407182143063a187e058d6d23091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Stemmer?= Date: Tue, 8 Oct 2019 00:32:03 +0100 Subject: [PATCH] Update go-junit-report to use new parser --- go-junit-report.go | 40 ++++++++++++++++++++++++++++++---------- go-junit-report_test.go | 16 +++++++++++++++- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/go-junit-report.go b/go-junit-report.go index 55a484d..e1a38aa 100644 --- a/go-junit-report.go +++ b/go-junit-report.go @@ -1,12 +1,13 @@ package main import ( + "encoding/xml" "flag" "fmt" "os" - "github.com/jstemmer/go-junit-report/formatter" - "github.com/jstemmer/go-junit-report/parser" + "github.com/jstemmer/go-junit-report/v2/pkg/gtr" + "github.com/jstemmer/go-junit-report/v2/pkg/parser/gotest" ) 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") setExitCode = flag.Bool("set-exit-code", false, "set exit code to 1 if tests failed") 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() { @@ -38,20 +42,36 @@ func main() { } // Read input - report, err := parser.Parse(os.Stdin, *packageName) + events, err := gotest.Parse(os.Stdin) if err != nil { - fmt.Printf("Error reading input: %s\n", err) + fmt.Fprintf(os.Stderr, "Error reading input: %s\n", err) os.Exit(1) } - // Write xml - err = formatter.JUnitReportXML(report, *noXMLHeader, *goVersionFlag, os.Stdout) - if err != nil { - fmt.Printf("Error writing XML: %s\n", err) - os.Exit(1) + if *printEvents { + for i, ev := range events { + fmt.Printf("%02d: %#v\n", i, ev) + } + } + 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) } } diff --git a/go-junit-report_test.go b/go-junit-report_test.go index 2e9c1db..ae1840b 100644 --- a/go-junit-report_test.go +++ b/go-junit-report_test.go @@ -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) { matchRegex := compileMatch(t) for _, testCase := range testCases { if !matchRegex.MatchString(testCase.name) { continue } - t.Logf("Test %s", testCase.name) file, err := os.Open("testdata/" + testCase.name) if err != nil {