config/module: Can look up Child with Tree.Child

This commit is contained in:
Mitchell Hashimoto 2014-10-07 20:00:36 -07:00
parent 679ab1d515
commit 267d45df86
5 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,2 @@
# Hello

View File

@ -0,0 +1,5 @@
# Hello
module "bar" {
source = "./bar"
}

View File

@ -0,0 +1,5 @@
# Hello
module "foo" {
source = "./foo"
}

View File

@ -67,6 +67,24 @@ func (t *Tree) Config() *config.Config {
return t.config return t.config
} }
// Child returns the child with the given path (by name).
func (t *Tree) Child(path []string) *Tree {
if len(path) == 0 {
return nil
}
c := t.Children()[path[0]]
if c == nil {
return nil
}
if len(path) == 1 {
return c
}
return c.Child(path[1:])
}
// Children returns the children of this tree (the modules that are // Children returns the children of this tree (the modules that are
// imported by this root). // imported by this root).
// //

View File

@ -6,6 +6,28 @@ import (
"testing" "testing"
) )
func TestTreeChild(t *testing.T) {
storage := testStorage(t)
tree := NewTree("", testConfig(t, "child"))
if err := tree.Load(storage, GetModeGet); err != nil {
t.Fatalf("err: %s", err)
}
// Should be able to get the foo child
if c := tree.Child([]string{"foo"}); c == nil {
t.Fatal("should not be nil")
} else if c.Name() != "foo" {
t.Fatalf("bad: %#v", c.Name())
}
// Should be able to get the nested child
if c := tree.Child([]string{"foo", "bar"}); c == nil {
t.Fatal("should not be nil")
} else if c.Name() != "bar" {
t.Fatalf("bad: %#v", c.Name())
}
}
func TestTreeLoad(t *testing.T) { func TestTreeLoad(t *testing.T) {
storage := testStorage(t) storage := testStorage(t)
tree := NewTree("", testConfig(t, "basic")) tree := NewTree("", testConfig(t, "basic"))