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.

diff_test.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repofiles
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/test"
  9. "code.gitea.io/gitea/services/gitdiff"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestGetDiffPreview(t *testing.T) {
  13. models.PrepareTestEnv(t)
  14. ctx := test.MockContext(t, "user2/repo1")
  15. ctx.SetParams(":id", "1")
  16. test.LoadRepo(t, ctx, 1)
  17. test.LoadRepoCommit(t, ctx)
  18. test.LoadUser(t, ctx, 2)
  19. test.LoadGitRepo(t, ctx)
  20. defer ctx.Repo.GitRepo.Close()
  21. branch := ctx.Repo.Repository.DefaultBranch
  22. treePath := "README.md"
  23. content := "# repo1\n\nDescription for repo1\nthis is a new line"
  24. expectedDiff := &gitdiff.Diff{
  25. TotalAddition: 2,
  26. TotalDeletion: 1,
  27. Files: []*gitdiff.DiffFile{
  28. {
  29. Name: "README.md",
  30. OldName: "README.md",
  31. Index: 1,
  32. Addition: 2,
  33. Deletion: 1,
  34. Type: 2,
  35. IsCreated: false,
  36. IsDeleted: false,
  37. IsBin: false,
  38. IsLFSFile: false,
  39. IsRenamed: false,
  40. IsSubmodule: false,
  41. Sections: []*gitdiff.DiffSection{
  42. {
  43. Name: "",
  44. Lines: []*gitdiff.DiffLine{
  45. {
  46. LeftIdx: 0,
  47. RightIdx: 0,
  48. Type: 4,
  49. Content: "@@ -1,3 +1,4 @@",
  50. Comments: nil,
  51. SectionInfo: &gitdiff.DiffLineSectionInfo{
  52. Path: "README.md",
  53. LastLeftIdx: 0,
  54. LastRightIdx: 0,
  55. LeftIdx: 1,
  56. RightIdx: 1,
  57. LeftHunkSize: 3,
  58. RightHunkSize: 4,
  59. },
  60. },
  61. {
  62. LeftIdx: 1,
  63. RightIdx: 1,
  64. Type: 1,
  65. Content: " # repo1",
  66. Comments: nil,
  67. },
  68. {
  69. LeftIdx: 2,
  70. RightIdx: 2,
  71. Type: 1,
  72. Content: " ",
  73. Comments: nil,
  74. },
  75. {
  76. LeftIdx: 3,
  77. RightIdx: 0,
  78. Type: 3,
  79. Content: "-Description for repo1",
  80. Comments: nil,
  81. },
  82. {
  83. LeftIdx: 0,
  84. RightIdx: 3,
  85. Type: 2,
  86. Content: "+Description for repo1",
  87. Comments: nil,
  88. },
  89. {
  90. LeftIdx: 0,
  91. RightIdx: 4,
  92. Type: 2,
  93. Content: "+this is a new line",
  94. Comments: nil,
  95. },
  96. },
  97. },
  98. },
  99. IsIncomplete: false,
  100. },
  101. },
  102. IsIncomplete: false,
  103. }
  104. t.Run("with given branch", func(t *testing.T) {
  105. diff, err := GetDiffPreview(ctx.Repo.Repository, branch, treePath, content)
  106. assert.Nil(t, err)
  107. assert.EqualValues(t, expectedDiff, diff)
  108. })
  109. t.Run("empty branch, same results", func(t *testing.T) {
  110. diff, err := GetDiffPreview(ctx.Repo.Repository, "", treePath, content)
  111. assert.Nil(t, err)
  112. assert.EqualValues(t, expectedDiff, diff)
  113. })
  114. }
  115. func TestGetDiffPreviewErrors(t *testing.T) {
  116. models.PrepareTestEnv(t)
  117. ctx := test.MockContext(t, "user2/repo1")
  118. ctx.SetParams(":id", "1")
  119. test.LoadRepo(t, ctx, 1)
  120. test.LoadRepoCommit(t, ctx)
  121. test.LoadUser(t, ctx, 2)
  122. test.LoadGitRepo(t, ctx)
  123. defer ctx.Repo.GitRepo.Close()
  124. branch := ctx.Repo.Repository.DefaultBranch
  125. treePath := "README.md"
  126. content := "# repo1\n\nDescription for repo1\nthis is a new line"
  127. t.Run("empty repo", func(t *testing.T) {
  128. diff, err := GetDiffPreview(&models.Repository{}, branch, treePath, content)
  129. assert.Nil(t, diff)
  130. assert.EqualError(t, err, "repository does not exist [id: 0, uid: 0, owner_name: , name: ]")
  131. })
  132. t.Run("bad branch", func(t *testing.T) {
  133. badBranch := "bad_branch"
  134. diff, err := GetDiffPreview(ctx.Repo.Repository, badBranch, treePath, content)
  135. assert.Nil(t, diff)
  136. assert.EqualError(t, err, "branch does not exist [name: "+badBranch+"]")
  137. })
  138. t.Run("empty treePath", func(t *testing.T) {
  139. diff, err := GetDiffPreview(ctx.Repo.Repository, branch, "", content)
  140. assert.Nil(t, diff)
  141. assert.EqualError(t, err, "path is invalid [path: ]")
  142. })
  143. }