-
Notifications
You must be signed in to change notification settings - Fork 0
/
fedup_internal_test.go
95 lines (86 loc) · 2.08 KB
/
fedup_internal_test.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package fedup
import (
"errors"
"fmt"
"io"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
const (
// b3sum of testdata/sample.
goodB3Sum = "05c9ecf478842d332ce3f4a860f4f92f2df040ca5af670b931b007cc2e537862"
)
var (
errRead = errors.New("this is a mock Read failure")
errClose = errors.New("this is a mock Close failure")
)
type badReader struct{}
func (*badReader) Read([]byte) (int, error) {
return 0, fmt.Errorf("%w", errRead)
}
func (*badReader) Close() error {
return fmt.Errorf("%w", errClose)
}
func Test_computeB3Sum(t *testing.T) {
t.Parallel()
computeB3SumTests := []struct {
description string
shouldErr bool
filename string
reader io.Reader
expected string
errMsg string
}{
{
description: "should work on typical files",
filename: "testdata/sample",
expected: goodB3Sum,
},
{
description: "should fail when cannot read from file",
shouldErr: true,
errMsg: "this is a mock Read failure",
},
}
for _, test := range computeB3SumTests {
test := test
t.Run(test.description, func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
var err error
if test.filename != "" {
test.reader, err = os.Open(test.filename)
if err != nil {
t.Errorf("Unable to open %s", test.filename)
}
} else {
test.reader = &badReader{}
}
sum, err := computeB3Sum(test.reader)
if test.shouldErr {
assert.EqualError(err, test.errMsg)
} else {
assert.NoError(err)
assert.Equal(test.expected, sum)
}
})
}
}
func Test_computeB3SumFromFile(t *testing.T) {
t.Parallel()
t.Run("should not fail if given a valid file", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
output, err := computeB3SumFromFile("testdata/sample")
assert.Equal(goodB3Sum, output)
assert.NoError(err)
})
t.Run("should fail if given a bad file", func(t *testing.T) {
t.Parallel()
assert := assert.New(t)
output, err := computeB3SumFromFile("testdata/no_such_file")
assert.Equal("", output)
assert.EqualError(err, "open testdata/no_such_file: no such file or directory")
})
}