本站源代码
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

101 linhas
3.4KB

  1. // Copyright 2017 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 git
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "gopkg.in/src-d/go-git.v4/plumbing/filemode"
  9. "gopkg.in/src-d/go-git.v4/plumbing/object"
  10. )
  11. func getTestEntries() Entries {
  12. return Entries{
  13. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v1.0", Mode: filemode.Dir}},
  14. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.0", Mode: filemode.Dir}},
  15. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.1", Mode: filemode.Dir}},
  16. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.12", Mode: filemode.Dir}},
  17. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.2", Mode: filemode.Dir}},
  18. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v12.0", Mode: filemode.Dir}},
  19. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "abc", Mode: filemode.Regular}},
  20. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "bcd", Mode: filemode.Regular}},
  21. }
  22. }
  23. func TestEntriesSort(t *testing.T) {
  24. entries := getTestEntries()
  25. entries.Sort()
  26. assert.Equal(t, "v1.0", entries[0].Name())
  27. assert.Equal(t, "v12.0", entries[1].Name())
  28. assert.Equal(t, "v2.0", entries[2].Name())
  29. assert.Equal(t, "v2.1", entries[3].Name())
  30. assert.Equal(t, "v2.12", entries[4].Name())
  31. assert.Equal(t, "v2.2", entries[5].Name())
  32. assert.Equal(t, "abc", entries[6].Name())
  33. assert.Equal(t, "bcd", entries[7].Name())
  34. }
  35. func TestEntriesCustomSort(t *testing.T) {
  36. entries := getTestEntries()
  37. entries.CustomSort(func(s1, s2 string) bool {
  38. return s1 > s2
  39. })
  40. assert.Equal(t, "v2.2", entries[0].Name())
  41. assert.Equal(t, "v2.12", entries[1].Name())
  42. assert.Equal(t, "v2.1", entries[2].Name())
  43. assert.Equal(t, "v2.0", entries[3].Name())
  44. assert.Equal(t, "v12.0", entries[4].Name())
  45. assert.Equal(t, "v1.0", entries[5].Name())
  46. assert.Equal(t, "bcd", entries[6].Name())
  47. assert.Equal(t, "abc", entries[7].Name())
  48. }
  49. func TestFollowLink(t *testing.T) {
  50. r, err := OpenRepository("tests/repos/repo1_bare")
  51. assert.NoError(t, err)
  52. commit, err := r.GetCommit("37991dec2c8e592043f47155ce4808d4580f9123")
  53. assert.NoError(t, err)
  54. // get the symlink
  55. lnk, err := commit.Tree.GetTreeEntryByPath("foo/bar/link_to_hello")
  56. assert.NoError(t, err)
  57. assert.True(t, lnk.IsLink())
  58. // should be able to dereference to target
  59. target, err := lnk.FollowLink()
  60. assert.NoError(t, err)
  61. assert.Equal(t, target.Name(), "hello")
  62. assert.False(t, target.IsLink())
  63. assert.Equal(t, target.ID.String(), "b14df6442ea5a1b382985a6549b85d435376c351")
  64. // should error when called on normal file
  65. target, err = commit.Tree.GetTreeEntryByPath("file1.txt")
  66. assert.NoError(t, err)
  67. _, err = target.FollowLink()
  68. assert.Equal(t, err.Error(), "file1.txt: not a symlink")
  69. // should error for broken links
  70. target, err = commit.Tree.GetTreeEntryByPath("foo/broken_link")
  71. assert.NoError(t, err)
  72. assert.True(t, target.IsLink())
  73. _, err = target.FollowLink()
  74. assert.Equal(t, err.Error(), "broken_link: broken link")
  75. // should error for external links
  76. target, err = commit.Tree.GetTreeEntryByPath("foo/outside_repo")
  77. assert.NoError(t, err)
  78. assert.True(t, target.IsLink())
  79. _, err = target.FollowLink()
  80. assert.Equal(t, err.Error(), "outside_repo: points outside of repo")
  81. // testing fix for short link bug
  82. target, err = commit.Tree.GetTreeEntryByPath("foo/link_short")
  83. assert.NoError(t, err)
  84. _, err = target.FollowLink()
  85. assert.Equal(t, err.Error(), "link_short: broken link")
  86. }
上海开阖软件有限公司 沪ICP备12045867号-1