@@ -104,6 +104,177 @@ func TestDiffToHTML(t *testing.T) {
104
104
}, DiffLineAdd ))
105
105
}
106
106
107
+ func TestParsePatch_skipTo (t * testing.T ) {
108
+ type testcase struct {
109
+ name string
110
+ gitdiff string
111
+ wantErr bool
112
+ addition int
113
+ deletion int
114
+ oldFilename string
115
+ filename string
116
+ skipTo string
117
+ }
118
+ tests := []testcase {
119
+ {
120
+ name : "readme.md2readme.md" ,
121
+ gitdiff : `diff --git "a/A \\ B" "b/A \\ B"
122
+ --- "a/A \\ B"
123
+ +++ "b/A \\ B"
124
+ @@ -1,3 +1,6 @@
125
+ # gitea-github-migrator
126
+ +
127
+ + Build Status
128
+ - Latest Release
129
+ Docker Pulls
130
+ + cut off
131
+ + cut off
132
+ diff --git "\\a/README.md" "\\b/README.md"
133
+ --- "\\a/README.md"
134
+ +++ "\\b/README.md"
135
+ @@ -1,3 +1,6 @@
136
+ # gitea-github-migrator
137
+ +
138
+ + Build Status
139
+ - Latest Release
140
+ Docker Pulls
141
+ + cut off
142
+ + cut off
143
+ ` ,
144
+ addition : 4 ,
145
+ deletion : 1 ,
146
+ filename : "README.md" ,
147
+ oldFilename : "README.md" ,
148
+ skipTo : "README.md" ,
149
+ },
150
+ {
151
+ name : "A \\ B" ,
152
+ gitdiff : `diff --git "a/A \\ B" "b/A \\ B"
153
+ --- "a/A \\ B"
154
+ +++ "b/A \\ B"
155
+ @@ -1,3 +1,6 @@
156
+ # gitea-github-migrator
157
+ +
158
+ + Build Status
159
+ - Latest Release
160
+ Docker Pulls
161
+ + cut off
162
+ + cut off` ,
163
+ addition : 4 ,
164
+ deletion : 1 ,
165
+ filename : "A \\ B" ,
166
+ oldFilename : "A \\ B" ,
167
+ skipTo : "A \\ B" ,
168
+ },
169
+ {
170
+ name : "A \\ B" ,
171
+ gitdiff : `diff --git "\\a/README.md" "\\b/README.md"
172
+ --- "\\a/README.md"
173
+ +++ "\\b/README.md"
174
+ @@ -1,3 +1,6 @@
175
+ # gitea-github-migrator
176
+ +
177
+ + Build Status
178
+ - Latest Release
179
+ Docker Pulls
180
+ + cut off
181
+ + cut off
182
+ diff --git "a/A \\ B" "b/A \\ B"
183
+ --- "a/A \\ B"
184
+ +++ "b/A \\ B"
185
+ @@ -1,3 +1,6 @@
186
+ # gitea-github-migrator
187
+ +
188
+ + Build Status
189
+ - Latest Release
190
+ Docker Pulls
191
+ + cut off
192
+ + cut off` ,
193
+ addition : 4 ,
194
+ deletion : 1 ,
195
+ filename : "A \\ B" ,
196
+ oldFilename : "A \\ B" ,
197
+ skipTo : "A \\ B" ,
198
+ },
199
+ {
200
+ name : "readme.md2readme.md" ,
201
+ gitdiff : `diff --git "a/A \\ B" "b/A \\ B"
202
+ --- "a/A \\ B"
203
+ +++ "b/A \\ B"
204
+ @@ -1,3 +1,6 @@
205
+ # gitea-github-migrator
206
+ +
207
+ + Build Status
208
+ - Latest Release
209
+ Docker Pulls
210
+ + cut off
211
+ + cut off
212
+ diff --git "a/A \\ B" "b/A \\ B"
213
+ --- "a/A \\ B"
214
+ +++ "b/A \\ B"
215
+ @@ -1,3 +1,6 @@
216
+ # gitea-github-migrator
217
+ +
218
+ + Build Status
219
+ - Latest Release
220
+ Docker Pulls
221
+ + cut off
222
+ + cut off
223
+ diff --git "\\a/README.md" "\\b/README.md"
224
+ --- "\\a/README.md"
225
+ +++ "\\b/README.md"
226
+ @@ -1,3 +1,6 @@
227
+ # gitea-github-migrator
228
+ +
229
+ + Build Status
230
+ - Latest Release
231
+ Docker Pulls
232
+ + cut off
233
+ + cut off
234
+ ` ,
235
+ addition : 4 ,
236
+ deletion : 1 ,
237
+ filename : "README.md" ,
238
+ oldFilename : "README.md" ,
239
+ skipTo : "README.md" ,
240
+ },
241
+ }
242
+ for _ , testcase := range tests {
243
+ t .Run (testcase .name , func (t * testing.T ) {
244
+ got , err := ParsePatch (setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (testcase .gitdiff ), testcase .skipTo )
245
+ if (err != nil ) != testcase .wantErr {
246
+ t .Errorf ("ParsePatch(%q) error = %v, wantErr %v" , testcase .name , err , testcase .wantErr )
247
+ return
248
+ }
249
+
250
+ gotMarshaled , _ := json .MarshalIndent (got , "" , " " )
251
+ if got .NumFiles != 1 {
252
+ t .Errorf ("ParsePath(%q) did not receive 1 file:\n %s" , testcase .name , string (gotMarshaled ))
253
+ return
254
+ }
255
+ if got .TotalAddition != testcase .addition {
256
+ t .Errorf ("ParsePath(%q) does not have correct totalAddition %d, wanted %d" , testcase .name , got .TotalAddition , testcase .addition )
257
+ }
258
+ if got .TotalDeletion != testcase .deletion {
259
+ t .Errorf ("ParsePath(%q) did not have correct totalDeletion %d, wanted %d" , testcase .name , got .TotalDeletion , testcase .deletion )
260
+ }
261
+ file := got .Files [0 ]
262
+ if file .Addition != testcase .addition {
263
+ t .Errorf ("ParsePath(%q) does not have correct file addition %d, wanted %d" , testcase .name , file .Addition , testcase .addition )
264
+ }
265
+ if file .Deletion != testcase .deletion {
266
+ t .Errorf ("ParsePath(%q) did not have correct file deletion %d, wanted %d" , testcase .name , file .Deletion , testcase .deletion )
267
+ }
268
+ if file .OldName != testcase .oldFilename {
269
+ t .Errorf ("ParsePath(%q) did not have correct OldName %q, wanted %q" , testcase .name , file .OldName , testcase .oldFilename )
270
+ }
271
+ if file .Name != testcase .filename {
272
+ t .Errorf ("ParsePath(%q) did not have correct Name %q, wanted %q" , testcase .name , file .Name , testcase .filename )
273
+ }
274
+ })
275
+ }
276
+ }
277
+
107
278
func TestParsePatch_singlefile (t * testing.T ) {
108
279
type testcase struct {
109
280
name string
@@ -295,7 +466,7 @@ index 6961180..9ba1a00 100644
295
466
296
467
for _ , testcase := range tests {
297
468
t .Run (testcase .name , func (t * testing.T ) {
298
- got , err := ParsePatch (setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (testcase .gitdiff ))
469
+ got , err := ParsePatch (setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (testcase .gitdiff ), "" )
299
470
if (err != nil ) != testcase .wantErr {
300
471
t .Errorf ("ParsePatch(%q) error = %v, wantErr %v" , testcase .name , err , testcase .wantErr )
301
472
return
@@ -344,21 +515,21 @@ index 0000000..6bb8f39
344
515
diffBuilder .WriteString ("+line" + strconv .Itoa (i ) + "\n " )
345
516
}
346
517
diff = diffBuilder .String ()
347
- result , err := ParsePatch (20 , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (diff ))
518
+ result , err := ParsePatch (20 , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (diff ), "" )
348
519
if err != nil {
349
520
t .Errorf ("There should not be an error: %v" , err )
350
521
}
351
522
if ! result .Files [0 ].IsIncomplete {
352
523
t .Errorf ("Files should be incomplete! %v" , result .Files [0 ])
353
524
}
354
- result , err = ParsePatch (40 , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (diff ))
525
+ result , err = ParsePatch (40 , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (diff ), "" )
355
526
if err != nil {
356
527
t .Errorf ("There should not be an error: %v" , err )
357
528
}
358
529
if result .Files [0 ].IsIncomplete {
359
530
t .Errorf ("Files should not be incomplete! %v" , result .Files [0 ])
360
531
}
361
- result , err = ParsePatch (40 , 5 , setting .Git .MaxGitDiffFiles , strings .NewReader (diff ))
532
+ result , err = ParsePatch (40 , 5 , setting .Git .MaxGitDiffFiles , strings .NewReader (diff ), "" )
362
533
if err != nil {
363
534
t .Errorf ("There should not be an error: %v" , err )
364
535
}
@@ -389,14 +560,14 @@ index 0000000..6bb8f39
389
560
diffBuilder .WriteString ("+line" + strconv .Itoa (35 ) + "\n " )
390
561
diff = diffBuilder .String ()
391
562
392
- result , err = ParsePatch (20 , 4096 , setting .Git .MaxGitDiffFiles , strings .NewReader (diff ))
563
+ result , err = ParsePatch (20 , 4096 , setting .Git .MaxGitDiffFiles , strings .NewReader (diff ), "" )
393
564
if err != nil {
394
565
t .Errorf ("There should not be an error: %v" , err )
395
566
}
396
567
if ! result .Files [0 ].IsIncomplete {
397
568
t .Errorf ("Files should be incomplete! %v" , result .Files [0 ])
398
569
}
399
- result , err = ParsePatch (40 , 4096 , setting .Git .MaxGitDiffFiles , strings .NewReader (diff ))
570
+ result , err = ParsePatch (40 , 4096 , setting .Git .MaxGitDiffFiles , strings .NewReader (diff ), "" )
400
571
if err != nil {
401
572
t .Errorf ("There should not be an error: %v" , err )
402
573
}
@@ -415,7 +586,7 @@ index 0000000..6bb8f39
415
586
Docker Pulls
416
587
+ cut off
417
588
+ cut off`
418
- result , err = ParsePatch (setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (diff ))
589
+ result , err = ParsePatch (setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (diff ), "" )
419
590
if err != nil {
420
591
t .Errorf ("ParsePatch failed: %s" , err )
421
592
}
@@ -432,7 +603,7 @@ index 0000000..6bb8f39
432
603
Docker Pulls
433
604
+ cut off
434
605
+ cut off`
435
- result , err = ParsePatch (setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (diff2 ))
606
+ result , err = ParsePatch (setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (diff2 ), "" )
436
607
if err != nil {
437
608
t .Errorf ("ParsePatch failed: %s" , err )
438
609
}
@@ -449,7 +620,7 @@ index 0000000..6bb8f39
449
620
Docker Pulls
450
621
+ cut off
451
622
+ cut off`
452
- result , err = ParsePatch (setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (diff2a ))
623
+ result , err = ParsePatch (setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (diff2a ), "" )
453
624
if err != nil {
454
625
t .Errorf ("ParsePatch failed: %s" , err )
455
626
}
@@ -466,7 +637,7 @@ index 0000000..6bb8f39
466
637
Docker Pulls
467
638
+ cut off
468
639
+ cut off`
469
- result , err = ParsePatch (setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (diff3 ))
640
+ result , err = ParsePatch (setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (diff3 ), "" )
470
641
if err != nil {
471
642
t .Errorf ("ParsePatch failed: %s" , err )
472
643
}
@@ -557,6 +728,6 @@ func TestNoCrashes(t *testing.T) {
557
728
}
558
729
for _ , testcase := range tests {
559
730
// It shouldn't crash, so don't care about the output.
560
- ParsePatch (setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (testcase .gitdiff ))
731
+ ParsePatch (setting .Git .MaxGitDiffLines , setting .Git .MaxGitDiffLineCharacters , setting .Git .MaxGitDiffFiles , strings .NewReader (testcase .gitdiff ), "" )
561
732
}
562
733
}
0 commit comments