this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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.

+132 -26
+64 -26
gitdiff/apply_test.go
··· 4 4 "bytes" 5 5 "io/ioutil" 6 6 "path/filepath" 7 + "strings" 7 8 "testing" 8 9 ) 9 10 10 11 func TestTextFragmentApplyStrict(t *testing.T) { 11 12 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 14 19 }{ 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"}, 17 27 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"}, 22 33 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 28 70 } 29 71 30 72 for name, test := range tests { 31 73 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") 33 76 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") 45 80 } 46 81 47 82 files, _, err := Parse(bytes.NewReader(patch)) ··· 53 88 54 89 var dst bytes.Buffer 55 90 err = frag.ApplyStrict(&dst, NewLineReader(bytes.NewReader(src), 0)) 56 - if test.Err { 91 + if test.Err != "" { 57 92 if err == nil { 58 93 t.Fatalf("expected error applying fragment, but got nil") 94 + } 95 + if !strings.Contains(err.Error(), test.Err) { 96 + t.Fatalf("incorrect apply error: expected %q, actual %q", test.Err, err.Error()) 59 97 } 60 98 return 61 99 }
+13
gitdiff/testdata/apply/text_fragment_error.src
··· 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
+12
gitdiff/testdata/apply/text_fragment_error_context_conflict.patch
··· 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
+12
gitdiff/testdata/apply/text_fragment_error_delete_conflict.patch
··· 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
+7
gitdiff/testdata/apply/text_fragment_error_new_file.patch
··· 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
+12
gitdiff/testdata/apply/text_fragment_error_short_src.patch
··· 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
+12
gitdiff/testdata/apply/text_fragment_error_short_src_before.patch
··· 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