Skip to content

Commit 1b341e0

Browse files
committed
Add error tests for TextFragment#ApplyStrict
These cover the errors that can happen with a single fragment and no additional manipulation by the caller of ApplyStrict.
1 parent 6a055aa commit 1b341e0

7 files changed

+132
-26
lines changed

gitdiff/apply_test.go

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,79 @@ import (
44
"bytes"
55
"io/ioutil"
66
"path/filepath"
7+
"strings"
78
"testing"
89
)
910

1011
func TestTextFragmentApplyStrict(t *testing.T) {
1112
tests := map[string]struct {
12-
File string
13-
Err bool
13+
File string
14+
SrcFile string
15+
PatchFile string
16+
DstFile string
17+
18+
Err string
1419
}{
15-
"createFile": {File: "new"},
16-
"deleteFile": {File: "delete_all"},
20+
"createFile": {File: "text_fragment_new"},
21+
"deleteFile": {File: "text_fragment_delete_all"},
22+
23+
"addStart": {File: "text_fragment_add_start"},
24+
"addMiddle": {File: "text_fragment_add_middle"},
25+
"addEnd": {File: "text_fragment_add_end"},
26+
"addEndNoEOL": {File: "text_fragment_add_end_noeol"},
1727

18-
"addStart": {File: "add_start"},
19-
"addMiddle": {File: "add_middle"},
20-
"addEnd": {File: "add_end"},
21-
"addEndNoEOL": {File: "add_end_noeol"},
28+
"changeStart": {File: "text_fragment_change_start"},
29+
"changeMiddle": {File: "text_fragment_change_middle"},
30+
"changeEnd": {File: "text_fragment_change_end"},
31+
"changeExact": {File: "text_fragment_change_exact"},
32+
"changeSingleNoEOL": {File: "text_fragment_change_single_noeol"},
2233

23-
"changeStart": {File: "change_start"},
24-
"changeMiddle": {File: "change_middle"},
25-
"changeEnd": {File: "change_end"},
26-
"changeExact": {File: "change_exact"},
27-
"changeSingleNoEOL": {File: "change_single_noeol"},
34+
"errorShortSrcBefore": {
35+
SrcFile: "text_fragment_error",
36+
PatchFile: "text_fragment_error_short_src_before",
37+
Err: "unexpected EOF",
38+
},
39+
"errorShortSrc": {
40+
SrcFile: "text_fragment_error",
41+
PatchFile: "text_fragment_error_short_src",
42+
Err: "unexpected EOF",
43+
},
44+
"errorContextConflict": {
45+
SrcFile: "text_fragment_error",
46+
PatchFile: "text_fragment_error_context_conflict",
47+
Err: "conflict",
48+
},
49+
"errorDeleteConflict": {
50+
SrcFile: "text_fragment_error",
51+
PatchFile: "text_fragment_error_delete_conflict",
52+
Err: "conflict",
53+
},
54+
"errorNewFile": {
55+
SrcFile: "text_fragment_error",
56+
PatchFile: "text_fragment_error_new_file",
57+
Err: "conflict",
58+
},
59+
}
60+
61+
loadFile := func(name, defaultName, ext string) []byte {
62+
if name == "" {
63+
name = defaultName
64+
}
65+
d, err := ioutil.ReadFile(filepath.Join("testdata", "apply", name+"."+ext))
66+
if err != nil {
67+
t.Fatalf("failed to read %s file: %v", ext, err)
68+
}
69+
return d
2870
}
2971

3072
for name, test := range tests {
3173
t.Run(name, func(t *testing.T) {
32-
base := filepath.Join("testdata", "apply", "text_fragment_"+test.File)
74+
src := loadFile(test.SrcFile, test.File, "src")
75+
patch := loadFile(test.PatchFile, test.File, "patch")
3376

34-
src, err := ioutil.ReadFile(base + ".src")
35-
if err != nil {
36-
t.Fatalf("failed to read source file: %v", err)
37-
}
38-
patch, err := ioutil.ReadFile(base + ".patch")
39-
if err != nil {
40-
t.Fatalf("failed to read patch file: %v", err)
41-
}
42-
result, err := ioutil.ReadFile(base + ".dst")
43-
if err != nil {
44-
t.Fatalf("failed to read result file: %v", err)
77+
var result []byte
78+
if test.Err == "" {
79+
result = loadFile(test.DstFile, test.File, "dst")
4580
}
4681

4782
files, _, err := Parse(bytes.NewReader(patch))
@@ -53,10 +88,13 @@ func TestTextFragmentApplyStrict(t *testing.T) {
5388

5489
var dst bytes.Buffer
5590
err = frag.ApplyStrict(&dst, NewLineReader(bytes.NewReader(src), 0))
56-
if test.Err {
91+
if test.Err != "" {
5792
if err == nil {
5893
t.Fatalf("expected error applying fragment, but got nil")
5994
}
95+
if !strings.Contains(err.Error(), test.Err) {
96+
t.Fatalf("incorrect apply error: expected %q, actual %q", test.Err, err.Error())
97+
}
6098
return
6199
}
62100
if err != nil {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
line 1
2+
line 2
3+
line 3
4+
line 4
5+
line 5
6+
line 6
7+
line 7
8+
line 8
9+
line 9
10+
line 10
11+
line 11
12+
line 12
13+
line 13
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/gitdiff/testdata/apply/text_fragment_error.src b/gitdiff/testdata/apply/text_fragment_error.src
2+
--- a/gitdiff/testdata/apply/text_fragment_error.src
3+
+++ b/gitdiff/testdata/apply/text_fragment_error.src
4+
@@ -4,7 +4,7 @@ line 3
5+
line 4
6+
line 5
7+
line conflict
8+
-line 7
9+
+new line a
10+
line 8
11+
line 9
12+
line 10
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/gitdiff/testdata/apply/text_fragment_error.src b/gitdiff/testdata/apply/text_fragment_error.src
2+
--- a/gitdiff/testdata/apply/text_fragment_error.src
3+
+++ b/gitdiff/testdata/apply/text_fragment_error.src
4+
@@ -4,7 +4,7 @@ line 3
5+
line 4
6+
line 5
7+
line 6
8+
-line conflict
9+
+new line a
10+
line 8
11+
line 9
12+
line 10
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
diff --git a/gitdiff/testdata/apply/text_fragment_error.src b/gitdiff/testdata/apply/text_fragment_error.src
2+
--- a/gitdiff/testdata/apply/text_fragment_error.src
3+
+++ b/gitdiff/testdata/apply/text_fragment_error.src
4+
@@ -0,0 +1,3 @@
5+
+line 1
6+
+line 2
7+
+line 3
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/gitdiff/testdata/apply/text_fragment_error.src b/gitdiff/testdata/apply/text_fragment_error.src
2+
--- a/gitdiff/testdata/apply/text_fragment_error.src
3+
+++ b/gitdiff/testdata/apply/text_fragment_error.src
4+
@@ -9,7 +9,7 @@ line 8
5+
line 9
6+
line 10
7+
line 11
8+
-line 12
9+
+new line a
10+
line 13
11+
line 14
12+
line 15
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/gitdiff/testdata/apply/text_fragment_error.src b/gitdiff/testdata/apply/text_fragment_error.src
2+
--- a/gitdiff/testdata/apply/text_fragment_error.src
3+
+++ b/gitdiff/testdata/apply/text_fragment_error.src
4+
@@ -15,7 +15,7 @@ line 14
5+
line 15
6+
line 16
7+
line 17
8+
-line 18
9+
+new line a
10+
line 19
11+
line 20
12+
line 21

0 commit comments

Comments
 (0)