Skip to content

Conversation

@pohly
Copy link

@pohly pohly commented Jun 15, 2024

It can be useful to let the user run the test command(s) itself, for example when complex shell scripts are involved which need to invoke go test multiple times.

With -stdin, the go test stdout is expected on stdin of gotestsum, so it can be used in a pipe. To detect abnormal termination of the test commands, bash with "set -o pipefail" should be used. Beware that such failures are not detected by gotestsum.

To also capture stderr with gotestsum, stderr must get redirected like this:

mkfifo /tmp/pipe
go test ... 2>/tmp/pipe | gotestsum -stdin -stderr 3 3</tmp/pipe

Fixes: #414

"don't prepend 'go test -json' to the 'go test' command")
flags.BoolVar(&opts.readStdin, "stdin", false,
"don't run any command, instead read go test stdout from stdin")
flags.IntVar(&opts.readStderrFD, "stderr", 0,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a named file instead?

Perhaps some elaborate invocation could work without a named pipe. In practice, I think with bash one has to rely on one.

@pohly pohly changed the title WIP: support reading stdout/stderr from streams support reading stdout/stderr from streams Jun 16, 2024
@pohly
Copy link
Author

pohly commented Jun 16, 2024

I added documentation and unit tests. I also checked that this really works for Kubernetes: kubernetes/kubernetes#125534.

It can be useful to let the user run the test command(s) itself, for example
when complex shell scripts are involved which need to invoke `go test` multiple
times.

With -stdin, the `go test` stdout is expected on stdin of gotestsum, so it can
be used in a pipe. To detect abnormal termination of the test commands, bash
with "set -o pipefail" should be used. Beware that such failures are not
detected by gotestsum.

To also capture stderr with gotestsum, stderr must get redirected like this:

   mkfifo /tmp/pipe
   go test ... 2>/tmp/pipe | gotestsum -stdin -stderr 3 3</tmp/pipe

in := `{"Time":"2024-06-16T14:46:00.343974039+02:00","Action":"start","Package":"example.com/test"}
{"Time":"2024-06-16T14:46:00.378597503+02:00","Action":"run","Package":"example.com/test","Test":"TestSomething"}
{"Time":"2024-06-16T14:46:00.378798569+02:00","Action":"pass","Package":"example.com/test","Test":"TestSomething","Elapsed":0}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The linter is unhappy about the line length.

Is that a hard criteria for code? Any suggestion how to avoid it? Splitting up the line in the middle IMHO would be worse than a long line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

read "go test" output from stdin

1 participant