diff --git a/go-junit-report.go b/go-junit-report.go index 08caaca..eff031a 100644 --- a/go-junit-report.go +++ b/go-junit-report.go @@ -4,6 +4,7 @@ import ( "encoding/xml" "flag" "fmt" + "io" "os" "time" @@ -23,6 +24,8 @@ var ( goVersionFlag = flag.String("go-version", "", "specify the value to use for the go.version property in the generated XML") setExitCode = flag.Bool("set-exit-code", false, "set exit code to 1 if tests failed") version = flag.Bool("version", false, "print version") + input = flag.String("in", "", "read go test log from file") + output = flag.String("out", "", "write XML report to file") // debug flags printEvents = flag.Bool("debug.print-events", false, "print events generated by the go test parser") @@ -43,7 +46,16 @@ func main() { } // Read input - events, err := gotest.Parse(os.Stdin) + var in io.Reader = os.Stdin + if *input != "" { + f, err := os.Open(*input) + if err != nil { + exitf("error opening input file: %v", err) + } + defer f.Close() + in = f + } + events, err := gotest.Parse(in) if err != nil { exitf("error reading input: %s\n", err) } @@ -58,11 +70,20 @@ func main() { hostname, _ := os.Hostname() // ignore error testsuites := gtr.JUnit(report, hostname, time.Now()) + var out io.Writer = os.Stdout + if *output != "" { + f, err := os.Create(*output) + if err != nil { + exitf("error creating output file: %v", err) + } + defer f.Close() + out = f + } if !*noXMLHeader { - fmt.Fprintf(os.Stdout, xml.Header) + fmt.Fprintf(out, xml.Header) } - enc := xml.NewEncoder(os.Stdout) + enc := xml.NewEncoder(out) enc.Indent("", "\t") if err := enc.Encode(testsuites); err != nil { exitf("error writing XML: %v", err) @@ -70,7 +91,7 @@ func main() { if err := enc.Flush(); err != nil { exitf("error flusing XML: %v", err) } - fmt.Fprintf(os.Stdout, "\n") + fmt.Fprintf(out, "\n") if *setExitCode && report.HasFailures() { os.Exit(1)