Skip to content

Commit 0ba5de6

Browse files
committed
Add multiplexed writer test
1 parent 631fe7e commit 0ba5de6

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

command_linux_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,10 @@ func TestWithStandardStreams(t *testing.T) {
131131
assertEqualWithLineBreak(t, "stderr\nstdout", out)
132132
assert.Nil(t, err)
133133
}
134+
135+
func TestWithEnvironmentVariables(t *testing.T) {
136+
c := NewCommand("echo $env", WithEnvironmentVariables(map[string]string{"env": "value"}))
137+
c.Execute()
138+
139+
assertEqualWithLineBreak(t, "value", c.Stdout())
140+
}

command_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,3 @@ func TestCommand_SetOptions(t *testing.T) {
126126
assert.Equal(t, time.Duration(1000000000), c.Timeout)
127127
assertEqualWithLineBreak(t, "test", writer.String())
128128
}
129-
130-
func TestWithEnvironmentVariables(t *testing.T) {
131-
c := NewCommand("echo $env", WithEnvironmentVariables(map[string]string{"env": "value"}))
132-
c.Execute()
133-
134-
assertEqualWithLineBreak(t, "value", c.Stdout())
135-
}

multiplexed_writer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"io"
56
)
67

@@ -20,7 +21,7 @@ func (w MultiplexedWriter) Write(p []byte) (n int, err error) {
2021
for _, o := range w.Outputs {
2122
n, err = o.Write(p)
2223
if err != nil {
23-
return 0, nil
24+
return 0, fmt.Errorf("Error in writer: %s", err.Error())
2425
}
2526
}
2627

multiplexed_writer_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"bytes"
5+
"fmt"
56
"github.com/stretchr/testify/assert"
67
"os"
78
"testing"
@@ -38,3 +39,19 @@ func TestMultiplexedWriter_SingleWirter(t *testing.T) {
3839
assert.Equal(t, 7, n)
3940
assert.Equal(t, "another", writer01.String())
4041
}
42+
43+
func TestMultiplexedWriter_Fail(t *testing.T) {
44+
writer := NewMultiplexedWriter(InvalidWriter{})
45+
46+
n, err := writer.Write([]byte(`another`))
47+
48+
assert.Equal(t, 0, n)
49+
assert.Equal(t, "Error in writer: failed", err.Error())
50+
}
51+
52+
type InvalidWriter struct {
53+
}
54+
55+
func (w InvalidWriter) Write(p []byte) (n int, err error) {
56+
return 0, fmt.Errorf("failed")
57+
}

0 commit comments

Comments
 (0)