mirror of
https://github.com/jstemmer/go-junit-report.git
synced 2025-04-05 05:00:15 -05:00

Package gtr shouldn't need to know about the existence of different output formats like junit.
117 lines
2.7 KiB
Go
117 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/jstemmer/go-junit-report/v2/pkg/gtr"
|
|
"github.com/jstemmer/go-junit-report/v2/pkg/junit"
|
|
"github.com/jstemmer/go-junit-report/v2/pkg/parser/gotest"
|
|
)
|
|
|
|
var (
|
|
Version = "v2.0.0-dev"
|
|
Revision = "HEAD"
|
|
BuildTime string
|
|
)
|
|
|
|
var (
|
|
noXMLHeader = flag.Bool("no-xml-header", false, "do not print xml header")
|
|
packageName = flag.String("package-name", "", "specify a package name (compiled test have no package name in output)")
|
|
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")
|
|
iocopy = flag.Bool("iocopy", false, "copy input to stdout; can only be used in conjunction with -out")
|
|
|
|
// debug flags
|
|
printEvents = flag.Bool("debug.print-events", false, "print events generated by the go test parser")
|
|
)
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
if *iocopy && *output == "" {
|
|
exitf("you must specify an output file with -out when using -iocopy")
|
|
}
|
|
|
|
if *version {
|
|
fmt.Printf("go-junit-report %s %s (%s)\n", Version, BuildTime, Revision)
|
|
return
|
|
}
|
|
|
|
if flag.NArg() != 0 {
|
|
fmt.Fprintf(os.Stderr, "%s does not accept positional arguments\n", os.Args[0])
|
|
flag.Usage()
|
|
exitf("")
|
|
}
|
|
|
|
// Read input
|
|
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
|
|
}
|
|
|
|
if *iocopy {
|
|
in = io.TeeReader(in, os.Stdout)
|
|
}
|
|
|
|
events, err := gotest.Parse(in)
|
|
if err != nil {
|
|
exitf("error reading input: %s\n", err)
|
|
}
|
|
|
|
if *printEvents {
|
|
for i, ev := range events {
|
|
fmt.Printf("%02d: %#v\n", i, ev)
|
|
}
|
|
}
|
|
report := gtr.FromEvents(events, *packageName)
|
|
|
|
hostname, _ := os.Hostname() // ignore error
|
|
testsuites := junit.CreateFromReport(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(out, xml.Header)
|
|
}
|
|
|
|
enc := xml.NewEncoder(out)
|
|
enc.Indent("", "\t")
|
|
if err := enc.Encode(testsuites); 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 exitf(msg string, args ...interface{}) {
|
|
if msg != "" {
|
|
fmt.Fprintf(os.Stderr, msg+"\n", args...)
|
|
}
|
|
os.Exit(2)
|
|
}
|