github.com/dtx/terraform@v0.7.4-0.20160907225157-926acfd0821e/command/flag_kv_test.go (about) 1 package command 2 3 import ( 4 "flag" 5 "github.com/davecgh/go-spew/spew" 6 "io/ioutil" 7 "reflect" 8 "testing" 9 ) 10 11 func TestFlagStringKV_impl(t *testing.T) { 12 var _ flag.Value = new(FlagStringKV) 13 } 14 15 func TestFlagStringKV(t *testing.T) { 16 cases := []struct { 17 Input string 18 Output map[string]string 19 Error bool 20 }{ 21 { 22 "key=value", 23 map[string]string{"key": "value"}, 24 false, 25 }, 26 27 { 28 "key=", 29 map[string]string{"key": ""}, 30 false, 31 }, 32 33 { 34 "key=foo=bar", 35 map[string]string{"key": "foo=bar"}, 36 false, 37 }, 38 39 { 40 "map.key=foo", 41 map[string]string{"map.key": "foo"}, 42 false, 43 }, 44 45 { 46 "key", 47 nil, 48 true, 49 }, 50 51 { 52 "key=/path", 53 map[string]string{"key": "/path"}, 54 false, 55 }, 56 } 57 58 for _, tc := range cases { 59 f := new(FlagStringKV) 60 err := f.Set(tc.Input) 61 if err != nil != tc.Error { 62 t.Fatalf("bad error. Input: %#v\n\nError: %s", tc.Input, err) 63 } 64 65 actual := map[string]string(*f) 66 if !reflect.DeepEqual(actual, tc.Output) { 67 t.Fatalf("bad: %#v", actual) 68 } 69 } 70 } 71 72 func TestFlagTypedKV_impl(t *testing.T) { 73 var _ flag.Value = new(FlagTypedKV) 74 } 75 76 func TestFlagTypedKV(t *testing.T) { 77 cases := []struct { 78 Input string 79 Output map[string]interface{} 80 Error bool 81 }{ 82 { 83 "key=value", 84 map[string]interface{}{"key": "value"}, 85 false, 86 }, 87 88 { 89 "key=", 90 map[string]interface{}{"key": ""}, 91 false, 92 }, 93 94 { 95 "key=foo=bar", 96 map[string]interface{}{"key": "foo=bar"}, 97 false, 98 }, 99 100 { 101 "map.key=foo", 102 map[string]interface{}{"map.key": "foo"}, 103 false, 104 }, 105 106 { 107 "key", 108 nil, 109 true, 110 }, 111 112 { 113 `key=["hello", "world"]`, 114 map[string]interface{}{"key": []interface{}{"hello", "world"}}, 115 false, 116 }, 117 118 { 119 `key={"hello" = "world", "foo" = "bar"}`, 120 map[string]interface{}{ 121 "key": []map[string]interface{}{ 122 map[string]interface{}{ 123 "hello": "world", 124 "foo": "bar", 125 }, 126 }, 127 }, 128 false, 129 }, 130 131 { 132 `key={"hello" = "world", "foo" = "bar"}\nkey2="invalid"`, 133 nil, 134 true, 135 }, 136 137 { 138 "key=/path", 139 map[string]interface{}{"key": "/path"}, 140 false, 141 }, 142 143 { 144 "key=1234.dkr.ecr.us-east-1.amazonaws.com/proj:abcdef", 145 map[string]interface{}{"key": "1234.dkr.ecr.us-east-1.amazonaws.com/proj:abcdef"}, 146 false, 147 }, 148 } 149 150 for _, tc := range cases { 151 f := new(FlagTypedKV) 152 err := f.Set(tc.Input) 153 if err != nil != tc.Error { 154 t.Fatalf("bad error. Input: %#v\n\nError: %s", tc.Input, err) 155 } 156 157 actual := map[string]interface{}(*f) 158 if !reflect.DeepEqual(actual, tc.Output) { 159 t.Fatalf("bad:\nexpected: %s\n\n got: %s\n", spew.Sdump(tc.Output), spew.Sdump(actual)) 160 } 161 } 162 } 163 164 func TestFlagKVFile_impl(t *testing.T) { 165 var _ flag.Value = new(FlagKVFile) 166 } 167 168 func TestFlagKVFile(t *testing.T) { 169 inputLibucl := ` 170 foo = "bar" 171 ` 172 173 inputJson := `{ 174 "foo": "bar"}` 175 176 cases := []struct { 177 Input string 178 Output map[string]interface{} 179 Error bool 180 }{ 181 { 182 inputLibucl, 183 map[string]interface{}{"foo": "bar"}, 184 false, 185 }, 186 187 { 188 inputJson, 189 map[string]interface{}{"foo": "bar"}, 190 false, 191 }, 192 193 { 194 `map.key = "foo"`, 195 map[string]interface{}{"map.key": "foo"}, 196 false, 197 }, 198 } 199 200 path := testTempFile(t) 201 202 for _, tc := range cases { 203 if err := ioutil.WriteFile(path, []byte(tc.Input), 0644); err != nil { 204 t.Fatalf("err: %s", err) 205 } 206 207 f := new(FlagKVFile) 208 err := f.Set(path) 209 if err != nil != tc.Error { 210 t.Fatalf("bad error. Input: %#v, err: %s", tc.Input, err) 211 } 212 213 actual := map[string]interface{}(*f) 214 if !reflect.DeepEqual(actual, tc.Output) { 215 t.Fatalf("bad: %#v", actual) 216 } 217 } 218 }