mirror of
https://github.com/go-gitea/gitea.git
synced 2024-11-30 21:06:08 +01:00
511f6138d4
* Move last commit cache back into modules/git Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove go-git from the interface for last commit cache Signed-off-by: Andrew Thornton <art27@cantab.net> * move cacheref to last_commit_cache Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove go-git from routers/private/hook Signed-off-by: Andrew Thornton <art27@cantab.net> * Move FindLFSFiles to pipeline Signed-off-by: Andrew Thornton <art27@cantab.net> * Make no-go-git variants Signed-off-by: Andrew Thornton <art27@cantab.net> * Submodule RefID Signed-off-by: Andrew Thornton <art27@cantab.net> * fix issue with GetCommitsInfo Signed-off-by: Andrew Thornton <art27@cantab.net> * fix GetLastCommitForPaths Signed-off-by: Andrew Thornton <art27@cantab.net> * Improve efficiency Signed-off-by: Andrew Thornton <art27@cantab.net> * More efficiency Signed-off-by: Andrew Thornton <art27@cantab.net> * even faster Signed-off-by: Andrew Thornton <art27@cantab.net> * Reduce duplication * As per @lunny Signed-off-by: Andrew Thornton <art27@cantab.net> * attempt to fix drone Signed-off-by: Andrew Thornton <art27@cantab.net> * fix test-tags Signed-off-by: Andrew Thornton <art27@cantab.net> * default to use no-go-git variants and add gogit build tag Signed-off-by: Andrew Thornton <art27@cantab.net> * placate lint Signed-off-by: Andrew Thornton <art27@cantab.net> * as per @6543 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
104 lines
3.5 KiB
Go
104 lines
3.5 KiB
Go
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// +build gogit
|
|
|
|
package git
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/filemode"
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func getTestEntries() Entries {
|
|
return Entries{
|
|
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v1.0", Mode: filemode.Dir}},
|
|
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.0", Mode: filemode.Dir}},
|
|
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.1", Mode: filemode.Dir}},
|
|
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.12", Mode: filemode.Dir}},
|
|
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.2", Mode: filemode.Dir}},
|
|
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v12.0", Mode: filemode.Dir}},
|
|
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "abc", Mode: filemode.Regular}},
|
|
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "bcd", Mode: filemode.Regular}},
|
|
}
|
|
}
|
|
|
|
func TestEntriesSort(t *testing.T) {
|
|
entries := getTestEntries()
|
|
entries.Sort()
|
|
assert.Equal(t, "v1.0", entries[0].Name())
|
|
assert.Equal(t, "v12.0", entries[1].Name())
|
|
assert.Equal(t, "v2.0", entries[2].Name())
|
|
assert.Equal(t, "v2.1", entries[3].Name())
|
|
assert.Equal(t, "v2.12", entries[4].Name())
|
|
assert.Equal(t, "v2.2", entries[5].Name())
|
|
assert.Equal(t, "abc", entries[6].Name())
|
|
assert.Equal(t, "bcd", entries[7].Name())
|
|
}
|
|
|
|
func TestEntriesCustomSort(t *testing.T) {
|
|
entries := getTestEntries()
|
|
entries.CustomSort(func(s1, s2 string) bool {
|
|
return s1 > s2
|
|
})
|
|
assert.Equal(t, "v2.2", entries[0].Name())
|
|
assert.Equal(t, "v2.12", entries[1].Name())
|
|
assert.Equal(t, "v2.1", entries[2].Name())
|
|
assert.Equal(t, "v2.0", entries[3].Name())
|
|
assert.Equal(t, "v12.0", entries[4].Name())
|
|
assert.Equal(t, "v1.0", entries[5].Name())
|
|
assert.Equal(t, "bcd", entries[6].Name())
|
|
assert.Equal(t, "abc", entries[7].Name())
|
|
}
|
|
|
|
func TestFollowLink(t *testing.T) {
|
|
r, err := OpenRepository("tests/repos/repo1_bare")
|
|
assert.NoError(t, err)
|
|
defer r.Close()
|
|
|
|
commit, err := r.GetCommit("37991dec2c8e592043f47155ce4808d4580f9123")
|
|
assert.NoError(t, err)
|
|
|
|
// get the symlink
|
|
lnk, err := commit.Tree.GetTreeEntryByPath("foo/bar/link_to_hello")
|
|
assert.NoError(t, err)
|
|
assert.True(t, lnk.IsLink())
|
|
|
|
// should be able to dereference to target
|
|
target, err := lnk.FollowLink()
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, target.Name(), "hello")
|
|
assert.False(t, target.IsLink())
|
|
assert.Equal(t, target.ID.String(), "b14df6442ea5a1b382985a6549b85d435376c351")
|
|
|
|
// should error when called on normal file
|
|
target, err = commit.Tree.GetTreeEntryByPath("file1.txt")
|
|
assert.NoError(t, err)
|
|
_, err = target.FollowLink()
|
|
assert.Equal(t, err.Error(), "file1.txt: not a symlink")
|
|
|
|
// should error for broken links
|
|
target, err = commit.Tree.GetTreeEntryByPath("foo/broken_link")
|
|
assert.NoError(t, err)
|
|
assert.True(t, target.IsLink())
|
|
_, err = target.FollowLink()
|
|
assert.Equal(t, err.Error(), "broken_link: broken link")
|
|
|
|
// should error for external links
|
|
target, err = commit.Tree.GetTreeEntryByPath("foo/outside_repo")
|
|
assert.NoError(t, err)
|
|
assert.True(t, target.IsLink())
|
|
_, err = target.FollowLink()
|
|
assert.Equal(t, err.Error(), "outside_repo: points outside of repo")
|
|
|
|
// testing fix for short link bug
|
|
target, err = commit.Tree.GetTreeEntryByPath("foo/link_short")
|
|
assert.NoError(t, err)
|
|
_, err = target.FollowLink()
|
|
assert.Equal(t, err.Error(), "link_short: broken link")
|
|
}
|