| 1 |
package ioutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"io" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
. "gopkg.in/check.v1" |
| 11 |
) |
| 12 |
|
| 13 |
func Test(t *testing.T) { TestingT(t) } |
| 14 |
|
| 15 |
type CommonSuite struct{} |
| 16 |
|
| 17 |
var _ = Suite(&CommonSuite{}) |
| 18 |
|
| 19 |
type closer struct { |
| 20 |
called int |
| 21 |
} |
| 22 |
|
| 23 |
func (c *closer) Close() error { |
| 24 |
c.called++ |
| 25 |
return nil |
| 26 |
} |
| 27 |
|
| 28 |
func (s *CommonSuite) TestNonEmptyReader_Empty(c *C) { |
| 29 |
var buf bytes.Buffer |
| 30 |
r, err := NonEmptyReader(&buf) |
| 31 |
c.Assert(err, Equals, ErrEmptyReader) |
| 32 |
c.Assert(r, IsNil) |
| 33 |
} |
| 34 |
|
| 35 |
func (s *CommonSuite) TestNonEmptyReader_NonEmpty(c *C) { |
| 36 |
buf := bytes.NewBuffer([]byte("1")) |
| 37 |
r, err := NonEmptyReader(buf) |
| 38 |
c.Assert(err, IsNil) |
| 39 |
c.Assert(r, NotNil) |
| 40 |
|
| 41 |
read, err := io.ReadAll(r) |
| 42 |
c.Assert(err, IsNil) |
| 43 |
c.Assert(string(read), Equals, "1") |
| 44 |
} |
| 45 |
|
| 46 |
func (s *CommonSuite) TestNewReadCloser(c *C) { |
| 47 |
buf := bytes.NewBuffer([]byte("1")) |
| 48 |
closer := &closer{} |
| 49 |
r := NewReadCloser(buf, closer) |
| 50 |
|
| 51 |
read, err := io.ReadAll(r) |
| 52 |
c.Assert(err, IsNil) |
| 53 |
c.Assert(string(read), Equals, "1") |
| 54 |
|
| 55 |
c.Assert(r.Close(), IsNil) |
| 56 |
c.Assert(closer.called, Equals, 1) |
| 57 |
} |
| 58 |
|
| 59 |
func (s *CommonSuite) TestNewContextReader(c *C) { |
| 60 |
buf := bytes.NewBuffer([]byte("12")) |
| 61 |
ctx, close := context.WithCancel(context.Background()) |
| 62 |
|
| 63 |
r := NewContextReader(ctx, buf) |
| 64 |
|
| 65 |
b := make([]byte, 1) |
| 66 |
n, err := r.Read(b) |
| 67 |
c.Assert(n, Equals, 1) |
| 68 |
c.Assert(err, IsNil) |
| 69 |
|
| 70 |
close() |
| 71 |
n, err = r.Read(b) |
| 72 |
c.Assert(n, Equals, 0) |
| 73 |
c.Assert(err, NotNil) |
| 74 |
} |
| 75 |
|
| 76 |
func (s *CommonSuite) TestNewContextReadCloser(c *C) { |
| 77 |
buf := NewReadCloser(bytes.NewBuffer([]byte("12")), &closer{}) |
| 78 |
ctx, close := context.WithCancel(context.Background()) |
| 79 |
|
| 80 |
r := NewContextReadCloser(ctx, buf) |
| 81 |
|
| 82 |
b := make([]byte, 1) |
| 83 |
n, err := r.Read(b) |
| 84 |
c.Assert(n, Equals, 1) |
| 85 |
c.Assert(err, IsNil) |
| 86 |
|
| 87 |
close() |
| 88 |
n, err = r.Read(b) |
| 89 |
c.Assert(n, Equals, 0) |
| 90 |
c.Assert(err, NotNil) |
| 91 |
|
| 92 |
c.Assert(r.Close(), IsNil) |
| 93 |
} |
| 94 |
|
| 95 |
func (s *CommonSuite) TestNewContextWriter(c *C) { |
| 96 |
buf := bytes.NewBuffer(nil) |
| 97 |
ctx, close := context.WithCancel(context.Background()) |
| 98 |
|
| 99 |
r := NewContextWriter(ctx, buf) |
| 100 |
|
| 101 |
n, err := r.Write([]byte("1")) |
| 102 |
c.Assert(n, Equals, 1) |
| 103 |
c.Assert(err, IsNil) |
| 104 |
|
| 105 |
close() |
| 106 |
n, err = r.Write([]byte("1")) |
| 107 |
c.Assert(n, Equals, 0) |
| 108 |
c.Assert(err, NotNil) |
| 109 |
} |
| 110 |
|
| 111 |
func (s *CommonSuite) TestNewContextWriteCloser(c *C) { |
| 112 |
buf := NewWriteCloser(bytes.NewBuffer(nil), &closer{}) |
| 113 |
ctx, close := context.WithCancel(context.Background()) |
| 114 |
|
| 115 |
w := NewContextWriteCloser(ctx, buf) |
| 116 |
|
| 117 |
n, err := w.Write([]byte("1")) |
| 118 |
c.Assert(n, Equals, 1) |
| 119 |
c.Assert(err, IsNil) |
| 120 |
|
| 121 |
close() |
| 122 |
n, err = w.Write([]byte("1")) |
| 123 |
c.Assert(n, Equals, 0) |
| 124 |
c.Assert(err, NotNil) |
| 125 |
|
| 126 |
c.Assert(w.Close(), IsNil) |
| 127 |
} |
| 128 |
|
| 129 |
func (s *CommonSuite) TestNewWriteCloserOnError(c *C) { |
| 130 |
buf := NewWriteCloser(bytes.NewBuffer(nil), &closer{}) |
| 131 |
|
| 132 |
ctx, close := context.WithCancel(context.Background()) |
| 133 |
|
| 134 |
var called error |
| 135 |
w := NewWriteCloserOnError(NewContextWriteCloser(ctx, buf), func(err error) { |
| 136 |
called = err |
| 137 |
}) |
| 138 |
|
| 139 |
close() |
| 140 |
w.Write(nil) |
| 141 |
|
| 142 |
c.Assert(called, NotNil) |
| 143 |
} |
| 144 |
|
| 145 |
func (s *CommonSuite) TestNewReadCloserOnError(c *C) { |
| 146 |
buf := NewReadCloser(bytes.NewBuffer(nil), &closer{}) |
| 147 |
ctx, close := context.WithCancel(context.Background()) |
| 148 |
|
| 149 |
var called error |
| 150 |
w := NewReadCloserOnError(NewContextReadCloser(ctx, buf), func(err error) { |
| 151 |
called = err |
| 152 |
}) |
| 153 |
|
| 154 |
close() |
| 155 |
w.Read(nil) |
| 156 |
|
| 157 |
c.Assert(called, NotNil) |
| 158 |
} |
| 159 |
func ExampleCheckClose() { |
| 160 |
// CheckClose is commonly used with named return values |
| 161 |
f := func() (err error) { |
| 162 |
// Get a io.ReadCloser |
| 163 |
r := io.NopCloser(strings.NewReader("foo")) |
| 164 |
|
| 165 |
// defer CheckClose call with an io.Closer and pointer to error |
| 166 |
defer CheckClose(r, &err) |
| 167 |
|
| 168 |
// ... work with r ... |
| 169 |
|
| 170 |
// if err is not nil, CheckClose will assign any close errors to it |
| 171 |
return err |
| 172 |
} |
| 173 |
|
| 174 |
err := f() |
| 175 |
if err != nil { |
| 176 |
panic(err) |
| 177 |
} |
| 178 |
} |