github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/evaluate_test.go (about) 1 package terraform 2 3 import ( 4 "testing" 5 6 "github.com/davecgh/go-spew/spew" 7 "github.com/zclconf/go-cty/cty" 8 9 "github.com/hashicorp/terraform-plugin-sdk/internal/addrs" 10 "github.com/hashicorp/terraform-plugin-sdk/internal/configs" 11 "github.com/hashicorp/terraform-plugin-sdk/internal/tfdiags" 12 ) 13 14 func TestEvaluatorGetTerraformAttr(t *testing.T) { 15 evaluator := &Evaluator{ 16 Meta: &ContextMeta{ 17 Env: "foo", 18 }, 19 } 20 data := &evaluationStateData{ 21 Evaluator: evaluator, 22 } 23 scope := evaluator.Scope(data, nil) 24 25 t.Run("workspace", func(t *testing.T) { 26 want := cty.StringVal("foo") 27 got, diags := scope.Data.GetTerraformAttr(addrs.TerraformAttr{ 28 Name: "workspace", 29 }, tfdiags.SourceRange{}) 30 if len(diags) != 0 { 31 t.Errorf("unexpected diagnostics %s", spew.Sdump(diags)) 32 } 33 if !got.RawEquals(want) { 34 t.Errorf("wrong result %q; want %q", got, want) 35 } 36 }) 37 } 38 39 func TestEvaluatorGetPathAttr(t *testing.T) { 40 evaluator := &Evaluator{ 41 Meta: &ContextMeta{ 42 Env: "foo", 43 }, 44 Config: &configs.Config{ 45 Module: &configs.Module{ 46 SourceDir: "bar/baz", 47 }, 48 }, 49 } 50 data := &evaluationStateData{ 51 Evaluator: evaluator, 52 } 53 scope := evaluator.Scope(data, nil) 54 55 t.Run("module", func(t *testing.T) { 56 want := cty.StringVal("bar/baz") 57 got, diags := scope.Data.GetPathAttr(addrs.PathAttr{ 58 Name: "module", 59 }, tfdiags.SourceRange{}) 60 if len(diags) != 0 { 61 t.Errorf("unexpected diagnostics %s", spew.Sdump(diags)) 62 } 63 if !got.RawEquals(want) { 64 t.Errorf("wrong result %#v; want %#v", got, want) 65 } 66 }) 67 68 t.Run("root", func(t *testing.T) { 69 want := cty.StringVal("bar/baz") 70 got, diags := scope.Data.GetPathAttr(addrs.PathAttr{ 71 Name: "root", 72 }, tfdiags.SourceRange{}) 73 if len(diags) != 0 { 74 t.Errorf("unexpected diagnostics %s", spew.Sdump(diags)) 75 } 76 if !got.RawEquals(want) { 77 t.Errorf("wrong result %#v; want %#v", got, want) 78 } 79 }) 80 }