diff options
author | oakio <ilya.dubadenko@gmail.com> | 2024-02-25 17:38:58 +0300 |
---|---|---|
committer | Stefan Haller <stefan@haller-berlin.de> | 2024-04-10 17:38:57 +0200 |
commit | 5c3aacb4cb90e3bd5c922c6a384426e1aa287af9 (patch) | |
tree | d905b674cb936f104c10fba05a85454f5d01d7e5 | |
parent | 2b5c8140805ee11be798ff1ff919652e2f92a5e7 (diff) |
UserConfig validation
-rw-r--r-- | pkg/config/app_config.go | 4 | ||||
-rw-r--r-- | pkg/config/user_config_validation.go | 22 | ||||
-rw-r--r-- | pkg/config/user_config_validation_test.go | 49 |
3 files changed, 75 insertions, 0 deletions
diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index cd0d3e316..97f32688e 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -164,6 +164,10 @@ func loadUserConfig(configFiles []string, base *UserConfig) (*UserConfig, error) if err := yaml.Unmarshal(content, base); err != nil { return nil, fmt.Errorf("The config at `%s` couldn't be parsed, please inspect it before opening up an issue.\n%w", path, err) } + + if err := base.Validate(); err != nil { + return nil, fmt.Errorf("The config at `%s` has a validation error.\n%w", path, err) + } } return base, nil diff --git a/pkg/config/user_config_validation.go b/pkg/config/user_config_validation.go new file mode 100644 index 000000000..945979db9 --- /dev/null +++ b/pkg/config/user_config_validation.go @@ -0,0 +1,22 @@ +package config + +import ( + "fmt" + "slices" + "strings" +) + +func (config *UserConfig) Validate() error { + if err := validateEnum("gui.statusPanelView", config.Gui.StatusPanelView, []string{"dashboard", "allBranchesLog"}); err != nil { + return err + } + return nil +} + +func validateEnum(name string, value string, allowedValues []string) error { + if slices.Contains(allowedValues, value) { + return nil + } + allowedValuesStr := strings.Join(allowedValues, ", ") + return fmt.Errorf("Unexpected value '%s' for '%s'. Allowed values: %s", value, name, allowedValuesStr) +} diff --git a/pkg/config/user_config_validation_test.go b/pkg/config/user_config_validation_test.go new file mode 100644 index 000000000..9f7b4d74c --- /dev/null +++ b/pkg/config/user_config_validation_test.go @@ -0,0 +1,49 @@ +package config + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestUserConfigValidate_enums(t *testing.T) { + type testCase struct { + value string + valid bool + } + + scenarios := []struct { + name string + setup func(config *UserConfig, value string) + testCases []testCase + }{ + { + name: "Gui.StatusPanelView", + setup: func(config *UserConfig, value string) { + config.Gui.StatusPanelView = value + }, + testCases: []testCase{ + {value: "dashboard", valid: true}, + {value: "allBranchesLog", valid: true}, + {value: "", valid: false}, + {value: "invalid_value", valid: false}, + }, + }, + } + + for _, s := range scenarios { + t.Run(s.name, func(t *testing.T) { + for _, testCase := range s.testCases { + config := GetDefaultConfig() + s.setup(config, testCase.value) + err := config.Validate() + + if testCase.valid { + assert.NoError(t, err) + } else { + assert.Error(t, err) + } + } + }) + } +} |