rewriter: test KeepEmptyDirectory option

This commit is contained in:
Michael Eischer 2026-01-31 21:56:23 +01:00
parent 0d71f70a22
commit 74d60ad223

View file

@ -329,6 +329,74 @@ func TestSnapshotSizeQuery(t *testing.T) {
}
func TestRewriterKeepEmptyDirectory(t *testing.T) {
var paths []string
tests := []struct {
name string
keepEmpty NodeKeepEmptyDirectoryFunc
assert func(t *testing.T, newRoot restic.ID)
}{
{
name: "Keep",
keepEmpty: func(string) bool { return true },
assert: func(t *testing.T, newRoot restic.ID) {
_, expRoot := BuildTreeMap(TestTree{"empty": TestTree{}})
test.Assert(t, newRoot == expRoot, "expected empty dir kept")
},
},
{
name: "Drop subdir only",
keepEmpty: func(p string) bool { return p != "/empty" },
assert: func(t *testing.T, newRoot restic.ID) {
_, expRoot := BuildTreeMap(TestTree{})
test.Assert(t, newRoot == expRoot, "expected empty root")
},
},
{
name: "Drop all",
keepEmpty: func(string) bool { return false },
assert: func(t *testing.T, newRoot restic.ID) {
test.Assert(t, newRoot.IsNull(), "expected null root")
},
},
{
name: "Paths",
keepEmpty: func(p string) bool {
paths = append(paths, p)
return p != "/empty"
},
assert: func(t *testing.T, newRoot restic.ID) {
test.Assert(t, len(paths) >= 2, "expected at least two KeepEmptyDirectory calls")
var hasRoot, hasEmpty bool
for _, p := range paths {
if p == "/" {
hasRoot = true
}
if p == "/empty" {
hasEmpty = true
}
}
test.Assert(t, hasRoot && hasEmpty, "expected paths \"/\" and \"/empty\", got %v", paths)
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
repo, root := BuildTreeMap(TestTree{"empty": TestTree{}})
modrepo := data.TestWritableTreeMap{TestTreeMap: repo}
rw := NewTreeRewriter(RewriteOpts{KeepEmptyDirectory: tc.keepEmpty})
newRoot, err := rw.RewriteTree(ctx, modrepo, modrepo, "/", root)
test.OK(t, err)
tc.assert(t, newRoot)
})
}
}
func TestRewriterFailOnUnknownFields(t *testing.T) {
tm := data.TestWritableTreeMap{TestTreeMap: data.TestTreeMap{}}
node := []byte(`{"nodes":[{"name":"subfile","type":"file","mtime":"0001-01-01T00:00:00Z","atime":"0001-01-01T00:00:00Z","ctime":"0001-01-01T00:00:00Z","uid":0,"gid":0,"content":null,"unknown_field":42}]}`)