-
Notifications
You must be signed in to change notification settings - Fork 657
Expand file tree
/
Copy pathio_test.go
More file actions
50 lines (45 loc) · 999 Bytes
/
io_test.go
File metadata and controls
50 lines (45 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package utilities
import (
"errors"
"io"
"testing"
"github.com/stretchr/testify/require"
)
type closerFunc func() error
func (f closerFunc) Close() error { return f() }
func TestSafeClose(t *testing.T) {
tests := []struct {
name string
closer io.Closer
closerErr error
}{
{
name: "happy path: Close returns nil",
closerErr: nil,
},
{
name: "Close returns error: SafeClose must not panic",
closerErr: errors.New("close failed"),
},
{
name: "io.NopCloser: SafeClose must not panic",
closer: io.NopCloser(nil),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
closer := tt.closer
if closer == nil {
called := false
closer = closerFunc(func() error {
called = true
return tt.closerErr
})
require.NotPanics(t, func() { SafeClose(closer) })
require.True(t, called, "Close should have been invoked")
return
}
require.NotPanics(t, func() { SafeClose(closer) })
})
}
}