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