From 00fed1a31fc21c97710d2ccebf251d213a1e9172 Mon Sep 17 00:00:00 2001 From: Joel Speed Date: Wed, 29 Apr 2020 19:45:26 +0100 Subject: [PATCH] Return an error when unknown options are found in the config file --- pkg/apis/options/load.go | 4 +++- pkg/apis/options/load_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/apis/options/load.go b/pkg/apis/options/load.go index 72294c28..aeb39b9a 100644 --- a/pkg/apis/options/load.go +++ b/pkg/apis/options/load.go @@ -38,7 +38,9 @@ func Load(configFileName string, flagSet *pflag.FlagSet, into interface{}) error return fmt.Errorf("unable to register flags: %w", err) } - err = v.Unmarshal(into, decodeFromCfgTag) + // UnmarhsalExact will return an error if the config includes options that are + // not mapped to felds of the into struct + err = v.UnmarshalExact(into, decodeFromCfgTag) if err != nil { return fmt.Errorf("error unmarshalling config: %w", err) } diff --git a/pkg/apis/options/load_test.go b/pkg/apis/options/load_test.go index 0d316fcd..49268018 100644 --- a/pkg/apis/options/load_test.go +++ b/pkg/apis/options/load_test.go @@ -283,6 +283,18 @@ var _ = Describe("Load", func() { unexported: "unexported", }, }), + Entry("with an unknown option in the config file", &testOptionsTableInput{ + configFile: []byte(`unknown_option="foo"`), + flagSet: func() *pflag.FlagSet { return testOptionsFlagSet }, + expectedErr: fmt.Errorf("error unmarshalling config: 1 error(s) decoding:\n\n* '' has invalid keys: unknown_option"), + // Viper will unmarshal before returning the error, so this is the default output + expectedOutput: &TestOptions{ + StringOption: "default", + Sub: TestOptionSubStruct{ + StringSliceOption: []string{"a", "b"}, + }, + }, + }), ) }) })