8
8
"fmt"
9
9
"net/http"
10
10
"net/url"
11
- "path/filepath "
11
+ "path"
12
12
"testing"
13
13
"time"
14
14
@@ -49,29 +49,41 @@ func getCreateFileOptions() api.CreateFileOptions {
49
49
}
50
50
}
51
51
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 time format, the timezone may differ
54
+ c .LastCommitterWhen = c .LastCommitterWhen .UTC ()
55
+ c .LastAuthorWhen = c .LastAuthorWhen .UTC ()
56
+ }
57
+
58
+ type apiFileResponseInfo struct {
59
+ repoFullName , commitID , treePath , lastCommitSHA string
60
+ lastCommitterWhen , lastAuthorWhen time.Time
61
+ }
62
+
63
+ func getExpectedFileResponseForCreate (info apiFileResponseInfo ) * api.FileResponse {
53
64
sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
54
65
encoding := "base64"
55
66
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 {
67
+ selfURL := setting .AppURL + "api/v1/repos/" + info . repoFullName + "/contents/" + info . treePath + "?ref=master"
68
+ htmlURL := setting .AppURL + info . repoFullName + "/src/branch/master/" + info . treePath
69
+ gitURL := setting .AppURL + "api/v1/repos/" + info . repoFullName + "/git/blobs/" + sha
70
+ downloadURL := setting .AppURL + info . repoFullName + "/raw/branch/master/" + info . treePath
71
+ ret := & api.FileResponse {
61
72
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 ,
73
+ Name : path .Base (info .treePath ),
74
+ Path : info .treePath ,
75
+ SHA : sha ,
76
+ LastCommitSHA : info .lastCommitSHA ,
77
+ LastCommitterWhen : info .lastCommitterWhen ,
78
+ LastAuthorWhen : info .lastAuthorWhen ,
79
+ Size : 16 ,
80
+ Type : "file" ,
81
+ Encoding : & encoding ,
82
+ Content : & content ,
83
+ URL : & selfURL ,
84
+ HTMLURL : & htmlURL ,
85
+ GitURL : & gitURL ,
86
+ DownloadURL : & downloadURL ,
75
87
Links : & api.FileLinksResponse {
76
88
Self : & selfURL ,
77
89
GitURL : & gitURL ,
@@ -80,10 +92,10 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
80
92
},
81
93
Commit : & api.FileCommitResponse {
82
94
CommitMeta : api.CommitMeta {
83
- URL : setting .AppURL + "api/v1/repos/" + repoFullName + "/git/commits/" + commitID ,
84
- SHA : commitID ,
95
+ URL : setting .AppURL + "api/v1/repos/" + info . repoFullName + "/git/commits/" + info . commitID ,
96
+ SHA : info . commitID ,
85
97
},
86
- HTMLURL : setting .AppURL + repoFullName + "/commit/" + commitID ,
98
+ HTMLURL : setting .AppURL + info . repoFullName + "/commit/" + info . commitID ,
87
99
Author : & api.CommitUser {
88
100
Identity : api.Identity {
89
101
Name : "Anne Doe" ,
@@ -107,6 +119,8 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
107
119
Payload : "" ,
108
120
},
109
121
}
122
+ normalizeFileContentResponseCommitTime (ret .Content )
123
+ return ret
110
124
}
111
125
112
126
func BenchmarkAPICreateFileSmall (b * testing.B ) {
@@ -168,16 +182,20 @@ func TestAPICreateFile(t *testing.T) {
168
182
AddTokenAuth (token2 )
169
183
resp := MakeRequest (t , req , http .StatusCreated )
170
184
gitRepo , _ := gitrepo .OpenRepository (t .Context (), repo1 )
185
+ defer gitRepo .Close ()
171
186
commitID , _ := gitRepo .GetBranchCommitID (createFileOptions .NewBranchName )
172
- latestCommit , _ := gitRepo .GetCommitByPath (treePath )
173
- expectedFileResponse := getExpectedFileResponseForCreate ("user2/repo1" , commitID , treePath , latestCommit .ID .String (), latestCommit .Committer .When )
187
+ lastCommit , _ := gitRepo .GetCommitByPath (treePath )
188
+ expectedFileResponse := getExpectedFileResponseForCreate (apiFileResponseInfo {
189
+ repoFullName : "user2/repo1" ,
190
+ commitID : commitID ,
191
+ treePath : treePath ,
192
+ lastCommitSHA : lastCommit .ID .String (),
193
+ lastCommitterWhen : lastCommit .Committer .When ,
194
+ lastAuthorWhen : lastCommit .Author .When ,
195
+ })
174
196
var fileResponse api.FileResponse
175
197
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
-
198
+ normalizeFileContentResponseCommitTime (fileResponse .Content )
181
199
assert .Equal (t , expectedFileResponse .Content , fileResponse .Content )
182
200
assert .Equal (t , expectedFileResponse .Commit .SHA , fileResponse .Commit .SHA )
183
201
assert .Equal (t , expectedFileResponse .Commit .HTMLURL , fileResponse .Commit .HTMLURL )
@@ -187,7 +205,6 @@ func TestAPICreateFile(t *testing.T) {
187
205
assert .Equal (t , expectedFileResponse .Commit .Committer .Email , fileResponse .Commit .Committer .Email )
188
206
assert .Equal (t , expectedFileResponse .Commit .Committer .Name , fileResponse .Commit .Committer .Name )
189
207
assert .Equal (t , expectedFileResponse .Commit .Committer .Date , fileResponse .Commit .Committer .Date )
190
- gitRepo .Close ()
191
208
}
192
209
193
210
// Test creating a file in a new branch
@@ -291,15 +308,19 @@ func TestAPICreateFile(t *testing.T) {
291
308
resp = MakeRequest (t , req , http .StatusCreated )
292
309
emptyRepo := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {OwnerName : "user2" , Name : "empty-repo" }) // public repo
293
310
gitRepo , _ := gitrepo .OpenRepository (t .Context (), emptyRepo )
311
+ defer gitRepo .Close ()
294
312
commitID , _ := gitRepo .GetBranchCommitID (createFileOptions .NewBranchName )
295
313
latestCommit , _ := gitRepo .GetCommitByPath (treePath )
296
- expectedFileResponse := getExpectedFileResponseForCreate ("user2/empty-repo" , commitID , treePath , latestCommit .ID .String (), latestCommit .Committer .When )
314
+ expectedFileResponse := getExpectedFileResponseForCreate (apiFileResponseInfo {
315
+ repoFullName : "user2/empty-repo" ,
316
+ commitID : commitID ,
317
+ treePath : treePath ,
318
+ lastCommitSHA : latestCommit .ID .String (),
319
+ lastCommitterWhen : latestCommit .Committer .When ,
320
+ lastAuthorWhen : latestCommit .Author .When ,
321
+ })
297
322
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
-
323
+ normalizeFileContentResponseCommitTime (fileResponse .Content )
303
324
assert .Equal (t , expectedFileResponse .Content , fileResponse .Content )
304
325
assert .Equal (t , expectedFileResponse .Commit .SHA , fileResponse .Commit .SHA )
305
326
assert .Equal (t , expectedFileResponse .Commit .HTMLURL , fileResponse .Commit .HTMLURL )
@@ -309,6 +330,5 @@ func TestAPICreateFile(t *testing.T) {
309
330
assert .Equal (t , expectedFileResponse .Commit .Committer .Email , fileResponse .Commit .Committer .Email )
310
331
assert .Equal (t , expectedFileResponse .Commit .Committer .Name , fileResponse .Commit .Committer .Name )
311
332
assert .Equal (t , expectedFileResponse .Commit .Committer .Date , fileResponse .Commit .Committer .Date )
312
- gitRepo .Close ()
313
333
})
314
334
}
0 commit comments