fork of go-gitdiff with jj support

Add positive tests for BinaryFragment#Apply

Covers basic literal and delta application. Input files were created by
using dd and the 'conv=notrunc' option to modify sections of files
created from /dev/urandom. All file start with two null bytes to
convince Git they are binary.

+4 -1
gitdiff/apply.go
··· 228 228 if err := applyBinaryDeltaFragment(dst, fullSrc, f.Data); err != nil { 229 229 return applyError(err) 230 230 } 231 + default: 232 + return applyError(fmt.Errorf("unsupported binary patch method: %v", f.Method)) 231 233 } 232 - return applyError(fmt.Errorf("unsupported binary patch method: %v", f.Method)) 234 + 235 + return nil 233 236 } 234 237 235 238 func applyBinaryDeltaFragment(dst io.Writer, src, frag []byte) error {
+64
gitdiff/apply_test.go
··· 108 108 }) 109 109 } 110 110 } 111 + 112 + func TestBinaryFragmentApply(t *testing.T) { 113 + tests := map[string]struct { 114 + File string 115 + SrcFile string 116 + PatchFile string 117 + DstFile string 118 + 119 + Err error 120 + }{ 121 + "literalCreate": {File: "bin_fragment_literal_create"}, 122 + "literalModify": {File: "bin_fragment_literal_modify"}, 123 + "deltaModify": {File: "bin_fragment_delta_modify"}, 124 + } 125 + 126 + loadFile := func(name, defaultName, ext string) []byte { 127 + if name == "" { 128 + name = defaultName 129 + } 130 + d, err := ioutil.ReadFile(filepath.Join("testdata", "apply", name+"."+ext)) 131 + if err != nil { 132 + t.Fatalf("failed to read %s file: %v", ext, err) 133 + } 134 + return d 135 + } 136 + 137 + for name, test := range tests { 138 + t.Run(name, func(t *testing.T) { 139 + src := loadFile(test.SrcFile, test.File, "src") 140 + patch := loadFile(test.PatchFile, test.File, "patch") 141 + 142 + var result []byte 143 + if test.Err == nil { 144 + result = loadFile(test.DstFile, test.File, "dst") 145 + } 146 + 147 + files, _, err := Parse(bytes.NewReader(patch)) 148 + if err != nil { 149 + t.Fatalf("failed to parse patch file: %v", err) 150 + } 151 + 152 + frag := files[0].BinaryFragment 153 + 154 + var dst bytes.Buffer 155 + err = frag.Apply(&dst, bytes.NewReader(src)) 156 + if test.Err != nil { 157 + if err == nil { 158 + t.Fatalf("expected error applying fragment, but got nil") 159 + } 160 + if !errors.Is(err, test.Err) { 161 + t.Fatalf("incorrect apply error: expected: %T (%v), actual: %T (%v)", test.Err, test.Err, err, err) 162 + } 163 + return 164 + } 165 + if err != nil { 166 + t.Fatalf("unexpected error applying fragment: %v", err) 167 + } 168 + 169 + if !bytes.Equal(result, dst.Bytes()) { 170 + t.Errorf("incorrect result after apply\nexpected:\n%x\nactual:\n%x", result, dst.Bytes()) 171 + } 172 + }) 173 + } 174 + }
gitdiff/testdata/apply/bin_fragment_delta_modify.dst

This is a binary file and will not be displayed.

+13
gitdiff/testdata/apply/bin_fragment_delta_modify.patch
··· 1 + diff --git a/gitdiff/testdata/apply/bin_fragment_delta_modify.src b/gitdiff/testdata/apply/bin_fragment_delta_modify.src 2 + GIT binary patch 3 + delta 172 4 + zcmV;d08{^f2)qc8AP{I3VQ>J`s>wb0HU+h#6w8q?tUO~cHmDjZi2<8yZ9XmKhhMdo 5 + zWu(4bg|8QwzZ|1e*rL4P#)`Fen<n~ik=E?$qG6?hzJ6$u{l5W#?uwHb0q6w)00000 6 + zlLZ3%0RfW%1N%UMJ{~Z~0@X${&1Kk#98tb3==a{J7A;`O`v&<T@514_mvMTz72b#n 7 + atf$#NLoPbNe?RPFJVt1aCFGoQbiKD!OHgJ2 8 + 9 + delta 112 10 + zcmV-$0FVE?2!IHXAP~DY<7&llQfwqYA%tL<sR@xVtUMD;+4ZG>XTQ5=J2y;^BfB}4 11 + zWkisH791|vOVl5e-@^VLX0s~Ky_UyN!3;CgPr>Edj0j+0gOSwSsFsr$0q6zUJph<q 12 + SlLZ3%0XmZb1N#I__7UCuR5Dxu 13 +
gitdiff/testdata/apply/bin_fragment_delta_modify.src

This is a binary file and will not be displayed.

gitdiff/testdata/apply/bin_fragment_literal_create.dst

This is a binary file and will not be displayed.

+8
gitdiff/testdata/apply/bin_fragment_literal_create.patch
··· 1 + diff --git a/gitdiff/testdata/apply/bin_fragment_literal_create.src b/gitdiff/testdata/apply/bin_fragment_literal_create.src 2 + GIT binary patch 3 + literal 32 4 + ocmZQzU`lR_IpTEv<rOIzfy0`|3>L*`JUiBtu5<US@-j_*0M$he#sB~S 5 + 6 + literal 0 7 + HcmV?d00001 8 +
gitdiff/testdata/apply/bin_fragment_literal_create.src

This is a binary file and will not be displayed.

gitdiff/testdata/apply/bin_fragment_literal_modify.dst

This is a binary file and will not be displayed.

+8
gitdiff/testdata/apply/bin_fragment_literal_modify.patch
··· 1 + diff --git a/gitdiff/testdata/apply/bin_fragment_literal_modify.src b/gitdiff/testdata/apply/bin_fragment_literal_modify.src 2 + GIT binary patch 3 + literal 32 4 + ocmZQzU`lR_IpTEv<rOIzfy0_p?@r&O@$6Vny3XCR%F8tM0pJ`CjQ{`u 5 + 6 + literal 32 7 + ocmZQzU`lR_IpTEv<rOIzfy0`|3>L*`JUiBtu5<US@-j_*0M$he#sB~S 8 +
gitdiff/testdata/apply/bin_fragment_literal_modify.src

This is a binary file and will not be displayed.