You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gitdiff_test.go 6.1KB

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