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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "gopkg.in/ini.v1"
  15. dmp "github.com/sergi/go-diff/diffmatchpatch"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
  19. if s1 != string(s2) {
  20. t.Errorf("%s should be equal %s", s2, s1)
  21. }
  22. }
  23. func TestDiffToHTML(t *testing.T) {
  24. setting.Cfg = ini.Empty()
  25. assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML("", []dmp.Diff{
  26. {Type: dmp.DiffEqual, Text: "foo "},
  27. {Type: dmp.DiffInsert, Text: "bar"},
  28. {Type: dmp.DiffDelete, Text: " baz"},
  29. {Type: dmp.DiffEqual, Text: " biz"},
  30. }, DiffLineAdd))
  31. assertEqual(t, "foo <span class=\"removed-code\">bar</span> biz", diffToHTML("", []dmp.Diff{
  32. {Type: dmp.DiffEqual, Text: "foo "},
  33. {Type: dmp.DiffDelete, Text: "bar"},
  34. {Type: dmp.DiffInsert, Text: " baz"},
  35. {Type: dmp.DiffEqual, Text: " biz"},
  36. }, DiffLineDel))
  37. }
  38. func TestParsePatch(t *testing.T) {
  39. var diff = `diff --git "a/README.md" "b/README.md"
  40. --- a/README.md
  41. +++ b/README.md
  42. @@ -1,3 +1,6 @@
  43. # gitea-github-migrator
  44. +
  45. + Build Status
  46. - Latest Release
  47. Docker Pulls
  48. + cut off
  49. + cut off`
  50. result, err := ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff))
  51. if err != nil {
  52. t.Errorf("ParsePatch failed: %s", err)
  53. }
  54. println(result)
  55. var diff2 = `diff --git "a/A \\ B" "b/A \\ B"
  56. --- "a/A \\ B"
  57. +++ "b/A \\ B"
  58. @@ -1,3 +1,6 @@
  59. # gitea-github-migrator
  60. +
  61. + Build Status
  62. - Latest Release
  63. Docker Pulls
  64. + cut off
  65. + cut off`
  66. result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff2))
  67. if err != nil {
  68. t.Errorf("ParsePatch failed: %s", err)
  69. }
  70. println(result)
  71. var diff3 = `diff --git a/README.md b/README.md
  72. --- a/README.md
  73. +++ b/README.md
  74. @@ -1,3 +1,6 @@
  75. # gitea-github-migrator
  76. +
  77. + Build Status
  78. - Latest Release
  79. Docker Pulls
  80. + cut off
  81. + cut off`
  82. result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff3))
  83. if err != nil {
  84. t.Errorf("ParsePatch failed: %s", err)
  85. }
  86. println(result)
  87. }
  88. func setupDefaultDiff() *Diff {
  89. return &Diff{
  90. Files: []*DiffFile{
  91. {
  92. Name: "README.md",
  93. Sections: []*DiffSection{
  94. {
  95. Lines: []*DiffLine{
  96. {
  97. LeftIdx: 4,
  98. RightIdx: 4,
  99. },
  100. },
  101. },
  102. },
  103. },
  104. },
  105. }
  106. }
  107. func TestDiff_LoadComments(t *testing.T) {
  108. assert.NoError(t, models.PrepareTestDatabase())
  109. issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
  110. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
  111. diff := setupDefaultDiff()
  112. assert.NoError(t, diff.LoadComments(issue, user))
  113. assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2)
  114. }
  115. func TestDiffLine_CanComment(t *testing.T) {
  116. assert.False(t, (&DiffLine{Type: DiffLineSection}).CanComment())
  117. assert.False(t, (&DiffLine{Type: DiffLineAdd, Comments: []*models.Comment{{Content: "bla"}}}).CanComment())
  118. assert.True(t, (&DiffLine{Type: DiffLineAdd}).CanComment())
  119. assert.True(t, (&DiffLine{Type: DiffLineDel}).CanComment())
  120. assert.True(t, (&DiffLine{Type: DiffLinePlain}).CanComment())
  121. }
  122. func TestDiffLine_GetCommentSide(t *testing.T) {
  123. assert.Equal(t, "previous", (&DiffLine{Comments: []*models.Comment{{Line: -3}}}).GetCommentSide())
  124. assert.Equal(t, "proposed", (&DiffLine{Comments: []*models.Comment{{Line: 3}}}).GetCommentSide())
  125. }
  126. func TestGetDiffRangeWithWhitespaceBehavior(t *testing.T) {
  127. git.Debug = true
  128. for _, behavior := range []string{"-w", "--ignore-space-at-eol", "-b", ""} {
  129. diffs, err := GetDiffRangeWithWhitespaceBehavior("./testdata/academic-module", "559c156f8e0178b71cb44355428f24001b08fc68", "bd7063cc7c04689c4d082183d32a604ed27a24f9",
  130. setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles, behavior)
  131. assert.NoError(t, err, fmt.Sprintf("Error when diff with %s", behavior))
  132. for _, f := range diffs.Files {
  133. assert.True(t, len(f.Sections) > 0, fmt.Sprintf("%s should have sections", f.Name))
  134. }
  135. }
  136. }