1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
package gitdiff
import (
"bytes"
"errors"
"io"
"io/ioutil"
"os/exec"
"path/filepath"
"testing"
)
func TestApplierInvariants(t *testing.T) {
binary := &BinaryFragment{
Method: BinaryPatchLiteral,
Size: 2,
Data: []byte("\xbe\xef"),
}
text := &TextFragment{
NewPosition: 1,
NewLines: 1,
LinesAdded: 1,
Lines: []Line{
{Op: OpAdd, Line: "new line\n"},
},
}
file := &File{
TextFragments: []*TextFragment{text},
}
src := bytes.NewReader(nil)
dst := ioutil.Discard
assertInProgress := func(t *testing.T, kind string, err error) {
if !errors.Is(err, errApplyInProgress) {
t.Fatalf("expected in-progress error for %s apply, but got: %v", kind, err)
}
}
t.Run("binaryFirst", func(t *testing.T) {
a := NewApplier(src)
if err := a.ApplyBinaryFragment(dst, binary); err != nil {
t.Fatalf("unexpected error applying fragment: %v", err)
}
assertInProgress(t, "text", a.ApplyTextFragment(dst, text))
assertInProgress(t, "binary", a.ApplyBinaryFragment(dst, binary))
assertInProgress(t, "file", a.ApplyFile(dst, file))
})
t.Run("textFirst", func(t *testing.T) {
a := NewApplier(src)
if err := a.ApplyTextFragment(dst, text); err != nil {
t.Fatalf("unexpected error applying fragment: %v", err)
}
// additional text fragments are allowed
if err := a.ApplyTextFragment(dst, text); err != nil {
t.Fatalf("unexpected error applying second fragment: %v", err)
}
assertInProgress(t, "binary", a.ApplyBinaryFragment(dst, binary))
assertInProgress(t, "file", a.ApplyFile(dst, file))
})
t.Run("fileFirst", func(t *testing.T) {
a := NewApplier(src)
if err := a.ApplyFile(dst, file); err != nil {
t.Fatalf("unexpected error applying file: %v", err)
}
assertInProgress(t, "text", a.ApplyTextFragment(dst, text))
assertInProgress(t, "binary", a.ApplyBinaryFragment(dst, binary))
assertInProgress(t, "file", a.ApplyFile(dst, file))
})
}
func TestApplyTextFragment(t *testing.T) {
tests := map[string]applyTest{
"createFile": {Files: getApplyFiles("text_fragment_new")},
"deleteFile": {Files: getApplyFiles("text_fragment_delete_all")},
"addStart": {Files: getApplyFiles("text_fragment_add_start")},
"addMiddle": {Files: getApplyFiles("text_fragment_add_middle")},
"addEnd": {Files: getApplyFiles("text_fragment_add_end")},
"addEndNoEOL": {Files: getApplyFiles("text_fragment_add_end_noeol")},
"changeStart": {Files: getApplyFiles("text_fragment_change_start")},
"changeMiddle": {Files: getApplyFiles("text_fragment_change_middle")},
"changeEnd": {Files: getApplyFiles("text_fragment_change_end")},
"changeExact": {Files: getApplyFiles("text_fragment_change_exact")},
"changeSingleNoEOL": {Files: getApplyFiles("text_fragment_change_single_noeol")},
"errorShortSrcBefore": {
Files: applyFiles{
Src: "text_fragment_error.src",
Patch: "text_fragment_error_short_src_before.patch",
},
Err: io.ErrUnexpectedEOF,
},
"errorShortSrc": {
Files: applyFiles{
Src: "text_fragment_error.src",
Patch: "text_fragment_error_short_src.patch",
},
Err: io.ErrUnexpectedEOF,
},
"errorContextConflict": {
Files: applyFiles{
Src: "text_fragment_error.src",
Patch: "text_fragment_error_context_conflict.patch",
},
Err: &Conflict{},
},
"errorDeleteConflict": {
Files: applyFiles{
Src: "text_fragment_error.src",
Patch: "text_fragment_error_delete_conflict.patch",
},
Err: &Conflict{},
},
"errorNewFile": {
Files: applyFiles{
Src: "text_fragment_error.src",
Patch: "text_fragment_error_new_file.patch",
},
Err: &Conflict{},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
test.run(t, func(w io.Writer, applier *Applier, file *File) error {
if len(file.TextFragments) != 1 {
t.Fatalf("patch should contain exactly one fragment, but it has %d", len(file.TextFragments))
}
return applier.ApplyTextFragment(w, file.TextFragments[0])
})
})
}
}
func TestApplyBinaryFragment(t *testing.T) {
tests := map[string]applyTest{
"literalCreate": {Files: getApplyFiles("bin_fragment_literal_create")},
"literalModify": {Files: getApplyFiles("bin_fragment_literal_modify")},
"deltaModify": {Files: getApplyFiles("bin_fragment_delta_modify")},
"deltaModifyLarge": {Files: getApplyFiles("bin_fragment_delta_modify_large")},
"errorIncompleteAdd": {
Files: applyFiles{
Src: "bin_fragment_delta_error.src",
Patch: "bin_fragment_delta_error_incomplete_add.patch",
},
Err: "incomplete add",
},
"errorIncompleteCopy": {
Files: applyFiles{
Src: "bin_fragment_delta_error.src",
Patch: "bin_fragment_delta_error_incomplete_copy.patch",
},
Err: "incomplete copy",
},
"errorSrcSize": {
Files: applyFiles{
Src: "bin_fragment_delta_error.src",
Patch: "bin_fragment_delta_error_src_size.patch",
},
Err: &Conflict{},
},
"errorDstSize": {
Files: applyFiles{
Src: "bin_fragment_delta_error.src",
Patch: "bin_fragment_delta_error_dst_size.patch",
},
Err: "insufficient or extra data",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
test.run(t, func(w io.Writer, applier *Applier, file *File) error {
return applier.ApplyBinaryFragment(w, file.BinaryFragment)
})
})
}
}
func TestApplyFile(t *testing.T) {
tests := map[string]applyTest{
"textModify": {
Files: applyFiles{
Src: "file_text.src",
Patch: "file_text_modify.patch",
Out: "file_text_modify.out",
},
},
"textDelete": {
Files: applyFiles{
Src: "file_text.src",
Patch: "file_text_delete.patch",
Out: "file_text_delete.out",
},
},
"textErrorPartialDelete": {
Files: applyFiles{
Src: "file_text.src",
Patch: "file_text_error_partial_delete.patch",
},
Err: &Conflict{},
},
"binaryModify": {
Files: getApplyFiles("file_bin_modify"),
},
"modeChange": {
Files: getApplyFiles("file_mode_change"),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
test.run(t, func(w io.Writer, applier *Applier, file *File) error {
return applier.ApplyFile(w, file)
})
})
}
}
type applyTest struct {
Files applyFiles
Err interface{}
}
func (at applyTest) run(t *testing.T, apply func(io.Writer, *Applier, *File) error) {
src, patch, out := at.Files.Load(t)
cmd := exec.Command("echo", "hello")
fileChan, err := Parse(cmd, io.NopCloser(bytes.NewReader(patch)))
if err != nil {
t.Fatalf("failed to parse patch file: %v", err)
}
var files []*File
for file := range fileChan {
files = append(files, file)
}
if len(files) != 1 {
t.Fatalf("patch should contain exactly one file, but it has %d", len(files))
}
applier := NewApplier(bytes.NewReader(src))
var dst bytes.Buffer
err = apply(&dst, applier, files[0])
if at.Err != nil {
assertError(t, at.Err, err, "applying fragment")
return
}
if err != nil {
var aerr *ApplyError
if errors.As(err, &aerr) {
t.Fatalf("unexpected error applying: at %d: fragment %d at %d: %v", aerr.Line, aerr.Fragment, aerr.FragmentLine, err)
} else {
t.Fatalf("unexpected error applying: %v", err)
}
}
if !bytes.Equal(out, dst.Bytes()) {
t.Errorf("incorrect result after apply\nexpected:\n%q\nactual:\n%q", out, dst.Bytes())
}
}
type applyFiles struct {
Src string
Patch string
Out string
}
func getApplyFiles(name string) applyFiles {
return applyFiles{
Src: name + ".src",
Patch: name + ".patch",
Out: name + ".out",
}
}
func (f applyFiles) Load(t *testing.T) (src []byte, patch []byte, out []byte) {
load := func(name, kind string) []byte {
d, err := ioutil.ReadFile(filepath.Join("testdata", "apply", name))
if err != nil {
t.Fatalf("failed to read %s file: %v", kind, err)
}
return d
}
if f.Src != "" {
src = load(f.Src, "source")
}
if f.Patch != "" {
patch = load(f.Patch, "patch")
}
if f.Out != "" {
out = load(f.Out, "output")
}
return
}
|