github.com/kcburge/terraform@v0.11.12-beta1/helper/variables/flag_file_test.go (about) 1 package variables 2 3 import ( 4 "flag" 5 "fmt" 6 "io/ioutil" 7 "reflect" 8 "testing" 9 ) 10 11 func TestFlagFile_impl(t *testing.T) { 12 var _ flag.Value = new(FlagFile) 13 } 14 15 func TestFlagFile(t *testing.T) { 16 inputLibucl := ` 17 foo = "bar" 18 ` 19 inputMap := ` 20 foo = { 21 k = "v" 22 }` 23 24 inputJson := `{ 25 "foo": "bar"}` 26 27 cases := []struct { 28 Input interface{} 29 Output map[string]interface{} 30 Error bool 31 }{ 32 { 33 inputLibucl, 34 map[string]interface{}{"foo": "bar"}, 35 false, 36 }, 37 38 { 39 inputJson, 40 map[string]interface{}{"foo": "bar"}, 41 false, 42 }, 43 44 { 45 `map.key = "foo"`, 46 map[string]interface{}{"map.key": "foo"}, 47 false, 48 }, 49 50 { 51 inputMap, 52 map[string]interface{}{ 53 "foo": map[string]interface{}{ 54 "k": "v", 55 }, 56 }, 57 false, 58 }, 59 60 { 61 []string{ 62 `foo = { "k" = "v"}`, 63 `foo = { "j" = "v" }`, 64 }, 65 map[string]interface{}{ 66 "foo": map[string]interface{}{ 67 "k": "v", 68 "j": "v", 69 }, 70 }, 71 false, 72 }, 73 } 74 75 path := testTempFile(t) 76 77 for i, tc := range cases { 78 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { 79 var input []string 80 switch i := tc.Input.(type) { 81 case string: 82 input = []string{i} 83 case []string: 84 input = i 85 default: 86 t.Fatalf("bad input type: %T", i) 87 } 88 89 f := new(FlagFile) 90 for _, input := range input { 91 if err := ioutil.WriteFile(path, []byte(input), 0644); err != nil { 92 t.Fatalf("err: %s", err) 93 } 94 95 err := f.Set(path) 96 if err != nil != tc.Error { 97 t.Fatalf("bad error. Input: %#v, err: %s", input, err) 98 } 99 } 100 101 actual := map[string]interface{}(*f) 102 if !reflect.DeepEqual(actual, tc.Output) { 103 t.Fatalf("bad: %#v", actual) 104 } 105 }) 106 } 107 }