test get root w deleted

master
Bel LaPointe 2022-02-09 11:43:42 -07:00
parent 2a278a33e0
commit c4b63825aa
2 changed files with 14 additions and 7 deletions

View File

@ -67,21 +67,21 @@ func (tree Tree) Find(baseId string) ([]string, bool) {
} }
func (tree Tree) GetRootMeta() (Branch, error) { func (tree Tree) GetRootMeta() (Branch, error) {
return tree.getRoot(false) return tree.getRoot(false, false)
} }
func (tree Tree) GetRoot() (Branch, error) { func (tree Tree) GetRoot() (Branch, error) {
if !tree.cachedRoot.IsZero() { if !tree.cachedRoot.IsZero() {
return tree.cachedRoot, nil return tree.cachedRoot, nil
} }
got, err := tree.getRoot(true) got, err := tree.getRoot(true, false)
if err == nil { if err == nil {
tree.cachedRoot = got tree.cachedRoot = got
} }
return got, err return got, err
} }
func (tree Tree) getRoot(withContent bool) (Branch, error) { func (tree Tree) getRoot(withContent, withDeleted bool) (Branch, error) {
m := Branch{Branches: map[string]Branch{}} m := Branch{Branches: map[string]Branch{}}
entries, err := os.ReadDir(tree.root) entries, err := os.ReadDir(tree.root)
if os.IsNotExist(err) { if os.IsNotExist(err) {
@ -100,13 +100,14 @@ func (tree Tree) getRoot(withContent bool) (Branch, error) {
if !withContent { if !withContent {
m.Leaf.Content = "" m.Leaf.Content = ""
} }
if m.Leaf.Deleted { if m.Leaf.Deleted && !withDeleted {
return m, nil return m, nil
} }
} else if entry.IsDir() { } else if entry.IsDir() {
if branch, err := tree.WithRoot(path.Join(tree.root, entry.Name())).getRoot(withContent); err != nil { subtree := tree.WithRoot(path.Join(tree.root, entry.Name()))
if branch, err := subtree.getRoot(withContent, withDeleted); err != nil {
return Branch{}, err return Branch{}, err
} else if !branch.Leaf.Deleted && !branch.IsZero() { } else if !branch.IsZero() && (!branch.Leaf.Deleted || withDeleted) {
m.Branches[entry.Name()] = branch m.Branches[entry.Name()] = branch
} }
} }

View File

@ -26,7 +26,13 @@ func TestTreeDel(t *testing.T) {
if root, err := tree.GetRoot(); err != nil { if root, err := tree.GetRoot(); err != nil {
t.Fatal(err) t.Fatal(err)
} else if len(root.Branches) > 0 { } else if len(root.Branches) != 0 {
t.Fatal(root.Branches)
}
if root, err := tree.getRoot(false, true); err != nil {
t.Fatal(err)
} else if len(root.Branches) != 1 {
t.Fatal(root.Branches) t.Fatal(root.Branches)
} }
} }