History

Fix yet another bug with diff file names (#12771)

Following further testing it has become apparent that the diff line cannot be used to determine filenames for diffs with any sort of predictability the answer therefore is to use the other lines that are provided with a diff Fix #12768

zeripath committed 6 years ago

Commit 96969dd1 parent 1fbc50f3 files · +247 −51Browse files

Showing the changes to services/gitdiff/gitdiff_test.go (1 of 3 files). Show all

Unified · Split

services/gitdiff/gitdiff_test.go+141−4
@@ -6,6 +6,7 @@ · 5 unmodified lines
66 package gitdiff
77
88 import (
9+ "encoding/json"
910 "fmt"
1011 "html/template"
1112 "strings"
@@ -14,11 +15,9 @@ import ( · 2 unmodified lines
1415 "code.gitea.io/gitea/models"
1516 "code.gitea.io/gitea/modules/git"
1617 "code.gitea.io/gitea/modules/setting"
17−
18− "gopkg.in/ini.v1"
19−
2018 dmp "github.com/sergi/go-diff/diffmatchpatch"
2119 "github.com/stretchr/testify/assert"
20+ "gopkg.in/ini.v1"
2221 )
2322
2423 func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
@@ -77,7 +76,145 @@ func TestDiffToHTML(t *testing.T) { · 52 unmodified lines
7776 }, DiffLineAdd))
7877 }
7978
80−func TestParsePatch(t *testing.T) {
79+func TestParsePatch_singlefile(t *testing.T) {
80+ type testcase struct {
81+ name string
82+ gitdiff string
83+ wantErr bool
84+ addition int
85+ deletion int
86+ oldFilename string
87+ filename string
88+ }
89+
90+ tests := []testcase{
91+ {
92+ name: "readme.md2readme.md",
93+ gitdiff: `diff --git "a/README.md" "b/README.md"
94+--- a/README.md
95++++ b/README.md
96+@@ -1,3 +1,6 @@
97+ # gitea-github-migrator
98++
99++ Build Status
100+- Latest Release
101+ Docker Pulls
102++ cut off
103++ cut off
104+`,
105+ addition: 4,
106+ deletion: 1,
107+ filename: "README.md",
108+ },
109+ {
110+ name: "A \\ B",
111+ gitdiff: `diff --git "a/A \\ B" "b/A \\ B"
112+--- "a/A \\ B"
113++++ "b/A \\ B"
114+@@ -1,3 +1,6 @@
115+ # gitea-github-migrator
116++
117++ Build Status
118+- Latest Release
119+ Docker Pulls
120++ cut off
121++ cut off`,
122+ addition: 4,
123+ deletion: 1,
124+ filename: "A \\ B",
125+ },
126+ {
127+ name: "really weird filename",
128+ gitdiff: `diff --git a/a b/file b/a a/file b/a b/file b/a a/file
129+index d2186f1..f5c8ed2 100644
130+--- a/a b/file b/a a/file
131++++ b/a b/file b/a a/file
132+@@ -1,3 +1,2 @@
133+ Create a weird file.
134+
135+-and what does diff do here?
136+\ No newline at end of file`,
137+ addition: 0,
138+ deletion: 1,
139+ filename: "a b/file b/a a/file",
140+ oldFilename: "a b/file b/a a/file",
141+ },
142+ {
143+ name: "delete file with blanks",
144+ gitdiff: `diff --git a/file with blanks b/file with blanks
145+deleted file mode 100644
146+index 898651a..0000000
147+--- a/file with blanks
148++++ /dev/null
149+@@ -1,5 +0,0 @@
150+-a blank file
151+-
152+-has a couple o line
153+-
154+-the 5th line is the last
155+`,
156+ addition: 0,
157+ deletion: 5,
158+ filename: "file with blanks",
159+ },
160+ {
161+ name: "rename a—as",
162+ gitdiff: `diff --git "a/\360\243\220\265b\342\200\240vs" "b/a\342\200\224as"
163+similarity index 100%
164+rename from "\360\243\220\265b\342\200\240vs"
165+rename to "a\342\200\224as"
166+`,
167+ addition: 0,
168+ deletion: 0,
169+ oldFilename: "𣐵b†vs",
170+ filename: "a—as",
171+ },
172+ {
173+ name: "rename with spaces",
174+ gitdiff: `diff --git a/a b/file b/a a/file b/a b/a a/file b/b file
175+similarity index 100%
176+rename from a b/file b/a a/file
177+rename to a b/a a/file b/b file
178+`,
179+ oldFilename: "a b/file b/a a/file",
180+ filename: "a b/a a/file b/b file",
181+ },
182+ }
183+
184+ for _, testcase := range tests {
185+ t.Run(testcase.name, func(t *testing.T) {
186+ got, err := ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(testcase.gitdiff))
187+ if (err != nil) != testcase.wantErr {
188+ t.Errorf("ParsePatch() error = %v, wantErr %v", err, testcase.wantErr)
189+ return
190+ }
191+ gotMarshaled, _ := json.MarshalIndent(got, " ", " ")
192+ if got.NumFiles != 1 {
193+ t.Errorf("ParsePath() did not receive 1 file:\n%s", string(gotMarshaled))
194+ return
195+ }
196+ if got.TotalAddition != testcase.addition {
197+ t.Errorf("ParsePath() does not have correct totalAddition %d, wanted %d", got.TotalAddition, testcase.addition)
198+ }
199+ if got.TotalDeletion != testcase.deletion {
200+ t.Errorf("ParsePath() did not have correct totalDeletion %d, wanted %d", got.TotalDeletion, testcase.deletion)
201+ }
202+ file := got.Files[0]
203+ if file.Addition != testcase.addition {
204+ t.Errorf("ParsePath() does not have correct file addition %d, wanted %d", file.Addition, testcase.addition)
205+ }
206+ if file.Deletion != testcase.deletion {
207+ t.Errorf("ParsePath() did not have correct file deletion %d, wanted %d", file.Deletion, testcase.deletion)
208+ }
209+ if file.OldName != testcase.oldFilename {
210+ t.Errorf("ParsePath() did not have correct OldName %s, wanted %s", file.OldName, testcase.oldFilename)
211+ }
212+ if file.Name != testcase.filename {
213+ t.Errorf("ParsePath() did not have correct Name %s, wanted %s", file.Name, testcase.filename)
214+ }
215+ })
216+ }
217+
81218 var diff = `diff --git "a/README.md" "b/README.md"
82219 --- a/README.md
83220 +++ b/README.md