diff --git a/internal/restic/blob_set.go b/internal/restic/blob_set.go index 928d0fb68..02de10649 100644 --- a/internal/restic/blob_set.go +++ b/internal/restic/blob_set.go @@ -122,8 +122,8 @@ func (s BlobSet) String() string { } sb.WriteString(k.String()) - if n++; n == maxelems { - fmt.Fprintf(sb, " (%d more)", len(s)-n-1) + if n++; n == maxelems && n < len(s) { + fmt.Fprintf(sb, " (%d more)", len(s)-n) break } } diff --git a/internal/restic/blob_set_test.go b/internal/restic/blob_set_test.go index 4786dd2d4..a1fa5a15e 100644 --- a/internal/restic/blob_set_test.go +++ b/internal/restic/blob_set_test.go @@ -1,6 +1,7 @@ package restic import ( + "fmt" "math/rand" "regexp" "testing" @@ -28,7 +29,44 @@ func TestBlobSetString(t *testing.T) { } r := regexp.MustCompile( - `^{(?:<(?:data|tree)/[0-9a-f]{8}> ){10}\(90 more\)}$`) + `^{(?:<(?:data|tree)/[0-9a-f]{8}> ){10}\(91 more\)}$`) str := s.String() rtest.Assert(t, r.MatchString(str), "%q doesn't match pattern", str) } + +// makeBlobSet returns a set with n distinct data blob handles. +func makeBlobSet(n int) BlobSet { + s := NewBlobSet() + for i := range n { + id, err := ParseID(fmt.Sprintf("%064x", i+1)) + if err != nil { + panic(err) + } + s.Insert(BlobHandle{ID: id, Type: DataBlob}) + } + return s +} + +// TestBlobSetStringLimits checks the summary printed by BlobSet.String at and +// around the 10 element display limit: the "(N more)" count must equal the +// number of elements that were not listed. +func TestBlobSetStringLimits(t *testing.T) { + for _, tt := range []struct { + count int + want string + }{ + {0, `^{}$`}, + {1, `^{}$`}, + {9, `^{(?: ){8}}$`}, // below the limit: no summary + {10, `^{(?: ){9}}$`}, // exactly at the limit: no summary + {11, `^{(?: ){10}\(1 more\)}$`}, // one above the limit + {12, `^{(?: ){10}\(2 more\)}$`}, + {101, `^{(?: ){10}\(91 more\)}$`}, + } { + t.Run(fmt.Sprintf("n=%d", tt.count), func(t *testing.T) { + r := regexp.MustCompile(tt.want) + str := makeBlobSet(tt.count).String() + rtest.Assert(t, r.MatchString(str), "%q doesn't match pattern %q", str, tt.want) + }) + } +}