Fill os.Stdin for function that reads from it
Asked Answered
Y

4

20

How do I fill os.Stdin in my test for a function that reads from it using a scanner?

I request a user command line input via a scanner using following function:

func userInput() error {
    scanner := bufio.NewScanner(os.Stdin)

    println("What is your name?")
    scanner.Scan()
    username = scanner.Text()

    /* ... */
}

Now how do I test this case and simulate a user input? Following example does not work. Stdin is still empty.

func TestUserInput(t *testing.T) {
    var file *os.File
    file.Write([]byte("Tom"))
    os.Stdin = file

    err := userInput()
    /* ... */
}
Yeung answered 22/9, 2017 at 12:49 Comment(0)
B
41

Mocking os.Stdin

You're on the right track that os.Stdin is a variable (of type *os.File) which you can modify, you can assign a new value to it in tests.

Simplest is to create a temporary file with the content you want to simulate as the input on os.Stdin. To create a temp file, use ioutil.TempFile(). Then write the content into it, and seek back to the beginning of the file. Now you can set it as os.Stdin and perform your tests. Don't forget to cleanup the temp file.

I modified your userInput() to this:

func userInput() error {
    scanner := bufio.NewScanner(os.Stdin)

    fmt.Println("What is your name?")
    var username string
    if scanner.Scan() {
        username = scanner.Text()
    }
    if err := scanner.Err(); err != nil {
        return err
    }

    fmt.Println("Entered:", username)
    return nil
}

And this is how you can test it:

func TestUserInput(t *testing.T) {
    content := []byte("Tom")
    tmpfile, err := ioutil.TempFile("", "example")
    if err != nil {
        log.Fatal(err)
    }

    defer os.Remove(tmpfile.Name()) // clean up

    if _, err := tmpfile.Write(content); err != nil {
        log.Fatal(err)
    }

    if _, err := tmpfile.Seek(0, 0); err != nil {
        log.Fatal(err)
    }

    oldStdin := os.Stdin
    defer func() { os.Stdin = oldStdin }() // Restore original Stdin

    os.Stdin = tmpfile
    if err := userInput(); err != nil {
        t.Errorf("userInput failed: %v", err)
    }

    if err := tmpfile.Close(); err != nil {
        log.Fatal(err)
    }
}

Running the test, we see an output:

What is your name?
Entered: Tom
PASS

Also see related question about mocking the file system: Example code for testing the filesystem in Golang

The easy, preferred way

Also note that you can refactor userInput() to not read from os.Stdin, but instead it could receive an io.Reader to read from. This would make it more robust and a lot easier to test.

In your app you can simply pass os.Stdin to it, and in tests you can pass any io.Reader to it created / prepared in the tests, e.g. using strings.NewReader(), bytes.NewBuffer() or bytes.NewBufferString().

Bathulda answered 22/9, 2017 at 13:7 Comment(3)
Thank you, it works as expected. I've got a question: What do I need to change when I have 2 inputs (e.g. name and then age)? How do I have to modify the test so it uses the 2nd line in the tempfile for the 2nd question?Yeung
@Yeung If the 2 inputs are both read by userInupt(), then the test needs to also write 2 inputs to the file, e.g. content := []byte("Tom\n22") (or content := []byte("Tom 22") depending on how userInput() reads them).Bathulda
Now it works. I had a slightly different setup in which the scanner has been reset every time which apparently does not work with the TempFile. Thank you.Yeung
C
10

os.Pipe()

Instead of messing with the actual file system and doing writes and reads to and from real files on a storage device, the simplest solution is using os.Pipe().

Example

The code of your userInput() does have to be adjusted, and @icza's solution would indeed do for that purpose. But the test itself should be something more like this:

func Test_userInput(t *testing.T) {
    input := []byte("Alice")
    r, w, err := os.Pipe()
    if err != nil {
        t.Fatal(err)
    }

    _, err = w.Write(input)
    if err != nil {
        t.Error(err)
    }
    w.Close()

    // Restore stdin right after the test.
    defer func(v *os.File) { os.Stdin = v }(os.Stdin)
    os.Stdin = r

    if err = userInput(); err != nil {
        t.Fatalf("userInput: %v", err)
    }
}

Details

There are several important points about this code:

  1. Always close your w stream when you're done writing. Many utilities rely on an io.EOF returned by a Read() call to know that no more data is coming, and the bufio.Scanner is no exception. If you don't close the stream, your scanner.Scan() call will never return, but keep looping internally and waiting for more input until the program is terminated forcefully (as when the test times out).

  2. The pipe buffer capacity varies from system to system, as discussed at length in a post in the Unix & Linux Stack Exchange, so if the size of your simulated input could exceed that, you should wrap your write(s) in a goroutine like so:

    //...
    go func() {
        _, err = w.Write(input)
        if err != nil {
            t.Error(err)
        }
        w.Close()
    }()
    //...
    

    This prevents a deadlock when the pipe is full and writes have to wait for it to start emptying, but the code that's supposed to be reading from and emptying the pipe (userInput() in this case) is not starting, because of writing not being over yet.

  3. A test should also verify that errors are handled properly, in this case, returned by userInput(). This means you'd have to figure out a way to make the scanner.Err() call return an error in a test. One approach could be closing the r stream it was supposed to be reading, before it has had the chance.

    Such a test would look almost identical to the nominal case, only you don't write anything at the w end of the pipe, just close the r end, and you actually expect and want userInput() to return an error. And when you have two or more tests of the same function that are almost identical, it is often a good time to implement them as a single table driven test. See Go playground for an example.

io.Reader

The example of userInput() is trivial enough that you could (and should) refactor it and similar cases to read from an io.Reader, just like @icza suggests (see the playground).

You should always strive to rely on some form of dependency injection instead of global state (os.Stdin, in this case, is a global variable in the os package), as that gives more control to the calling code to determine how a called piece of code behaves, which is essential to unit testing, and facilitates better code reuse in general.

Return of os.Pipe()

There may also be cases when you can't really alter a function to take injected dependencies, as when you have to test the main() function of a Go executable. Altering the global state in the test (and hoping that you can properly restore it by the end not to affect subsequent tests) is your only option then. This is where we come back to os.Pipe()

When testing main(), do use os.Pipe() to simulate input to stdin (unless you already hava a file prepared for the purpose) and to capture the output of stdout and stderr (see the playground for an example of the latter).

Casady answered 24/10, 2020 at 23:5 Comment(0)
S
3

Implementation of @icza's easy, preferred way:

Also note that you can refactor userInput() to not read from os.Stdin, but instead it could receive an io.Reader to read from. This would make it more robust and a lot easier to test.

In your app you can simply pass os.Stdin to it, and in tests you can pass any io.Reader to it created / prepared in the tests, e.g. using strings.NewReader(), bytes.NewBuffer() or bytes.NewBufferString().

hello.go

package main

import (
    "bufio"
    "fmt"
    "os"
    "io"
)

func userInput(reader io.Reader) error {
    scanner := bufio.NewScanner(reader)
    var username string
    fmt.Println("What is your name?")
    
    if scanner.Scan() {
        username = scanner.Text()
    }
    if scanner.Err() != nil {
        return scanner.Err()
    }

    fmt.Println("Hello", username)
    return nil
}

func main() {
    userInput(os.Stdin)
}

hello_test.go

package main

import (
    "bytes"
    "io"
    "strings"
    "testing"
)

func TestUserInputWithStringsNewReader(t *testing.T) {
    input := "Tom"
    var reader io.Reader = strings.NewReader(input)
    
    err := userInput(reader)
    if err != nil {
       t.Errorf("Failed to read from strings.NewReader: %w", err)
    }
}

func TestUserInputWithBytesNewBuffer(t *testing.T) {
    input := "Tom"
    var reader io.Reader = bytes.NewBuffer([]byte(input))
    
    err := userInput(reader)
    if err != nil {
        t.Errorf("Failed to read from bytes.NewBuffer: %w", err)
    }
}

func TestUserInputWithBytesNewBufferString(t *testing.T) {
    input := "Tom"
    var reader io.Reader = bytes.NewBufferString(input)
    
    err := userInput(reader)
    if err != nil {
       t.Errorf("Failed to read from bytes.NewBufferString: %w", err)
    }
}

Running the program:

go run hello.go

What is your name?
Tom
Hello Tom

Running the test:

go test hello_test.go hello.go -v

=== RUN   TestUserInputWithStringsNewReader
What is your name?
Hello Tom
--- PASS: TestUserInputWithStringsNewReader (0.00s)
=== RUN   TestUserInputWithBytesNewBuffer
What is your name?
Hello Tom
--- PASS: TestUserInputWithBytesNewBuffer (0.00s)
=== RUN   TestUserInputWithBytesNewBufferString
What is your name?
Hello Tom
--- PASS: TestUserInputWithBytesNewBufferString (0.00s)
PASS
ok      command-line-arguments  0.141s
Sinner answered 13/2, 2022 at 4:6 Comment(0)
H
1

You can use *bufio.Scanner to abstract io.Stdin and io.Writer to abstract io.Stdout while passing them as dependencies to your struct, see Gist: https://gist.github.com/antonzhukov/2a6749f780b24f38b08c9916caa96663 and Playground: https://play.golang.org/p/BZMqpACupSc

Huambo answered 14/7, 2021 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.