From 075629ad5f2934f016fa8fe79deb821f98bd8b44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Stemmer?= Date: Tue, 27 Sep 2022 21:04:24 +0100 Subject: [PATCH] junit: Add Testsuites.WriteXML method --- internal/gojunitreport/go-junit-report.go | 17 +++------------ junit/junit.go | 15 +++++++++++++ junit/junit_test.go | 26 +++++++++++++++++++++++ 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/internal/gojunitreport/go-junit-report.go b/internal/gojunitreport/go-junit-report.go index 528fbb8..df28d93 100644 --- a/internal/gojunitreport/go-junit-report.go +++ b/internal/gojunitreport/go-junit-report.go @@ -64,32 +64,21 @@ func (c Config) Run(input io.Reader, output io.Writer) (*gtr.Report, error) { } } - if err = c.writeXML(output, report); err != nil { + if err = c.writeJunitXML(output, report); err != nil { return nil, err } return &report, nil } -func (c Config) writeXML(w io.Writer, report gtr.Report) error { +func (c Config) writeJunitXML(w io.Writer, report gtr.Report) error { testsuites := junit.CreateFromReport(report, c.Hostname) - if !c.SkipXMLHeader { _, err := fmt.Fprintf(w, xml.Header) if 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 + return testsuites.WriteXML(w) } func (c Config) gotestOptions() []gotest.Option { diff --git a/junit/junit.go b/junit/junit.go index 38c8cfd..e87b6ed 100644 --- a/junit/junit.go +++ b/junit/junit.go @@ -5,6 +5,7 @@ package junit import ( "encoding/xml" "fmt" + "io" "strings" "time" @@ -36,6 +37,20 @@ func (t *Testsuites) AddSuite(ts Testsuite) { t.Disabled += ts.Disabled } +// WriteXML writes the XML representation of Testsuites t to writer w. +func (t *Testsuites) WriteXML(w io.Writer) error { + enc := xml.NewEncoder(w) + enc.Indent("", "\t") + if err := enc.Encode(t); err != nil { + return err + } + if err := enc.Flush(); err != nil { + return err + } + _, err := fmt.Fprintf(w, "\n") + return err +} + // Testsuite is a single JUnit testsuite containing testcases. type Testsuite struct { // required attributes diff --git a/junit/junit_test.go b/junit/junit_test.go index 627428c..b1fd4e5 100644 --- a/junit/junit_test.go +++ b/junit/junit_test.go @@ -1,6 +1,7 @@ package junit import ( + "bytes" "encoding/xml" "testing" "time" @@ -181,3 +182,28 @@ func TestMarshalUnmarshal(t *testing.T) { t.Errorf("Unmarshal result incorrect, diff (-want +got):\n%s\n", diff) } } + +func TestWriteXML(t *testing.T) { + want := ` + + + + +` + + var suites Testsuites + + ts := Testsuite{Name:"Example"} + ts.AddTestcase(Testcase{Name: "Test", }) + suites.AddSuite(ts) + + var buf bytes.Buffer + if err := suites.WriteXML(&buf); err != nil { + t.Fatalf("WriteXML failed: %v\n", err) + } + + got := buf.String() + if diff := cmp.Diff(want, got); diff != "" { + t.Errorf("WriteXML mismatch, diff (-want +got):\n%s\n", diff) + } +}