data: fix invalid trees used in test cases

data.TestCreateSnapshot which is used in particular by TestFindUsedBlobs
and TestFindUsedBlobs could generate trees with duplicate file names.
This is invalid and going forward will result in an error.
This commit is contained in:
Michael Eischer
2025-11-21 22:26:59 +01:00
parent 34fdf5ba96
commit d82ea53735
7 changed files with 24 additions and 20 deletions
+4
View File
@@ -127,6 +127,10 @@ func LoadTree(ctx context.Context, r restic.BlobLoader, id restic.ID) (*Tree, er
// checked against the index. The tree is only stored when the index does not
// contain the ID.
func SaveTree(ctx context.Context, r restic.BlobSaver, t *Tree) (restic.ID, error) {
if t.Nodes == nil {
// serialize an empty tree as `{"nodes":[]}` to be consistent with TreeJSONBuilder
t.Nodes = make([]*Node, 0)
}
buf, err := json.Marshal(t)
if err != nil {
return restic.ID{}, errors.Wrap(err, "MarshalJSON")