diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go index f2ac87f1..6d8dbadc 100644 --- a/lib/memfs/memfs_test.go +++ b/lib/memfs/memfs_test.go @@ -437,6 +437,39 @@ func TestMemFS_MarshalJSON(t *testing.T) { } } +func TestMemFS_RemoveChild(t *testing.T) { + var ( + opts = &Options{ + Root: `testdata`, + MaxFileSize: -1, + } + mfs *MemFS + err error + ) + + mfs, err = New(opts) + if err != nil { + t.Fatal(err) + } + + var child = mfs.Root.Child(`plain`) + if child == nil { + t.Fatal(`Expecting child "plain", got nil`) + } + + var nodeRemoved = mfs.RemoveChild(mfs.Root, child) + if nodeRemoved == nil { + t.Fatal(`Expecting child "plain", got nil`) + } + + test.Assert(t, `RemoveChild`, child, nodeRemoved) + + child = mfs.Root.Child(`plain`) + if child != nil { + t.Fatalf(`Expecting child "plain" has been removed, got %v`, child) + } +} + func TestMemFS_isIncluded(t *testing.T) { cases := []struct { desc string diff --git a/lib/memfs/node.go b/lib/memfs/node.go index cbb2b5da..43f16be4 100644 --- a/lib/memfs/node.go +++ b/lib/memfs/node.go @@ -134,6 +134,16 @@ func (node *Node) AddChild(child *Node) { child.Parent = node } +// Child return the child node based on its name. +func (node *Node) Child(name string) (cnode *Node) { + for _, cnode = range node.Childs { + if cnode.name == name { + return cnode + } + } + return nil +} + // Close reset the offset position back to zero. func (node *Node) Close() error { node.off = 0