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.
This commit is contained in:
Joël Stemmer 2022-03-28 00:01:27 +01:00
parent 1b2039eca6
commit f355bc72cc

View File

@ -9,6 +9,7 @@ import (
"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"
)
@ -28,6 +29,7 @@ var (
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")
@ -75,7 +77,17 @@ func main() {
in = io.TeeReader(in, os.Stdout)
}
parser := gotest.New(gotest.PackageName(*packageName))
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 {
@ -166,3 +178,8 @@ func (f *keyValueFlag) Set(value string) error {
(*f)[k] = v
return nil
}
type Parser interface {
Parse(r io.Reader) (gtr.Report, error)
Events() []gotest.Event
}