Merge pull request #22212 from theobarrague/fix/blobset-string-off-by-one

fix: correct blob count in BlobSet.String summary
This commit is contained in:
Michael Eischer authored and GitHub committed 2026-11-29 15:45:53 +01:00
commit 12073885f7
2 files changed
+41 -3

No files matched your search

+2 -2
View File
@@ -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
}
}
+39 -1
View File
@@ -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, `^{<data/[0-9a-f]{8}>}$`},
{9, `^{(?:<data/[0-9a-f]{8}> ){8}<data/[0-9a-f]{8}>}$`}, // below the limit: no summary
{10, `^{(?:<data/[0-9a-f]{8}> ){9}<data/[0-9a-f]{8}>}$`}, // exactly at the limit: no summary
{11, `^{(?:<data/[0-9a-f]{8}> ){10}\(1 more\)}$`}, // one above the limit
{12, `^{(?:<data/[0-9a-f]{8}> ){10}\(2 more\)}$`},
{101, `^{(?:<data/[0-9a-f]{8}> ){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)
})
}
}