github.com/cmalfait/terraform@v0.11.12-beta1/helper/variables/flag_test.go (about) 1 package variables 2 3 import ( 4 "flag" 5 "fmt" 6 "reflect" 7 "testing" 8 9 "github.com/davecgh/go-spew/spew" 10 ) 11 12 func TestFlag_impl(t *testing.T) { 13 var _ flag.Value = new(Flag) 14 } 15 16 func TestFlag(t *testing.T) { 17 cases := []struct { 18 Input interface{} 19 Output map[string]interface{} 20 Error bool 21 }{ 22 { 23 "=value", 24 nil, 25 true, 26 }, 27 28 { 29 " =value", 30 nil, 31 true, 32 }, 33 34 { 35 "key=value", 36 map[string]interface{}{"key": "value"}, 37 false, 38 }, 39 40 { 41 "key=", 42 map[string]interface{}{"key": ""}, 43 false, 44 }, 45 46 { 47 "key=foo=bar", 48 map[string]interface{}{"key": "foo=bar"}, 49 false, 50 }, 51 52 { 53 "key=false", 54 map[string]interface{}{"key": "false"}, 55 false, 56 }, 57 58 { 59 "key =value", 60 map[string]interface{}{"key": "value"}, 61 false, 62 }, 63 64 { 65 "key = value", 66 map[string]interface{}{"key": " value"}, 67 false, 68 }, 69 70 { 71 `key = "value"`, 72 map[string]interface{}{"key": "value"}, 73 false, 74 }, 75 76 { 77 "map.key=foo", 78 map[string]interface{}{"map.key": "foo"}, 79 false, 80 }, 81 82 { 83 "key", 84 nil, 85 true, 86 }, 87 88 { 89 `key=["hello", "world"]`, 90 map[string]interface{}{"key": []interface{}{"hello", "world"}}, 91 false, 92 }, 93 94 { 95 `key={"hello" = "world", "foo" = "bar"}`, 96 map[string]interface{}{ 97 "key": map[string]interface{}{ 98 "hello": "world", 99 "foo": "bar", 100 }, 101 }, 102 false, 103 }, 104 105 { 106 `key={"hello" = "world", "foo" = "bar"}\nkey2="invalid"`, 107 nil, 108 true, 109 }, 110 111 { 112 "key=/path", 113 map[string]interface{}{"key": "/path"}, 114 false, 115 }, 116 117 { 118 "key=1234.dkr.ecr.us-east-1.amazonaws.com/proj:abcdef", 119 map[string]interface{}{"key": "1234.dkr.ecr.us-east-1.amazonaws.com/proj:abcdef"}, 120 false, 121 }, 122 123 // simple values that can parse as numbers should remain strings 124 { 125 "key=1", 126 map[string]interface{}{ 127 "key": "1", 128 }, 129 false, 130 }, 131 { 132 "key=1.0", 133 map[string]interface{}{ 134 "key": "1.0", 135 }, 136 false, 137 }, 138 { 139 "key=0x10", 140 map[string]interface{}{ 141 "key": "0x10", 142 }, 143 false, 144 }, 145 146 // Test setting multiple times 147 { 148 []string{ 149 "foo=bar", 150 "bar=baz", 151 }, 152 map[string]interface{}{ 153 "foo": "bar", 154 "bar": "baz", 155 }, 156 false, 157 }, 158 159 // Test map merging 160 { 161 []string{ 162 `foo={ foo = "bar" }`, 163 `foo={ bar = "baz" }`, 164 }, 165 map[string]interface{}{ 166 "foo": map[string]interface{}{ 167 "foo": "bar", 168 "bar": "baz", 169 }, 170 }, 171 false, 172 }, 173 } 174 175 for i, tc := range cases { 176 t.Run(fmt.Sprintf("%d-%s", i, tc.Input), func(t *testing.T) { 177 var input []string 178 switch v := tc.Input.(type) { 179 case string: 180 input = []string{v} 181 case []string: 182 input = v 183 default: 184 t.Fatalf("bad input type: %T", tc.Input) 185 } 186 187 f := new(Flag) 188 for i, single := range input { 189 err := f.Set(single) 190 191 // Only check for expected errors on the final input 192 expected := tc.Error && i == len(input)-1 193 if err != nil != expected { 194 t.Fatalf("bad error. Input: %#v\n\nError: %s", single, err) 195 } 196 } 197 198 actual := map[string]interface{}(*f) 199 if !reflect.DeepEqual(actual, tc.Output) { 200 t.Fatalf("bad:\nexpected: %s\n\n got: %s\n", spew.Sdump(tc.Output), spew.Sdump(actual)) 201 } 202 }) 203 } 204 }