github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/lang/globalref/analyzer_meta_references_test.go (about)

     1  package globalref
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/hashicorp/terraform/internal/addrs"
     9  )
    10  
    11  func TestAnalyzerMetaReferences(t *testing.T) {
    12  	tests := []struct {
    13  		InputContainer string
    14  		InputRef       string
    15  		WantRefs       []string
    16  	}{
    17  		{
    18  			``,
    19  			`local.a`,
    20  			nil,
    21  		},
    22  		{
    23  			``,
    24  			`local.single`,
    25  			[]string{
    26  				"::test_thing.single.id",
    27  			},
    28  		},
    29  		{
    30  			``,
    31  			`test_thing.single`,
    32  			[]string{
    33  				"::local.a",
    34  				"::local.b",
    35  			},
    36  		},
    37  		{
    38  			``,
    39  			`test_thing.single.string`,
    40  			[]string{
    41  				"::local.a",
    42  			},
    43  		},
    44  		{
    45  			``,
    46  			`test_thing.for_each`,
    47  			[]string{
    48  				"::local.a",
    49  				"::test_thing.single.string",
    50  			},
    51  		},
    52  		{
    53  			``,
    54  			`test_thing.for_each["whatever"]`,
    55  			[]string{
    56  				"::local.a",
    57  				"::test_thing.single.string",
    58  			},
    59  		},
    60  		{
    61  			``,
    62  			`test_thing.for_each["whatever"].single`,
    63  			[]string{
    64  				"::test_thing.single.string",
    65  			},
    66  		},
    67  		{
    68  			``,
    69  			`test_thing.for_each["whatever"].single.z`,
    70  			[]string{
    71  				"::test_thing.single.string",
    72  			},
    73  		},
    74  		{
    75  			``,
    76  			`test_thing.count`,
    77  			[]string{
    78  				"::local.a",
    79  			},
    80  		},
    81  		{
    82  			``,
    83  			`test_thing.count[0]`,
    84  			[]string{
    85  				"::local.a",
    86  			},
    87  		},
    88  		{
    89  			``,
    90  			`module.single.a`,
    91  			[]string{
    92  				"module.single::test_thing.foo",
    93  				"module.single::var.a",
    94  			},
    95  		},
    96  		{
    97  			``,
    98  			`module.for_each["whatever"].a`,
    99  			[]string{
   100  				`module.for_each["whatever"]::test_thing.foo`,
   101  				`module.for_each["whatever"]::var.a`,
   102  			},
   103  		},
   104  		{
   105  			``,
   106  			`module.count[0].a`,
   107  			[]string{
   108  				`module.count[0]::test_thing.foo`,
   109  				`module.count[0]::var.a`,
   110  			},
   111  		},
   112  		{
   113  			`module.single`,
   114  			`var.a`,
   115  			[]string{
   116  				"::test_thing.single",
   117  			},
   118  		},
   119  		{
   120  			`module.single`,
   121  			`test_thing.foo`,
   122  			[]string{
   123  				"module.single::var.a",
   124  			},
   125  		},
   126  	}
   127  
   128  	azr := testAnalyzer(t, "assorted")
   129  
   130  	for _, test := range tests {
   131  		name := test.InputRef
   132  		if test.InputContainer != "" {
   133  			name = test.InputContainer + " " + test.InputRef
   134  		}
   135  		t.Run(name, func(t *testing.T) {
   136  			t.Logf("testing %s", name)
   137  			var containerAddr addrs.Targetable
   138  			containerAddr = addrs.RootModuleInstance
   139  			if test.InputContainer != "" {
   140  				moduleAddrTarget, diags := addrs.ParseTargetStr(test.InputContainer)
   141  				if diags.HasErrors() {
   142  					t.Fatalf("input module address is invalid: %s", diags.Err())
   143  				}
   144  				containerAddr = moduleAddrTarget.Subject
   145  			}
   146  
   147  			localRef, diags := addrs.ParseRefStr(test.InputRef)
   148  			if diags.HasErrors() {
   149  				t.Fatalf("input reference is invalid: %s", diags.Err())
   150  			}
   151  
   152  			ref := Reference{
   153  				ContainerAddr: containerAddr,
   154  				LocalRef:      localRef,
   155  			}
   156  
   157  			refs := azr.MetaReferences(ref)
   158  
   159  			want := test.WantRefs
   160  			var got []string
   161  			for _, ref := range refs {
   162  				got = append(got, ref.DebugString())
   163  			}
   164  			sort.Strings(got)
   165  			if diff := cmp.Diff(want, got); diff != "" {
   166  				t.Errorf("wrong references\n%s", diff)
   167  			}
   168  		})
   169  	}
   170  }