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

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