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.

git_diff_test.go 5.3KB

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