Skip to content

Commit c50036a

Browse files
committed
Abstract common file loading in apply tests
1 parent 79aa2a9 commit c50036a

15 files changed

+96
-81
lines changed

gitdiff/apply_test.go

Lines changed: 96 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -11,79 +11,74 @@ import (
1111

1212
func TestTextFragmentApplyStrict(t *testing.T) {
1313
tests := map[string]struct {
14-
File string
15-
SrcFile string
16-
PatchFile string
17-
DstFile string
18-
19-
Err error
14+
Files applyFiles
15+
Err error
2016
}{
21-
"createFile": {File: "text_fragment_new"},
22-
"deleteFile": {File: "text_fragment_delete_all"},
17+
"createFile": {Files: getApplyFiles("text_fragment_new")},
18+
"deleteFile": {Files: getApplyFiles("text_fragment_delete_all")},
2319

24-
"addStart": {File: "text_fragment_add_start"},
25-
"addMiddle": {File: "text_fragment_add_middle"},
26-
"addEnd": {File: "text_fragment_add_end"},
27-
"addEndNoEOL": {File: "text_fragment_add_end_noeol"},
20+
"addStart": {Files: getApplyFiles("text_fragment_add_start")},
21+
"addMiddle": {Files: getApplyFiles("text_fragment_add_middle")},
22+
"addEnd": {Files: getApplyFiles("text_fragment_add_end")},
23+
"addEndNoEOL": {Files: getApplyFiles("text_fragment_add_end_noeol")},
2824

29-
"changeStart": {File: "text_fragment_change_start"},
30-
"changeMiddle": {File: "text_fragment_change_middle"},
31-
"changeEnd": {File: "text_fragment_change_end"},
32-
"changeExact": {File: "text_fragment_change_exact"},
33-
"changeSingleNoEOL": {File: "text_fragment_change_single_noeol"},
25+
"changeStart": {Files: getApplyFiles("text_fragment_change_start")},
26+
"changeMiddle": {Files: getApplyFiles("text_fragment_change_middle")},
27+
"changeEnd": {Files: getApplyFiles("text_fragment_change_end")},
28+
"changeExact": {Files: getApplyFiles("text_fragment_change_exact")},
29+
"changeSingleNoEOL": {Files: getApplyFiles("text_fragment_change_single_noeol")},
3430

3531
"errorShortSrcBefore": {
36-
SrcFile: "text_fragment_error",
37-
PatchFile: "text_fragment_error_short_src_before",
38-
Err: io.ErrUnexpectedEOF,
32+
Files: applyFiles{
33+
Src: "text_fragment_error.src",
34+
Patch: "text_fragment_error_short_src_before.patch",
35+
},
36+
Err: io.ErrUnexpectedEOF,
3937
},
4038
"errorShortSrc": {
41-
SrcFile: "text_fragment_error",
42-
PatchFile: "text_fragment_error_short_src",
43-
Err: io.ErrUnexpectedEOF,
39+
Files: applyFiles{
40+
Src: "text_fragment_error.src",
41+
Patch: "text_fragment_error_short_src.patch",
42+
},
43+
Err: io.ErrUnexpectedEOF,
4444
},
4545
"errorContextConflict": {
46-
SrcFile: "text_fragment_error",
47-
PatchFile: "text_fragment_error_context_conflict",
48-
Err: &Conflict{},
46+
Files: applyFiles{
47+
Src: "text_fragment_error.src",
48+
Patch: "text_fragment_error_context_conflict.patch",
49+
},
50+
Err: &Conflict{},
4951
},
5052
"errorDeleteConflict": {
51-
SrcFile: "text_fragment_error",
52-
PatchFile: "text_fragment_error_delete_conflict",
53-
Err: &Conflict{},
53+
Files: applyFiles{
54+
Src: "text_fragment_error.src",
55+
Patch: "text_fragment_error_delete_conflict.patch",
56+
},
57+
Err: &Conflict{},
5458
},
5559
"errorNewFile": {
56-
SrcFile: "text_fragment_error",
57-
PatchFile: "text_fragment_error_new_file",
58-
Err: &Conflict{},
60+
Files: applyFiles{
61+
Src: "text_fragment_error.src",
62+
Patch: "text_fragment_error_new_file.patch",
63+
},
64+
Err: &Conflict{},
5965
},
6066
}
6167

62-
loadFile := func(name, defaultName, ext string) []byte {
63-
if name == "" {
64-
name = defaultName
65-
}
66-
d, err := ioutil.ReadFile(filepath.Join("testdata", "apply", name+"."+ext))
67-
if err != nil {
68-
t.Fatalf("failed to read %s file: %v", ext, err)
69-
}
70-
return d
71-
}
72-
7368
for name, test := range tests {
7469
t.Run(name, func(t *testing.T) {
75-
src := loadFile(test.SrcFile, test.File, "src")
76-
patch := loadFile(test.PatchFile, test.File, "patch")
77-
78-
var result []byte
79-
if test.Err == nil {
80-
result = loadFile(test.DstFile, test.File, "dst")
81-
}
70+
src, patch, out := test.Files.Load(t)
8271

8372
files, _, err := Parse(bytes.NewReader(patch))
8473
if err != nil {
8574
t.Fatalf("failed to parse patch file: %v", err)
8675
}
76+
if len(files) != 1 {
77+
t.Fatalf("patch should contain exactly one file, but it has %d", len(files))
78+
}
79+
if len(files[0].TextFragments) != 1 {
80+
t.Fatalf("patch should contain exactly one fragment, but it has %d", len(files[0].TextFragments))
81+
}
8782

8883
frag := files[0].TextFragments[0]
8984

@@ -102,54 +97,39 @@ func TestTextFragmentApplyStrict(t *testing.T) {
10297
t.Fatalf("unexpected error applying fragment: %v", err)
10398
}
10499

105-
if !bytes.Equal(result, dst.Bytes()) {
106-
t.Errorf("incorrect result after apply\nexpected:\n%s\nactual:\n%s", result, dst.Bytes())
100+
if !bytes.Equal(out, dst.Bytes()) {
101+
t.Errorf("incorrect result after apply\nexpected:\n%s\nactual:\n%s", out, dst.Bytes())
107102
}
108103
})
109104
}
110105
}
111106

112107
func TestBinaryFragmentApply(t *testing.T) {
113108
tests := map[string]struct {
114-
File string
115-
SrcFile string
116-
PatchFile string
117-
DstFile string
118-
119-
Err error
109+
Files applyFiles
110+
Err error
120111
}{
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
112+
"literalCreate": {Files: getApplyFiles("bin_fragment_literal_create")},
113+
"literalModify": {Files: getApplyFiles("bin_fragment_literal_modify")},
114+
"deltaModify": {Files: getApplyFiles("bin_fragment_delta_modify")},
135115
}
136116

137117
for name, test := range tests {
138118
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-
}
119+
src, patch, out := test.Files.Load(t)
146120

147121
files, _, err := Parse(bytes.NewReader(patch))
148122
if err != nil {
149123
t.Fatalf("failed to parse patch file: %v", err)
150124
}
125+
if len(files) != 1 {
126+
t.Fatalf("patch should contain exactly one file, but it has %d", len(files))
127+
}
151128

152129
frag := files[0].BinaryFragment
130+
if frag == nil {
131+
t.Fatalf("patch should contain a binary fragment, but it was nil")
132+
}
153133

154134
var dst bytes.Buffer
155135
err = frag.Apply(&dst, bytes.NewReader(src))
@@ -166,9 +146,44 @@ func TestBinaryFragmentApply(t *testing.T) {
166146
t.Fatalf("unexpected error applying fragment: %v", err)
167147
}
168148

169-
if !bytes.Equal(result, dst.Bytes()) {
170-
t.Errorf("incorrect result after apply\nexpected:\n%x\nactual:\n%x", result, dst.Bytes())
149+
if !bytes.Equal(out, dst.Bytes()) {
150+
t.Errorf("incorrect result after apply\nexpected:\n%x\nactual:\n%x", out, dst.Bytes())
171151
}
172152
})
173153
}
174154
}
155+
156+
type applyFiles struct {
157+
Src string
158+
Patch string
159+
Out string
160+
}
161+
162+
func getApplyFiles(name string) applyFiles {
163+
return applyFiles{
164+
Src: name + ".src",
165+
Patch: name + ".patch",
166+
Out: name + ".out",
167+
}
168+
}
169+
170+
func (f applyFiles) Load(t *testing.T) (src []byte, patch []byte, out []byte) {
171+
load := func(name, kind string) []byte {
172+
d, err := ioutil.ReadFile(filepath.Join("testdata", "apply", name))
173+
if err != nil {
174+
t.Fatalf("failed to read %s file: %v", kind, err)
175+
}
176+
return d
177+
}
178+
179+
if f.Src != "" {
180+
src = load(f.Src, "source")
181+
}
182+
if f.Patch != "" {
183+
patch = load(f.Patch, "patch")
184+
}
185+
if f.Out != "" {
186+
out = load(f.Out, "output")
187+
}
188+
return
189+
}

0 commit comments

Comments
 (0)