Move XML writing into separate function

This commit is contained in:
Joël Stemmer 2022-03-27 22:21:11 +01:00
parent 733873e344
commit 9c3fda9259

View File

@ -108,25 +108,35 @@ func main() {
defer f.Close() defer f.Close()
out = f out = f
} }
if !*noXMLHeader {
fmt.Fprintf(out, xml.Header)
}
enc := xml.NewEncoder(out) if err := writeXML(out, testsuites, *noXMLHeader); err != nil {
enc.Indent("", "\t")
if err := enc.Encode(testsuites); err != nil {
exitf("error writing XML: %v", err) 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() { if *setExitCode && !report.IsSuccessful() {
os.Exit(1) 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{}) { func exitf(msg string, args ...interface{}) {
if msg != "" { if msg != "" {
fmt.Fprintf(os.Stderr, msg+"\n", args...) fmt.Fprintf(os.Stderr, msg+"\n", args...)