From 2ffee2a142bb4fccafb479b7d26d2047e75feb60 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 23 May 2014 15:11:57 -0700 Subject: [PATCH] config: imports are relative to tf --- config/loader_libucl.go | 7 +++++++ config/loader_test.go | 30 ++++++++++++++++++++++++++++++ config/test-fixtures/import.tf | 6 ++++++ config/test-fixtures/import/one.tf | 1 + 4 files changed, 44 insertions(+) create mode 100644 config/test-fixtures/import.tf create mode 100644 config/test-fixtures/import/one.tf diff --git a/config/loader_libucl.go b/config/loader_libucl.go index 89c71f2d2..b020e8b89 100644 --- a/config/loader_libucl.go +++ b/config/loader_libucl.go @@ -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 diff --git a/config/loader_test.go b/config/loader_test.go index 332b74f5c..1eb4b3d09 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -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 +` diff --git a/config/test-fixtures/import.tf b/config/test-fixtures/import.tf new file mode 100644 index 000000000..af3434b60 --- /dev/null +++ b/config/test-fixtures/import.tf @@ -0,0 +1,6 @@ +import "import/one.tf"; + +variable "foo" { + default = "bar"; + description = "bar"; +} diff --git a/config/test-fixtures/import/one.tf b/config/test-fixtures/import/one.tf new file mode 100644 index 000000000..326c1171d --- /dev/null +++ b/config/test-fixtures/import/one.tf @@ -0,0 +1 @@ +variable "bar" {}