Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
github.com/rhysd/go-fakeio
fakeio
pacakge for Gofakeio
is a small library to fake stdout/stderr/stdin.
This is mainly for unit testing of CLI applications. Please see documentation
for more details.
$ go get github.com/rhysd/go-fakeio
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 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()
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)
}
.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)
Please see examples for actual examples.
https://github.com/rhysd/go-fakeio
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.