本站源代码
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

198 lines
5.5KB

  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. "html/template"
  8. "strings"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/setting"
  12. dmp "github.com/sergi/go-diff/diffmatchpatch"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
  16. if s1 != string(s2) {
  17. t.Errorf("%s should be equal %s", s2, s1)
  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. assert.NoError(t, models.PrepareTestDatabase())
  159. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
  160. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
  161. diff := setupDefaultDiff()
  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: []*models.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: []*models.Comment{{Line: -3}}}).GetCommentSide())
  174. assert.Equal(t, "proposed", (&DiffLine{Comments: []*models.Comment{{Line: 3}}}).GetCommentSide())
  175. }
上海开阖软件有限公司 沪ICP备12045867号-1