fakeio
pacakge for Go
fakeio
is a small library to fake stdout/stderr/stdin.
This is mainly for unit testing of CLI applications. Please see documentation
for more details.
Installation
$ go get github.com/rhysd/go-fakeio
Usage
Basic usage:
import (
"bufio"
"github.com/rhysd/go-fakeio"
)
fake := fakeio.Stdout().Stdin("hello!")
defer fake.Restore()
i, err := bufio.NewReader(os.Stdin).ReadString('!')
fmt.Print("bye")
o, err := fake.String()
Faking stdout/stderr/stdin
Faking stderr:
fake := fakeio.Stderr()
defer fake.Restore()
Faking stdin:
fake, err := fakeio.Stdin("hello")
defer fake.Restore()
Faking stderr/stdout/stdin
fake := fakeio.Stderr().Stdout().Stdin("Faked input to stdin")
defer fake.Restore()
Read bufferred stdout/stderr
Reading as string:
s, err := fake.String()
if err != nil {
panic(err)
}
fmt.Println(s)
Reading as bytes:
b, err := fake.Bytes()
if err != nil {
panic(err)
}
fmt.Println(b)
Reading via io.Reader
interface:
s := bufio.NewScanner(fake)
for s.Scan() {
line := s.Text()
fmt.Println(line)
}
if s.Err() != nil {
panic(s.Err)
}
Shortcut
.Do()
is a shortcut
s, err := fakeio.Stderr().Stdout().Do(func () {
})
if err != nil {
panic(err)
}
fmt.Println(s)
Examples
Please see examples for actual examples.
Repository
https://github.com/rhysd/go-fakeio
License
MIT License