config/module: error if duplicate module

This commit is contained in:
Mitchell Hashimoto 2014-09-15 09:53:29 -07:00
parent 4fdb6d1b52
commit 2419bf79f2
4 changed files with 34 additions and 7 deletions

View File

@ -0,0 +1,7 @@
module "foo" {
source = "./foo"
}
module "foo" {
source = "./foo"
}

View File

@ -18,7 +18,7 @@ import (
type Tree struct {
name string
config *config.Config
children []*Tree
children map[string]*Tree
lock sync.RWMutex
}
@ -50,7 +50,7 @@ func NewTree(c *config.Config) *Tree {
// imported by this root).
//
// This will only return a non-nil value after Load is called.
func (t *Tree) Children() []*Tree {
func (t *Tree) Children() map[string]*Tree {
t.lock.RLock()
defer t.lock.RUnlock()
return t.children
@ -119,11 +119,16 @@ func (t *Tree) Load(s Storage, mode GetMode) error {
t.children = nil
modules := t.Modules()
children := make([]*Tree, len(modules))
children := make(map[string]*Tree)
// Go through all the modules and get the directory for them.
update := mode == GetModeUpdate
for i, m := range modules {
for _, m := range modules {
if _, ok := children[m.Name]; ok {
return fmt.Errorf(
"module %s: duplicated. module names must be unique", m.Name)
}
source, err := Detect(m.Source, t.config.Dir)
if err != nil {
return fmt.Errorf("module %s: %s", m.Name, err)
@ -152,8 +157,9 @@ func (t *Tree) Load(s Storage, mode GetMode) error {
return fmt.Errorf(
"module %s: %s", m.Name, err)
}
children[i] = NewTree(c)
children[i].name = m.Name
children[m.Name] = NewTree(c)
children[m.Name].name = m.Name
}
// Go through all the children and load them.
@ -248,7 +254,7 @@ func (e *ValidateError) Error() string {
buf.WriteString(n)
buf.WriteString(".")
}
buf.Truncate(buf.Len()-1)
buf.Truncate(buf.Len() - 1)
// Format the value
return fmt.Sprintf("module %s: %s", buf.String(), e.Err)

View File

@ -44,6 +44,20 @@ func TestTreeLoad(t *testing.T) {
}
}
func TestTreeLoad_duplicate(t *testing.T) {
storage := testStorage(t)
tree := NewTree(testConfig(t, "dup"))
if tree.Loaded() {
t.Fatal("should not be loaded")
}
// This should get things
if err := tree.Load(storage, GetModeGet); err == nil {
t.Fatalf("should error")
}
}
func TestTreeModules(t *testing.T) {
tree := NewTree(testConfig(t, "basic"))
actual := tree.Modules()