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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. FileName: "README.md",
  44. Name: "",
  45. Lines: []*gitdiff.DiffLine{
  46. {
  47. LeftIdx: 0,
  48. RightIdx: 0,
  49. Type: 4,
  50. Content: "@@ -1,3 +1,4 @@",
  51. Comments: nil,
  52. SectionInfo: &gitdiff.DiffLineSectionInfo{
  53. Path: "README.md",
  54. LastLeftIdx: 0,
  55. LastRightIdx: 0,
  56. LeftIdx: 1,
  57. RightIdx: 1,
  58. LeftHunkSize: 3,
  59. RightHunkSize: 4,
  60. },
  61. },
  62. {
  63. LeftIdx: 1,
  64. RightIdx: 1,
  65. Type: 1,
  66. Content: " # repo1",
  67. Comments: nil,
  68. },
  69. {
  70. LeftIdx: 2,
  71. RightIdx: 2,
  72. Type: 1,
  73. Content: " ",
  74. Comments: nil,
  75. },
  76. {
  77. LeftIdx: 3,
  78. RightIdx: 0,
  79. Type: 3,
  80. Content: "-Description for repo1",
  81. Comments: nil,
  82. },
  83. {
  84. LeftIdx: 0,
  85. RightIdx: 3,
  86. Type: 2,
  87. Content: "+Description for repo1",
  88. Comments: nil,
  89. },
  90. {
  91. LeftIdx: 0,
  92. RightIdx: 4,
  93. Type: 2,
  94. Content: "+this is a new line",
  95. Comments: nil,
  96. },
  97. },
  98. },
  99. },
  100. IsIncomplete: false,
  101. },
  102. },
  103. IsIncomplete: false,
  104. }
  105. expectedDiff.NumFiles = len(expectedDiff.Files)
  106. t.Run("with given branch", func(t *testing.T) {
  107. diff, err := GetDiffPreview(ctx.Repo.Repository, branch, treePath, content)
  108. assert.Nil(t, err)
  109. assert.EqualValues(t, expectedDiff, diff)
  110. })
  111. t.Run("empty branch, same results", func(t *testing.T) {
  112. diff, err := GetDiffPreview(ctx.Repo.Repository, "", treePath, content)
  113. assert.Nil(t, err)
  114. assert.EqualValues(t, expectedDiff, diff)
  115. })
  116. }
  117. func TestGetDiffPreviewErrors(t *testing.T) {
  118. models.PrepareTestEnv(t)
  119. ctx := test.MockContext(t, "user2/repo1")
  120. ctx.SetParams(":id", "1")
  121. test.LoadRepo(t, ctx, 1)
  122. test.LoadRepoCommit(t, ctx)
  123. test.LoadUser(t, ctx, 2)
  124. test.LoadGitRepo(t, ctx)
  125. defer ctx.Repo.GitRepo.Close()
  126. branch := ctx.Repo.Repository.DefaultBranch
  127. treePath := "README.md"
  128. content := "# repo1\n\nDescription for repo1\nthis is a new line"
  129. t.Run("empty repo", func(t *testing.T) {
  130. diff, err := GetDiffPreview(&models.Repository{}, branch, treePath, content)
  131. assert.Nil(t, diff)
  132. assert.EqualError(t, err, "repository does not exist [id: 0, uid: 0, owner_name: , name: ]")
  133. })
  134. t.Run("bad branch", func(t *testing.T) {
  135. badBranch := "bad_branch"
  136. diff, err := GetDiffPreview(ctx.Repo.Repository, badBranch, treePath, content)
  137. assert.Nil(t, diff)
  138. assert.EqualError(t, err, "branch does not exist [name: "+badBranch+"]")
  139. })
  140. t.Run("empty treePath", func(t *testing.T) {
  141. diff, err := GetDiffPreview(ctx.Repo.Repository, branch, "", content)
  142. assert.Nil(t, diff)
  143. assert.EqualError(t, err, "path is invalid [path: ]")
  144. })
  145. }