-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
os/exec: example test setting Command.Stdout does not terminate #17768
Comments
@stapelberg Why did you expect the test to fail? The command is still running, so the test is still waiting for the output. |
How is |
I'm pretty sure this is related to the use of an Example and how it captures os.Stdout. When cmd.Stdout == nil it's effectively set to /dev/null and then closed during Start(). Also the use of exec.Start() isn't exactly correct as Start() doesn't wait for the program to complete, to do so you need to call cmd.Wait(). |
It seems like I think adjusting the documentation to mention that all uses of os.Stdout in an example test must close os.Stdout in order to have the example test terminate is sufficient to close this issue. |
I'm not sure where we'd document this, or even that we'd want to. Examples are supposed to be simple. If you're doing these sorts of shenanigans, I think this sort of pain is exactly the sort of feedback you need to stop writing complicated examples. This works fine for me: func ExampleFoo() {
cmd := exec.Command("/bin/sh", "-c", "echo hello")
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
// OUTPUT:
// hello
} So I don't think there's anything to do here. |
What version of Go are you using (
go version
)?What operating system and processor architecture are you using (
go env
)?What did you do?
The following example test does not terminate:
Removing the
cmd.Stdout = os.Stdout
line makes it terminate.In case it matters, I’m running
go test -v example/...
, the above example is stored ingocode/src/example/example_test.go
What did you expect to see?
I expected the test to fail.
What did you see instead?
The test does not terminate.
The text was updated successfully, but these errors were encountered: