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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. func TestParsePatch(t *testing.T) {
  37. var diff = `diff --git "a/README.md" "b/README.md"
  38. --- a/README.md
  39. +++ b/README.md
  40. @@ -1,3 +1,6 @@
  41. # gitea-github-migrator
  42. +
  43. + Build Status
  44. - Latest Release
  45. Docker Pulls
  46. + cut off
  47. + cut off`
  48. result, err := ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff))
  49. if err != nil {
  50. t.Errorf("ParsePatch failed: %s", err)
  51. }
  52. println(result)
  53. var diff2 = `diff --git "a/A \\ B" "b/A \\ B"
  54. --- "a/A \\ B"
  55. +++ "b/A \\ B"
  56. @@ -1,3 +1,6 @@
  57. # gitea-github-migrator
  58. +
  59. + Build Status
  60. - Latest Release
  61. Docker Pulls
  62. + cut off
  63. + cut off`
  64. result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff2))
  65. if err != nil {
  66. t.Errorf("ParsePatch failed: %s", err)
  67. }
  68. println(result)
  69. var diff2a = `diff --git "a/A \\ B" b/A/B
  70. --- "a/A \\ B"
  71. +++ b/A/B
  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, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff2a))
  81. if err != nil {
  82. t.Errorf("ParsePatch failed: %s", err)
  83. }
  84. println(result)
  85. var diff3 = `diff --git a/README.md b/README.md
  86. --- a/README.md
  87. +++ b/README.md
  88. @@ -1,3 +1,6 @@
  89. # gitea-github-migrator
  90. +
  91. + Build Status
  92. - Latest Release
  93. Docker Pulls
  94. + cut off
  95. + cut off`
  96. result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff3))
  97. if err != nil {
  98. t.Errorf("ParsePatch failed: %s", err)
  99. }
  100. println(result)
  101. }
  102. func setupDefaultDiff() *Diff {
  103. return &Diff{
  104. Files: []*DiffFile{
  105. {
  106. Name: "README.md",
  107. Sections: []*DiffSection{
  108. {
  109. Lines: []*DiffLine{
  110. {
  111. LeftIdx: 4,
  112. RightIdx: 4,
  113. },
  114. },
  115. },
  116. },
  117. },
  118. },
  119. }
  120. }
  121. func TestDiff_LoadComments(t *testing.T) {
  122. assert.NoError(t, models.PrepareTestDatabase())
  123. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
  124. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
  125. diff := setupDefaultDiff()
  126. assert.NoError(t, diff.LoadComments(issue, user))
  127. assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2)
  128. }
  129. func TestDiffLine_CanComment(t *testing.T) {
  130. assert.False(t, (&DiffLine{Type: DiffLineSection}).CanComment())
  131. assert.False(t, (&DiffLine{Type: DiffLineAdd, Comments: []*models.Comment{{Content: "bla"}}}).CanComment())
  132. assert.True(t, (&DiffLine{Type: DiffLineAdd}).CanComment())
  133. assert.True(t, (&DiffLine{Type: DiffLineDel}).CanComment())
  134. assert.True(t, (&DiffLine{Type: DiffLinePlain}).CanComment())
  135. }
  136. func TestDiffLine_GetCommentSide(t *testing.T) {
  137. assert.Equal(t, "previous", (&DiffLine{Comments: []*models.Comment{{Line: -3}}}).GetCommentSide())
  138. assert.Equal(t, "proposed", (&DiffLine{Comments: []*models.Comment{{Line: 3}}}).GetCommentSide())
  139. }
  140. func TestGetDiffRangeWithWhitespaceBehavior(t *testing.T) {
  141. git.Debug = true
  142. for _, behavior := range []string{"-w", "--ignore-space-at-eol", "-b", ""} {
  143. diffs, err := GetDiffRangeWithWhitespaceBehavior("./testdata/academic-module", "559c156f8e0178b71cb44355428f24001b08fc68", "bd7063cc7c04689c4d082183d32a604ed27a24f9",
  144. setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles, behavior)
  145. assert.NoError(t, err, fmt.Sprintf("Error when diff with %s", behavior))
  146. for _, f := range diffs.Files {
  147. assert.True(t, len(f.Sections) > 0, fmt.Sprintf("%s should have sections", f.Name))
  148. }
  149. }
  150. }