Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: go test changes stderr to stdout when you run specific tests #13976

Closed
sethgrid opened this issue Jan 15, 2016 · 1 comment
Closed

cmd/go: go test changes stderr to stdout when you run specific tests #13976

sethgrid opened this issue Jan 15, 2016 · 1 comment
Milestone

Comments

@sethgrid
Copy link

If you want to run a specific test file (ie, go test foo_test.go instead of go test), you lose the ability to capture stderr as it is then sent to stdout.
Example: http://play.golang.org/p/Q51fPoXxI3

This is because testStreamOutput is set to false here when you pass in a single test file but don't provide the verbose flag: https://github.com/golang/go/blob/master/src/cmd/go/test.go#L1079

I found this behavior surprising and think that go test foo_test.go should honor stderr. I had a couple of thoughts on how to approach this. One would be to alter https://github.com/golang/go/blob/master/src/cmd/go/test.go#L410 and have an additional OR clause for len(pkgArgs)==1, but I'm sure the current design was chosen for a reason. Maybe we could check if all args are .go files? Another thought was to assign cmd.Stderr to a separate buffer for printing later. Though, I'm not sure the best way to then print it later. Another thought was to not assign cmd.Stderr to the buffer, and let stderr come through even when we may not be streaming output, but that seems less than ideal and could be unexpected for the end user.

I would have prepared a couple of PRs to show either option working, but I can't seem to get a local build of Go to run against *_test.go files (it thinks it is importing something requiring C source files and complains about needing cgo).

Not sure there is anything else I can do until I figure out how to run go test with a local build of Go. Is this something y'all agree should be fixed? Thanks and cheers!

@ianlancetaylor ianlancetaylor changed the title Go test changes stderr to stdout when you run specific tests cmd/go: go test changes stderr to stdout when you run specific tests Jan 15, 2016
@ianlancetaylor ianlancetaylor added this to the Go1.7 milestone Jan 15, 2016
@rsc
Copy link
Contributor

rsc commented May 17, 2016

What you are seeing is that the output is buffered and printed as a block except in the special case of "go test" with no arguments.
The reason to do this is to separate the output from multiple tests. The reason not to include len(args) == 1 is that if you use a wildcard that resolves to 1 thing, that behavior needs to be the same as resolving to 2 things. At this point I think all these decisions are not likely to change.

If you care deeply about stdout vs stderr you can use 'go test -c && ./foo.test'.

@rsc rsc closed this as completed May 17, 2016
@golang golang locked and limited conversation to collaborators May 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants