github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/lang/blocktoattr/variables_test.go (about) 1 package blocktoattr 2 3 import ( 4 "testing" 5 6 "github.com/google/go-cmp/cmp" 7 "github.com/google/go-cmp/cmp/cmpopts" 8 "github.com/hashicorp/hcl/v2" 9 "github.com/hashicorp/hcl/v2/hclsyntax" 10 hcljson "github.com/hashicorp/hcl/v2/json" 11 "github.com/muratcelep/terraform/not-internal/configs/configschema" 12 "github.com/zclconf/go-cty/cty" 13 ) 14 15 func TestExpandedVariables(t *testing.T) { 16 fooSchema := &configschema.Block{ 17 Attributes: map[string]*configschema.Attribute{ 18 "foo": { 19 Type: cty.List(cty.Object(map[string]cty.Type{ 20 "bar": cty.String, 21 })), 22 Optional: true, 23 }, 24 "bar": { 25 Type: cty.Map(cty.String), 26 Optional: true, 27 }, 28 }, 29 } 30 31 tests := map[string]struct { 32 src string 33 json bool 34 schema *configschema.Block 35 want []hcl.Traversal 36 }{ 37 "empty": { 38 src: ``, 39 schema: &configschema.Block{}, 40 want: nil, 41 }, 42 "attribute syntax": { 43 src: ` 44 foo = [ 45 { 46 bar = baz 47 }, 48 ] 49 `, 50 schema: fooSchema, 51 want: []hcl.Traversal{ 52 { 53 hcl.TraverseRoot{ 54 Name: "baz", 55 SrcRange: hcl.Range{ 56 Filename: "test.tf", 57 Start: hcl.Pos{Line: 4, Column: 11, Byte: 23}, 58 End: hcl.Pos{Line: 4, Column: 14, Byte: 26}, 59 }, 60 }, 61 }, 62 }, 63 }, 64 "block syntax": { 65 src: ` 66 foo { 67 bar = baz 68 } 69 `, 70 schema: fooSchema, 71 want: []hcl.Traversal{ 72 { 73 hcl.TraverseRoot{ 74 Name: "baz", 75 SrcRange: hcl.Range{ 76 Filename: "test.tf", 77 Start: hcl.Pos{Line: 3, Column: 9, Byte: 15}, 78 End: hcl.Pos{Line: 3, Column: 12, Byte: 18}, 79 }, 80 }, 81 }, 82 }, 83 }, 84 "block syntax with nested blocks": { 85 src: ` 86 foo { 87 bar { 88 boop = baz 89 } 90 } 91 `, 92 schema: &configschema.Block{ 93 Attributes: map[string]*configschema.Attribute{ 94 "foo": { 95 Type: cty.List(cty.Object(map[string]cty.Type{ 96 "bar": cty.List(cty.Object(map[string]cty.Type{ 97 "boop": cty.String, 98 })), 99 })), 100 Optional: true, 101 }, 102 }, 103 }, 104 want: []hcl.Traversal{ 105 { 106 hcl.TraverseRoot{ 107 Name: "baz", 108 SrcRange: hcl.Range{ 109 Filename: "test.tf", 110 Start: hcl.Pos{Line: 4, Column: 12, Byte: 26}, 111 End: hcl.Pos{Line: 4, Column: 15, Byte: 29}, 112 }, 113 }, 114 }, 115 }, 116 }, 117 "dynamic block syntax": { 118 src: ` 119 dynamic "foo" { 120 for_each = beep 121 content { 122 bar = baz 123 } 124 } 125 `, 126 schema: fooSchema, 127 want: []hcl.Traversal{ 128 { 129 hcl.TraverseRoot{ 130 Name: "beep", 131 SrcRange: hcl.Range{ 132 Filename: "test.tf", 133 Start: hcl.Pos{Line: 3, Column: 14, Byte: 30}, 134 End: hcl.Pos{Line: 3, Column: 18, Byte: 34}, 135 }, 136 }, 137 }, 138 { 139 hcl.TraverseRoot{ 140 Name: "baz", 141 SrcRange: hcl.Range{ 142 Filename: "test.tf", 143 Start: hcl.Pos{Line: 5, Column: 11, Byte: 57}, 144 End: hcl.Pos{Line: 5, Column: 14, Byte: 60}, 145 }, 146 }, 147 }, 148 }, 149 }, 150 "misplaced dynamic block": { 151 src: ` 152 dynamic "bar" { 153 for_each = beep 154 content { 155 key = val 156 } 157 } 158 `, 159 schema: fooSchema, 160 want: []hcl.Traversal{ 161 { 162 hcl.TraverseRoot{ 163 Name: "beep", 164 SrcRange: hcl.Range{ 165 Filename: "test.tf", 166 Start: hcl.Pos{Line: 3, Column: 14, Byte: 30}, 167 End: hcl.Pos{Line: 3, Column: 18, Byte: 34}, 168 }, 169 }, 170 }, 171 }, 172 }, 173 } 174 175 for name, test := range tests { 176 t.Run(name, func(t *testing.T) { 177 var f *hcl.File 178 var diags hcl.Diagnostics 179 if test.json { 180 f, diags = hcljson.Parse([]byte(test.src), "test.tf.json") 181 } else { 182 f, diags = hclsyntax.ParseConfig([]byte(test.src), "test.tf", hcl.Pos{Line: 1, Column: 1}) 183 } 184 if diags.HasErrors() { 185 for _, diag := range diags { 186 t.Errorf("unexpected diagnostic: %s", diag) 187 } 188 t.FailNow() 189 } 190 191 got := ExpandedVariables(f.Body, test.schema) 192 193 co := cmpopts.IgnoreUnexported(hcl.TraverseRoot{}) 194 if !cmp.Equal(got, test.want, co) { 195 t.Errorf("wrong result\n%s", cmp.Diff(test.want, got, co)) 196 } 197 }) 198 } 199 200 }