History

Move git diff codes from models to services/gitdiff (#7889)

* move git diff codes from models to services/gitdiff * fix template * fix test * fix template

Lunny Xiao committed 7 years ago

Commit c03d75f1 parent b660a7315 files · +164 −111Browse files

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

Unified · Split

services/gitdiff/gitdiff_test.go+197−0
@@ -0,0 +1,197 @@
1+// Copyright 2014 The Gogs Authors. All rights reserved.
2+// Copyright 2019 The Gitea Authors. All rights reserved.
3+// Use of this source code is governed by a MIT-style
4+// license that can be found in the LICENSE file.
5+
6+package gitdiff
7+
8+import (
9+ "html/template"
10+ "strings"
11+ "testing"
12+
13+ "code.gitea.io/gitea/models"
14+ "code.gitea.io/gitea/modules/setting"
15+
16+ dmp "github.com/sergi/go-diff/diffmatchpatch"
17+ "github.com/stretchr/testify/assert"
18+)
19+
20+func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
21+ if s1 != string(s2) {
22+ t.Errorf("%s should be equal %s", s2, s1)
23+ }
24+}
25+
26+func TestDiffToHTML(t *testing.T) {
27+ assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{
28+ {Type: dmp.DiffEqual, Text: "foo "},
29+ {Type: dmp.DiffInsert, Text: "bar"},
30+ {Type: dmp.DiffDelete, Text: " baz"},
31+ {Type: dmp.DiffEqual, Text: " biz"},
32+ }, DiffLineAdd))
33+
34+ assertEqual(t, "foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{
35+ {Type: dmp.DiffEqual, Text: "foo "},
36+ {Type: dmp.DiffDelete, Text: "bar"},
37+ {Type: dmp.DiffInsert, Text: " baz"},
38+ {Type: dmp.DiffEqual, Text: " biz"},
39+ }, DiffLineDel))
40+}
41+
42+const exampleDiff = `diff --git a/README.md b/README.md
43+--- a/README.md
44++++ b/README.md
45+@@ -1,3 +1,6 @@
46+ # gitea-github-migrator
47++
48++ Build Status
49+- Latest Release
50+ Docker Pulls
51++ cut off
52++ cut off`
53+
54+func TestCutDiffAroundLine(t *testing.T) {
55+ result := CutDiffAroundLine(strings.NewReader(exampleDiff), 4, false, 3)
56+ resultByLine := strings.Split(result, "\n")
57+ assert.Len(t, resultByLine, 7)
58+ // Check if headers got transferred
59+ assert.Equal(t, "diff --git a/README.md b/README.md", resultByLine[0])
60+ assert.Equal(t, "--- a/README.md", resultByLine[1])
61+ assert.Equal(t, "+++ b/README.md", resultByLine[2])
62+ // Check if hunk header is calculated correctly
63+ assert.Equal(t, "@@ -2,2 +3,2 @@", resultByLine[3])
64+ // Check if line got transferred
65+ assert.Equal(t, "+ Build Status", resultByLine[4])
66+
67+ // Must be same result as before since old line 3 == new line 5
68+ newResult := CutDiffAroundLine(strings.NewReader(exampleDiff), 3, true, 3)
69+ assert.Equal(t, result, newResult, "Must be same result as before since old line 3 == new line 5")
70+
71+ newResult = CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 300)
72+ assert.Equal(t, exampleDiff, newResult)
73+
74+ emptyResult := CutDiffAroundLine(strings.NewReader(exampleDiff), 6, false, 0)
75+ assert.Empty(t, emptyResult)
76+
77+ // Line is out of scope
78+ emptyResult = CutDiffAroundLine(strings.NewReader(exampleDiff), 434, false, 0)
79+ assert.Empty(t, emptyResult)
80+}
81+
82+func BenchmarkCutDiffAroundLine(b *testing.B) {
83+ for n := 0; n < b.N; n++ {
84+ CutDiffAroundLine(strings.NewReader(exampleDiff), 3, true, 3)
85+ }
86+}
87+
88+func ExampleCutDiffAroundLine() {
89+ const diff = `diff --git a/README.md b/README.md
90+--- a/README.md
91++++ b/README.md
92+@@ -1,3 +1,6 @@
93+ # gitea-github-migrator
94++
95++ Build Status
96+- Latest Release
97+ Docker Pulls
98++ cut off
99++ cut off`
100+ result := CutDiffAroundLine(strings.NewReader(diff), 4, false, 3)
101+ println(result)
102+}
103+
104+func TestParsePatch(t *testing.T) {
105+ var diff = `diff --git "a/README.md" "b/README.md"
106+--- a/README.md
107++++ b/README.md
108+@@ -1,3 +1,6 @@
109+ # gitea-github-migrator
110++
111++ Build Status
112+- Latest Release
113+ Docker Pulls
114++ cut off
115++ cut off`
116+ result, err := ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff))
117+ if err != nil {
118+ t.Errorf("ParsePatch failed: %s", err)
119+ }
120+ println(result)
121+
122+ var diff2 = `diff --git "a/A \\ B" "b/A \\ B"
123+--- "a/A \\ B"
124++++ "b/A \\ B"
125+@@ -1,3 +1,6 @@
126+ # gitea-github-migrator
127++
128++ Build Status
129+- Latest Release
130+ Docker Pulls
131++ cut off
132++ cut off`
133+ result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff2))
134+ if err != nil {
135+ t.Errorf("ParsePatch failed: %s", err)
136+ }
137+ println(result)
138+
139+ var diff3 = `diff --git a/README.md b/README.md
140+--- a/README.md
141++++ b/README.md
142+@@ -1,3 +1,6 @@
143+ # gitea-github-migrator
144++
145++ Build Status
146+- Latest Release
147+ Docker Pulls
148++ cut off
149++ cut off`
150+ result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff3))
151+ if err != nil {
152+ t.Errorf("ParsePatch failed: %s", err)
153+ }
154+ println(result)
155+}
156+
157+func setupDefaultDiff() *Diff {
158+ return &Diff{
159+ Files: []*DiffFile{
160+ {
161+ Name: "README.md",
162+ Sections: []*DiffSection{
163+ {
164+ Lines: []*DiffLine{
165+ {
166+ LeftIdx: 4,
167+ RightIdx: 4,
168+ },
169+ },
170+ },
171+ },
172+ },
173+ },
174+ }
175+}
176+func TestDiff_LoadComments(t *testing.T) {
177+ assert.NoError(t, models.PrepareTestDatabase())
178+
179+ issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
180+ user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
181+ diff := setupDefaultDiff()
182+ assert.NoError(t, diff.LoadComments(issue, user))
183+ assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2)
184+}
185+
186+func TestDiffLine_CanComment(t *testing.T) {
187+ assert.False(t, (&DiffLine{Type: DiffLineSection}).CanComment())
188+ assert.False(t, (&DiffLine{Type: DiffLineAdd, Comments: []*models.Comment{{Content: "bla"}}}).CanComment())
189+ assert.True(t, (&DiffLine{Type: DiffLineAdd}).CanComment())
190+ assert.True(t, (&DiffLine{Type: DiffLineDel}).CanComment())
191+ assert.True(t, (&DiffLine{Type: DiffLinePlain}).CanComment())
192+}
193+
194+func TestDiffLine_GetCommentSide(t *testing.T) {
195+ assert.Equal(t, "previous", (&DiffLine{Comments: []*models.Comment{{Line: -3}}}).GetCommentSide())
196+ assert.Equal(t, "proposed", (&DiffLine{Comments: []*models.Comment{{Line: 3}}}).GetCommentSide())
197+}