forked from git-lfs/gitobj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_writer_test.go
144 lines (108 loc) · 2.93 KB
/
object_writer_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package gitobj
import (
"bytes"
"compress/zlib"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"errors"
"hash"
"io"
"io/ioutil"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
)
func TestObjectWriterWritesHeaders(t *testing.T) {
var buf bytes.Buffer
w := NewObjectWriter(&buf, sha1.New())
n, err := w.WriteHeader(BlobObjectType, 1)
assert.Equal(t, 7, n)
assert.Nil(t, err)
assert.Nil(t, w.Close())
r, err := zlib.NewReader(&buf)
assert.Nil(t, err)
all, err := ioutil.ReadAll(r)
assert.Nil(t, err)
assert.Equal(t, []byte("blob 1\x00"), all)
assert.Nil(t, r.Close())
}
func TestObjectWriterWritesData(t *testing.T) {
testCases := []struct {
h hash.Hash
sha string
}{
{
sha1.New(), "56a6051ca2b02b04ef92d5150c9ef600403cb1de",
},
{
sha256.New(), "36456d9b87f21fc54ed5babf1222a9ab0fbbd0c4ad239a7933522d5e4447049c",
},
}
for _, test := range testCases {
var buf bytes.Buffer
w := NewObjectWriter(&buf, test.h)
w.WriteHeader(BlobObjectType, 1)
n, err := w.Write([]byte{0x31})
assert.Equal(t, 1, n)
assert.Nil(t, err)
assert.Nil(t, w.Close())
r, err := zlib.NewReader(&buf)
assert.Nil(t, err)
all, err := ioutil.ReadAll(r)
assert.Nil(t, err)
assert.Equal(t, []byte("blob 1\x001"), all)
assert.Nil(t, r.Close())
assert.Equal(t, test.sha, hex.EncodeToString(w.Sha()))
}
}
func TestObjectWriterPanicsOnWritesWithoutHeader(t *testing.T) {
defer func() {
err := recover()
assert.NotNil(t, err)
assert.Equal(t, "gitobj: cannot write data without header", err)
}()
w := NewObjectWriter(new(bytes.Buffer), sha1.New())
w.Write(nil)
}
func TestObjectWriterPanicsOnMultipleHeaderWrites(t *testing.T) {
defer func() {
err := recover()
assert.NotNil(t, err)
assert.Equal(t, "gitobj: cannot write headers more than once", err)
}()
w := NewObjectWriter(new(bytes.Buffer), sha1.New())
w.WriteHeader(BlobObjectType, 1)
w.WriteHeader(TreeObjectType, 2)
}
func TestObjectWriterKeepsTrackOfHash(t *testing.T) {
w := NewObjectWriter(new(bytes.Buffer), sha1.New())
n, err := w.WriteHeader(BlobObjectType, 1)
assert.Nil(t, err)
assert.Equal(t, 7, n)
assert.Equal(t, "bb6ca78b66403a67c6281df142de5ef472186283", hex.EncodeToString(w.Sha()))
w = NewObjectWriter(new(bytes.Buffer), sha256.New())
n, err = w.WriteHeader(BlobObjectType, 1)
assert.Nil(t, err)
assert.Equal(t, 7, n)
assert.Equal(t, "3a68c454a6eb75cc55bda147a53756f0f581497eb80b9b67156fb8a8d3931cd7", hex.EncodeToString(w.Sha()))
}
type WriteCloserFn struct {
io.Writer
closeFn func() error
}
func (r *WriteCloserFn) Close() error { return r.closeFn() }
func TestObjectWriterCallsClose(t *testing.T) {
var calls uint32
expected := errors.New("close error")
w := NewObjectWriteCloser(&WriteCloserFn{
Writer: new(bytes.Buffer),
closeFn: func() error {
atomic.AddUint32(&calls, 1)
return expected
},
}, sha1.New())
got := w.Close()
assert.EqualValues(t, 1, calls)
assert.Equal(t, expected, got)
}