9
9
"net/url"
10
10
"strings"
11
11
"testing"
12
+ "time"
12
13
13
14
"code.gitea.io/gitea/models/db"
14
15
"code.gitea.io/gitea/models/unittest"
@@ -160,7 +161,7 @@ func TestCompareCodeExpand(t *testing.T) {
160
161
})
161
162
}
162
163
163
- func TestCompareRawDiff (t * testing.T ) {
164
+ func TestCompareRawDiffNormal (t * testing.T ) {
164
165
onGiteaRun (t , func (t * testing.T , u * url.URL ) {
165
166
user1 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 1 })
166
167
repo , err := repo_service .CreateRepositoryDirectly (db .DefaultContext , user1 , user1 , repo_service.CreateRepoOptions {
@@ -171,16 +172,19 @@ func TestCompareRawDiff(t *testing.T) {
171
172
}, true )
172
173
assert .NoError (t , err )
173
174
session := loginUser (t , user1 .Name )
175
+
174
176
r , _ := gitrepo .OpenRepository (db .DefaultContext , repo )
177
+
175
178
oldRef , _ := r .GetBranchCommit (repo .DefaultBranch )
179
+ oldBlobRef , _ := r .RevParse (oldRef .ID .String (), "README.md" )
180
+
176
181
testEditFile (t , session , user1 .Name , repo .Name , "main" , "README.md" , strings .Repeat ("a\n " , 2 ))
182
+
177
183
newRef , _ := r .GetBranchCommit (repo .DefaultBranch )
178
- fmt .Println ("oldRef" , oldRef .ID .String ())
179
- fmt .Println ("newRef" , newRef .ID .String ())
184
+ newBlobRef , _ := r .RevParse (newRef .ID .String (), "README.md" )
180
185
181
186
req := NewRequest (t , "GET" , fmt .Sprintf ("/user1/test_raw_diff/compare/%s...%s.diff" , oldRef .ID .String (), newRef .ID .String ()))
182
187
resp := session .MakeRequest (t , req , http .StatusOK )
183
- fmt .Println ("resp" , resp .Body .String ())
184
188
185
189
expected := fmt .Sprintf (`diff --git a/README.md b/README.md
186
190
index %s..%s 100644
@@ -191,9 +195,64 @@ index %s..%s 100644
191
195
-
192
196
+a
193
197
+a
194
- ` ,
195
- oldRef .ID .String ()[:7 ], newRef .ID .String ()[:7 ])
198
+ ` , oldBlobRef [:7 ], newBlobRef [:7 ])
199
+ assert .Equal (t , expected , resp .Body .String ())
200
+ })
201
+ }
202
+
203
+ func TestCompareRawDiffPatch (t * testing.T ) {
204
+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
205
+ user1 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 1 })
206
+ repo , err := repo_service .CreateRepositoryDirectly (db .DefaultContext , user1 , user1 , repo_service.CreateRepoOptions {
207
+ Name : "test_raw_diff" ,
208
+ Readme : "Default" ,
209
+ AutoInit : true ,
210
+ DefaultBranch : "main" ,
211
+ }, true )
212
+ assert .NoError (t , err )
213
+ session := loginUser (t , user1 .Name )
214
+
215
+ r , _ := gitrepo .OpenRepository (db .DefaultContext , repo )
196
216
197
- assert .Equal (t , resp .Body .String (), expected )
217
+ // Get the old commit and blob reference
218
+ oldRef , _ := r .GetBranchCommit (repo .DefaultBranch )
219
+ oldBlobRef , _ := r .RevParse (oldRef .ID .String (), "README.md" )
220
+
221
+ resp := testEditFile (t , session , user1 .Name , repo .Name , "main" , "README.md" , strings .Repeat ("a\n " , 2 ))
222
+
223
+ newRef , _ := r .GetBranchCommit (repo .DefaultBranch )
224
+ newBlobRef , _ := r .RevParse (newRef .ID .String (), "README.md" )
225
+
226
+ // Get the last modified time from the response header
227
+ respTs , _ := time .Parse (time .RFC1123 , resp .Result ().Header .Get ("Last-Modified" ))
228
+ respTs = respTs .In (time .Local )
229
+
230
+ // Format the timestamp to match the expected format in the patch
231
+ customFormat := "Mon, 02 Jan 2006 15:04:05"
232
+ respTsStr := respTs .Format (customFormat )
233
+
234
+ req := NewRequest (t , "GET" , fmt .Sprintf ("/user1/test_raw_diff/compare/%s...%s.patch" , oldRef .ID .String (), newRef .ID .String ()))
235
+ resp = session .MakeRequest (t , req , http .StatusOK )
236
+
237
+ expected := fmt .Sprintf (`From %s Mon Sep 17 00:00:00 2001
238
+ From: User One <[email protected] >
239
+ Date: %s +0300
240
+ Subject: [PATCH] Update README.md
241
+
242
+ ---
243
+ README.md | 4 ++--
244
+ 1 file changed, 2 insertions(+), 2 deletions(-)
245
+
246
+ diff --git a/README.md b/README.md
247
+ index %s..%s 100644
248
+ --- a/README.md
249
+ +++ b/README.md
250
+ @@ -1,2 +1,2 @@
251
+ -# test_raw_diff
252
+ -
253
+ +a
254
+ +a
255
+ ` , newRef .ID .String (), respTsStr , oldBlobRef [:7 ], newBlobRef [:7 ])
256
+ assert .Equal (t , expected , resp .Body .String ())
198
257
})
199
258
}
0 commit comments