88 "fmt"
99 "net/http"
1010 "net/url"
11- "path/filepath "
11+ "path"
1212 "testing"
1313 "time"
1414
@@ -49,29 +49,35 @@ func getCreateFileOptions() api.CreateFileOptions {
4949 }
5050}
5151
52- func getExpectedFileResponseForCreate (repoFullName , commitID , treePath , latestCommitSHA string , latestCommitWhen time.Time ) * api.FileResponse {
52+ type apiFileResponseInfo struct {
53+ repoFullName , commitID , treePath , lastCommitSHA string
54+ lastCommitterWhen , lastAuthorWhen time.Time
55+ }
56+
57+ func getExpectedFileResponseForCreate (info apiFileResponseInfo ) * api.FileResponse {
5358 sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
5459 encoding := "base64"
5560 content := "VGhpcyBpcyBuZXcgdGV4dA=="
56- selfURL := setting .AppURL + "api/v1/repos/" + repoFullName + "/contents/" + treePath + "?ref=master"
57- htmlURL := setting .AppURL + repoFullName + "/src/branch/master/" + treePath
58- gitURL := setting .AppURL + "api/v1/repos/" + repoFullName + "/git/blobs/" + sha
59- downloadURL := setting .AppURL + repoFullName + "/raw/branch/master/" + treePath
61+ selfURL := setting .AppURL + "api/v1/repos/" + info . repoFullName + "/contents/" + info . treePath + "?ref=master"
62+ htmlURL := setting .AppURL + info . repoFullName + "/src/branch/master/" + info . treePath
63+ gitURL := setting .AppURL + "api/v1/repos/" + info . repoFullName + "/git/blobs/" + sha
64+ downloadURL := setting .AppURL + info . repoFullName + "/raw/branch/master/" + info . treePath
6065 return & api.FileResponse {
6166 Content : & api.ContentsResponse {
62- Name : filepath .Base (treePath ),
63- Path : treePath ,
64- SHA : sha ,
65- LastCommitSHA : latestCommitSHA ,
66- LastCommitWhen : latestCommitWhen ,
67- Size : 16 ,
68- Type : "file" ,
69- Encoding : & encoding ,
70- Content : & content ,
71- URL : & selfURL ,
72- HTMLURL : & htmlURL ,
73- GitURL : & gitURL ,
74- DownloadURL : & downloadURL ,
67+ Name : path .Base (info .treePath ),
68+ Path : info .treePath ,
69+ SHA : sha ,
70+ LastCommitSHA : info .lastCommitSHA ,
71+ LastCommitterWhen : info .lastCommitterWhen ,
72+ LastAuthorWhen : info .lastAuthorWhen ,
73+ Size : 16 ,
74+ Type : "file" ,
75+ Encoding : & encoding ,
76+ Content : & content ,
77+ URL : & selfURL ,
78+ HTMLURL : & htmlURL ,
79+ GitURL : & gitURL ,
80+ DownloadURL : & downloadURL ,
7581 Links : & api.FileLinksResponse {
7682 Self : & selfURL ,
7783 GitURL : & gitURL ,
@@ -80,10 +86,10 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
8086 },
8187 Commit : & api.FileCommitResponse {
8288 CommitMeta : api.CommitMeta {
83- URL : setting .AppURL + "api/v1/repos/" + repoFullName + "/git/commits/" + commitID ,
84- SHA : commitID ,
89+ URL : setting .AppURL + "api/v1/repos/" + info . repoFullName + "/git/commits/" + info . commitID ,
90+ SHA : info . commitID ,
8591 },
86- HTMLURL : setting .AppURL + repoFullName + "/commit/" + commitID ,
92+ HTMLURL : setting .AppURL + info . repoFullName + "/commit/" + info . commitID ,
8793 Author : & api.CommitUser {
8894 Identity : api.Identity {
8995 Name : "Anne Doe" ,
@@ -168,16 +174,19 @@ func TestAPICreateFile(t *testing.T) {
168174 AddTokenAuth (token2 )
169175 resp := MakeRequest (t , req , http .StatusCreated )
170176 gitRepo , _ := gitrepo .OpenRepository (t .Context (), repo1 )
177+ defer gitRepo .Close ()
171178 commitID , _ := gitRepo .GetBranchCommitID (createFileOptions .NewBranchName )
172179 latestCommit , _ := gitRepo .GetCommitByPath (treePath )
173- expectedFileResponse := getExpectedFileResponseForCreate ("user2/repo1" , commitID , treePath , latestCommit .ID .String (), latestCommit .Committer .When )
180+ expectedFileResponse := getExpectedFileResponseForCreate (apiFileResponseInfo {
181+ repoFullName : "user2/repo1" ,
182+ commitID : commitID ,
183+ treePath : treePath ,
184+ lastCommitSHA : latestCommit .ID .String (),
185+ lastCommitterWhen : latestCommit .Committer .When ,
186+ lastAuthorWhen : latestCommit .Author .When ,
187+ })
174188 var fileResponse api.FileResponse
175189 DecodeJSON (t , resp , & fileResponse )
176-
177- // FIXME: This is a workaround to compare time.Time values. This maybe a bug of Golang,
178- // assume your local timezone is UTC, but a location with zero offset is not equal to UTC but they should be.
179- expectedFileResponse .Content .LastCommitWhen , _ = time .Parse (time .RFC3339 , expectedFileResponse .Content .LastCommitWhen .Format (time .RFC3339 ))
180-
181190 assert .Equal (t , expectedFileResponse .Content , fileResponse .Content )
182191 assert .Equal (t , expectedFileResponse .Commit .SHA , fileResponse .Commit .SHA )
183192 assert .Equal (t , expectedFileResponse .Commit .HTMLURL , fileResponse .Commit .HTMLURL )
@@ -187,7 +196,6 @@ func TestAPICreateFile(t *testing.T) {
187196 assert .Equal (t , expectedFileResponse .Commit .Committer .Email , fileResponse .Commit .Committer .Email )
188197 assert .Equal (t , expectedFileResponse .Commit .Committer .Name , fileResponse .Commit .Committer .Name )
189198 assert .Equal (t , expectedFileResponse .Commit .Committer .Date , fileResponse .Commit .Committer .Date )
190- gitRepo .Close ()
191199 }
192200
193201 // Test creating a file in a new branch
@@ -291,15 +299,18 @@ func TestAPICreateFile(t *testing.T) {
291299 resp = MakeRequest (t , req , http .StatusCreated )
292300 emptyRepo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {OwnerName : "user2" , Name : "empty-repo" }) // public repo
293301 gitRepo , _ := gitrepo .OpenRepository (t .Context (), emptyRepo )
302+ defer gitRepo .Close ()
294303 commitID , _ := gitRepo .GetBranchCommitID (createFileOptions .NewBranchName )
295304 latestCommit , _ := gitRepo .GetCommitByPath (treePath )
296- expectedFileResponse := getExpectedFileResponseForCreate ("user2/empty-repo" , commitID , treePath , latestCommit .ID .String (), latestCommit .Committer .When )
305+ expectedFileResponse := getExpectedFileResponseForCreate (apiFileResponseInfo {
306+ repoFullName : "user2/empty-repo" ,
307+ commitID : commitID ,
308+ treePath : treePath ,
309+ lastCommitSHA : latestCommit .ID .String (),
310+ lastCommitterWhen : latestCommit .Committer .When ,
311+ lastAuthorWhen : latestCommit .Author .When ,
312+ })
297313 DecodeJSON (t , resp , & fileResponse )
298-
299- // FIXME: This is a workaround to compare time.Time values. This maybe a bug of Golang,
300- // assume your local timezone is UTC, but a location with zero offset is not equal to UTC but they should be.
301- expectedFileResponse .Content .LastCommitWhen , _ = time .Parse (time .RFC3339 , expectedFileResponse .Content .LastCommitWhen .Format (time .RFC3339 ))
302-
303314 assert .Equal (t , expectedFileResponse .Content , fileResponse .Content )
304315 assert .Equal (t , expectedFileResponse .Commit .SHA , fileResponse .Commit .SHA )
305316 assert .Equal (t , expectedFileResponse .Commit .HTMLURL , fileResponse .Commit .HTMLURL )
@@ -309,6 +320,5 @@ func TestAPICreateFile(t *testing.T) {
309320 assert .Equal (t , expectedFileResponse .Commit .Committer .Email , fileResponse .Commit .Committer .Email )
310321 assert .Equal (t , expectedFileResponse .Commit .Committer .Name , fileResponse .Commit .Committer .Name )
311322 assert .Equal (t , expectedFileResponse .Commit .Committer .Date , fileResponse .Commit .Committer .Date )
312- gitRepo .Close ()
313323 })
314324}
0 commit comments