本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

145 lines
3.6KB

  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. branch := ctx.Repo.Repository.DefaultBranch
  21. treePath := "README.md"
  22. content := "# repo1\n\nDescription for repo1\nthis is a new line"
  23. expectedDiff := &gitdiff.Diff{
  24. TotalAddition: 2,
  25. TotalDeletion: 1,
  26. Files: []*gitdiff.DiffFile{
  27. {
  28. Name: "README.md",
  29. OldName: "README.md",
  30. Index: 1,
  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. Name: "",
  43. Lines: []*gitdiff.DiffLine{
  44. {
  45. LeftIdx: 0,
  46. RightIdx: 0,
  47. Type: 4,
  48. Content: "@@ -1,3 +1,4 @@",
  49. Comments: nil,
  50. },
  51. {
  52. LeftIdx: 1,
  53. RightIdx: 1,
  54. Type: 1,
  55. Content: " # repo1",
  56. Comments: nil,
  57. },
  58. {
  59. LeftIdx: 2,
  60. RightIdx: 2,
  61. Type: 1,
  62. Content: " ",
  63. Comments: nil,
  64. },
  65. {
  66. LeftIdx: 3,
  67. RightIdx: 0,
  68. Type: 3,
  69. Content: "-Description for repo1",
  70. Comments: nil,
  71. },
  72. {
  73. LeftIdx: 0,
  74. RightIdx: 3,
  75. Type: 2,
  76. Content: "+Description for repo1",
  77. Comments: nil,
  78. },
  79. {
  80. LeftIdx: 0,
  81. RightIdx: 4,
  82. Type: 2,
  83. Content: "+this is a new line",
  84. Comments: nil,
  85. },
  86. },
  87. },
  88. },
  89. IsIncomplete: false,
  90. },
  91. },
  92. IsIncomplete: false,
  93. }
  94. t.Run("with given branch", func(t *testing.T) {
  95. diff, err := GetDiffPreview(ctx.Repo.Repository, branch, treePath, content)
  96. assert.Nil(t, err)
  97. assert.EqualValues(t, expectedDiff, diff)
  98. })
  99. t.Run("empty branch, same results", func(t *testing.T) {
  100. diff, err := GetDiffPreview(ctx.Repo.Repository, "", treePath, content)
  101. assert.Nil(t, err)
  102. assert.EqualValues(t, expectedDiff, diff)
  103. })
  104. }
  105. func TestGetDiffPreviewErrors(t *testing.T) {
  106. models.PrepareTestEnv(t)
  107. ctx := test.MockContext(t, "user2/repo1")
  108. ctx.SetParams(":id", "1")
  109. test.LoadRepo(t, ctx, 1)
  110. test.LoadRepoCommit(t, ctx)
  111. test.LoadUser(t, ctx, 2)
  112. test.LoadGitRepo(t, ctx)
  113. branch := ctx.Repo.Repository.DefaultBranch
  114. treePath := "README.md"
  115. content := "# repo1\n\nDescription for repo1\nthis is a new line"
  116. t.Run("empty repo", func(t *testing.T) {
  117. diff, err := GetDiffPreview(&models.Repository{}, branch, treePath, content)
  118. assert.Nil(t, diff)
  119. assert.EqualError(t, err, "repository does not exist [id: 0, uid: 0, owner_name: , name: ]")
  120. })
  121. t.Run("bad branch", func(t *testing.T) {
  122. badBranch := "bad_branch"
  123. diff, err := GetDiffPreview(ctx.Repo.Repository, badBranch, treePath, content)
  124. assert.Nil(t, diff)
  125. assert.EqualError(t, err, "branch does not exist [name: "+badBranch+"]")
  126. })
  127. t.Run("empty treePath", func(t *testing.T) {
  128. diff, err := GetDiffPreview(ctx.Repo.Repository, branch, "", content)
  129. assert.Nil(t, diff)
  130. assert.EqualError(t, err, "path is invalid [path: ]")
  131. })
  132. }
上海开阖软件有限公司 沪ICP备12045867号-1