Skip to content

Commit

Permalink
Extend API, more error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisBrunner committed Aug 18, 2018
1 parent 501ff52 commit 26fc330
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BINARY =
TARGET =
LINT_PACKAGES = io*.go
LINT_PACKAGES = *.go
COVERPROFILES = *.coverprofile
COVERPROFILE = goverprofile
CWD = $(shell pwd)
Expand Down
30 changes: 22 additions & 8 deletions io.go → wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,44 @@ import (

// WrapStdout will collect everything sent to the standard output
// while `functor` is executed in return it in a string
func WrapStdout(functor func()) []byte {
func WrapStdout(functor func()) ([]byte, error) {
backup := os.Stdout
r, w, _ := os.Pipe()
r, w, err := os.Pipe()
if err != nil {
return nil, err
}
os.Stdout = w
defer func() {
os.Stdout = backup
}()
functor()
w.Close()
content, _ := ioutil.ReadAll(r)
return content
content, err := ioutil.ReadAll(r)
r.Close()
if err != nil {
return nil, err
}
return content, nil
}

// WrapStderr will collect everything sent to the standard error output
// while `functor` is executed in return it in a string
func WrapStderr(functor func()) []byte {
func WrapStderr(functor func()) ([]byte, error) {
backup := os.Stderr
r, w, _ := os.Pipe()
r, w, err := os.Pipe()
if err != nil {
return nil, err
}
os.Stderr = w
defer func() {
os.Stderr = backup
}()
functor()
w.Close()
content, _ := ioutil.ReadAll(r)
return content
content, err := ioutil.ReadAll(r)
r.Close()
if err != nil {
return nil, err
}
return content, nil
}
12 changes: 8 additions & 4 deletions io_test.go → wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ import (
)

func TestWrapStdout_Works(t *testing.T) {
content := WrapStdout(func() {
content, err := WrapStdout(func() {
fmt.Printf("123\n")
})
assert.Equal(t, "123\n", string(content))
if assert.NoError(t, err) {
assert.Equal(t, "123\n", string(content))
}
}

func TestWrapStderr_Works(t *testing.T) {
content := WrapStderr(func() {
content, err := WrapStderr(func() {
fmt.Fprintf(os.Stderr, "456\n")
})
assert.Equal(t, "456\n", string(content))
if assert.NoError(t, err) {
assert.Equal(t, "456\n", string(content))
}
}
25 changes: 25 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package iowrap

import (
"io"
"io/ioutil"
"os"
)

// Writer returns a writer that will collect all output and which can then be
// accessed by calling the second return (which will also close the writer)
func Writer() (io.Writer, func() ([]byte, error), error) {
r, w, err := os.Pipe()
if err != nil {
return nil, nil, err
}
return w, func() ([]byte, error) {
w.Close()
content, err := ioutil.ReadAll(r)
r.Close()
if err != nil {
return nil, err
}
return content, nil
}, nil
}
22 changes: 22 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package iowrap

import (
"log"
"testing"

"github.com/stretchr/testify/assert"
)

func TestWriter_Works(t *testing.T) {
w, collect, err := Writer()
if !assert.NoError(t, err) {
return
}

log.SetOutput(w)
log.Printf("hello123")
content, err := collect()
if assert.NoError(t, err) {
assert.Regexp(t, " hello123\n$", string(content))
}
}

0 comments on commit 26fc330

Please sign in to comment.