options: fail when key is configured twice

This commit is contained in:
Alexander Neumann 2017-03-25 15:39:38 +01:00
parent 946b4f4b86
commit f587a5f4f0
2 changed files with 17 additions and 0 deletions

View file

@ -32,6 +32,11 @@ func Parse(in []string) (Options, error) {
if key == "" { if key == "" {
return Options{}, errors.Fatalf("empty key is not a valid option") return Options{}, errors.Fatalf("empty key is not a valid option")
} }
if v, ok := opts[key]; ok && v != value {
return Options{}, errors.Fatalf("key %q present more than once", key)
}
opts[key] = value opts[key] = value
} }

View file

@ -33,6 +33,14 @@ var optsTests = []struct {
"k2": "more spaces = not evil", "k2": "more spaces = not evil",
}, },
}, },
{
[]string{"x=1", "foo=bar", "y=2", "foo=bar"},
Options{
"x": "1",
"y": "2",
"foo": "bar",
},
},
} }
func TestParseOptions(t *testing.T) { func TestParseOptions(t *testing.T) {
@ -58,6 +66,10 @@ var invalidOptsTests = []struct {
[]string{"=bar", "bar=baz", "k="}, []string{"=bar", "bar=baz", "k="},
"empty key is not a valid option", "empty key is not a valid option",
}, },
{
[]string{"x=1", "foo=bar", "y=2", "foo=baz"},
`key "foo" present more than once`,
},
} }
func TestParseInvalidOptions(t *testing.T) { func TestParseInvalidOptions(t *testing.T) {