2019-03-27 12:33:00 +03:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2019-03-27 12:33:00 +03:00
2021-08-24 19:47:09 +03:00
//go:build gogit
2020-12-17 17:00:47 +03:00
2019-03-27 12:33:00 +03:00
package git
import (
"testing"
2020-03-17 19:19:58 +03:00
"github.com/go-git/go-git/v5/plumbing/filemode"
"github.com/go-git/go-git/v5/plumbing/object"
2019-03-27 12:33:00 +03:00
"github.com/stretchr/testify/assert"
)
func getTestEntries ( ) Entries {
return Entries {
2019-04-19 15:17:27 +03:00
& 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 } } ,
2019-03-27 12:33:00 +03:00
}
}
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 ) {
2022-03-29 22:13:41 +03:00
r , err := openRepositoryWithDefaultContext ( "tests/repos/repo1_bare" )
2019-03-27 12:33:00 +03:00
assert . NoError ( t , err )
2019-11-13 10:01:19 +03:00
defer r . Close ( )
2019-03-27 12:33:00 +03:00
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 )
2021-06-07 08:27:09 +03:00
assert . Equal ( t , "hello" , target . Name ( ) )
2019-03-27 12:33:00 +03:00
assert . False ( t , target . IsLink ( ) )
2021-06-07 08:27:09 +03:00
assert . Equal ( t , "b14df6442ea5a1b382985a6549b85d435376c351" , target . ID . String ( ) )
2019-03-27 12:33:00 +03:00
// should error when called on normal file
target , err = commit . Tree . GetTreeEntryByPath ( "file1.txt" )
assert . NoError ( t , err )
_ , err = target . FollowLink ( )
2021-06-07 08:27:09 +03:00
assert . EqualError ( t , err , "file1.txt: not a symlink" )
2019-03-27 12:33:00 +03:00
// 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 ( )
2021-06-07 08:27:09 +03:00
assert . EqualError ( t , err , "broken_link: broken link" )
2019-03-27 12:33:00 +03:00
// 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 ( )
2021-06-07 08:27:09 +03:00
assert . EqualError ( t , err , "outside_repo: points outside of repo" )
2019-03-27 12:33:00 +03:00
// testing fix for short link bug
target , err = commit . Tree . GetTreeEntryByPath ( "foo/link_short" )
assert . NoError ( t , err )
_ , err = target . FollowLink ( )
2021-06-07 08:27:09 +03:00
assert . EqualError ( t , err , "link_short: broken link" )
2019-03-27 12:33:00 +03:00
}