本站源代码
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

79 lines
2.4KB

  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. "testing"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/test"
  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. expectedBranchName := "user2-patch-1"
  45. branchName := GetUniquePatchBranchName(ctx)
  46. assert.Equal(t, expectedBranchName, branchName)
  47. }
  48. func TestGetClosestParentWithFiles(t *testing.T) {
  49. models.PrepareTestEnv(t)
  50. ctx := test.MockContext(t, "user2/repo1")
  51. ctx.SetParams(":id", "1")
  52. test.LoadRepo(t, ctx, 1)
  53. test.LoadRepoCommit(t, ctx)
  54. test.LoadUser(t, ctx, 2)
  55. test.LoadGitRepo(t, ctx)
  56. repo := ctx.Repo.Repository
  57. branch := repo.DefaultBranch
  58. gitRepo, _ := git.OpenRepository(repo.RepoPath())
  59. commit, _ := gitRepo.GetBranchCommit(branch)
  60. expectedTreePath := ""
  61. expectedTreePath = "" // Should return the root dir, empty string, since there are no subdirs in this repo
  62. for _, deletedFile := range []string{
  63. "dir1/dir2/dir3/file.txt",
  64. "file.txt",
  65. } {
  66. treePath := GetClosestParentWithFiles(deletedFile, commit)
  67. assert.Equal(t, expectedTreePath, treePath)
  68. }
  69. }
上海开阖软件有限公司 沪ICP备12045867号-1