terraform/command/flag_kv_test.go

120 lines
1.7 KiB
Go
Raw Normal View History

2014-07-18 20:37:27 +02:00
package command
import (
"flag"
2014-07-18 23:00:40 +02:00
"io/ioutil"
2014-07-18 20:37:27 +02:00
"reflect"
"testing"
)
2015-02-22 03:30:41 +01:00
func TestFlagKV_impl(t *testing.T) {
var _ flag.Value = new(FlagKV)
2014-07-18 20:37:27 +02:00
}
2015-02-22 03:30:41 +01:00
func TestFlagKV(t *testing.T) {
2014-07-18 20:37:27 +02:00
cases := []struct {
Input string
Output map[string]string
Error bool
}{
{
"key=value",
map[string]string{"key": "value"},
false,
},
{
"key=",
map[string]string{"key": ""},
false,
},
{
"key=foo=bar",
map[string]string{"key": "foo=bar"},
false,
},
{
"map.key=foo",
map[string]string{"map.key": "foo"},
false,
},
2014-07-18 20:37:27 +02:00
{
"key",
nil,
true,
},
}
for _, tc := range cases {
2015-02-22 03:30:41 +01:00
f := new(FlagKV)
2014-07-18 20:37:27 +02:00
err := f.Set(tc.Input)
2015-10-08 14:48:04 +02:00
if err != nil != tc.Error {
2014-07-18 20:37:27 +02:00
t.Fatalf("bad error. Input: %#v", tc.Input)
}
actual := map[string]string(*f)
if !reflect.DeepEqual(actual, tc.Output) {
t.Fatalf("bad: %#v", actual)
}
}
}
2014-07-18 23:00:40 +02:00
2015-02-22 03:30:41 +01:00
func TestFlagKVFile_impl(t *testing.T) {
var _ flag.Value = new(FlagKVFile)
2014-07-18 23:00:40 +02:00
}
2015-02-22 03:30:41 +01:00
func TestFlagKVFile(t *testing.T) {
2014-07-18 23:00:40 +02:00
inputLibucl := `
foo = "bar"
`
inputJson := `{
"foo": "bar"}`
cases := []struct {
Input string
Output map[string]string
Error bool
}{
{
inputLibucl,
map[string]string{"foo": "bar"},
false,
},
{
inputJson,
map[string]string{"foo": "bar"},
false,
},
{
`map.key = "foo"`,
map[string]string{"map.key": "foo"},
false,
},
2014-07-18 23:00:40 +02:00
}
path := testTempFile(t)
for _, tc := range cases {
if err := ioutil.WriteFile(path, []byte(tc.Input), 0644); err != nil {
t.Fatalf("err: %s", err)
}
2015-02-22 03:30:41 +01:00
f := new(FlagKVFile)
2014-07-18 23:00:40 +02:00
err := f.Set(path)
2015-10-08 14:48:04 +02:00
if err != nil != tc.Error {
t.Fatalf("bad error. Input: %#v, err: %s", tc.Input, err)
2014-07-18 23:00:40 +02:00
}
actual := map[string]string(*f)
if !reflect.DeepEqual(actual, tc.Output) {
t.Fatalf("bad: %#v", actual)
}
}
}