From bd4aaac71a1607ec0b40096d44fab043e1a6a1bb Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 26 Mar 2015 09:11:32 -0700 Subject: [PATCH 1/3] config/module: failing unit test for GH-1232 --- .../module/test-fixtures/basic-parent/a/a.tf | 3 ++ .../module/test-fixtures/basic-parent/c/c.tf | 1 + .../module/test-fixtures/basic-parent/main.tf | 3 ++ config/module/tree_test.go | 38 +++++++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 config/module/test-fixtures/basic-parent/a/a.tf create mode 100644 config/module/test-fixtures/basic-parent/c/c.tf create mode 100644 config/module/test-fixtures/basic-parent/main.tf diff --git a/config/module/test-fixtures/basic-parent/a/a.tf b/config/module/test-fixtures/basic-parent/a/a.tf new file mode 100644 index 000000000..b9b44f464 --- /dev/null +++ b/config/module/test-fixtures/basic-parent/a/a.tf @@ -0,0 +1,3 @@ +module "b" { + source = "../c" +} diff --git a/config/module/test-fixtures/basic-parent/c/c.tf b/config/module/test-fixtures/basic-parent/c/c.tf new file mode 100644 index 000000000..fec56017d --- /dev/null +++ b/config/module/test-fixtures/basic-parent/c/c.tf @@ -0,0 +1 @@ +# Hello diff --git a/config/module/test-fixtures/basic-parent/main.tf b/config/module/test-fixtures/basic-parent/main.tf new file mode 100644 index 000000000..2326ee22a --- /dev/null +++ b/config/module/test-fixtures/basic-parent/main.tf @@ -0,0 +1,3 @@ +module "a" { + source = "./a" +} diff --git a/config/module/tree_test.go b/config/module/tree_test.go index d8a753522..2e4a82eec 100644 --- a/config/module/tree_test.go +++ b/config/module/tree_test.go @@ -94,6 +94,44 @@ func TestTreeLoad_duplicate(t *testing.T) { } } +func TestTreeLoad_parentRef(t *testing.T) { + storage := testStorage(t) + tree := NewTree("", testConfig(t, "basic-parent")) + + if tree.Loaded() { + t.Fatal("should not be loaded") + } + + // This should error because we haven't gotten things yet + if err := tree.Load(storage, GetModeNone); err == nil { + t.Fatal("should error") + } + + if tree.Loaded() { + t.Fatal("should not be loaded") + } + + // This should get things + if err := tree.Load(storage, GetModeGet); err != nil { + t.Fatalf("err: %s", err) + } + + if !tree.Loaded() { + t.Fatal("should be loaded") + } + + // This should no longer error + if err := tree.Load(storage, GetModeNone); err != nil { + t.Fatalf("err: %s", err) + } + + actual := strings.TrimSpace(tree.String()) + expected := strings.TrimSpace(treeLoadStr) + if actual != expected { + t.Fatalf("bad: \n\n%s", actual) + } +} + func TestTreeLoad_subdir(t *testing.T) { storage := testStorage(t) tree := NewTree("", testConfig(t, "basic-subdir")) From 2e11ca68df3e956dd7b23a924e4187cb481a25d8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 26 Mar 2015 09:30:32 -0700 Subject: [PATCH 2/3] config/module: go back to the original folder when doing parent references --- config/module/detect_file.go | 20 ++++++++++++++++++++ config/module/tree_test.go | 7 ++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/config/module/detect_file.go b/config/module/detect_file.go index 2b8dbacbe..a7eb4c49f 100644 --- a/config/module/detect_file.go +++ b/config/module/detect_file.go @@ -2,6 +2,7 @@ package module import ( "fmt" + "os" "path/filepath" "runtime" ) @@ -20,7 +21,26 @@ func (d *FileDetector) Detect(src, pwd string) (string, bool, error) { "relative paths require a module with a pwd") } + // Stat the pwd to determine if its a symbolic link. If it is, + // then the pwd becomes the original directory. Otherwise, + // `filepath.Join` below does some weird stuff. + // + // We just ignore if the pwd doesn't exist. That error will be + // caught later when we try to use the URL. + if fi, err := os.Lstat(pwd); !os.IsNotExist(err) { + if err != nil { + return "", true, err + } + if fi.Mode()&os.ModeSymlink != 0 { + pwd, err = os.Readlink(pwd) + if err != nil { + return "", true, err + } + } + } + src = filepath.Join(pwd, src) + println(src) } return fmtFileURL(src), true, nil } diff --git a/config/module/tree_test.go b/config/module/tree_test.go index 2e4a82eec..9ac0c582d 100644 --- a/config/module/tree_test.go +++ b/config/module/tree_test.go @@ -126,7 +126,7 @@ func TestTreeLoad_parentRef(t *testing.T) { } actual := strings.TrimSpace(tree.String()) - expected := strings.TrimSpace(treeLoadStr) + expected := strings.TrimSpace(treeLoadParentStr) if actual != expected { t.Fatalf("bad: \n\n%s", actual) } @@ -277,6 +277,11 @@ root foo ` +const treeLoadParentStr = ` +root + a + b +` const treeLoadSubdirStr = ` root foo From 44fce5ce600027f251353e9925f3bde35709ba2c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 26 Mar 2015 09:31:58 -0700 Subject: [PATCH 3/3] config/module: remove debug --- config/module/detect_file.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/module/detect_file.go b/config/module/detect_file.go index a7eb4c49f..859739f95 100644 --- a/config/module/detect_file.go +++ b/config/module/detect_file.go @@ -40,8 +40,8 @@ func (d *FileDetector) Detect(src, pwd string) (string, bool, error) { } src = filepath.Join(pwd, src) - println(src) } + return fmtFileURL(src), true, nil }