Skip to content

Commit

Permalink
Add --ignore-non-json-output-lines option
Browse files Browse the repository at this point in the history
--ignore-non-json-output-lines causes gotestsum to write non-JSON test
output lines to os.Stderr instead of failing.
  • Loading branch information
dagood committed May 10, 2021
1 parent bb9679f commit cee4211
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cmd/main.go
Expand Up @@ -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")
Expand Down Expand Up @@ -138,6 +140,7 @@ type options struct {
format string
debug bool
rawCommand bool
ignoreNonJSONOutputLines bool
jsonFile string
junitFile string
postRunHookCmd *commandValue
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cmd/testdata/gotestsum-help-text
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions testjson/execution.go
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"os"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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))
}

Expand Down

0 comments on commit cee4211

Please sign in to comment.