github.com/jaredpalmer/terraform@v1.1.0-alpha20210908.0.20210911170307-88705c943a03/internal/terraform/node_module_variable_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/go-test/deep"
     8  	"github.com/hashicorp/hcl/v2"
     9  	"github.com/hashicorp/hcl/v2/hclsyntax"
    10  
    11  	"github.com/hashicorp/terraform/internal/addrs"
    12  	"github.com/hashicorp/terraform/internal/configs"
    13  )
    14  
    15  func TestNodeModuleVariablePath(t *testing.T) {
    16  	n := &nodeModuleVariable{
    17  		Addr: addrs.RootModuleInstance.InputVariable("foo"),
    18  		Config: &configs.Variable{
    19  			Name: "foo",
    20  		},
    21  	}
    22  
    23  	want := addrs.RootModuleInstance
    24  	got := n.Path()
    25  	if got.String() != want.String() {
    26  		t.Fatalf("wrong module address %s; want %s", got, want)
    27  	}
    28  }
    29  
    30  func TestNodeModuleVariableReferenceableName(t *testing.T) {
    31  	n := &nodeExpandModuleVariable{
    32  		Addr: addrs.InputVariable{Name: "foo"},
    33  		Config: &configs.Variable{
    34  			Name: "foo",
    35  		},
    36  	}
    37  
    38  	{
    39  		expected := []addrs.Referenceable{
    40  			addrs.InputVariable{Name: "foo"},
    41  		}
    42  		actual := n.ReferenceableAddrs()
    43  		if !reflect.DeepEqual(actual, expected) {
    44  			t.Fatalf("%#v != %#v", actual, expected)
    45  		}
    46  	}
    47  
    48  	{
    49  		gotSelfPath, gotReferencePath := n.ReferenceOutside()
    50  		wantSelfPath := addrs.RootModuleInstance
    51  		wantReferencePath := addrs.RootModuleInstance
    52  		if got, want := gotSelfPath.String(), wantSelfPath.String(); got != want {
    53  			t.Errorf("wrong self path\ngot:  %s\nwant: %s", got, want)
    54  		}
    55  		if got, want := gotReferencePath.String(), wantReferencePath.String(); got != want {
    56  			t.Errorf("wrong reference path\ngot:  %s\nwant: %s", got, want)
    57  		}
    58  	}
    59  
    60  }
    61  
    62  func TestNodeModuleVariableReference(t *testing.T) {
    63  	n := &nodeExpandModuleVariable{
    64  		Addr:   addrs.InputVariable{Name: "foo"},
    65  		Module: addrs.RootModule.Child("bar"),
    66  		Config: &configs.Variable{
    67  			Name: "foo",
    68  		},
    69  		Expr: &hclsyntax.ScopeTraversalExpr{
    70  			Traversal: hcl.Traversal{
    71  				hcl.TraverseRoot{Name: "var"},
    72  				hcl.TraverseAttr{Name: "foo"},
    73  			},
    74  		},
    75  	}
    76  
    77  	want := []*addrs.Reference{
    78  		{
    79  			Subject: addrs.InputVariable{Name: "foo"},
    80  		},
    81  	}
    82  	got := n.References()
    83  	for _, problem := range deep.Equal(got, want) {
    84  		t.Error(problem)
    85  	}
    86  }
    87  
    88  func TestNodeModuleVariableReference_grandchild(t *testing.T) {
    89  	n := &nodeExpandModuleVariable{
    90  		Addr:   addrs.InputVariable{Name: "foo"},
    91  		Module: addrs.RootModule.Child("bar"),
    92  		Config: &configs.Variable{
    93  			Name: "foo",
    94  		},
    95  		Expr: &hclsyntax.ScopeTraversalExpr{
    96  			Traversal: hcl.Traversal{
    97  				hcl.TraverseRoot{Name: "var"},
    98  				hcl.TraverseAttr{Name: "foo"},
    99  			},
   100  		},
   101  	}
   102  
   103  	want := []*addrs.Reference{
   104  		{
   105  			Subject: addrs.InputVariable{Name: "foo"},
   106  		},
   107  	}
   108  	got := n.References()
   109  	for _, problem := range deep.Equal(got, want) {
   110  		t.Error(problem)
   111  	}
   112  }