88 "fmt"
99 "net/http"
1010 "net/url"
11- "path/filepath "
11+ "path"
1212 "testing"
1313 "time"
1414
@@ -49,29 +49,42 @@ func getCreateFileOptions() api.CreateFileOptions {
4949 }
5050}
5151
52- func getExpectedFileResponseForCreate (repoFullName , commitID , treePath , latestCommitSHA string , latestCommitWhen time.Time ) * api.FileResponse {
52+ func normalizeFileContentResponseCommitTime (c * api.ContentsResponse ) {
53+ // decoded JSON response may contain different timezone from the one parsed by git commit
54+ // so we need to normalize the time to UTC to make "assert.Equal" pass
55+ c .LastCommitterWhen = c .LastCommitterWhen .UTC ()
56+ c .LastAuthorWhen = c .LastAuthorWhen .UTC ()
57+ }
58+
59+ type apiFileResponseInfo struct {
60+ repoFullName , commitID , treePath , lastCommitSHA string
61+ lastCommitterWhen , lastAuthorWhen time.Time
62+ }
63+
64+ func getExpectedFileResponseForCreate (info apiFileResponseInfo ) * api.FileResponse {
5365 sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
5466 encoding := "base64"
5567 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
60- return & api.FileResponse {
68+ selfURL := setting .AppURL + "api/v1/repos/" + info . repoFullName + "/contents/" + info . treePath + "?ref=master"
69+ htmlURL := setting .AppURL + info . repoFullName + "/src/branch/master/" + info . treePath
70+ gitURL := setting .AppURL + "api/v1/repos/" + info . repoFullName + "/git/blobs/" + sha
71+ downloadURL := setting .AppURL + info . repoFullName + "/raw/branch/master/" + info . treePath
72+ ret := & api.FileResponse {
6173 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 ,
74+ Name : path .Base (info .treePath ),
75+ Path : info .treePath ,
76+ SHA : sha ,
77+ LastCommitSHA : info .lastCommitSHA ,
78+ LastCommitterWhen : info .lastCommitterWhen ,
79+ LastAuthorWhen : info .lastAuthorWhen ,
80+ Size : 16 ,
81+ Type : "file" ,
82+ Encoding : & encoding ,
83+ Content : & content ,
84+ URL : & selfURL ,
85+ HTMLURL : & htmlURL ,
86+ GitURL : & gitURL ,
87+ DownloadURL : & downloadURL ,
7588 Links : & api.FileLinksResponse {
7689 Self : & selfURL ,
7790 GitURL : & gitURL ,
@@ -80,10 +93,10 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
8093 },
8194 Commit : & api.FileCommitResponse {
8295 CommitMeta : api.CommitMeta {
83- URL : setting .AppURL + "api/v1/repos/" + repoFullName + "/git/commits/" + commitID ,
84- SHA : commitID ,
96+ URL : setting .AppURL + "api/v1/repos/" + info . repoFullName + "/git/commits/" + info . commitID ,
97+ SHA : info . commitID ,
8598 },
86- HTMLURL : setting .AppURL + repoFullName + "/commit/" + commitID ,
99+ HTMLURL : setting .AppURL + info . repoFullName + "/commit/" + info . commitID ,
87100 Author : & api.CommitUser {
88101 Identity : api.Identity {
89102 Name : "Anne Doe" ,
@@ -107,6 +120,8 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
107120 Payload : "" ,
108121 },
109122 }
123+ normalizeFileContentResponseCommitTime (ret .Content )
124+ return ret
110125}
111126
112127func BenchmarkAPICreateFileSmall (b * testing.B ) {
@@ -168,16 +183,20 @@ func TestAPICreateFile(t *testing.T) {
168183 AddTokenAuth (token2 )
169184 resp := MakeRequest (t , req , http .StatusCreated )
170185 gitRepo , _ := gitrepo .OpenRepository (t .Context (), repo1 )
186+ defer gitRepo .Close ()
171187 commitID , _ := gitRepo .GetBranchCommitID (createFileOptions .NewBranchName )
172- latestCommit , _ := gitRepo .GetCommitByPath (treePath )
173- expectedFileResponse := getExpectedFileResponseForCreate ("user2/repo1" , commitID , treePath , latestCommit .ID .String (), latestCommit .Committer .When )
188+ lastCommit , _ := gitRepo .GetCommitByPath (treePath )
189+ expectedFileResponse := getExpectedFileResponseForCreate (apiFileResponseInfo {
190+ repoFullName : "user2/repo1" ,
191+ commitID : commitID ,
192+ treePath : treePath ,
193+ lastCommitSHA : lastCommit .ID .String (),
194+ lastCommitterWhen : lastCommit .Committer .When ,
195+ lastAuthorWhen : lastCommit .Author .When ,
196+ })
174197 var fileResponse api.FileResponse
175198 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-
199+ normalizeFileContentResponseCommitTime (fileResponse .Content )
181200 assert .Equal (t , expectedFileResponse .Content , fileResponse .Content )
182201 assert .Equal (t , expectedFileResponse .Commit .SHA , fileResponse .Commit .SHA )
183202 assert .Equal (t , expectedFileResponse .Commit .HTMLURL , fileResponse .Commit .HTMLURL )
@@ -187,7 +206,6 @@ func TestAPICreateFile(t *testing.T) {
187206 assert .Equal (t , expectedFileResponse .Commit .Committer .Email , fileResponse .Commit .Committer .Email )
188207 assert .Equal (t , expectedFileResponse .Commit .Committer .Name , fileResponse .Commit .Committer .Name )
189208 assert .Equal (t , expectedFileResponse .Commit .Committer .Date , fileResponse .Commit .Committer .Date )
190- gitRepo .Close ()
191209 }
192210
193211 // Test creating a file in a new branch
@@ -291,15 +309,19 @@ func TestAPICreateFile(t *testing.T) {
291309 resp = MakeRequest (t , req , http .StatusCreated )
292310 emptyRepo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {OwnerName : "user2" , Name : "empty-repo" }) // public repo
293311 gitRepo , _ := gitrepo .OpenRepository (t .Context (), emptyRepo )
312+ defer gitRepo .Close ()
294313 commitID , _ := gitRepo .GetBranchCommitID (createFileOptions .NewBranchName )
295314 latestCommit , _ := gitRepo .GetCommitByPath (treePath )
296- expectedFileResponse := getExpectedFileResponseForCreate ("user2/empty-repo" , commitID , treePath , latestCommit .ID .String (), latestCommit .Committer .When )
315+ expectedFileResponse := getExpectedFileResponseForCreate (apiFileResponseInfo {
316+ repoFullName : "user2/empty-repo" ,
317+ commitID : commitID ,
318+ treePath : treePath ,
319+ lastCommitSHA : latestCommit .ID .String (),
320+ lastCommitterWhen : latestCommit .Committer .When ,
321+ lastAuthorWhen : latestCommit .Author .When ,
322+ })
297323 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-
324+ normalizeFileContentResponseCommitTime (fileResponse .Content )
303325 assert .Equal (t , expectedFileResponse .Content , fileResponse .Content )
304326 assert .Equal (t , expectedFileResponse .Commit .SHA , fileResponse .Commit .SHA )
305327 assert .Equal (t , expectedFileResponse .Commit .HTMLURL , fileResponse .Commit .HTMLURL )
@@ -309,6 +331,5 @@ func TestAPICreateFile(t *testing.T) {
309331 assert .Equal (t , expectedFileResponse .Commit .Committer .Email , fileResponse .Commit .Committer .Email )
310332 assert .Equal (t , expectedFileResponse .Commit .Committer .Name , fileResponse .Commit .Committer .Name )
311333 assert .Equal (t , expectedFileResponse .Commit .Committer .Date , fileResponse .Commit .Committer .Date )
312- gitRepo .Close ()
313334 })
314335}
0 commit comments