diff --git a/gitdiff/parser_binary_test.go b/gitdiff/parser_binary_test.go index 3662389..a31a0e0 100644 --- a/gitdiff/parser_binary_test.go +++ b/gitdiff/parser_binary_test.go @@ -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 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 +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, }, } diff --git a/gitdiff/testdata/new_binary_file.patch b/gitdiff/testdata/new_binary_file.patch new file mode 100644 index 0000000..e9ad45d --- /dev/null +++ b/gitdiff/testdata/new_binary_file.patch @@ -0,0 +1,16 @@ +commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe +Author: Morton Haypenny +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