diff --git a/config/module/module_test.go b/config/module/module_test.go index 8360a8bf22..2ce7229de5 100644 --- a/config/module/module_test.go +++ b/config/module/module_test.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" "testing" + + "github.com/hashicorp/terraform/config" ) const fixtureDir = "./test-fixtures" @@ -22,6 +24,15 @@ func tempDir(t *testing.T) string { return dir } +func testConfig(t *testing.T, n string) *config.Config { + c, err := config.LoadDir(filepath.Join(fixtureDir, n)) + if err != nil { + t.Fatalf("err: %s", err) + } + + return c +} + func testModule(n string) string { p := filepath.Join(fixtureDir, n) p, err := filepath.Abs(p) diff --git a/config/module/test-fixtures/basic/main.tf b/config/module/test-fixtures/basic/main.tf index fec56017dc..3830637158 100644 --- a/config/module/test-fixtures/basic/main.tf +++ b/config/module/test-fixtures/basic/main.tf @@ -1 +1,5 @@ # Hello + +module "foo" { + source = "./foo" +} diff --git a/config/module/tree.go b/config/module/tree.go index 851057c0f0..9a64a7dc8b 100644 --- a/config/module/tree.go +++ b/config/module/tree.go @@ -33,6 +33,11 @@ const ( GetModeUpdate ) +// NewTree returns a new Tree for the given config structure. +func NewTree(c *config.Config) *Tree { + return &Tree{Config: c} +} + // Flatten takes the entire module tree and flattens it into a single // namespace in *config.Config with no module imports. // @@ -45,8 +50,19 @@ func (t *Tree) Flatten() (*config.Config, error) { } // Modules returns the list of modules that this tree imports. +// +// This is only the imports of _this_ level of the tree. To retrieve the +// full nested imports, you'll have to traverse the tree. func (t *Tree) Modules() []*Module { - return nil + result := make([]*Module, len(t.Config.Modules)) + for i, m := range t.Config.Modules { + result[i] = &Module{ + Name: m.Name, + Source: m.Source, + } + } + + return result } // Load loads the configuration of the entire tree. diff --git a/config/module/tree_test.go b/config/module/tree_test.go new file mode 100644 index 0000000000..27fd1f06d4 --- /dev/null +++ b/config/module/tree_test.go @@ -0,0 +1,19 @@ +package module + +import ( + "reflect" + "testing" +) + +func TestTree(t *testing.T) { + tree := NewTree(testConfig(t, "basic")) + actual := tree.Modules() + + expected := []*Module{ + &Module{Name: "foo", Source: "./foo"}, + } + + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("bad: %#v", actual) + } +}