% go test -v
=== RUN TestInput
Your name please? Press the Enter key when done
stdin_test.go:21: empty input
stdin_test.go:23:
--- PASS: TestInput (0.00s)
PASS
ok test 0.370s
However, when i compile the test and then run the binary, it waits for me to enter the input:
% go test -c
% ./test.test
Your name please? Press the Enter key when done
The latter behavior is more inline with what i was expecting in the first case as well.
I thought may be it has something to do with the fact that go test is executing the binary (i think) after compiling, and started looking at: https://github.com/golang/go/blob/c7f2f51fed15b410dea5f608420858b401887d0a/src/cmd/go/internal/test/test.go , but can't see anything obvious.
Wondering if anyone of you folks have an answer?
Thanks,
Amit.
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/25A4D3EA-F228-447C-A19E-8BE062840E20%40gmail.com.
First, to point out the obvious: It is a bad idea to have a test read from stdin. You should pass a separate io.Reader and use that.Next: exec.Cmd.{Stdin,Stdout,Stderr} are all set to os.DevNull by default, meaning you don't get any input or output. I assume- `go test` does not modify Cmd.Stdin (leaving it at os.DevNull) of the sub-process it launches and redirects Cmd.{Stdout,Stderr} to buffers.- Your test (if you use `exec.Command(…).Run()`) also does not modify any of them, so they stay at os.DevNull- Other languages have them default to stdin/stdout/stderr of the parent process, so they try to read from stdin, which is a line-buffered terminal, so it blocks until you input a line.Again, the solution here should be to not rely on os.Stdin at all, but instead test a function which uses a general io.Reader.