mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-24 16:10:46 -06:00
108 lines
1.6 KiB
Go
108 lines
1.6 KiB
Go
package command
|
|
|
|
import (
|
|
"flag"
|
|
"io/ioutil"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestFlagKV_impl(t *testing.T) {
|
|
var _ flag.Value = new(FlagKV)
|
|
}
|
|
|
|
func TestFlagKV(t *testing.T) {
|
|
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,
|
|
},
|
|
|
|
{
|
|
"key",
|
|
nil,
|
|
true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
f := new(FlagKV)
|
|
err := f.Set(tc.Input)
|
|
if (err != nil) != tc.Error {
|
|
t.Fatalf("bad error. Input: %#v", tc.Input)
|
|
}
|
|
|
|
actual := map[string]string(*f)
|
|
if !reflect.DeepEqual(actual, tc.Output) {
|
|
t.Fatalf("bad: %#v", actual)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFlagKVFile_impl(t *testing.T) {
|
|
var _ flag.Value = new(FlagKVFile)
|
|
}
|
|
|
|
func TestFlagKVFile(t *testing.T) {
|
|
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,
|
|
},
|
|
}
|
|
|
|
path := testTempFile(t)
|
|
|
|
for _, tc := range cases {
|
|
if err := ioutil.WriteFile(path, []byte(tc.Input), 0644); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
f := new(FlagKVFile)
|
|
err := f.Set(path)
|
|
if (err != nil) != tc.Error {
|
|
t.Fatalf("bad error. Input: %#v", tc.Input)
|
|
}
|
|
|
|
actual := map[string]string(*f)
|
|
if !reflect.DeepEqual(actual, tc.Output) {
|
|
t.Fatalf("bad: %#v", actual)
|
|
}
|
|
}
|
|
}
|