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.2KB

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