Skip to content

Commit 16539ad

Browse files
committed
Add tests for parsing full binary fragments
1 parent d104a6c commit 16539ad

File tree

3 files changed

+164
-7
lines changed

3 files changed

+164
-7
lines changed

gitdiff/parser_binary_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,114 @@ func TestParseBinaryChunk(t *testing.T) {
193193
}
194194
}
195195

196+
func TestParseBinaryFragments(t *testing.T) {
197+
tests := map[string]struct {
198+
Input string
199+
File File
200+
201+
Binary bool
202+
Fragment *BinaryFragment
203+
ReverseFragment *BinaryFragment
204+
Err bool
205+
}{
206+
"dataWithReverse": {
207+
Input: `GIT binary patch
208+
literal 40
209+
gcmZQzU|?i` + "`" + `U?w2V48*KJ%mKu_Kr9NxN<eH500b)lkN^Mx
210+
211+
literal 0
212+
HcmV?d00001
213+
214+
`,
215+
Binary: true,
216+
Fragment: &BinaryFragment{
217+
Method: BinaryPatchLiteral,
218+
Size: 40,
219+
Data: fib(10, binary.BigEndian),
220+
},
221+
ReverseFragment: &BinaryFragment{
222+
Method: BinaryPatchLiteral,
223+
Size: 0,
224+
Data: []byte{},
225+
},
226+
},
227+
"dataWithoutReverse": {
228+
Input: `GIT binary patch
229+
literal 40
230+
gcmZQzU|?i` + "`" + `U?w2V48*KJ%mKu_Kr9NxN<eH500b)lkN^Mx
231+
232+
`,
233+
Binary: true,
234+
Fragment: &BinaryFragment{
235+
Method: BinaryPatchLiteral,
236+
Size: 40,
237+
Data: fib(10, binary.BigEndian),
238+
},
239+
},
240+
"noData": {
241+
Input: "Binary files differ\n",
242+
Binary: true,
243+
},
244+
"text": {
245+
Input: `@@ -1 +1 @@
246+
-old line
247+
+new line
248+
`,
249+
Binary: false,
250+
},
251+
"missingData": {
252+
Input: "GIT binary patch\n",
253+
Err: true,
254+
},
255+
"invalidData": {
256+
Input: `GIT binary patch
257+
literal 20
258+
TcmZQzU|?i'U?w2V48*Je09XJG
259+
260+
`,
261+
Err: true,
262+
},
263+
"invalidReverseData": {
264+
Input: `GIT binary patch
265+
literal 20
266+
TcmZQzU|?i` + "`" + `U?w2V48*Je09XJG
267+
268+
literal 0
269+
zcmV?d00001
270+
271+
`,
272+
Err: true,
273+
},
274+
}
275+
276+
for name, test := range tests {
277+
t.Run(name, func(t *testing.T) {
278+
p := newTestParser(test.Input, true)
279+
280+
file := test.File
281+
_, err := p.ParseBinaryFragments(&file)
282+
if test.Err {
283+
if err == nil || err == io.EOF {
284+
t.Fatalf("expected error parsing binary fragments, but got %v", err)
285+
}
286+
return
287+
}
288+
if err != nil {
289+
t.Fatalf("unexpected error parsing binary fragments: %v", err)
290+
}
291+
if test.Binary != file.IsBinary {
292+
t.Errorf("incorrect binary state: expected %t, actual %t", test.Binary, file.IsBinary)
293+
}
294+
if !reflect.DeepEqual(test.Fragment, file.BinaryFragment) {
295+
t.Errorf("incorrect binary fragment\nexpected: %+v\n actual: %+v", test.Fragment, file.BinaryFragment)
296+
}
297+
if !reflect.DeepEqual(test.ReverseFragment, file.ReverseBinaryFragment) {
298+
t.Errorf("incorrect reverse binary fragment\nexpected: %+v\n actual: %+v", test.ReverseFragment, file.ReverseBinaryFragment)
299+
}
300+
})
301+
}
302+
}
303+
196304
func fib(n int, ord binary.ByteOrder) []byte {
197305
buf := make([]byte, 4*n)
198306
for i := 0; i < len(buf); i += 4 {

gitdiff/parser_test.go

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gitdiff
22

33
import (
44
"bufio"
5+
"encoding/binary"
56
"encoding/json"
67
"io"
78
"os"
@@ -280,7 +281,7 @@ a wild fragment appears?
280281
}
281282

282283
func TestParse(t *testing.T) {
283-
expectedFragments := []*TextFragment{
284+
textFragments := []*TextFragment{
284285
{
285286
OldPosition: 3,
286287
OldLines: 6,
@@ -321,7 +322,7 @@ func TestParse(t *testing.T) {
321322
},
322323
}
323324

324-
expectedPreamble := `commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe
325+
textPreamble := `commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe
325326
Author: Morton Haypenny <mhaypenny@example.com>
326327
Date: Tue Apr 2 22:55:40 2019 -0700
327328
@@ -331,6 +332,13 @@ Date: Tue Apr 2 22:55:40 2019 -0700
331332
332333
`
333334

335+
binaryPreamble := `commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe
336+
Author: Morton Haypenny <mhaypenny@example.com>
337+
Date: Tue Apr 2 22:55:40 2019 -0700
338+
339+
A binary file with the first 10 fibonacci numbers.
340+
341+
`
334342
tests := map[string]struct {
335343
InputFile string
336344
Output []*File
@@ -346,10 +354,10 @@ Date: Tue Apr 2 22:55:40 2019 -0700
346354
OldMode: os.FileMode(0100644),
347355
OldOIDPrefix: "ebe9fa54",
348356
NewOIDPrefix: "fe103e1d",
349-
TextFragments: expectedFragments,
357+
TextFragments: textFragments,
350358
},
351359
},
352-
Preamble: expectedPreamble,
360+
Preamble: textPreamble,
353361
},
354362
"twoFiles": {
355363
InputFile: "testdata/two_files.patch",
@@ -360,18 +368,43 @@ Date: Tue Apr 2 22:55:40 2019 -0700
360368
OldMode: os.FileMode(0100644),
361369
OldOIDPrefix: "ebe9fa54",
362370
NewOIDPrefix: "fe103e1d",
363-
TextFragments: expectedFragments,
371+
TextFragments: textFragments,
364372
},
365373
{
366374
OldName: "dir/file2.txt",
367375
NewName: "dir/file2.txt",
368376
OldMode: os.FileMode(0100644),
369377
OldOIDPrefix: "417ebc70",
370378
NewOIDPrefix: "67514b7f",
371-
TextFragments: expectedFragments,
379+
TextFragments: textFragments,
380+
},
381+
},
382+
Preamble: textPreamble,
383+
},
384+
"newBinaryFile": {
385+
InputFile: "testdata/new_binary_file.patch",
386+
Output: []*File{
387+
{
388+
OldName: "",
389+
NewName: "dir/ten.bin",
390+
NewMode: os.FileMode(0100644),
391+
OldOIDPrefix: "0000000000000000000000000000000000000000",
392+
NewOIDPrefix: "77b068ba48c356156944ea714740d0d5ca07bfec",
393+
IsNew: true,
394+
IsBinary: true,
395+
BinaryFragment: &BinaryFragment{
396+
Method: BinaryPatchLiteral,
397+
Size: 40,
398+
Data: fib(10, binary.BigEndian),
399+
},
400+
ReverseBinaryFragment: &BinaryFragment{
401+
Method: BinaryPatchLiteral,
402+
Size: 0,
403+
Data: []byte{},
404+
},
372405
},
373406
},
374-
Preamble: expectedPreamble,
407+
Preamble: binaryPreamble,
375408
},
376409
}
377410

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe
2+
Author: Morton Haypenny <mhaypenny@example.com>
3+
Date: Tue Apr 2 22:55:40 2019 -0700
4+
5+
A binary file with the first 10 fibonacci numbers.
6+
7+
diff --git a/dir/ten.bin b/dir/ten.bin
8+
new file mode 100644
9+
index 0000000000000000000000000000000000000000..77b068ba48c356156944ea714740d0d5ca07bfec
10+
GIT binary patch
11+
literal 40
12+
gcmZQzU|?i`U?w2V48*KJ%mKu_Kr9NxN<eH500b)lkN^Mx
13+
14+
literal 0
15+
HcmV?d00001
16+

0 commit comments

Comments
 (0)