On Sunday, May 6, 2018 at 7:32:47 AM UTC+2, Lars Seipel wrote:
>
> On Sat, May 05, 2018 at 08:55:17AM -0700, [email protected]
> <javascript:> wrote:
> > * Now in golang `os.Stdout` is an `*os.File` which is mostly a wrapper
> for
> > an OS specific file.
>
> You can always use os.Pipe to get an actual file descriptor to write to.
>
Hello Lars,
thanks, I now have
func dieOn(err error, t *testing.T) {
if err != nil {
t.Fatal(err)
}
}
// Returns output to `os.Stdout` from `runnable` as string.
func catchStdOut(t *testing.T, runnable func()) (string) {
realStdout := os.Stdout
defer func() { os.Stdout = realStdout }()
r, fakeStdout, err := os.Pipe()
dieOn(err, t)
os.Stdout = fakeStdout
runnable()
// need to close here, otherwise ReadAll never gets "EOF".
dieOn(fakeStdout.Close(), t)
newOutBytes, err := ioutil.ReadAll(r)
dieOn(err, t)
dieOn(r.Close(), t)
return string(newOutBytes)
}
I like that better
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.