@@ -5,56 +5,21 @@ package files
55
66import (
77 "testing"
8- "time"
98
109 "code.gitea.io/gitea/models/unittest"
1110 api "code.gitea.io/gitea/modules/structs"
1211 "code.gitea.io/gitea/modules/util"
13- "code.gitea.io/gitea/routers/api/v1/utils"
1412 "code.gitea.io/gitea/services/contexttest"
1513
1614 _ "code.gitea.io/gitea/models/actions"
1715
1816 "github.com/stretchr/testify/assert"
19- "github.com/stretchr/testify/require"
2017)
2118
2219func TestMain (m * testing.M ) {
2320 unittest .MainTest (m )
2421}
2522
26- func getExpectedReadmeContentsResponse () * api.ContentsResponse {
27- treePath := "README.md"
28- sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f"
29- encoding := "base64"
30- content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x"
31- selfURL := "https://try.gitea.io/api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master"
32- htmlURL := "https://try.gitea.io/user2/repo1/src/branch/master/" + treePath
33- gitURL := "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/" + sha
34- downloadURL := "https://try.gitea.io/user2/repo1/raw/branch/master/" + treePath
35- return & api.ContentsResponse {
36- Name : treePath ,
37- Path : treePath ,
38- SHA : "4b4851ad51df6a7d9f25c979345979eaeb5b349f" ,
39- LastCommitSHA : "65f1bf27bc3bf70f64657658635e66094edbcb4d" ,
40- LastCommitterDate : time .Date (2017 , time .March , 19 , 16 , 47 , 59 , 0 , time .FixedZone ("" , - 14400 )),
41- LastAuthorDate : time .Date (2017 , time .March , 19 , 16 , 47 , 59 , 0 , time .FixedZone ("" , - 14400 )),
42- Type : "file" ,
43- Size : 30 ,
44- Encoding : & encoding ,
45- Content : & content ,
46- URL : & selfURL ,
47- HTMLURL : & htmlURL ,
48- GitURL : & gitURL ,
49- DownloadURL : & downloadURL ,
50- Links : & api.FileLinksResponse {
51- Self : & selfURL ,
52- GitURL : & gitURL ,
53- HTMLURL : & htmlURL ,
54- },
55- }
56- }
57-
5823func TestGetContents (t * testing.T ) {
5924 unittest .PrepareTestEnv (t )
6025 ctx , _ := contexttest .MockContext (t , "user2/repo1" )
@@ -63,45 +28,8 @@ func TestGetContents(t *testing.T) {
6328 contexttest .LoadRepoCommit (t , ctx )
6429 contexttest .LoadUser (t , ctx , 2 )
6530 contexttest .LoadGitRepo (t , ctx )
66- defer ctx .Repo .GitRepo .Close ()
67- repo , gitRepo := ctx .Repo .Repository , ctx .Repo .GitRepo
68- refCommit , err := utils .ResolveRefCommit (ctx , ctx .Repo .Repository , ctx .Repo .Repository .DefaultBranch )
69- require .NoError (t , err )
70-
71- t .Run ("GetContentsOrList(README.md)-MetaOnly" , func (t * testing.T ) {
72- expectedContentsResponse := getExpectedReadmeContentsResponse ()
73- expectedContentsResponse .Encoding = nil // because will be in a list, doesn't have encoding and content
74- expectedContentsResponse .Content = nil
75- extResp , err := GetContentsOrList (ctx , repo , gitRepo , refCommit , GetContentsOrListOptions {TreePath : "README.md" , IncludeSingleFileContent : false })
76- assert .Equal (t , expectedContentsResponse , extResp .FileContents )
77- assert .NoError (t , err )
78- })
79-
80- t .Run ("GetContentsOrList(README.md)" , func (t * testing.T ) {
81- expectedContentsResponse := getExpectedReadmeContentsResponse ()
82- extResp , err := GetContentsOrList (ctx , repo , gitRepo , refCommit , GetContentsOrListOptions {TreePath : "README.md" , IncludeSingleFileContent : true })
83- assert .Equal (t , expectedContentsResponse , extResp .FileContents )
84- assert .NoError (t , err )
85- })
86-
87- t .Run ("GetContentsOrList(RootDir)" , func (t * testing.T ) {
88- readmeContentsResponse := getExpectedReadmeContentsResponse ()
89- readmeContentsResponse .Encoding = nil // because will be in a list, doesn't have encoding and content
90- readmeContentsResponse .Content = nil
91- expectedContentsListResponse := []* api.ContentsResponse {readmeContentsResponse }
92- // even if IncludeFileContent is true, it has no effect for directory listing
93- extResp , err := GetContentsOrList (ctx , repo , gitRepo , refCommit , GetContentsOrListOptions {TreePath : "" , IncludeSingleFileContent : true })
94- assert .Equal (t , expectedContentsListResponse , extResp .DirContents )
95- assert .NoError (t , err )
96- })
9731
98- t .Run ("GetContentsOrList(NoSuchTreePath)" , func (t * testing.T ) {
99- extResp , err := GetContentsOrList (ctx , repo , gitRepo , refCommit , GetContentsOrListOptions {TreePath : "no-such/file.md" })
100- assert .Error (t , err )
101- assert .EqualError (t , err , "object does not exist [id: , rel_path: no-such]" )
102- assert .Nil (t , extResp .DirContents )
103- assert .Nil (t , extResp .FileContents )
104- })
32+ // GetContentsOrList's behavior is fully tested in integration tests, so we don't need to test it here.
10533
10634 t .Run ("GetBlobBySHA" , func (t * testing.T ) {
10735 sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
0 commit comments