Files
gitea/modules/git/tree_entry_common_test.go
T
silverwind 8abda62312 test: consolidate Go tests (#39962)
Consolidate all Go tests. Coverage is identical to before. Go tests are
around 5% faster while being 7% smaller. No non-test code is touched.
2026-10-24 13:48:19 +00:00

68 lines
2.1 KiB
Go

// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package git
import (
"path/filepath"
"testing"
"gitea.dev/modules/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFollowLink(t *testing.T) {
r, err := OpenRepositoryLocal(t.Context(), filepath.Join(testReposDir, "repo1_bare"))
require.NoError(t, err)
defer r.Close()
commit, err := r.GetCommit(t.Context(), "37991dec2c8e592043f47155ce4808d4580f9123")
require.NoError(t, err)
// get the symlink
{
lnkFullPath := "foo/bar/link_to_hello"
lnk, err := commit.Tree().GetTreeEntryByPath(t.Context(), r, "foo/bar/link_to_hello")
require.NoError(t, err)
assert.True(t, lnk.IsLink())
// should be able to dereference to target
res, err := EntryFollowLink(t.Context(), r, commit, lnkFullPath, lnk)
require.NoError(t, err)
assert.Equal(t, "hello", res.TargetEntry.Name())
assert.Equal(t, "foo/nar/hello", res.TargetFullPath)
assert.False(t, res.TargetEntry.IsLink())
assert.Equal(t, "b14df6442ea5a1b382985a6549b85d435376c351", res.TargetEntry.ID.String())
}
{
// should error when called on a normal file
entry, err := commit.Tree().GetTreeEntryByPath(t.Context(), r, "file1.txt")
require.NoError(t, err)
res, err := EntryFollowLink(t.Context(), r, commit, "file1.txt", entry)
assert.ErrorIs(t, err, util.ErrUnprocessableContent)
assert.Nil(t, res)
}
for _, test := range []struct {
fullPath string
symlinkContent string
checkLink bool
}{
{"foo/broken_link", "nar/broken_link", true}, // should error for broken links
{"foo/outside_repo", "../../outside_repo", true}, // should error for external links
{"foo/link_short", "a", false}, // testing fix for short link bug
} {
entry, err := commit.Tree().GetTreeEntryByPath(t.Context(), r, test.fullPath)
require.NoError(t, err)
if test.checkLink {
assert.True(t, entry.IsLink())
}
res, err := EntryFollowLink(t.Context(), r, commit, test.fullPath, entry)
assert.ErrorIs(t, err, util.ErrNotExist)
assert.Equal(t, test.symlinkContent, res.SymlinkContent)
}
}