go-junit-report/go-junit-report.go
Joël Stemmer f355bc72cc Add -parser flag to choose which parser to use
The two parsers that are supported are `gotest` (default), and `gojson`.
They handle the standard `go test -v` and `go test -json` output
respectively.
2022-03-31 21:51:25 +01:00

186 lines
4.5 KiB
Go

package main
import (
"encoding/json"
"encoding/xml"
"flag"
"fmt"
"io"
"os"
"strings"
"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 default package `name` to use if output does not contain a package name")
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")
properties = make(keyValueFlag)
inputParser = flag.String("parser", "gotest", "set input parser: gotest, gojson")
// debug flags
printEvents = flag.Bool("debug.print-events", false, "print events generated by the go test parser")
// deprecated flags
goVersionFlag = flag.String("go-version", "", "(deprecated, use -prop) the value to use for the go.version property in the generated XML")
)
func main() {
flag.Var(&properties, "prop", "add property to generated report; properties should be specified as `key=value`")
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 *goVersionFlag != "" {
fmt.Fprintf(os.Stderr, "the -go-version flag is deprecated and will be removed in the future.\n")
properties["go.version"] = *goVersionFlag
}
if flag.NArg() != 0 {
fmt.Fprintf(os.Stderr, "invalid argument(s): %s\n", strings.Join(flag.Args(), " "))
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)
}
var parser Parser
switch *inputParser {
case "gotest":
parser = gotest.New(gotest.PackageName(*packageName))
case "gojson":
parser = gotest.NewJSON(gotest.PackageName(*packageName))
default:
fmt.Fprintf(os.Stderr, "invalid parser: %s\n", *inputParser)
flag.Usage()
os.Exit(1)
}
report, err := parser.Parse(in)
if err != nil {
exitf("error parsing input: %s\n", err)
}
if *printEvents {
enc := json.NewEncoder(os.Stderr)
for _, event := range parser.Events() {
if err := enc.Encode(event); err != nil {
exitf("error printing events: %v\n", err)
}
}
}
for i := range report.Packages {
for k, v := range properties {
report.Packages[i].SetProperty(k, v)
}
}
hostname, _ := os.Hostname() // ignore error
testsuites := junit.CreateFromReport(report, hostname)
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 err := writeXML(out, testsuites, *noXMLHeader); err != nil {
exitf("error writing XML: %v", err)
}
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...)
}
os.Exit(2)
}
type keyValueFlag map[string]string
func (f *keyValueFlag) String() string {
if f != nil {
var pairs []string
for k, v := range *f {
pairs = append(pairs, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(pairs, ",")
}
return ""
}
func (f *keyValueFlag) Set(value string) error {
idx := strings.IndexByte(value, '=')
if idx == -1 {
return fmt.Errorf("%v is not specified as \"key=value\"", value)
}
k, v := value[:idx], value[idx+1:]
(*f)[k] = v
return nil
}
type Parser interface {
Parse(r io.Reader) (gtr.Report, error)
Events() []gotest.Event
}