fork of go-gitdiff with jj support

Abstract common file loading in apply tests

+96 -81
gitdiff/apply_test.go
··· 11 11 12 12 func TestTextFragmentApplyStrict(t *testing.T) { 13 13 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 20 16 }{ 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")}, 23 19 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")}, 28 24 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")}, 34 30 35 31 "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, 39 37 }, 40 38 "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, 44 44 }, 45 45 "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{}, 49 51 }, 50 52 "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{}, 54 58 }, 55 59 "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{}, 59 65 }, 60 66 } 61 67 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 - 73 68 for name, test := range tests { 74 69 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) 82 71 83 72 files, _, err := Parse(bytes.NewReader(patch)) 84 73 if err != nil { 85 74 t.Fatalf("failed to parse patch file: %v", err) 86 75 } 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 + } 87 82 88 83 frag := files[0].TextFragments[0] 89 84 ··· 102 97 t.Fatalf("unexpected error applying fragment: %v", err) 103 98 } 104 99 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()) 107 102 } 108 103 }) 109 104 } ··· 111 106 112 107 func TestBinaryFragmentApply(t *testing.T) { 113 108 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 120 111 }{ 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")}, 135 115 } 136 116 137 117 for name, test := range tests { 138 118 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) 146 120 147 121 files, _, err := Parse(bytes.NewReader(patch)) 148 122 if err != nil { 149 123 t.Fatalf("failed to parse patch file: %v", err) 150 124 } 125 + if len(files) != 1 { 126 + t.Fatalf("patch should contain exactly one file, but it has %d", len(files)) 127 + } 151 128 152 129 frag := files[0].BinaryFragment 130 + if frag == nil { 131 + t.Fatalf("patch should contain a binary fragment, but it was nil") 132 + } 153 133 154 134 var dst bytes.Buffer 155 135 err = frag.Apply(&dst, bytes.NewReader(src)) ··· 166 146 t.Fatalf("unexpected error applying fragment: %v", err) 167 147 } 168 148 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()) 171 151 } 172 152 }) 173 153 } 174 154 } 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 + }
gitdiff/testdata/apply/bin_fragment_delta_modify.dst gitdiff/testdata/apply/bin_fragment_delta_modify.out
gitdiff/testdata/apply/bin_fragment_literal_create.dst gitdiff/testdata/apply/bin_fragment_literal_create.out
gitdiff/testdata/apply/bin_fragment_literal_modify.dst gitdiff/testdata/apply/bin_fragment_literal_modify.out
gitdiff/testdata/apply/text_fragment_add_end.dst gitdiff/testdata/apply/text_fragment_add_end.out
gitdiff/testdata/apply/text_fragment_add_end_noeol.dst gitdiff/testdata/apply/text_fragment_add_end_noeol.out
gitdiff/testdata/apply/text_fragment_add_middle.dst gitdiff/testdata/apply/text_fragment_add_middle.out
gitdiff/testdata/apply/text_fragment_add_start.dst gitdiff/testdata/apply/text_fragment_add_start.out
gitdiff/testdata/apply/text_fragment_change_end.dst gitdiff/testdata/apply/text_fragment_change_end.out
gitdiff/testdata/apply/text_fragment_change_exact.dst gitdiff/testdata/apply/text_fragment_change_exact.out
gitdiff/testdata/apply/text_fragment_change_middle.dst gitdiff/testdata/apply/text_fragment_change_middle.out
gitdiff/testdata/apply/text_fragment_change_single_noeol.dst gitdiff/testdata/apply/text_fragment_change_single_noeol.out
gitdiff/testdata/apply/text_fragment_change_start.dst gitdiff/testdata/apply/text_fragment_change_start.out
gitdiff/testdata/apply/text_fragment_delete_all.dst gitdiff/testdata/apply/text_fragment_delete_all.out
gitdiff/testdata/apply/text_fragment_new.dst gitdiff/testdata/apply/text_fragment_new.out