From 321cc35cded6bf361812fe6668236d780803ab6a Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sun, 16 Jul 2023 15:43:27 +0200 Subject: [PATCH] restic: add test for FindTreeDirectory --- internal/restic/tree_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/internal/restic/tree_test.go b/internal/restic/tree_test.go index fb25ca373..62a0c7def 100644 --- a/internal/restic/tree_test.go +++ b/internal/restic/tree_test.go @@ -210,3 +210,37 @@ func benchmarkLoadTree(t *testing.B, version uint) { rtest.OK(t, err) } } + +func TestFindTreeDirectory(t *testing.T) { + repo := repository.TestRepository(t) + sn := restic.TestCreateSnapshot(t, repo, parseTimeUTC("2017-07-07 07:07:08"), 3, 0) + + for _, exp := range []struct { + subpath string + id restic.ID + err error + }{ + {"", restic.TestParseID("c25199703a67455b34cc0c6e49a8ac8861b268a5dd09dc5b2e31e7380973fc97"), nil}, + {"/", restic.TestParseID("c25199703a67455b34cc0c6e49a8ac8861b268a5dd09dc5b2e31e7380973fc97"), nil}, + {".", restic.TestParseID("c25199703a67455b34cc0c6e49a8ac8861b268a5dd09dc5b2e31e7380973fc97"), nil}, + {"..", restic.ID{}, errors.New("path ..: not found")}, + {"file-1", restic.ID{}, errors.New("path file-1: not a directory")}, + {"dir-21", restic.TestParseID("76172f9dec15d7e4cb98d2993032e99f06b73b2f02ffea3b7cfd9e6b4d762712"), nil}, + {"/dir-21", restic.TestParseID("76172f9dec15d7e4cb98d2993032e99f06b73b2f02ffea3b7cfd9e6b4d762712"), nil}, + {"dir-21/", restic.TestParseID("76172f9dec15d7e4cb98d2993032e99f06b73b2f02ffea3b7cfd9e6b4d762712"), nil}, + {"dir-21/dir-24", restic.TestParseID("74626b3fb2bd4b3e572b81a4059b3e912bcf2a8f69fecd9c187613b7173f13b1"), nil}, + } { + t.Run("", func(t *testing.T) { + id, err := restic.FindTreeDirectory(context.TODO(), repo, sn.Tree, exp.subpath) + if exp.err == nil { + rtest.OK(t, err) + rtest.Assert(t, exp.id == *id, "unexpected id, expected %v, got %v", exp.id, id) + } else { + rtest.Assert(t, exp.err.Error() == err.Error(), "unexpected err, expected %v, got %v", exp.err, err) + } + }) + } + + _, err := restic.FindTreeDirectory(context.TODO(), repo, nil, "") + rtest.Assert(t, err != nil, "missing error on null tree id") +}