github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/configs/config_build_test.go (about) 1 package configs 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "reflect" 7 "sort" 8 "strings" 9 "testing" 10 11 "github.com/davecgh/go-spew/spew" 12 13 version "github.com/hashicorp/go-version" 14 "github.com/hashicorp/hcl/v2" 15 ) 16 17 func TestBuildConfig(t *testing.T) { 18 parser := NewParser(nil) 19 mod, diags := parser.LoadConfigDir("testdata/config-build") 20 assertNoDiagnostics(t, diags) 21 if mod == nil { 22 t.Fatal("got nil root module; want non-nil") 23 } 24 25 versionI := 0 26 cfg, diags := BuildConfig(mod, ModuleWalkerFunc( 27 func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) { 28 // For the sake of this test we're going to just treat our 29 // SourceAddr as a path relative to our fixture directory. 30 // A "real" implementation of ModuleWalker should accept the 31 // various different source address syntaxes Terraform supports. 32 sourcePath := filepath.Join("testdata/config-build", req.SourceAddr) 33 34 mod, diags := parser.LoadConfigDir(sourcePath) 35 version, _ := version.NewVersion(fmt.Sprintf("1.0.%d", versionI)) 36 versionI++ 37 return mod, version, diags 38 }, 39 )) 40 assertNoDiagnostics(t, diags) 41 if cfg == nil { 42 t.Fatal("got nil config; want non-nil") 43 } 44 45 var got []string 46 cfg.DeepEach(func(c *Config) { 47 got = append(got, fmt.Sprintf("%s %s", strings.Join(c.Path, "."), c.Version)) 48 }) 49 sort.Strings(got) 50 want := []string{ 51 " <nil>", 52 "child_a 1.0.0", 53 "child_a.child_c 1.0.1", 54 "child_b 1.0.2", 55 "child_b.child_c 1.0.3", 56 } 57 58 if !reflect.DeepEqual(got, want) { 59 t.Fatalf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want)) 60 } 61 62 if _, exists := cfg.Children["child_a"].Children["child_c"].Module.Outputs["hello"]; !exists { 63 t.Fatalf("missing output 'hello' in child_a.child_c") 64 } 65 if _, exists := cfg.Children["child_b"].Children["child_c"].Module.Outputs["hello"]; !exists { 66 t.Fatalf("missing output 'hello' in child_b.child_c") 67 } 68 if cfg.Children["child_a"].Children["child_c"].Module == cfg.Children["child_b"].Children["child_c"].Module { 69 t.Fatalf("child_a.child_c is same object as child_b.child_c; should not be") 70 } 71 } 72 73 func TestBuildConfigDiags(t *testing.T) { 74 parser := NewParser(nil) 75 mod, diags := parser.LoadConfigDir("testdata/nested-errors") 76 assertNoDiagnostics(t, diags) 77 if mod == nil { 78 t.Fatal("got nil root module; want non-nil") 79 } 80 81 versionI := 0 82 cfg, diags := BuildConfig(mod, ModuleWalkerFunc( 83 func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) { 84 // For the sake of this test we're going to just treat our 85 // SourceAddr as a path relative to our fixture directory. 86 // A "real" implementation of ModuleWalker should accept the 87 // various different source address syntaxes Terraform supports. 88 sourcePath := filepath.Join("testdata/nested-errors", req.SourceAddr) 89 90 mod, diags := parser.LoadConfigDir(sourcePath) 91 version, _ := version.NewVersion(fmt.Sprintf("1.0.%d", versionI)) 92 versionI++ 93 return mod, version, diags 94 }, 95 )) 96 97 wantDiag := `testdata/nested-errors/child_c/child_c.tf:5,1-8: ` + 98 `Unsupported block type; Blocks of type "invalid" are not expected here.` 99 assertExactDiagnostics(t, diags, []string{wantDiag}) 100 101 // we should still have module structure loaded 102 var got []string 103 cfg.DeepEach(func(c *Config) { 104 got = append(got, fmt.Sprintf("%s %s", strings.Join(c.Path, "."), c.Version)) 105 }) 106 sort.Strings(got) 107 want := []string{ 108 " <nil>", 109 "child_a 1.0.0", 110 "child_a.child_c 1.0.1", 111 } 112 113 if !reflect.DeepEqual(got, want) { 114 t.Fatalf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want)) 115 } 116 }