gitea源码

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