The tests added cover the case when the Writer field returns and error
and, related to that, when the number of written bytes is less than 0.
Signed-off-by: Federico Gimenez <fgimenez@coit.es>
| ... | ... |
@@ -2,6 +2,7 @@ package stdcopy |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"bytes" |
| 5 |
+ "errors" |
|
| 5 | 6 |
"io/ioutil" |
| 6 | 7 |
"strings" |
| 7 | 8 |
"testing" |
| ... | ... |
@@ -49,6 +50,41 @@ func TestWrite(t *testing.T) {
|
| 49 | 49 |
} |
| 50 | 50 |
} |
| 51 | 51 |
|
| 52 |
+type errWriter struct {
|
|
| 53 |
+ n int |
|
| 54 |
+ err error |
|
| 55 |
+} |
|
| 56 |
+ |
|
| 57 |
+func (f *errWriter) Write(buf []byte) (int, error) {
|
|
| 58 |
+ return f.n, f.err |
|
| 59 |
+} |
|
| 60 |
+ |
|
| 61 |
+func TestWriteWithWriterError(t *testing.T) {
|
|
| 62 |
+ expectedError := errors.New("expected")
|
|
| 63 |
+ expectedReturnedBytes := 10 |
|
| 64 |
+ writer := NewStdWriter(&errWriter{
|
|
| 65 |
+ n: stdWriterPrefixLen + expectedReturnedBytes, |
|
| 66 |
+ err: expectedError}, Stdout) |
|
| 67 |
+ data := []byte("This won't get written, sigh")
|
|
| 68 |
+ n, err := writer.Write(data) |
|
| 69 |
+ if err != expectedError {
|
|
| 70 |
+ t.Fatalf("Didn't get expected error.")
|
|
| 71 |
+ } |
|
| 72 |
+ if n != expectedReturnedBytes {
|
|
| 73 |
+ t.Fatalf("Didn't get expected writen bytes %d, got %d.",
|
|
| 74 |
+ expectedReturnedBytes, n) |
|
| 75 |
+ } |
|
| 76 |
+} |
|
| 77 |
+ |
|
| 78 |
+func TestWriteDoesNotReturnNegativeWrittenBytes(t *testing.T) {
|
|
| 79 |
+ writer := NewStdWriter(&errWriter{n: -1}, Stdout)
|
|
| 80 |
+ data := []byte("This won't get written, sigh")
|
|
| 81 |
+ actual, _ := writer.Write(data) |
|
| 82 |
+ if actual != 0 {
|
|
| 83 |
+ t.Fatalf("Expected returned written bytes equal to 0, got %d", actual)
|
|
| 84 |
+ } |
|
| 85 |
+} |
|
| 86 |
+ |
|
| 52 | 87 |
func TestStdCopyWithInvalidInputHeader(t *testing.T) {
|
| 53 | 88 |
dstOut := NewStdWriter(ioutil.Discard, Stdout) |
| 54 | 89 |
dstErr := NewStdWriter(ioutil.Discard, Stderr) |