Skip to content

Commit 5c3aacb

Browse files
oakiostefanhaller
authored andcommitted
UserConfig validation
1 parent 2b5c814 commit 5c3aacb

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

pkg/config/app_config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ func loadUserConfig(configFiles []string, base *UserConfig) (*UserConfig, error)
164164
if err := yaml.Unmarshal(content, base); err != nil {
165165
return nil, fmt.Errorf("The config at `%s` couldn't be parsed, please inspect it before opening up an issue.\n%w", path, err)
166166
}
167+
168+
if err := base.Validate(); err != nil {
169+
return nil, fmt.Errorf("The config at `%s` has a validation error.\n%w", path, err)
170+
}
167171
}
168172

169173
return base, nil
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"slices"
6+
"strings"
7+
)
8+
9+
func (config *UserConfig) Validate() error {
10+
if err := validateEnum("gui.statusPanelView", config.Gui.StatusPanelView, []string{"dashboard", "allBranchesLog"}); err != nil {
11+
return err
12+
}
13+
return nil
14+
}
15+
16+
func validateEnum(name string, value string, allowedValues []string) error {
17+
if slices.Contains(allowedValues, value) {
18+
return nil
19+
}
20+
allowedValuesStr := strings.Join(allowedValues, ", ")
21+
return fmt.Errorf("Unexpected value '%s' for '%s'. Allowed values: %s", value, name, allowedValuesStr)
22+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestUserConfigValidate_enums(t *testing.T) {
10+
type testCase struct {
11+
value string
12+
valid bool
13+
}
14+
15+
scenarios := []struct {
16+
name string
17+
setup func(config *UserConfig, value string)
18+
testCases []testCase
19+
}{
20+
{
21+
name: "Gui.StatusPanelView",
22+
setup: func(config *UserConfig, value string) {
23+
config.Gui.StatusPanelView = value
24+
},
25+
testCases: []testCase{
26+
{value: "dashboard", valid: true},
27+
{value: "allBranchesLog", valid: true},
28+
{value: "", valid: false},
29+
{value: "invalid_value", valid: false},
30+
},
31+
},
32+
}
33+
34+
for _, s := range scenarios {
35+
t.Run(s.name, func(t *testing.T) {
36+
for _, testCase := range s.testCases {
37+
config := GetDefaultConfig()
38+
s.setup(config, testCase.value)
39+
err := config.Validate()
40+
41+
if testCase.valid {
42+
assert.NoError(t, err)
43+
} else {
44+
assert.Error(t, err)
45+
}
46+
}
47+
})
48+
}
49+
}

0 commit comments

Comments
 (0)