@@ -3,8 +3,11 @@ package gitobj
3
3
import (
4
4
"bytes"
5
5
"compress/zlib"
6
+ "crypto/sha1"
7
+ "crypto/sha256"
6
8
"encoding/hex"
7
9
"errors"
10
+ "hash"
8
11
"io"
9
12
"io/ioutil"
10
13
"sync/atomic"
@@ -16,7 +19,7 @@ import (
16
19
func TestObjectWriterWritesHeaders (t * testing.T ) {
17
20
var buf bytes.Buffer
18
21
19
- w := NewObjectWriter (& buf )
22
+ w := NewObjectWriter (& buf , sha1 . New () )
20
23
21
24
n , err := w .WriteHeader (BlobObjectType , 1 )
22
25
assert .Equal (t , 7 , n )
@@ -35,25 +38,40 @@ func TestObjectWriterWritesHeaders(t *testing.T) {
35
38
}
36
39
37
40
func TestObjectWriterWritesData (t * testing.T ) {
38
- var buf bytes.Buffer
41
+ testCases := []struct {
42
+ h hash.Hash
43
+ sha string
44
+ }{
45
+ {
46
+ sha1 .New (), "56a6051ca2b02b04ef92d5150c9ef600403cb1de" ,
47
+ },
48
+ {
49
+ sha256 .New (), "36456d9b87f21fc54ed5babf1222a9ab0fbbd0c4ad239a7933522d5e4447049c" ,
50
+ },
51
+ }
39
52
40
- w := NewObjectWriter ( & buf )
41
- w . WriteHeader ( BlobObjectType , 1 )
53
+ for _ , test := range testCases {
54
+ var buf bytes. Buffer
42
55
43
- n , err := w .Write ([]byte {0x31 })
44
- assert .Equal (t , 1 , n )
45
- assert .Nil (t , err )
56
+ w := NewObjectWriter (& buf , test .h )
57
+ w .WriteHeader (BlobObjectType , 1 )
46
58
47
- assert .Nil (t , w .Close ())
59
+ n , err := w .Write ([]byte {0x31 })
60
+ assert .Equal (t , 1 , n )
61
+ assert .Nil (t , err )
48
62
49
- r , err := zlib .NewReader (& buf )
50
- assert .Nil (t , err )
63
+ assert .Nil (t , w .Close ())
51
64
52
- all , err := ioutil .ReadAll (r )
53
- assert .Nil (t , err )
54
- assert .Equal (t , []byte ("blob 1\x00 1" ), all )
65
+ r , err := zlib .NewReader (& buf )
66
+ assert .Nil (t , err )
55
67
56
- assert .Nil (t , r .Close ())
68
+ all , err := ioutil .ReadAll (r )
69
+ assert .Nil (t , err )
70
+ assert .Equal (t , []byte ("blob 1\x00 1" ), all )
71
+
72
+ assert .Nil (t , r .Close ())
73
+ assert .Equal (t , test .sha , hex .EncodeToString (w .Sha ()))
74
+ }
57
75
}
58
76
59
77
func TestObjectWriterPanicsOnWritesWithoutHeader (t * testing.T ) {
@@ -64,7 +82,7 @@ func TestObjectWriterPanicsOnWritesWithoutHeader(t *testing.T) {
64
82
assert .Equal (t , "gitobj: cannot write data without header" , err )
65
83
}()
66
84
67
- w := NewObjectWriter (new (bytes.Buffer ))
85
+ w := NewObjectWriter (new (bytes.Buffer ), sha1 . New () )
68
86
w .Write (nil )
69
87
}
70
88
@@ -76,19 +94,27 @@ func TestObjectWriterPanicsOnMultipleHeaderWrites(t *testing.T) {
76
94
assert .Equal (t , "gitobj: cannot write headers more than once" , err )
77
95
}()
78
96
79
- w := NewObjectWriter (new (bytes.Buffer ))
97
+ w := NewObjectWriter (new (bytes.Buffer ), sha1 . New () )
80
98
w .WriteHeader (BlobObjectType , 1 )
81
99
w .WriteHeader (TreeObjectType , 2 )
82
100
}
83
101
84
102
func TestObjectWriterKeepsTrackOfHash (t * testing.T ) {
85
- w := NewObjectWriter (new (bytes.Buffer ))
103
+ w := NewObjectWriter (new (bytes.Buffer ), sha1 . New () )
86
104
n , err := w .WriteHeader (BlobObjectType , 1 )
87
105
88
106
assert .Nil (t , err )
89
107
assert .Equal (t , 7 , n )
90
108
91
109
assert .Equal (t , "bb6ca78b66403a67c6281df142de5ef472186283" , hex .EncodeToString (w .Sha ()))
110
+
111
+ w = NewObjectWriter (new (bytes.Buffer ), sha256 .New ())
112
+ n , err = w .WriteHeader (BlobObjectType , 1 )
113
+
114
+ assert .Nil (t , err )
115
+ assert .Equal (t , 7 , n )
116
+
117
+ assert .Equal (t , "3a68c454a6eb75cc55bda147a53756f0f581497eb80b9b67156fb8a8d3931cd7" , hex .EncodeToString (w .Sha ()))
92
118
}
93
119
94
120
type WriteCloserFn struct {
@@ -109,7 +135,7 @@ func TestObjectWriterCallsClose(t *testing.T) {
109
135
atomic .AddUint32 (& calls , 1 )
110
136
return expected
111
137
},
112
- })
138
+ }, sha1 . New () )
113
139
114
140
got := w .Close ()
115
141
0 commit comments