From 9c3fda925906f856a21e0fd8905391dce5147273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Stemmer?= Date: Sun, 27 Mar 2022 22:21:11 +0100 Subject: [PATCH] Move XML writing into separate function --- go-junit-report.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/go-junit-report.go b/go-junit-report.go index 30cf408..fc44ad4 100644 --- a/go-junit-report.go +++ b/go-junit-report.go @@ -108,25 +108,35 @@ func main() { defer f.Close() out = f } - if !*noXMLHeader { - fmt.Fprintf(out, xml.Header) - } - enc := xml.NewEncoder(out) - enc.Indent("", "\t") - if err := enc.Encode(testsuites); err != nil { + if err := writeXML(out, testsuites, *noXMLHeader); err != nil { exitf("error writing XML: %v", err) } - if err := enc.Flush(); err != nil { - exitf("error flusing XML: %v", err) - } - fmt.Fprintf(out, "\n") if *setExitCode && !report.IsSuccessful() { os.Exit(1) } } +func writeXML(w io.Writer, testsuites junit.Testsuites, skipHeader bool) error { + if !skipHeader { + _, 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 +} + func exitf(msg string, args ...interface{}) { if msg != "" { fmt.Fprintf(os.Stderr, msg+"\n", args...)