@@ -5,56 +5,21 @@ package files
5
5
6
6
import (
7
7
"testing"
8
- "time"
9
8
10
9
"code.gitea.io/gitea/models/unittest"
11
10
api "code.gitea.io/gitea/modules/structs"
12
11
"code.gitea.io/gitea/modules/util"
13
- "code.gitea.io/gitea/routers/api/v1/utils"
14
12
"code.gitea.io/gitea/services/contexttest"
15
13
16
14
_ "code.gitea.io/gitea/models/actions"
17
15
18
16
"github.com/stretchr/testify/assert"
19
- "github.com/stretchr/testify/require"
20
17
)
21
18
22
19
func TestMain (m * testing.M ) {
23
20
unittest .MainTest (m )
24
21
}
25
22
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
-
58
23
func TestGetContents (t * testing.T ) {
59
24
unittest .PrepareTestEnv (t )
60
25
ctx , _ := contexttest .MockContext (t , "user2/repo1" )
@@ -63,45 +28,8 @@ func TestGetContents(t *testing.T) {
63
28
contexttest .LoadRepoCommit (t , ctx )
64
29
contexttest .LoadUser (t , ctx , 2 )
65
30
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
- })
97
31
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.
105
33
106
34
t .Run ("GetBlobBySHA" , func (t * testing.T ) {
107
35
sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
0 commit comments