-
Notifications
You must be signed in to change notification settings - Fork 10
/
object_reader_test.go
76 lines (57 loc) · 1.37 KB
/
object_reader_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
package gitobj
import (
"bytes"
"compress/zlib"
"errors"
"io"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
)
func TestObjectReaderReadsHeaders(t *testing.T) {
var compressed bytes.Buffer
zw := zlib.NewWriter(&compressed)
zw.Write([]byte("blob 1\x00"))
zw.Close()
or, err := NewObjectReader(&compressed)
assert.Nil(t, err)
typ, size, err := or.Header()
assert.Nil(t, err)
assert.EqualValues(t, 1, size)
assert.Equal(t, BlobObjectType, typ)
}
func TestObjectReaderConsumesHeaderBeforeReads(t *testing.T) {
var compressed bytes.Buffer
zw := zlib.NewWriter(&compressed)
zw.Write([]byte("blob 1\x00asdf"))
zw.Close()
or, err := NewObjectReader(&compressed)
assert.Nil(t, err)
var buf [4]byte
n, err := or.Read(buf[:])
assert.Equal(t, 4, n)
assert.Equal(t, []byte{'a', 's', 'd', 'f'}, buf[:])
assert.Nil(t, err)
}
type ReadCloserFn struct {
io.Reader
closeFn func() error
}
func (r *ReadCloserFn) Close() error {
return r.closeFn()
}
func TestObjectReaderCallsClose(t *testing.T) {
var calls uint32
expected := errors.New("expected")
or, err := NewObjectReadCloser(&ReadCloserFn{
Reader: bytes.NewBuffer([]byte{0x78, 0x01}),
closeFn: func() error {
atomic.AddUint32(&calls, 1)
return expected
},
})
assert.Nil(t, err)
got := or.Close()
assert.Equal(t, expected, got)
assert.EqualValues(t, 1, atomic.LoadUint32(&calls))
}