From 7d420f7f4f71a17b4907de1589520ac14197a114 Mon Sep 17 00:00:00 2001 From: shibayu36 Date: Sun, 20 Aug 2023 08:49:24 +0900 Subject: [PATCH] Use jstemmer/go-junit-report/v2 to correctly parse the output of go test --- go.mod | 2 +- go.sum | 6 ++++-- run.go | 30 +++++++++++++++++++++++++----- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 3f2df89..e2869ef 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module github.com/Songmu/gotesplit go 1.18 require ( - github.com/jstemmer/go-junit-report v1.0.0 + github.com/jstemmer/go-junit-report/v2 v2.0.0 golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 ) diff --git a/go.sum b/go.sum index 72338c9..2e65f6f 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ -github.com/jstemmer/go-junit-report v1.0.0 h1:8X1gzZpR+nVQLAht+L/foqOeX2l9DTZoaIPbEQHxsds= -github.com/jstemmer/go-junit-report v1.0.0/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/jstemmer/go-junit-report/v2 v2.0.0 h1:bMZNO9B16VFn07tKyi4YJFIbZtVmJaa5Xakv9dcwK58= +github.com/jstemmer/go-junit-report/v2 v2.0.0/go.mod h1:mgHVr7VUo5Tn8OLVr1cKnLuEy0M92wdRntM99h7RkgQ= golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 h1:w8s32wxx3sY+OjLlv9qltkLU5yvJzxjjgiHWLjdIcw4= golang.org/x/sync v0.0.0-20220513210516-0976fa681c29/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/run.go b/run.go index ac71d70..7c3f352 100644 --- a/run.go +++ b/run.go @@ -3,6 +3,7 @@ package gotesplit import ( "bytes" "context" + "encoding/xml" "errors" "fmt" "io" @@ -13,8 +14,9 @@ import ( "path/filepath" "strings" - "github.com/jstemmer/go-junit-report/formatter" - "github.com/jstemmer/go-junit-report/parser" + "github.com/jstemmer/go-junit-report/v2/gtr" + "github.com/jstemmer/go-junit-report/v2/junit" + parser "github.com/jstemmer/go-junit-report/v2/parser/gotest" "golang.org/x/sync/errgroup" ) @@ -138,7 +140,7 @@ func run(ctx context.Context, total, idx uint, junitDir string, argv []string, o log.Printf("failed to open file to store test report: %s\n", err) } else { defer f.Close() - if err := formatter.JUnitReportXML(report.report.report, false, "", f); err != nil { + if err := writeJUnitReportXML(f, report.report.report); err != nil { log.Printf("failed to store test report: %s\n", err) } } @@ -149,7 +151,7 @@ func run(ctx context.Context, total, idx uint, junitDir string, argv []string, o } type junitReport struct { - report *parser.Report + report gtr.Report err error } @@ -225,7 +227,7 @@ func goTest(args []string, stdout, stderr io.Writer, junitDir string) *testRepor err: err, } if junitDir != "" { - report, err := parser.Parse(outBuf, "") + report, err := parser.NewParser().Parse(outBuf) ret.report = &junitReport{ report: report, err: err, @@ -233,3 +235,21 @@ func goTest(args []string, stdout, stderr io.Writer, junitDir string) *testRepor } return ret } + +func writeJUnitReportXML(w io.Writer, report gtr.Report) error { + testsuites := junit.CreateFromReport(report, "") + if _, err := fmt.Fprintf(w, xml.Header); err != nil { + return err + } + + enc := xml.NewEncoder(w) + enc.Indent("", "\t") + if err := enc.Encode(testsuites); err != nil { + return err + } + if err := enc.Flush(); err != nil { + return err + } + _, err := fmt.Fprintf(w, "\n") + return err +}