-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuniconfig_test.go
208 lines (187 loc) · 5.3 KB
/
uniconfig_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package uniconfig
import (
"flag"
"os"
"runtime/debug"
"strings"
"testing"
"time"
)
const ()
func AssertEquals(t *testing.T, actual, expected interface{}) {
switch actual := actual.(type) {
case string:
if expected, ok := expected.(string); ok {
if actual != expected {
debug.PrintStack()
t.Fatalf("%s != %s", actual, expected)
}
} else {
t.Fatalf("Cannot compare: %v and %v (not string)", actual, expected)
}
case int:
if expected, ok := expected.(int); ok {
if actual != expected {
debug.PrintStack()
t.Fatalf("%d != %d", actual, expected)
}
} else {
debug.PrintStack()
t.Fatalf("Cannot compare: %v and %v (not int)", actual, expected)
}
case bool:
if expected, ok := expected.(bool); ok {
if actual != expected {
debug.PrintStack()
t.Fatalf("%s != %s", actual, expected)
}
} else {
debug.PrintStack()
t.Fatalf("Cannot compare: %v and %v (not bool)", actual, expected)
}
case time.Duration:
if expected, ok := expected.(time.Duration); ok {
if actual != expected {
debug.PrintStack()
t.Fatalf("%s != %s", actual, expected)
}
} else {
t.Fatalf("Cannot compare: %v and %v (not string)", actual, expected)
}
default:
debug.PrintStack()
t.Fatalf("Cannot compare: %v and %v", actual, expected)
}
}
type TestConfig struct {
Debug bool
Count int `help:"number of items"`
Nested1 struct {
A string
B string
ignored string
}
Nested2 struct {
Zzz bool
}
Count2 int64
TTL time.Duration
}
func TestScanConfig(t *testing.T) {
config := &TestConfig{}
// some defaults
config.Count = 42
config.Nested1.B = "baa"
config.TTL = time.Second
items := ScanConfig(config)
AssertEquals(t, len(items), 7)
AssertEquals(t, items[0].Section, "")
AssertEquals(t, items[0].Name, "Debug")
AssertEquals(t, items[0].Value.Interface(), false)
AssertEquals(t, items[0].Help, "")
AssertEquals(t, items[1].Section, "")
AssertEquals(t, items[1].Name, "Count")
AssertEquals(t, items[1].Value.Interface(), 42)
AssertEquals(t, items[1].Help, "number of items")
AssertEquals(t, items[2].Section, "Nested1")
AssertEquals(t, items[2].Name, "A")
AssertEquals(t, items[2].Value.Interface(), "")
AssertEquals(t, items[3].Section, "Nested1")
AssertEquals(t, items[3].Name, "B")
AssertEquals(t, items[3].Value.Interface(), "baa")
AssertEquals(t, items[4].Section, "Nested2")
AssertEquals(t, items[4].Name, "Zzz")
AssertEquals(t, items[4].Value.Interface(), false)
AssertEquals(t, items[4].Name, "Zzz")
AssertEquals(t, items[4].Value.Interface(), false)
AssertEquals(t, items[6].Section, "")
AssertEquals(t, items[6].Name, "TTL")
AssertEquals(t, items[6].Value.Interface(), time.Second)
AssertEquals(t, items[6].Help, "")
}
func TestLoadFromEnv(t *testing.T) {
config := TestConfig{}
// some defaults
config.Count = 42
config.Nested1.B = "baa"
items := ScanConfig(&config)
os.Setenv("DEBUG", "true")
os.Setenv("TTL", "10s")
os.Setenv("NESTED1_B", "buu")
os.Setenv("NESTED1_A", "wtf")
// need to reset the state between tests
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
InitFlags(items)
LoadFromEnv(items)
AssertEquals(t, config.Debug, true)
AssertEquals(t, config.Count, 42)
AssertEquals(t, config.TTL, 10*time.Second)
AssertEquals(t, config.Nested1.A, "wtf")
AssertEquals(t, config.Nested1.B, "buu")
AssertEquals(t, config.Nested2.Zzz, false)
}
func TestLoadFromIni(t *testing.T) {
config := TestConfig{}
// some defaults
config.Count = 42
config.Nested1.B = "baa"
items := ScanConfig(&config)
// need to reset the state between tests
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
InitFlags(items)
testIni := `
debug = true
count = 65535
; this is a comment
# also a comment
TTL=30m
[Nested1]
A = sometag
`
dict := ParseIniFile(strings.NewReader(testIni))
SetFromParsedIniFile(items, dict)
AssertEquals(t, config.Debug, true)
AssertEquals(t, config.Count, 65535)
AssertEquals(t, config.TTL, 30*time.Minute)
AssertEquals(t, config.Nested1.A, "sometag")
AssertEquals(t, config.Nested1.B, "baa")
AssertEquals(t, config.Nested2.Zzz, false)
testIni2 := `
debug = true
count = 65535
; this is a comment
# also a comment
[Nested1]
A = sometag
unknown_parameter = must panic
`
dict2 := ParseIniFile(strings.NewReader(testIni2))
func() {
defer func() {
if err := recover(); err != nil {
}
}()
SetFromParsedIniFile(items, dict2)
t.Fatal("Test must panic with unknown parameter error")
}()
}
func TestParseCmdline(t *testing.T) {
args := []string{}
configFile := GetConfigPathFromCmd(args)
AssertEquals(t, configFile, "")
args = []string{"--bubu", "config", "--bebe"}
configFile = GetConfigPathFromCmd(args)
AssertEquals(t, configFile, "")
args = []string{"--test", "--config", "megaconfig", "--bebe"}
configFile = GetConfigPathFromCmd(args)
AssertEquals(t, configFile, "megaconfig")
args = []string{"--test", "-config", "megaconfig2 42", "--bebe"}
configFile = GetConfigPathFromCmd(args)
AssertEquals(t, configFile, "megaconfig2 42")
args = []string{"--test", "-config=\"4249\"", "megaconfig2 42", "--bebe"}
configFile = GetConfigPathFromCmd(args)
AssertEquals(t, configFile, "4249")
args = []string{"--test", "--config=\"44991\"", "megaconfig2 42", "--bebe"}
configFile = GetConfigPathFromCmd(args)
AssertEquals(t, configFile, "44991")
}