-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathplugin_test.go
More file actions
120 lines (95 loc) · 3.35 KB
/
Copy pathplugin_test.go
File metadata and controls
120 lines (95 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package config
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type testPluginConfig struct {
Enabled bool `mapstructure:"enabled"`
APIKey string `mapstructure:"api_key"`
}
func TestLoadPlugin(t *testing.T) {
t.Run("nil viper", func(t *testing.T) {
cfg := Config{}
pluginCfg := testPluginConfig{APIKey: "default"}
require.NoError(t, cfg.LoadPlugin("my_plugin", &pluginCfg))
// defaults are untouched
assert.Equal(t, "default", pluginCfg.APIKey)
})
t.Run("load from plugins section", func(t *testing.T) {
cfg := Config{}
cfg.Set("plugins§my_plugin", map[string]any{
"enabled": true,
"api_key": "secret",
})
pluginCfg := testPluginConfig{}
require.NoError(t, cfg.LoadPlugin("my_plugin", &pluginCfg))
assert.True(t, pluginCfg.Enabled)
assert.Equal(t, "secret", pluginCfg.APIKey)
})
t.Run("fallback to legacy top-level key", func(t *testing.T) {
cfg := Config{}
cfg.Set("my_plugin", map[string]any{
"api_key": "legacy-secret",
})
pluginCfg := testPluginConfig{}
require.NoError(t, cfg.LoadPlugin("my_plugin", &pluginCfg))
assert.Equal(t, "legacy-secret", pluginCfg.APIKey)
})
t.Run("plugins section wins over legacy key", func(t *testing.T) {
cfg := Config{}
cfg.Set("my_plugin", map[string]any{
"api_key": "legacy-secret",
})
cfg.Set("plugins§my_plugin", map[string]any{
"api_key": "new-secret",
})
pluginCfg := testPluginConfig{}
require.NoError(t, cfg.LoadPlugin("my_plugin", &pluginCfg))
assert.Equal(t, "new-secret", pluginCfg.APIKey)
})
t.Run("absent config keeps defaults", func(t *testing.T) {
cfg := Config{}
cfg.Set("other_plugin", map[string]any{"api_key": "other"})
pluginCfg := testPluginConfig{APIKey: "default"}
require.NoError(t, cfg.LoadPlugin("my_plugin", &pluginCfg))
assert.Equal(t, "default", pluginCfg.APIKey)
})
}
func TestIsPluginEnabled(t *testing.T) {
t.Run("nil viper", func(t *testing.T) {
cfg := Config{}
assert.True(t, cfg.IsPluginEnabled("my_plugin"))
})
t.Run("no plugins section", func(t *testing.T) {
cfg := Config{}
cfg.Set("something", "else")
assert.True(t, cfg.IsPluginEnabled("my_plugin"))
})
t.Run("section without enabled flag", func(t *testing.T) {
cfg := Config{}
cfg.Set("plugins§my_plugin", map[string]any{"api_key": "secret"})
assert.True(t, cfg.IsPluginEnabled("my_plugin"))
})
t.Run("explicitly disabled", func(t *testing.T) {
cfg := Config{}
cfg.Set("plugins§my_plugin", map[string]any{"enabled": false})
assert.False(t, cfg.IsPluginEnabled("my_plugin"))
})
t.Run("explicitly enabled", func(t *testing.T) {
cfg := Config{}
cfg.Set("plugins§my_plugin", map[string]any{"enabled": true})
assert.True(t, cfg.IsPluginEnabled("my_plugin"))
})
}
// the whole plugin config design relies on nested key traversal (IsSet/UnmarshalKey
// with the "§" delimiter) working in the viper fork - lock that behavior in
func TestNestedViperKeys(t *testing.T) {
cfg := Config{}
cfg.Set("plugins§my_plugin", map[string]any{"enabled": false, "api_key": "secret"})
assert.True(t, cfg.viper.IsSet("plugins§my_plugin"))
assert.True(t, cfg.viper.IsSet("plugins§my_plugin§enabled"))
assert.False(t, cfg.viper.IsSet("plugins§other_plugin"))
assert.False(t, cfg.viper.GetBool("plugins§my_plugin§enabled"))
assert.Equal(t, "secret", cfg.viper.GetString("plugins§my_plugin§api_key"))
}