fakeio

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2018 License: MIT Imports: 4 Imported by: 3

README

fakeio pacakge for Go

Linux/macOS Build Status Windows Build status Documentation

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 stdout and input 'hello' to stdin
fake := fakeio.Stdout().Stdin("hello!")
defer fake.Restore()

// Do something...

// "hello!" is stored to variable `i`
i, err := bufio.NewReader(os.Stdin).ReadString('!')

// At this point, this line outputs nothing
fmt.Print("bye")

// "bye" is stored to variable `o`
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 {
    // Faking IO failed
    panic(err)
}
fmt.Println(s)

Reading as bytes:

b, err := fake.Bytes()
if err != nil {
    // Faking IO failed
    panic(err)
}
fmt.Println(b)

Reading via io.Reader interface:

s := bufio.NewScanner(fake)
for s.Scan() {
    // Reading line by line
    line := s.Text()
    fmt.Println(line)
}
if s.Err() != nil {
    // Error happened while reading
    panic(s.Err)
}
Shortcut

.Do() is a shortcut

s, err := fakeio.Stderr().Stdout().Do(func () {
    // Do something

    // Faked stderr and stdout are restored at exit of this scope
})
if err != nil {
    // Faking IO failed
    panic(err)
}
fmt.Println(s)
Examples

Please see examples for actual examples.

Repository

https://github.com/rhysd/go-fakeio

License

MIT License

Documentation

Overview

Package fakeio is a small library to fake stdout/stderr/stdin mainly for unit testing.

Following example fakes stdout/stderr/stdin. Set 'from stdin!' as stdin and get output from both stdout and stderr as string.

f := fakeio.Stdout().Stderr().Stdin("from stdin!")

fromInput, err := bufio.NewReader(os.Stdin).ReadString('!')
if err != nil {
    f.Restore()
    panic(err)
}

fmt.Println("from stdout!")
fmt.Fprintln(os.Stderr, "from stderr!")

fromOutput, err := f.String()
if err != nil {
    f.Restore()
    panic(err)
}

// 'defer' is better, but here it's unavailable due to output test
f.Restore()

// Output: from stdin!
fmt.Println(fromInput)

// Output:
// from stdout!
// from stderr!
fmt.Println(fromOutput)

Please read example/example_test.go to see live examples.

If you find some bugs, please report it to repository page: https://github.com/rhysd/go-fakeio

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FakedIO

type FakedIO struct {
	// contains filtered or unexported fields
}

FakedIO represents state of faking stdout/stderr/stdin. Restore() must be called finally to restore the state.

func Stderr

func Stderr() *FakedIO

Stderr starts to fake stderr and returns fakedIO object to restore input/output finally

func Stdin

func Stdin(in string) *FakedIO

Stdin sets given string as faked stdin input and returns fakedIO object to restore input/output finally

func StdinBytes

func StdinBytes(in []byte) *FakedIO

StdinBytes sets given bytes as faked stdin input and returns fakedIO object to restore input/output finally

func Stdout

func Stdout() *FakedIO

Stdout starts to fake stdout and returns fakedIO object to restore input/output finally

func (*FakedIO) Bytes

func (fake *FakedIO) Bytes() ([]byte, error)

Bytes returns buffer as []byte while faking stdout/stderr

func (*FakedIO) CloseStdin

func (fake *FakedIO) CloseStdin() *FakedIO

CloseStdin closes faked stdin

func (*FakedIO) Do

func (fake *FakedIO) Do(f func()) (string, error)

Do runs predicate f and returns output as string

func (*FakedIO) Err

func (fake *FakedIO) Err() error

Err returns error which occurred while setting faked stdin/stdout/stderr

func (*FakedIO) Read

func (fake *FakedIO) Read(p []byte) (int, error)

Read reads bytes from buffer while faking stdout/stderr

func (*FakedIO) Restore

func (fake *FakedIO) Restore()

Restore restores faked stdin/stdout/stderr. This must be called finally.

func (*FakedIO) Stderr

func (fake *FakedIO) Stderr() *FakedIO

Stderr replace stderr with faked output buffer

func (*FakedIO) Stdin

func (fake *FakedIO) Stdin(in string) *FakedIO

Stdin sets given string as faked input for stdin

func (*FakedIO) StdinBytes

func (fake *FakedIO) StdinBytes(in []byte) *FakedIO

StdinBytes sets input buffer for stdin with bytes

func (*FakedIO) Stdout

func (fake *FakedIO) Stdout() *FakedIO

Stdout replace stdout with faked output buffer

func (*FakedIO) String

func (fake *FakedIO) String() (string, error)

String returns buffer as string while faking stdout/stderr

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL