config/module: tree.Modules()

This commit is contained in:
Mitchell Hashimoto 2014-09-14 14:46:45 -07:00
parent 8dc8eac4bf
commit 799ffbb3ac
4 changed files with 51 additions and 1 deletions

View File

@ -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)

View File

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

View File

@ -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.

View File

@ -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)
}
}