1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
|
package gitpipe
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/internal/git"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/localrepo"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper/testcfg"
)
func TestRevlist(t *testing.T) {
cfg := testcfg.Build(t)
repoProto, _ := gittest.CloneRepo(t, cfg, cfg.Storages[0])
repo := localrepo.NewTestRepo(t, cfg, repoProto)
for _, tc := range []struct {
desc string
revisions []string
options []RevlistOption
expectedResults []RevisionResult
expectedErr error
}{
{
desc: "single blob",
revisions: []string{
lfsPointer1,
},
options: []RevlistOption{
WithObjects(),
},
expectedResults: []RevisionResult{
{OID: lfsPointer1},
},
},
{
desc: "multiple blobs",
revisions: []string{
lfsPointer1,
lfsPointer2,
lfsPointer3,
lfsPointer4,
},
options: []RevlistOption{
WithObjects(),
},
expectedResults: []RevisionResult{
{OID: lfsPointer1},
{OID: lfsPointer2},
{OID: lfsPointer3},
{OID: lfsPointer4},
},
},
{
desc: "multiple blobs without objects",
revisions: []string{
lfsPointer1,
lfsPointer2,
lfsPointer3,
lfsPointer4,
},
expectedResults: nil,
},
{
desc: "duplicated blob prints blob once only",
revisions: []string{
lfsPointer1,
lfsPointer1,
},
options: []RevlistOption{
WithObjects(),
},
expectedResults: []RevisionResult{
{OID: lfsPointer1},
},
},
{
desc: "tree results in object names",
revisions: []string{
"b95c0fad32f4361845f91d9ce4c1721b52b82793",
},
options: []RevlistOption{
WithObjects(),
},
expectedResults: []RevisionResult{
{OID: "b95c0fad32f4361845f91d9ce4c1721b52b82793"},
{OID: "93e123ac8a3e6a0b600953d7598af629dec7b735", ObjectName: []byte("branch-test.txt")},
},
},
{
desc: "tree without objects returns nothing",
revisions: []string{
"b95c0fad32f4361845f91d9ce4c1721b52b82793",
},
expectedResults: nil,
},
{
desc: "revision without disabled walk",
revisions: []string{
"refs/heads/master",
},
options: []RevlistOption{
WithDisabledWalk(),
},
expectedResults: []RevisionResult{
{OID: "1e292f8fedd741b75372e19097c76d327140c312"},
},
},
{
desc: "revision range",
revisions: []string{
"^refs/heads/master~",
"refs/heads/master",
},
options: []RevlistOption{
WithObjects(),
},
expectedResults: []RevisionResult{
{OID: "1e292f8fedd741b75372e19097c76d327140c312"},
{OID: "07f8147e8e73aab6c935c296e8cdc5194dee729b"},
{OID: "ceb102b8d3f9a95c2eb979213e49f7cc1b23d56e", ObjectName: []byte("files")},
{OID: "2132d150328bd9334cc4e62a16a5d998a7e399b9", ObjectName: []byte("files/flat")},
{OID: "f3942dc8b824a2c9359e518d48e68f84461bd2f7", ObjectName: []byte("files/flat/path")},
{OID: "ea7249055466085d0a6c69951908ef47757e92f4", ObjectName: []byte("files/flat/path/correct")},
{OID: "c1c67abbaf91f624347bb3ae96eabe3a1b742478"},
},
},
{
desc: "revision range without objects",
revisions: []string{
"^refs/heads/master~",
"refs/heads/master",
},
expectedResults: []RevisionResult{
{OID: "1e292f8fedd741b75372e19097c76d327140c312"},
{OID: "c1c67abbaf91f624347bb3ae96eabe3a1b742478"},
},
},
{
desc: "revision range without objects with at most one parent",
revisions: []string{
"^refs/heads/master~",
"refs/heads/master",
},
options: []RevlistOption{
WithMaxParents(1),
},
expectedResults: []RevisionResult{
{OID: "c1c67abbaf91f624347bb3ae96eabe3a1b742478"},
},
},
{
desc: "reverse revision range without objects",
revisions: []string{
"^refs/heads/master~",
"refs/heads/master",
},
options: []RevlistOption{
WithReverse(),
},
expectedResults: []RevisionResult{
{OID: "c1c67abbaf91f624347bb3ae96eabe3a1b742478"},
{OID: "1e292f8fedd741b75372e19097c76d327140c312"},
},
},
{
desc: "reverse revision range with objects",
revisions: []string{
"^refs/heads/master~",
"refs/heads/master",
},
options: []RevlistOption{
WithReverse(),
WithObjects(),
},
expectedResults: []RevisionResult{
// Note that only commits are listed in reverse,
// their referenced objects stay in the same order.
{OID: "c1c67abbaf91f624347bb3ae96eabe3a1b742478"},
{OID: "07f8147e8e73aab6c935c296e8cdc5194dee729b"},
{OID: "ceb102b8d3f9a95c2eb979213e49f7cc1b23d56e", ObjectName: []byte("files")},
{OID: "2132d150328bd9334cc4e62a16a5d998a7e399b9", ObjectName: []byte("files/flat")},
{OID: "f3942dc8b824a2c9359e518d48e68f84461bd2f7", ObjectName: []byte("files/flat/path")},
{OID: "ea7249055466085d0a6c69951908ef47757e92f4", ObjectName: []byte("files/flat/path/correct")},
{OID: "1e292f8fedd741b75372e19097c76d327140c312"},
},
},
{
desc: "revision range with topo order",
revisions: []string{
// This is one of the smaller examples I've found which reproduces
// different sorting orders between topo- and date-sorting. Expected
// results contain the same object for this and the next test case,
// but ordering is different.
"master",
"^master~5",
"flat-path",
},
options: []RevlistOption{
WithOrder(OrderTopo),
},
expectedResults: []RevisionResult{
{OID: "1e292f8fedd741b75372e19097c76d327140c312"},
{OID: "c1c67abbaf91f624347bb3ae96eabe3a1b742478"},
{OID: "7975be0116940bf2ad4321f79d02a55c5f7779aa"},
{OID: "c84ff944ff4529a70788a5e9003c2b7feae29047"},
{OID: "60ecb67744cb56576c30214ff52294f8ce2def98"},
{OID: "55bc176024cfa3baaceb71db584c7e5df900ea65"},
{OID: "e63f41fe459e62e1228fcef60d7189127aeba95a"},
{OID: "4a24d82dbca5c11c61556f3b35ca472b7463187e"},
{OID: "b83d6e391c22777fca1ed3012fce84f633d7fed0"},
{OID: "498214de67004b1da3d820901307bed2a68a8ef6"},
// The following commit is sorted differently in the next testcase.
{OID: "ce369011c189f62c815f5971d096b26759bab0d1"},
},
},
{
desc: "revision range with date order",
revisions: []string{
"master",
"^master~5",
"flat-path",
},
options: []RevlistOption{
WithOrder(OrderDate),
},
expectedResults: []RevisionResult{
{OID: "1e292f8fedd741b75372e19097c76d327140c312"},
{OID: "c1c67abbaf91f624347bb3ae96eabe3a1b742478"},
{OID: "7975be0116940bf2ad4321f79d02a55c5f7779aa"},
{OID: "c84ff944ff4529a70788a5e9003c2b7feae29047"},
{OID: "60ecb67744cb56576c30214ff52294f8ce2def98"},
{OID: "55bc176024cfa3baaceb71db584c7e5df900ea65"},
// The following commit is sorted differently in the previous
// testcase.
{OID: "ce369011c189f62c815f5971d096b26759bab0d1"},
{OID: "e63f41fe459e62e1228fcef60d7189127aeba95a"},
{OID: "4a24d82dbca5c11c61556f3b35ca472b7463187e"},
{OID: "b83d6e391c22777fca1ed3012fce84f633d7fed0"},
{OID: "498214de67004b1da3d820901307bed2a68a8ef6"},
},
},
{
desc: "revision range with dates",
revisions: []string{
"refs/heads/master",
},
options: []RevlistOption{
WithBefore(time.Date(2016, 6, 30, 18, 30, 0, 0, time.UTC)),
WithAfter(time.Date(2016, 6, 30, 18, 28, 0, 0, time.UTC)),
},
expectedResults: []RevisionResult{
{OID: "6907208d755b60ebeacb2e9dfea74c92c3449a1f"},
{OID: "c347ca2e140aa667b968e51ed0ffe055501fe4f4"},
},
},
{
desc: "revision range with author",
revisions: []string{
"refs/heads/master",
},
options: []RevlistOption{
WithAuthor([]byte("Sytse")),
},
expectedResults: []RevisionResult{
{OID: "e56497bb5f03a90a51293fc6d516788730953899"},
},
},
{
desc: "first parent chain",
revisions: []string{
"master",
"^master~4",
},
options: []RevlistOption{
WithFirstParent(),
},
expectedResults: []RevisionResult{
{OID: "1e292f8fedd741b75372e19097c76d327140c312"},
{OID: "7975be0116940bf2ad4321f79d02a55c5f7779aa"},
{OID: "60ecb67744cb56576c30214ff52294f8ce2def98"},
{OID: "e63f41fe459e62e1228fcef60d7189127aeba95a"},
},
},
{
// This is a tree object with multiple blobs. We cannot directly filter
// blobs given that Git will always print whatever's been provided on the
// command line. While we can already fix this with Git v2.32.0 via
// the new `--filter-provided` option, let's defer this fix to a later
// point. We demonstrate that this option is working by having the same test
// twice, once without and once with limit.
desc: "tree with multiple blobs without limit",
revisions: []string{
"79d5f98270ad677c86a7e1ab2baa922958565135",
},
options: []RevlistOption{
WithObjects(),
},
expectedResults: []RevisionResult{
{OID: "79d5f98270ad677c86a7e1ab2baa922958565135"},
{OID: "8af7f880ce38649fc49f66e3f38857bfbec3f0b7", ObjectName: []byte("feature-1.txt")},
{OID: "16ca0b267f82cd2f5ca1157dd162dae98745eab8", ObjectName: []byte("feature-2.txt")},
{OID: "0fb47f093f769008049a0b0976ac3fa6d6125033", ObjectName: []byte("hotfix-1.txt")},
{OID: "4ae6c5e14452a35d04156277ae63e8356eb17cae", ObjectName: []byte("hotfix-2.txt")},
{OID: "b988ffed90cb6a9b7f98a3686a933edb3c5d70c0", ObjectName: []byte("iso8859.txt")},
{OID: "570f8e1dfe8149c1d17002712310d43dfeb43159", ObjectName: []byte("russian.rb")},
{OID: "7a17968582c21c9153ec24c6a9d5f33592ad9103", ObjectName: []byte("test.txt")},
{OID: "f3064a3aa9c14277483f690250072e987e2c8356", ObjectName: []byte("\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88.txt")},
{OID: "3a26c18b02e843b459732e7ade7ab9a154a1002b", ObjectName: []byte("\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88.xls")},
},
},
{
// And now the second time we execute this test with a limit and see that we
// get less blobs as result.
desc: "tree with multiple blobs with limit",
revisions: []string{
"79d5f98270ad677c86a7e1ab2baa922958565135",
},
options: []RevlistOption{
WithObjects(),
WithBlobLimit(10),
},
expectedResults: []RevisionResult{
{OID: "79d5f98270ad677c86a7e1ab2baa922958565135"},
{OID: "0fb47f093f769008049a0b0976ac3fa6d6125033", ObjectName: []byte("hotfix-1.txt")},
{OID: "4ae6c5e14452a35d04156277ae63e8356eb17cae", ObjectName: []byte("hotfix-2.txt")},
{OID: "b988ffed90cb6a9b7f98a3686a933edb3c5d70c0", ObjectName: []byte("iso8859.txt")},
},
},
{
desc: "tree with blob object type filter",
revisions: []string{
"79d5f98270ad677c86a7e1ab2baa922958565135",
},
options: []RevlistOption{
WithObjects(),
WithObjectTypeFilter(ObjectTypeBlob),
},
expectedResults: []RevisionResult{
{OID: "8af7f880ce38649fc49f66e3f38857bfbec3f0b7", ObjectName: []byte("feature-1.txt")},
{OID: "16ca0b267f82cd2f5ca1157dd162dae98745eab8", ObjectName: []byte("feature-2.txt")},
{OID: "0fb47f093f769008049a0b0976ac3fa6d6125033", ObjectName: []byte("hotfix-1.txt")},
{OID: "4ae6c5e14452a35d04156277ae63e8356eb17cae", ObjectName: []byte("hotfix-2.txt")},
{OID: "b988ffed90cb6a9b7f98a3686a933edb3c5d70c0", ObjectName: []byte("iso8859.txt")},
{OID: "570f8e1dfe8149c1d17002712310d43dfeb43159", ObjectName: []byte("russian.rb")},
{OID: "7a17968582c21c9153ec24c6a9d5f33592ad9103", ObjectName: []byte("test.txt")},
{OID: "f3064a3aa9c14277483f690250072e987e2c8356", ObjectName: []byte("\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88.txt")},
{OID: "3a26c18b02e843b459732e7ade7ab9a154a1002b", ObjectName: []byte("\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88.xls")},
},
},
{
desc: "tree with tag object type filter",
revisions: []string{
"--all",
},
options: []RevlistOption{
WithObjects(),
WithObjectTypeFilter(ObjectTypeTag),
},
expectedResults: []RevisionResult{
{OID: "f4e6814c3e4e7a0de82a9e7cd20c626cc963a2f8", ObjectName: []byte("v1.0.0")},
{OID: "8a2a6eb295bb170b34c24c76c49ed0e9b2eaf34b", ObjectName: []byte("v1.1.0")},
{OID: "8f03acbcd11c53d9c9468078f32a2622005a4841", ObjectName: []byte("v1.1.1")},
},
},
{
desc: "tree with commit object type filter",
revisions: []string{
"79d5f98270ad677c86a7e1ab2baa922958565135",
},
options: []RevlistOption{
WithObjects(),
WithObjectTypeFilter(ObjectTypeTree),
},
expectedResults: []RevisionResult{
{OID: "79d5f98270ad677c86a7e1ab2baa922958565135"},
},
},
{
desc: "tree with commit object type filter",
revisions: []string{
"^refs/heads/master~",
"refs/heads/master",
},
options: []RevlistOption{
WithObjects(),
WithObjectTypeFilter(ObjectTypeCommit),
},
expectedResults: []RevisionResult{
{OID: "1e292f8fedd741b75372e19097c76d327140c312"},
{OID: "c1c67abbaf91f624347bb3ae96eabe3a1b742478"},
},
},
{
desc: "tree with object type and blob size filter",
revisions: []string{
"79d5f98270ad677c86a7e1ab2baa922958565135",
},
options: []RevlistOption{
WithObjects(),
WithBlobLimit(10),
WithObjectTypeFilter(ObjectTypeBlob),
},
expectedResults: []RevisionResult{
{OID: "0fb47f093f769008049a0b0976ac3fa6d6125033", ObjectName: []byte("hotfix-1.txt")},
{OID: "4ae6c5e14452a35d04156277ae63e8356eb17cae", ObjectName: []byte("hotfix-2.txt")},
{OID: "b988ffed90cb6a9b7f98a3686a933edb3c5d70c0", ObjectName: []byte("iso8859.txt")},
},
},
{
desc: "invalid revision",
revisions: []string{
"refs/heads/does-not-exist",
},
expectedErr: errors.New("rev-list pipeline command: exit status 128"),
},
{
desc: "mixed valid and invalid revision",
revisions: []string{
lfsPointer1,
"refs/heads/does-not-exist",
},
expectedErr: errors.New("rev-list pipeline command: exit status 128"),
},
} {
t.Run(tc.desc, func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
it := Revlist(ctx, repo, tc.revisions, tc.options...)
var results []RevisionResult
for it.Next() {
results = append(results, it.Result())
}
// We're converting the error here to a plain un-nested error such that we
// don't have to replicate the complete error's structure.
err := it.Err()
if err != nil {
err = errors.New(err.Error())
}
require.Equal(t, tc.expectedErr, err)
require.Equal(t, tc.expectedResults, results)
})
}
}
func TestForEachRef(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
readRefs := func(t *testing.T, repo *localrepo.Repo, patterns ...string) []RevisionResult {
it := ForEachRef(ctx, repo, patterns, "")
var results []RevisionResult
for it.Next() {
results = append(results, it.Result())
}
require.NoError(t, it.Err())
return results
}
cfg, repoProto, _ := testcfg.BuildWithRepo(t)
repo := localrepo.NewTestRepo(t, cfg, repoProto)
revisions := make(map[string]git.ObjectID)
for _, reference := range []string{"refs/heads/master", "refs/heads/feature"} {
revision, err := repo.ResolveRevision(ctx, git.Revision(reference))
require.NoError(t, err)
revisions[reference] = revision
}
t.Run("single fully qualified branch", func(t *testing.T) {
require.Equal(t, []RevisionResult{
{
ObjectName: []byte("refs/heads/master"),
OID: revisions["refs/heads/master"],
},
}, readRefs(t, repo, "refs/heads/master"))
})
t.Run("unqualified branch name", func(t *testing.T) {
require.Nil(t, readRefs(t, repo, "master"))
})
t.Run("multiple branches", func(t *testing.T) {
require.Equal(t, []RevisionResult{
{
ObjectName: []byte("refs/heads/feature"),
OID: revisions["refs/heads/feature"],
},
{
ObjectName: []byte("refs/heads/master"),
OID: revisions["refs/heads/master"],
},
}, readRefs(t, repo, "refs/heads/master", "refs/heads/feature"))
})
t.Run("branches pattern", func(t *testing.T) {
refs := readRefs(t, repo, "refs/heads/*")
require.Greater(t, len(refs), 90)
require.Subset(t, refs, []RevisionResult{
{
ObjectName: []byte("refs/heads/master"),
OID: revisions["refs/heads/master"],
},
{
ObjectName: []byte("refs/heads/feature"),
OID: revisions["refs/heads/feature"],
},
})
})
t.Run("multiple patterns", func(t *testing.T) {
refs := readRefs(t, repo, "refs/heads/*", "refs/tags/*")
require.Greater(t, len(refs), 90)
})
t.Run("nonexisting branch", func(t *testing.T) {
require.Nil(t, readRefs(t, repo, "refs/heads/idontexist"))
})
t.Run("nonexisting pattern", func(t *testing.T) {
require.Nil(t, readRefs(t, repo, "refs/idontexist/*"))
})
}
func TestRevisionFilter(t *testing.T) {
for _, tc := range []struct {
desc string
input []RevisionResult
filter func(RevisionResult) bool
expectedResults []RevisionResult
expectedErr error
}{
{
desc: "all accepted",
input: []RevisionResult{
{OID: "a"},
{OID: "b"},
{OID: "c"},
},
filter: func(RevisionResult) bool {
return true
},
expectedResults: []RevisionResult{
{OID: "a"},
{OID: "b"},
{OID: "c"},
},
},
{
desc: "all filtered",
input: []RevisionResult{
{OID: "a"},
{OID: "b"},
{OID: "c"},
},
filter: func(RevisionResult) bool {
return false
},
expectedResults: nil,
},
{
desc: "errors always get through",
input: []RevisionResult{
{OID: "a"},
{OID: "b"},
{err: errors.New("foobar")},
{OID: "c"},
},
filter: func(RevisionResult) bool {
return false
},
expectedErr: errors.New("foobar"),
},
{
desc: "subset filtered",
input: []RevisionResult{
{OID: "a"},
{OID: "b"},
{OID: "c"},
},
filter: func(r RevisionResult) bool {
return r.OID == "b"
},
expectedResults: []RevisionResult{
{OID: "b"},
},
},
} {
t.Run(tc.desc, func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
it := RevisionFilter(ctx, NewRevisionIterator(tc.input), tc.filter)
var results []RevisionResult
for it.Next() {
results = append(results, it.Result())
}
require.Equal(t, tc.expectedErr, it.Err())
require.Equal(t, tc.expectedResults, results)
})
}
}
func TestRevisionTransform(t *testing.T) {
for _, tc := range []struct {
desc string
input []RevisionResult
transform func(RevisionResult) []RevisionResult
expectedResults []RevisionResult
expectedErr error
}{
{
desc: "identity mapping",
input: []RevisionResult{
{OID: "a"},
{OID: "b"},
{OID: "c"},
},
transform: func(r RevisionResult) []RevisionResult {
return []RevisionResult{r}
},
expectedResults: []RevisionResult{
{OID: "a"},
{OID: "b"},
{OID: "c"},
},
},
{
desc: "strip object",
input: []RevisionResult{
{OID: "a"},
{OID: "b"},
{OID: "c"},
},
transform: func(r RevisionResult) []RevisionResult {
if r.OID == "b" {
return []RevisionResult{}
}
return []RevisionResult{r}
},
expectedResults: []RevisionResult{
{OID: "a"},
{OID: "c"},
},
},
{
desc: "replace items",
input: []RevisionResult{
{OID: "a"},
{OID: "b"},
{OID: "c"},
},
transform: func(RevisionResult) []RevisionResult {
return []RevisionResult{{OID: "x"}}
},
expectedResults: []RevisionResult{
{OID: "x"},
{OID: "x"},
{OID: "x"},
},
},
{
desc: "add additional items",
input: []RevisionResult{
{OID: "a"},
{OID: "b"},
{OID: "c"},
},
transform: func(r RevisionResult) []RevisionResult {
return []RevisionResult{
r,
{OID: r.OID + "x"},
}
},
expectedResults: []RevisionResult{
{OID: "a"},
{OID: "ax"},
{OID: "b"},
{OID: "bx"},
{OID: "c"},
{OID: "cx"},
},
},
{
desc: "error handling",
input: []RevisionResult{
{OID: "a"},
{OID: "b"},
{err: errors.New("foobar")},
{OID: "c"},
},
transform: func(r RevisionResult) []RevisionResult {
return []RevisionResult{r}
},
expectedResults: []RevisionResult{
{OID: "a"},
{OID: "b"},
},
expectedErr: errors.New("foobar"),
},
} {
t.Run(tc.desc, func(t *testing.T) {
ctx, cancel := testhelper.Context()
defer cancel()
it := RevisionTransform(ctx, NewRevisionIterator(tc.input), tc.transform)
var results []RevisionResult
for it.Next() {
results = append(results, it.Result())
}
require.Equal(t, tc.expectedErr, it.Err())
require.Equal(t, tc.expectedResults, results)
})
}
}
|