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.

editor_test.go 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2018 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 repo
  5. import (
  6. "code.gitea.io/gitea/modules/git"
  7. "code.gitea.io/gitea/modules/test"
  8. "testing"
  9. "code.gitea.io/gitea/models"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestCleanUploadName(t *testing.T) {
  13. models.PrepareTestEnv(t)
  14. var kases = map[string]string{
  15. ".git/refs/master": "",
  16. "/root/abc": "root/abc",
  17. "./../../abc": "abc",
  18. "a/../.git": "",
  19. "a/../../../abc": "abc",
  20. "../../../acd": "acd",
  21. "../../.git/abc": "",
  22. "..\\..\\.git/abc": "..\\..\\.git/abc",
  23. "..\\../.git/abc": "",
  24. "..\\../.git": "",
  25. "abc/../def": "def",
  26. ".drone.yml": ".drone.yml",
  27. ".abc/def/.drone.yml": ".abc/def/.drone.yml",
  28. "..drone.yml.": "..drone.yml.",
  29. "..a.dotty...name...": "..a.dotty...name...",
  30. "..a.dotty../.folder../.name...": "..a.dotty../.folder../.name...",
  31. }
  32. for k, v := range kases {
  33. assert.EqualValues(t, cleanUploadFileName(k), v)
  34. }
  35. }
  36. func TestGetUniquePatchBranchName(t *testing.T) {
  37. models.PrepareTestEnv(t)
  38. ctx := test.MockContext(t, "user2/repo1")
  39. ctx.SetParams(":id", "1")
  40. test.LoadRepo(t, ctx, 1)
  41. test.LoadRepoCommit(t, ctx)
  42. test.LoadUser(t, ctx, 2)
  43. test.LoadGitRepo(t, ctx)
  44. defer ctx.Repo.GitRepo.Close()
  45. expectedBranchName := "user2-patch-1"
  46. branchName := GetUniquePatchBranchName(ctx)
  47. assert.Equal(t, expectedBranchName, branchName)
  48. }
  49. func TestGetClosestParentWithFiles(t *testing.T) {
  50. models.PrepareTestEnv(t)
  51. ctx := test.MockContext(t, "user2/repo1")
  52. ctx.SetParams(":id", "1")
  53. test.LoadRepo(t, ctx, 1)
  54. test.LoadRepoCommit(t, ctx)
  55. test.LoadUser(t, ctx, 2)
  56. test.LoadGitRepo(t, ctx)
  57. defer ctx.Repo.GitRepo.Close()
  58. repo := ctx.Repo.Repository
  59. branch := repo.DefaultBranch
  60. gitRepo, _ := git.OpenRepository(repo.RepoPath())
  61. defer gitRepo.Close()
  62. commit, _ := gitRepo.GetBranchCommit(branch)
  63. expectedTreePath := ""
  64. expectedTreePath = "" // Should return the root dir, empty string, since there are no subdirs in this repo
  65. for _, deletedFile := range []string{
  66. "dir1/dir2/dir3/file.txt",
  67. "file.txt",
  68. } {
  69. treePath := GetClosestParentWithFiles(deletedFile, commit)
  70. assert.Equal(t, expectedTreePath, treePath)
  71. }
  72. }