0

I'm experimenting getting keyboard input in a test case, basically I followed this example,

https://www.socketloop.com/tutorials/golang-read-input-from-console-line

and in my unit test case, it always result in error of "EOF" without giving me a chance to type in from keyboard.

Is there any special in Go unit test environment? Or I should look into another better option?

My code looks like,

func (o *Player) consoleReadLn() (line string) {
    consoleReader := bufio.NewReader(os.Stdin)
    line, err := consoleReader.ReadString('\n')
    if err != nil {
        panic(err.Error())   // it just panic: EOF
    }
    return
}
Jason Xu
  • 2,903
  • 5
  • 31
  • 54
  • 2
    It makes no sense for a `go test` invocation to allow a test to read from stdin. – Dave C Aug 08 '15 at 13:32
  • @DaveC, yes. for me it's just for easier interaction with code before really commit to repo. but I just wondering why it come out differently comparing to running in main. – Jason Xu Aug 09 '15 at 03:09

1 Answers1

1

Firstly, your code should be corrected to:

import "testing/iotest"

func (o *Player) consoleReadLn() string {
    consoleReader := bufio.NewReader(os.Stdin)
    s := "" 
    for {
        s1, err := consoleReader.ReadString('\n')
        if err == io.EOF {
            break
        }
        if err != nil && err != iotest.ErrTimeout {
            panic("GetLines: " + err.Error())
        }
        s += s1
    }
    return s
}

Because you expect using \n as delimiter of a line of string, so it will return the data with EOF which is \n in Unix OS, see godoc bufio#Reader.ReadString:

ReadString reads until the first occurrence of delim in the input, returning a string containing the data up to and including the delimiter. If ReadString encounters an error before finding a delimiter, it returns the data read before the error and the error itself (often io.EOF). ReadString returns err != nil if and only if the returned data does not end in delim. For simple uses, a Scanner may be more convenient.

However, I suggest reading this answer Read from initial stdin in GO?

Secondly, it is hard to test STDIN in unit test context like go test. I found this mail saying:

the new go test runs tests with standard input connected to /dev/null.

So I think it is hard to test os.Stdin via go test directly, for example, the following code confirmed it doesn't read /dev/stdin at all when running command echo this is stdin | go test ./:

import "io/ioutil"
import "testing"
import "fmt"

func TestSTDIN(t *testing.T) {
    bytes, err := ioutil.ReadAll(os.Stdin)

    if err != nil {
        t.Fatal(err)
    }

    fmt.Println(string(bytes))
}
Community
  • 1
  • 1
shawnzhu
  • 7,233
  • 4
  • 35
  • 51