Skip to content

Commit 79aa2a9

Browse files
committed
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.
1 parent a1d2195 commit 79aa2a9

11 files changed

+97
-1
lines changed

gitdiff/apply.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,11 @@ func (f *BinaryFragment) Apply(dst io.Writer, src io.Reader) error {
228228
if err := applyBinaryDeltaFragment(dst, fullSrc, f.Data); err != nil {
229229
return applyError(err)
230230
}
231+
default:
232+
return applyError(fmt.Errorf("unsupported binary patch method: %v", f.Method))
231233
}
232-
return applyError(fmt.Errorf("unsupported binary patch method: %v", f.Method))
234+
235+
return nil
233236
}
234237

235238
func applyBinaryDeltaFragment(dst io.Writer, src, frag []byte) error {

gitdiff/apply_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,67 @@ func TestTextFragmentApplyStrict(t *testing.T) {
108108
})
109109
}
110110
}
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+
}
1.06 KB
Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
1 KB
Binary file not shown.
32 Bytes
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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

Whitespace-only changes.
32 Bytes
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+

0 commit comments

Comments
 (0)