mirror of
https://github.com/bluekeyes/go-gitdiff
synced 2026-06-08 13:18:30 +00:00
Add positive tests for TextFragment#ApplyStrict
These cover all of the cases (I think) where application should succeed.
This commit is contained in:
+14
-8
@@ -122,19 +122,22 @@ func (f *TextFragment) ApplyStrict(dst io.Writer, src LineReader) error {
|
||||
return applyError(err, lineNum(n))
|
||||
}
|
||||
|
||||
used := int64(0)
|
||||
for i, line := range f.Lines {
|
||||
fromSrc, err := applyTextLine(dst, nextLine, line)
|
||||
if err != nil {
|
||||
if err := applyTextLine(dst, nextLine, line); err != nil {
|
||||
return applyError(err, lineNum(n), fragLineNum(i))
|
||||
}
|
||||
|
||||
if fromSrc && i < len(f.Lines)-1 {
|
||||
if fromSrc(line) {
|
||||
used++
|
||||
}
|
||||
// advance reader if the next fragment line appears in src and we're behind
|
||||
if i < len(f.Lines)-1 && fromSrc(f.Lines[i+1]) && int64(n)-limit < used {
|
||||
nextLine, n, err = src.ReadLine()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
err = io.ErrUnexpectedEOF
|
||||
}
|
||||
return applyError(err, lineNum(n), fragLineNum(i+1))
|
||||
return applyError(err, lineNum(n), fragLineNum(i+1)) // report for _next_ line in fragment
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -142,12 +145,11 @@ func (f *TextFragment) ApplyStrict(dst io.Writer, src LineReader) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func applyTextLine(dst io.Writer, src string, line Line) (fromSrc bool, err error) {
|
||||
func applyTextLine(dst io.Writer, src string, line Line) (err error) {
|
||||
switch line.Op {
|
||||
case OpContext, OpDelete:
|
||||
fromSrc = true
|
||||
if src != line.Line {
|
||||
return fromSrc, conflictError("fragment line does not match src line")
|
||||
return conflictError("fragment line does not match src line")
|
||||
}
|
||||
}
|
||||
switch line.Op {
|
||||
@@ -157,6 +159,10 @@ func applyTextLine(dst io.Writer, src string, line Line) (fromSrc bool, err erro
|
||||
return
|
||||
}
|
||||
|
||||
func fromSrc(line Line) bool {
|
||||
return line.Op != OpAdd
|
||||
}
|
||||
|
||||
// copyLines copies from src to dst until the line at limit, exclusive. Returns
|
||||
// the line at limit and the line number. The line number may not equal the
|
||||
// limit if and only if a non-EOF error occurs. A negative limit means the
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package gitdiff
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTextFragmentApplyStrict(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
File string
|
||||
Err bool
|
||||
}{
|
||||
"createFile": {File: "new"},
|
||||
"deleteFile": {File: "delete_all"},
|
||||
"addStart": {File: "add_start"},
|
||||
"addMiddle": {File: "add_middle"},
|
||||
"addEnd": {File: "add_end"},
|
||||
"changeStart": {File: "change_start"},
|
||||
"changeMiddle": {File: "change_middle"},
|
||||
"changeEnd": {File: "change_end"},
|
||||
}
|
||||
|
||||
for name, test := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
base := filepath.Join("testdata", "apply", "text_fragment_"+test.File)
|
||||
|
||||
src, err := ioutil.ReadFile(base + ".src")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read source file: %v", err)
|
||||
}
|
||||
patch, err := ioutil.ReadFile(base + ".patch")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read patch file: %v", err)
|
||||
}
|
||||
result, err := ioutil.ReadFile(base + ".dst")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read result file: %v", err)
|
||||
}
|
||||
|
||||
files, _, err := Parse(bytes.NewReader(patch))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to parse patch file: %v", err)
|
||||
}
|
||||
|
||||
frag := files[0].TextFragments[0]
|
||||
|
||||
var dst bytes.Buffer
|
||||
err = frag.ApplyStrict(&dst, NewLineReader(bytes.NewReader(src), 0))
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatalf("expected error applying fragment, but got nil")
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error applying fragment: %v", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(result, dst.Bytes()) {
|
||||
t.Errorf("incorrect result after apply\nexpected:\n%s\nactual:\n%s", result, dst.Bytes())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
new line a
|
||||
new line b
|
||||
@@ -0,0 +1,9 @@
|
||||
diff --git a/gitdiff/testdata/apply/fragment_add_end.src b/gitdiff/testdata/apply/fragment_add_end.src
|
||||
--- a/gitdiff/testdata/apply/fragment_add_end.src
|
||||
+++ b/gitdiff/testdata/apply/fragment_add_end.src
|
||||
@@ -1,3 +1,5 @@
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
+new line a
|
||||
+new line b
|
||||
@@ -0,0 +1,3 @@
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
@@ -0,0 +1,5 @@
|
||||
line 1
|
||||
line 2
|
||||
new line a
|
||||
new line b
|
||||
line 3
|
||||
@@ -0,0 +1,9 @@
|
||||
diff --git a/gitdiff/testdata/apply/fragment_add_middle.src b/gitdiff/testdata/apply/fragment_add_middle.src
|
||||
--- a/gitdiff/testdata/apply/fragment_add_middle.src
|
||||
+++ b/gitdiff/testdata/apply/fragment_add_middle.src
|
||||
@@ -1,3 +1,5 @@
|
||||
line 1
|
||||
line 2
|
||||
+new line a
|
||||
+new line b
|
||||
line 3
|
||||
@@ -0,0 +1,3 @@
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
@@ -0,0 +1,4 @@
|
||||
new line a
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
@@ -0,0 +1,8 @@
|
||||
diff --git a/gitdiff/testdata/apply/fragment_add_start.src b/gitdiff/testdata/apply/fragment_add_start.src
|
||||
--- a/gitdiff/testdata/apply/fragment_add_start.src
|
||||
+++ b/gitdiff/testdata/apply/fragment_add_start.src
|
||||
@@ -1,3 +1,4 @@
|
||||
+new line a
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
@@ -0,0 +1,3 @@
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
@@ -0,0 +1,10 @@
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
line 5
|
||||
line 6
|
||||
line 7
|
||||
line 8
|
||||
line 9
|
||||
new line a
|
||||
@@ -0,0 +1,9 @@
|
||||
diff --git a/gitdiff/testdata/apply/text_fragment_change_end.src b/gitdiff/testdata/apply/text_fragment_change_end.src
|
||||
--- a/gitdiff/testdata/apply/text_fragment_change_end.src
|
||||
+++ b/gitdiff/testdata/apply/text_fragment_change_end.src
|
||||
@@ -7,4 +7,4 @@ line 6
|
||||
line 7
|
||||
line 8
|
||||
line 9
|
||||
-line 10
|
||||
+new line a
|
||||
@@ -0,0 +1,10 @@
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
line 5
|
||||
line 6
|
||||
line 7
|
||||
line 8
|
||||
line 9
|
||||
line 10
|
||||
@@ -0,0 +1,9 @@
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
line 5
|
||||
new line a
|
||||
line 7
|
||||
line 8
|
||||
line 9
|
||||
@@ -0,0 +1,12 @@
|
||||
diff --git a/gitdiff/testdata/apply/text_fragment_change_middle.src b/gitdiff/testdata/apply/text_fragment_change_middle.src
|
||||
--- a/gitdiff/testdata/apply/text_fragment_change_middle.src
|
||||
+++ b/gitdiff/testdata/apply/text_fragment_change_middle.src
|
||||
@@ -3,7 +3,7 @@ line 2
|
||||
line 3
|
||||
line 4
|
||||
line 5
|
||||
-line 6
|
||||
+new line a
|
||||
line 7
|
||||
line 8
|
||||
line 9
|
||||
@@ -0,0 +1,10 @@
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
line 5
|
||||
line 6
|
||||
line 7
|
||||
line 8
|
||||
line 9
|
||||
line 10
|
||||
@@ -0,0 +1,4 @@
|
||||
new line a
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
@@ -0,0 +1,9 @@
|
||||
diff --git a/gitdiff/testdata/apply/text_fragment_change_start.src b/gitdiff/testdata/apply/text_fragment_change_start.src
|
||||
--- a/gitdiff/testdata/apply/text_fragment_change_start.src
|
||||
+++ b/gitdiff/testdata/apply/text_fragment_change_start.src
|
||||
@@ -1,4 +1,4 @@
|
||||
-line 1
|
||||
+new line a
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
@@ -0,0 +1,10 @@
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
line 4
|
||||
line 5
|
||||
line 6
|
||||
line 7
|
||||
line 8
|
||||
line 9
|
||||
line 10
|
||||
@@ -0,0 +1,8 @@
|
||||
diff --git a/gitdiff/testdata/apply/fragment_delete_all.src b/gitdiff/testdata/apply/fragment_delete_all.src
|
||||
--- a/gitdiff/testdata/apply/fragment_delete_all.src
|
||||
+++ b/gitdiff/testdata/apply/fragment_delete_all.src
|
||||
@@ -1,4 +0,0 @@
|
||||
-line a
|
||||
-line b
|
||||
-line c
|
||||
-line d
|
||||
@@ -0,0 +1,4 @@
|
||||
line a
|
||||
line b
|
||||
line c
|
||||
line d
|
||||
@@ -0,0 +1,3 @@
|
||||
line 1
|
||||
line 2
|
||||
line 3
|
||||
@@ -0,0 +1,7 @@
|
||||
diff --git a/gitdiff/testdata/apply/fragment_new.src b/gitdiff/testdata/apply/fragment_new.src
|
||||
--- a/gitdiff/testdata/apply/fragment_new.src
|
||||
+++ b/gitdiff/testdata/apply/fragment_new.src
|
||||
@@ -0,0 +1,3 @@
|
||||
+line 1
|
||||
+line 2
|
||||
+line 3
|
||||
Reference in New Issue
Block a user