You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

diff_test.go 3.7 kB

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