github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/not-internal/configs/parser_values_test.go (about) 1 package configs 2 3 import ( 4 "testing" 5 6 "github.com/zclconf/go-cty/cty" 7 ) 8 9 func TestParserLoadValuesFile(t *testing.T) { 10 tests := map[string]struct { 11 Source string 12 Want map[string]cty.Value 13 DiagCount int 14 }{ 15 "empty.tfvars": { 16 "", 17 map[string]cty.Value{}, 18 0, 19 }, 20 "empty.json": { 21 "{}", 22 map[string]cty.Value{}, 23 0, 24 }, 25 "zerolen.json": { 26 "", 27 map[string]cty.Value{}, 28 2, // syntax error and missing root object 29 }, 30 "one-number.tfvars": { 31 "foo = 1\n", 32 map[string]cty.Value{ 33 "foo": cty.NumberIntVal(1), 34 }, 35 0, 36 }, 37 "one-number.tfvars.json": { 38 `{"foo": 1}`, 39 map[string]cty.Value{ 40 "foo": cty.NumberIntVal(1), 41 }, 42 0, 43 }, 44 "two-bools.tfvars": { 45 "foo = true\nbar = false\n", 46 map[string]cty.Value{ 47 "foo": cty.True, 48 "bar": cty.False, 49 }, 50 0, 51 }, 52 "two-bools.tfvars.json": { 53 `{"foo": true, "bar": false}`, 54 map[string]cty.Value{ 55 "foo": cty.True, 56 "bar": cty.False, 57 }, 58 0, 59 }, 60 "invalid-syntax.tfvars": { 61 "foo bar baz\n", 62 map[string]cty.Value{}, 63 2, // invalid block definition, and unexpected foo block (the latter due to parser recovery behavior) 64 }, 65 "block.tfvars": { 66 "foo = true\ninvalid {\n}\n", 67 map[string]cty.Value{ 68 "foo": cty.True, 69 }, 70 1, // blocks are not allowed 71 }, 72 "variables.tfvars": { 73 "baz = true\nfoo = var.baz\n", 74 map[string]cty.Value{ 75 "baz": cty.True, 76 "foo": cty.DynamicVal, 77 }, 78 1, // variables cannot be referenced here 79 }, 80 } 81 82 for name, test := range tests { 83 t.Run(name, func(t *testing.T) { 84 p := testParser(map[string]string{ 85 name: test.Source, 86 }) 87 got, diags := p.LoadValuesFile(name) 88 if len(diags) != test.DiagCount { 89 t.Errorf("wrong number of diagnostics %d; want %d", len(diags), test.DiagCount) 90 for _, diag := range diags { 91 t.Logf("- %s", diag) 92 } 93 } 94 95 if len(got) != len(test.Want) { 96 t.Errorf("wrong number of result keys %d; want %d", len(got), len(test.Want)) 97 } 98 99 for name, gotVal := range got { 100 wantVal := test.Want[name] 101 if wantVal == cty.NilVal { 102 t.Errorf("unexpected result key %q", name) 103 continue 104 } 105 106 if !gotVal.RawEquals(wantVal) { 107 t.Errorf("wrong value for %q\ngot: %#v\nwant: %#v", name, gotVal, wantVal) 108 continue 109 } 110 } 111 }) 112 } 113 }