diff --git a/cmd/main.go b/cmd/main.go index 878761d5..7c97d714 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -59,6 +59,8 @@ func setupFlags(name string) (*pflag.FlagSet, *options) { "print format of test input") flags.BoolVar(&opts.rawCommand, "raw-command", false, "don't prepend 'go test -json' to the 'go test' command") + flags.BoolVar(&opts.ignoreNonJSONOutputLines, "ignore-non-json-output-lines", false, + "write non-JSON 'go test' output lines to stderr instead of failing") flags.StringVar(&opts.jsonFile, "jsonfile", lookEnvWithDefault("GOTESTSUM_JSONFILE", ""), "write all TestEvents to file") @@ -138,6 +140,7 @@ type options struct { format string debug bool rawCommand bool + ignoreNonJSONOutputLines bool jsonFile string junitFile string postRunHookCmd *commandValue @@ -194,10 +197,11 @@ func run(opts *options) error { } defer handler.Close() // nolint: errcheck cfg := testjson.ScanConfig{ - Stdout: goTestProc.stdout, - Stderr: goTestProc.stderr, - Handler: handler, - Stop: cancel, + Stdout: goTestProc.stdout, + Stderr: goTestProc.stderr, + Handler: handler, + Stop: cancel, + IgnoreNonJSONOutputLines: opts.ignoreNonJSONOutputLines, } exec, err := testjson.ScanTestOutput(cfg) if err != nil { diff --git a/cmd/testdata/gotestsum-help-text b/cmd/testdata/gotestsum-help-text index d5996d38..02f43eef 100644 --- a/cmd/testdata/gotestsum-help-text +++ b/cmd/testdata/gotestsum-help-text @@ -6,6 +6,7 @@ Flags: --debug enabled debug logging -f, --format string print format of test input (default "short") --hide-summary summary hide sections of the summary: skipped,failed,errors,output (default none) + --ignore-non-json-output-lines write non-JSON 'go test' output lines to stderr instead of failing --jsonfile string write all TestEvents to file --junitfile string write a JUnit XML file --junitfile-testcase-classname field-format format the testcase classname field as: full, relative, short (default full) diff --git a/testjson/execution.go b/testjson/execution.go index c7b6f6d0..d8d774cc 100644 --- a/testjson/execution.go +++ b/testjson/execution.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io" + "os" "sort" "strings" "sync" @@ -590,6 +591,9 @@ type ScanConfig struct { Execution *Execution // Stop is called when ScanTestOutput fails during a scan. Stop func() + // IgnoreNonJSONOutputLines causes ScanTestOutput to print non-JSON lines to + // stderr rather than returning an error. + IgnoreNonJSONOutputLines bool } // EventHandler is called by ScanTestOutput for each event and write to stderr. @@ -662,6 +666,10 @@ func readStdout(config ScanConfig, execution *Execution) error { config.Handler.Err(errBadEvent.Error() + ": " + scanner.Text()) continue case err != nil: + if config.IgnoreNonJSONOutputLines { + fmt.Fprintf(os.Stderr, "%s\n", raw) + continue + } return errors.Wrapf(err, "failed to parse test output: %s", string(raw)) }