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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "github.com/stretchr/testify/assert"
  10. )
  11. func TestGetDiffPreview(t *testing.T) {
  12. models.PrepareTestEnv(t)
  13. ctx := test.MockContext(t, "user2/repo1")
  14. ctx.SetParams(":id", "1")
  15. test.LoadRepo(t, ctx, 1)
  16. test.LoadRepoCommit(t, ctx)
  17. test.LoadUser(t, ctx, 2)
  18. test.LoadGitRepo(t, ctx)
  19. branch := ctx.Repo.Repository.DefaultBranch
  20. treePath := "README.md"
  21. content := "# repo1\n\nDescription for repo1\nthis is a new line"
  22. expectedDiff := &models.Diff{
  23. TotalAddition: 2,
  24. TotalDeletion: 1,
  25. Files: []*models.DiffFile{
  26. {
  27. Name: "README.md",
  28. OldName: "README.md",
  29. Index: 1,
  30. Addition: 2,
  31. Deletion: 1,
  32. Type: 2,
  33. IsCreated: false,
  34. IsDeleted: false,
  35. IsBin: false,
  36. IsLFSFile: false,
  37. IsRenamed: false,
  38. IsSubmodule: false,
  39. Sections: []*models.DiffSection{
  40. {
  41. Name: "",
  42. Lines: []*models.DiffLine{
  43. {
  44. LeftIdx: 0,
  45. RightIdx: 0,
  46. Type: 4,
  47. Content: "@@ -1,3 +1,4 @@",
  48. Comments: nil,
  49. },
  50. {
  51. LeftIdx: 1,
  52. RightIdx: 1,
  53. Type: 1,
  54. Content: " # repo1",
  55. Comments: nil,
  56. },
  57. {
  58. LeftIdx: 2,
  59. RightIdx: 2,
  60. Type: 1,
  61. Content: " ",
  62. Comments: nil,
  63. },
  64. {
  65. LeftIdx: 3,
  66. RightIdx: 0,
  67. Type: 3,
  68. Content: "-Description for repo1",
  69. Comments: nil,
  70. },
  71. {
  72. LeftIdx: 0,
  73. RightIdx: 3,
  74. Type: 2,
  75. Content: "+Description for repo1",
  76. Comments: nil,
  77. },
  78. {
  79. LeftIdx: 0,
  80. RightIdx: 4,
  81. Type: 2,
  82. Content: "+this is a new line",
  83. Comments: nil,
  84. },
  85. },
  86. },
  87. },
  88. IsIncomplete: false,
  89. },
  90. },
  91. IsIncomplete: false,
  92. }
  93. t.Run("with given branch", func(t *testing.T) {
  94. diff, err := GetDiffPreview(ctx.Repo.Repository, branch, treePath, content)
  95. assert.Nil(t, err)
  96. assert.EqualValues(t, expectedDiff, diff)
  97. })
  98. t.Run("empty branch, same results", func(t *testing.T) {
  99. diff, err := GetDiffPreview(ctx.Repo.Repository, "", treePath, content)
  100. assert.Nil(t, err)
  101. assert.EqualValues(t, expectedDiff, diff)
  102. })
  103. }
  104. func TestGetDiffPreviewErrors(t *testing.T) {
  105. models.PrepareTestEnv(t)
  106. ctx := test.MockContext(t, "user2/repo1")
  107. ctx.SetParams(":id", "1")
  108. test.LoadRepo(t, ctx, 1)
  109. test.LoadRepoCommit(t, ctx)
  110. test.LoadUser(t, ctx, 2)
  111. test.LoadGitRepo(t, ctx)
  112. branch := ctx.Repo.Repository.DefaultBranch
  113. treePath := "README.md"
  114. content := "# repo1\n\nDescription for repo1\nthis is a new line"
  115. t.Run("empty repo", func(t *testing.T) {
  116. diff, err := GetDiffPreview(&models.Repository{}, branch, treePath, content)
  117. assert.Nil(t, diff)
  118. assert.EqualError(t, err, "repository does not exist [id: 0, uid: 0, owner_name: , name: ]")
  119. })
  120. t.Run("bad branch", func(t *testing.T) {
  121. badBranch := "bad_branch"
  122. diff, err := GetDiffPreview(ctx.Repo.Repository, badBranch, treePath, content)
  123. assert.Nil(t, diff)
  124. assert.EqualError(t, err, "branch does not exist [name: "+badBranch+"]")
  125. })
  126. t.Run("empty treePath", func(t *testing.T) {
  127. diff, err := GetDiffPreview(ctx.Repo.Repository, branch, "", content)
  128. assert.Nil(t, diff)
  129. assert.EqualError(t, err, "path is invalid [path: ]")
  130. })
  131. }