-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from SimonBaeumer/add-multiplexed-writer
Add multiplexed writer
- Loading branch information
Showing
7 changed files
with
283 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// NewMultiplexedWriter returns a new multiplexer | ||
func NewMultiplexedWriter(outputs ...io.Writer) *MultiplexedWriter { | ||
return &MultiplexedWriter{Outputs: outputs} | ||
} | ||
|
||
// MultiplexedWriter writes to multiple writers at once | ||
type MultiplexedWriter struct { | ||
Outputs []io.Writer | ||
} | ||
|
||
// Write writes the given bytes. If one write fails it returns the error | ||
// and bytes of the failed write operation | ||
func (w MultiplexedWriter) Write(p []byte) (n int, err error) { | ||
for _, o := range w.Outputs { | ||
n, err = o.Write(p) | ||
if err != nil { | ||
return 0, fmt.Errorf("Error in writer: %s", err.Error()) | ||
} | ||
} | ||
|
||
return n, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestMultiplexedWriter(t *testing.T) { | ||
writer01 := bytes.Buffer{} | ||
writer02 := bytes.Buffer{} | ||
// Test another io.Writer interface type | ||
r, w, _ := os.Pipe() | ||
|
||
writer := NewMultiplexedWriter(&writer01, &writer02, w) | ||
n, err := writer.Write([]byte(`test`)) | ||
|
||
assert.Nil(t, err) | ||
assert.Equal(t, 4, n) | ||
assert.Equal(t, "test", writer01.String()) | ||
assert.Equal(t, "test", writer02.String()) | ||
|
||
data := make([]byte, 4) | ||
n, err = r.Read(data) | ||
assert.Nil(t, err) | ||
assert.Equal(t, 4, n) | ||
assert.Equal(t, "test", string(data)) | ||
} | ||
|
||
func TestMultiplexedWriter_SingleWirter(t *testing.T) { | ||
writer01 := bytes.Buffer{} | ||
|
||
writer := NewMultiplexedWriter(&writer01) | ||
|
||
n, _ := writer.Write([]byte(`another`)) | ||
|
||
assert.Equal(t, 7, n) | ||
assert.Equal(t, "another", writer01.String()) | ||
} | ||
|
||
func TestMultiplexedWriter_Fail(t *testing.T) { | ||
writer := NewMultiplexedWriter(InvalidWriter{}) | ||
|
||
n, err := writer.Write([]byte(`another`)) | ||
|
||
assert.Equal(t, 0, n) | ||
assert.Equal(t, "Error in writer: failed", err.Error()) | ||
} | ||
|
||
type InvalidWriter struct { | ||
} | ||
|
||
func (w InvalidWriter) Write(p []byte) (n int, err error) { | ||
return 0, fmt.Errorf("failed") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"github.com/stretchr/testify/assert" | ||
"io" | ||
"log" | ||
"os" | ||
"runtime" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
// CaptureStandardOutput allows to capture the output which will be written | ||
// to os.Stdout and os.Stderr. | ||
// It returns the captured output and the return value of the called function | ||
func CaptureStandardOutput(f func() interface{}) (string, interface{}) { | ||
reader, writer, err := os.Pipe() | ||
if err != nil { | ||
panic(err) | ||
} | ||
stdout := os.Stdout | ||
stderr := os.Stderr | ||
defer func() { | ||
os.Stdout = stdout | ||
os.Stderr = stderr | ||
log.SetOutput(os.Stderr) | ||
}() | ||
os.Stdout = writer | ||
os.Stderr = writer | ||
log.SetOutput(writer) | ||
out := make(chan string) | ||
wg := new(sync.WaitGroup) | ||
wg.Add(1) | ||
go func() { | ||
var buf bytes.Buffer | ||
wg.Done() | ||
io.Copy(&buf, reader) | ||
out <- buf.String() | ||
}() | ||
wg.Wait() | ||
result := f() | ||
writer.Close() | ||
return <-out, result | ||
} | ||
|
||
func assertEqualWithLineBreak(t *testing.T, expected string, actual string) { | ||
if runtime.GOOS == "windows" { | ||
expected = expected + "\r\n" | ||
} else { | ||
expected = expected + "\n" | ||
} | ||
|
||
assert.Equal(t, expected, actual) | ||
} |