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