Skip to content

Commit

Permalink
Use jstemmer/go-junit-report/v2 to correctly parse the output of go test
Browse files Browse the repository at this point in the history
  • Loading branch information
shibayu36 committed Aug 19, 2023
1 parent 0f408d5 commit 1450586
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
27 changes: 22 additions & 5 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gotesplit
import (
"bytes"
"context"
"encoding/xml"
"errors"
"fmt"
"io"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
}

Expand Down Expand Up @@ -225,11 +227,26 @@ 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,
}
}
return ret
}

func writeJUnitReportXML(w io.Writer, report gtr.Report) error {
testsuites := junit.CreateFromReport(report, "")

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
}

0 comments on commit 1450586

Please sign in to comment.