Jason Xu
Jason Xu

Reputation: 2963

Readline Failed in Go Test Case

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
}

Upvotes: 0

Views: 1350

Answers (1)

shawnzhu
shawnzhu

Reputation: 7585

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))
}

Upvotes: 1

Related Questions