config: imports are relative to tf

This commit is contained in:
Mitchell Hashimoto 2014-05-23 15:11:57 -07:00
parent 88bb42b5a4
commit 2ffee2a142
4 changed files with 44 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package config
import (
"fmt"
"path/filepath"
"github.com/mitchellh/go-libucl"
)
@ -96,6 +97,12 @@ func loadTreeLibucl(root string) (*libuclImportTree, error) {
// Load them all
result.Children = make([]*libuclImportTree, len(importPaths))
for i, path := range importPaths {
if !filepath.IsAbs(path) {
// Relative paths are relative to the Terraform file itself
dir := filepath.Dir(root)
path = filepath.Join(dir, path)
}
imp, err := loadTreeLibucl(path)
if err != nil {
return nil, err

View File

@ -28,6 +28,29 @@ func TestLoadBasic(t *testing.T) {
}
}
func TestLoadBasic_import(t *testing.T) {
c, err := Load(filepath.Join(fixtureDir, "import.tf"))
if err != nil {
t.Fatalf("err: %s", err)
}
if c == nil {
t.Fatal("config should not be nil")
}
actual := variablesStr(c.Variables)
if actual != strings.TrimSpace(importVariablesStr) {
t.Fatalf("bad:\n%s", actual)
}
/*
actual = resourcesStr(c.Resources)
if actual != strings.TrimSpace(basicResourcesStr) {
t.Fatalf("bad:\n%s", actual)
}
*/
}
// This helper turns a resources field into a deterministic
// string value for comparison in tests.
func resourcesStr(rs []Resource) string {
@ -73,3 +96,10 @@ foo
bar
bar
`
const importVariablesStr = `
foo
bar
bar
bar
`

View File

@ -0,0 +1,6 @@
import "import/one.tf";
variable "foo" {
default = "bar";
description = "bar";
}

View File

@ -0,0 +1 @@
variable "bar" {}