Add tests for parsing full binary fragments

This commit is contained in:
Billy Keyes
2019-04-14 19:18:08 -07:00
parent d104a6c22a
commit 16539ad031
3 changed files with 164 additions and 7 deletions
+108
View File
@@ -193,6 +193,114 @@ func TestParseBinaryChunk(t *testing.T) {
}
}
func TestParseBinaryFragments(t *testing.T) {
tests := map[string]struct {
Input string
File File
Binary bool
Fragment *BinaryFragment
ReverseFragment *BinaryFragment
Err bool
}{
"dataWithReverse": {
Input: `GIT binary patch
literal 40
gcmZQzU|?i` + "`" + `U?w2V48*KJ%mKu_Kr9NxN<eH500b)lkN^Mx
literal 0
HcmV?d00001
`,
Binary: true,
Fragment: &BinaryFragment{
Method: BinaryPatchLiteral,
Size: 40,
Data: fib(10, binary.BigEndian),
},
ReverseFragment: &BinaryFragment{
Method: BinaryPatchLiteral,
Size: 0,
Data: []byte{},
},
},
"dataWithoutReverse": {
Input: `GIT binary patch
literal 40
gcmZQzU|?i` + "`" + `U?w2V48*KJ%mKu_Kr9NxN<eH500b)lkN^Mx
`,
Binary: true,
Fragment: &BinaryFragment{
Method: BinaryPatchLiteral,
Size: 40,
Data: fib(10, binary.BigEndian),
},
},
"noData": {
Input: "Binary files differ\n",
Binary: true,
},
"text": {
Input: `@@ -1 +1 @@
-old line
+new line
`,
Binary: false,
},
"missingData": {
Input: "GIT binary patch\n",
Err: true,
},
"invalidData": {
Input: `GIT binary patch
literal 20
TcmZQzU|?i'U?w2V48*Je09XJG
`,
Err: true,
},
"invalidReverseData": {
Input: `GIT binary patch
literal 20
TcmZQzU|?i` + "`" + `U?w2V48*Je09XJG
literal 0
zcmV?d00001
`,
Err: true,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
p := newTestParser(test.Input, true)
file := test.File
_, err := p.ParseBinaryFragments(&file)
if test.Err {
if err == nil || err == io.EOF {
t.Fatalf("expected error parsing binary fragments, but got %v", err)
}
return
}
if err != nil {
t.Fatalf("unexpected error parsing binary fragments: %v", err)
}
if test.Binary != file.IsBinary {
t.Errorf("incorrect binary state: expected %t, actual %t", test.Binary, file.IsBinary)
}
if !reflect.DeepEqual(test.Fragment, file.BinaryFragment) {
t.Errorf("incorrect binary fragment\nexpected: %+v\n actual: %+v", test.Fragment, file.BinaryFragment)
}
if !reflect.DeepEqual(test.ReverseFragment, file.ReverseBinaryFragment) {
t.Errorf("incorrect reverse binary fragment\nexpected: %+v\n actual: %+v", test.ReverseFragment, file.ReverseBinaryFragment)
}
})
}
}
func fib(n int, ord binary.ByteOrder) []byte {
buf := make([]byte, 4*n)
for i := 0; i < len(buf); i += 4 {
+40 -7
View File
@@ -2,6 +2,7 @@ package gitdiff
import (
"bufio"
"encoding/binary"
"encoding/json"
"io"
"os"
@@ -280,7 +281,7 @@ a wild fragment appears?
}
func TestParse(t *testing.T) {
expectedFragments := []*TextFragment{
textFragments := []*TextFragment{
{
OldPosition: 3,
OldLines: 6,
@@ -321,7 +322,7 @@ func TestParse(t *testing.T) {
},
}
expectedPreamble := `commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe
textPreamble := `commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe
Author: Morton Haypenny <mhaypenny@example.com>
Date: Tue Apr 2 22:55:40 2019 -0700
@@ -331,6 +332,13 @@ Date: Tue Apr 2 22:55:40 2019 -0700
`
binaryPreamble := `commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe
Author: Morton Haypenny <mhaypenny@example.com>
Date: Tue Apr 2 22:55:40 2019 -0700
A binary file with the first 10 fibonacci numbers.
`
tests := map[string]struct {
InputFile string
Output []*File
@@ -346,10 +354,10 @@ Date: Tue Apr 2 22:55:40 2019 -0700
OldMode: os.FileMode(0100644),
OldOIDPrefix: "ebe9fa54",
NewOIDPrefix: "fe103e1d",
TextFragments: expectedFragments,
TextFragments: textFragments,
},
},
Preamble: expectedPreamble,
Preamble: textPreamble,
},
"twoFiles": {
InputFile: "testdata/two_files.patch",
@@ -360,7 +368,7 @@ Date: Tue Apr 2 22:55:40 2019 -0700
OldMode: os.FileMode(0100644),
OldOIDPrefix: "ebe9fa54",
NewOIDPrefix: "fe103e1d",
TextFragments: expectedFragments,
TextFragments: textFragments,
},
{
OldName: "dir/file2.txt",
@@ -368,10 +376,35 @@ Date: Tue Apr 2 22:55:40 2019 -0700
OldMode: os.FileMode(0100644),
OldOIDPrefix: "417ebc70",
NewOIDPrefix: "67514b7f",
TextFragments: expectedFragments,
TextFragments: textFragments,
},
},
Preamble: expectedPreamble,
Preamble: textPreamble,
},
"newBinaryFile": {
InputFile: "testdata/new_binary_file.patch",
Output: []*File{
{
OldName: "",
NewName: "dir/ten.bin",
NewMode: os.FileMode(0100644),
OldOIDPrefix: "0000000000000000000000000000000000000000",
NewOIDPrefix: "77b068ba48c356156944ea714740d0d5ca07bfec",
IsNew: true,
IsBinary: true,
BinaryFragment: &BinaryFragment{
Method: BinaryPatchLiteral,
Size: 40,
Data: fib(10, binary.BigEndian),
},
ReverseBinaryFragment: &BinaryFragment{
Method: BinaryPatchLiteral,
Size: 0,
Data: []byte{},
},
},
},
Preamble: binaryPreamble,
},
}
+16
View File
@@ -0,0 +1,16 @@
commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe
Author: Morton Haypenny <mhaypenny@example.com>
Date: Tue Apr 2 22:55:40 2019 -0700
A binary file with the first 10 fibonacci numbers.
diff --git a/dir/ten.bin b/dir/ten.bin
new file mode 100644
index 0000000000000000000000000000000000000000..77b068ba48c356156944ea714740d0d5ca07bfec
GIT binary patch
literal 40
gcmZQzU|?i`U?w2V48*KJ%mKu_Kr9NxN<eH500b)lkN^Mx
literal 0
HcmV?d00001