-
Notifications
You must be signed in to change notification settings - Fork 0
/
verdeter_test.go
189 lines (165 loc) · 4.48 KB
/
verdeter_test.go
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package verdeter_test
import (
"fmt"
"os"
"testing"
"github.com/ditrit/verdeter"
"github.com/ditrit/verdeter/validators"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
func TestNormalUse(t *testing.T) {
viper.Reset()
// set an env key for later
os.Setenv("TEST_ENVKEY", "envkeyvalue")
cfg := verdeter.NewVerdeterCommand(
"test",
"a test command",
"whatever",
func(cfg *verdeter.VerdeterCommand, args []string) error {
fmt.Printf("args=%v", args)
return nil
},
)
// Test the conputed values works
cfg.GKey("computed", verdeter.IsInt, "", "a computed value")
cfg.SetComputedValue("computed", func() interface{} {
return 1234
})
// Test the config file handling system
cfg.GKey("config_path", verdeter.IsStr, "", "path to the config directory")
cfg.SetNormalize("config_path",
func(val interface{}) interface{} {
strval, ok := val.(string)
if ok {
if strval != "" {
lastChar := strval[len(strval)-1:]
if lastChar != "/" {
return strval + "/"
}
return strval
}
}
return nil
})
cfg.SetDefault("config_path", "./fixtures/1/")
// Test contraints
cfg.SetConstraint("contraintname", func() bool {
return true
})
cfg.LKey("envkey", verdeter.IsStr, "", "test env key")
cfg.SetRequired("envkey")
cfg.AddValidator("envkey", validators.StringNotEmpty)
cfg.LKey("superkey", verdeter.IsInt, "", "test key in fixture dir")
cfg.SetDefault("superkey", -5)
cfg.LKey("myuintkey", verdeter.IsUint, "", "test uint key")
cfg.SetDefault("myuintkey", 25)
sub := verdeter.NewVerdeterCommand(
"sub",
"a sub command",
"whatever",
func(cfg *verdeter.VerdeterCommand, args []string) error {
fmt.Printf("args=%v", args)
return nil
},
)
cfg.AddSubCommand(sub)
cfg.Execute()
assert.Equal(t, "test", cfg.GetAppName(), "should return the name of the app")
assert.Equal(t, 125, viper.GetInt("superkey"))
assert.Equal(t, 1234, viper.GetInt("computed"))
assert.Equal(t, "envkeyvalue", viper.GetString("envkey"))
assert.Equal(t, uint(25), viper.GetUint("myuintkey"))
assert.NoError(t, cfg.Validate(true), "shouldn't ")
}
func TestForceConfigFileValid(t *testing.T) {
viper.Reset()
cfg := verdeter.NewVerdeterCommand(
"superuncommonappnamevalid",
"a test command",
"whatever",
func(cfg *verdeter.VerdeterCommand, args []string) error {
fmt.Printf("args=%v", args)
return nil
},
)
cfg.GKey("config_path", verdeter.IsStr, "", "path to the config directory")
cfg.SetDefault("config_path", "./fixtures/1/")
assert.NotPanics(t, func() { cfg.Execute() })
}
func TestForceConfigFileInvalid(t *testing.T) {
viper.Reset()
cfg := verdeter.NewVerdeterCommand(
"superuncommonappnameinvalid",
"a test command",
"whatever",
func(cfg *verdeter.VerdeterCommand, args []string) error {
fmt.Printf("args=%v", args)
return nil
},
)
cfg.GKey("config_path", verdeter.IsStr, "", "path to the config directory")
cfg.SetDefault("config_path", "./fixtures/1/")
viper.Set("config_path", "./superuncommonappnameforaconfigfile.yml")
assert.NotPanics(t, func() { cfg.Execute() })
}
func TestSetRequiredPanicOnInvalidKey(t *testing.T) {
viper.Reset()
cfg := verdeter.NewVerdeterCommand(
"superuncommonappnameinvalid",
"a test command",
"whatever",
func(cfg *verdeter.VerdeterCommand, args []string) error {
fmt.Printf("args=%v", args)
return nil
},
)
assert.Panics(t,
func() { cfg.SetRequired("unknow string") },
)
}
func TestSetDefaultPanicOnInvalidKey(t *testing.T) {
viper.Reset()
cfg := verdeter.NewVerdeterCommand(
"superuncommonappnameinvalid",
"a test command",
"whatever",
func(cfg *verdeter.VerdeterCommand, args []string) error {
fmt.Printf("args=%v", args)
return nil
},
)
assert.Panics(t,
func() { cfg.SetDefault("unknow string", 125) },
)
}
func TestSetNormalizePanicOnInvalidKey(t *testing.T) {
viper.Reset()
cfg := verdeter.NewVerdeterCommand(
"superuncommonappnameinvalid",
"a test command",
"whatever",
func(cfg *verdeter.VerdeterCommand, args []string) error {
fmt.Printf("args=%v", args)
return nil
},
)
assert.Panics(t,
func() { cfg.SetNormalize("unknow string", nil) },
)
}
func TestAddValidatorPanicOnInvalidKey(t *testing.T) {
viper.Reset()
cfg := verdeter.NewVerdeterCommand(
"superuncommonappnameinvalid",
"a test command",
"whatever",
func(cfg *verdeter.VerdeterCommand, args []string) error {
fmt.Printf("args=%v", args)
return nil
},
)
assert.Panics(t,
func() { cfg.AddValidator("unknow string", validators.StringNotEmpty) },
)
}