restic: correct blob count in BlobSet.String summary

The "(N more)" summary printed when a BlobSet is truncated to ten
elements was computed as len(s)-n-1 instead of len(s)-n, so it undercounted
the unlisted elements by one and even became negative ("(-1 more)") when
exactly ten elements were present. This is user-visible in the 'not found in
the index' error printed by prune when the index is incomplete.
This commit is contained in:
Théo BARRAGUÉ authored and Michael Eischer committed 2026-11-29 15:34:29 +01:00
1 parent 0cd0df6346
commit 4a9941c1e1
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)
})
}
}