github.com/opentofu/opentofu@v1.7.1/internal/lang/globalref/analyzer_meta_references_test.go (about)

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