forked from vrischmann/envconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvconfig_test.go
264 lines (216 loc) · 5.5 KB
/
envconfig_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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package envconfig_test
import (
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
"time"
"github.com/vrischmann/envconfig"
)
func TestParseSimpleConfig(t *testing.T) {
var conf struct {
Name string
Log struct {
Path string
}
}
// Go 1.2 and 1.3 don't have os.Unsetenv
os.Setenv("NAME", "")
os.Setenv("LOG_PATH", "")
err := envconfig.Init(&conf)
equals(t, "envconfig: key NAME not found", err.Error())
os.Setenv("NAME", "foobar")
err = envconfig.Init(&conf)
equals(t, "envconfig: key LOG_PATH not found", err.Error())
os.Setenv("LOG_PATH", "/var/log/foobar")
err = envconfig.Init(&conf)
ok(t, err)
equals(t, "foobar", conf.Name)
equals(t, "/var/log/foobar", conf.Log.Path)
}
func TestParseIntegerConfig(t *testing.T) {
var conf struct {
Port int
Long uint64
Version uint8
}
timestamp := time.Now().UnixNano()
os.Setenv("PORT", "80")
os.Setenv("LONG", fmt.Sprintf("%d", timestamp))
os.Setenv("VERSION", "2")
err := envconfig.Init(&conf)
ok(t, err)
equals(t, 80, conf.Port)
equals(t, uint64(timestamp), conf.Long)
equals(t, uint8(2), conf.Version)
}
func TestParseBoolConfig(t *testing.T) {
var conf struct {
DoIt bool
}
os.Setenv("DOIT", "true")
err := envconfig.Init(&conf)
ok(t, err)
equals(t, true, conf.DoIt)
}
func TestParseFloatConfig(t *testing.T) {
var conf struct {
Delta float32
DeltaV float64
}
os.Setenv("DELTA", "0.02")
os.Setenv("DELTAV", "400.20000000001")
err := envconfig.Init(&conf)
ok(t, err)
equals(t, float32(0.02), conf.Delta)
equals(t, float64(400.20000000001), conf.DeltaV)
}
func TestParseSliceConfig(t *testing.T) {
var conf struct {
Names []string
Ports []int
Shards []struct {
Name string
Addr string
}
}
os.Setenv("NAMES", "foobar,barbaz")
os.Setenv("PORTS", "900,100")
os.Setenv("SHARDS", "{foobar,localhost:2929},{barbaz,localhost:2828}")
err := envconfig.Init(&conf)
ok(t, err)
equals(t, 2, len(conf.Names))
equals(t, "foobar", conf.Names[0])
equals(t, "barbaz", conf.Names[1])
equals(t, 2, len(conf.Ports))
equals(t, 900, conf.Ports[0])
equals(t, 100, conf.Ports[1])
equals(t, 2, len(conf.Shards))
equals(t, "foobar", conf.Shards[0].Name)
equals(t, "localhost:2929", conf.Shards[0].Addr)
equals(t, "barbaz", conf.Shards[1].Name)
equals(t, "localhost:2828", conf.Shards[1].Addr)
}
func TestDurationConfig(t *testing.T) {
var conf struct {
Timeout time.Duration
}
os.Setenv("TIMEOUT", "1m")
err := envconfig.Init(&conf)
ok(t, err)
equals(t, time.Minute*1, conf.Timeout)
}
func TestAllPointerConfig(t *testing.T) {
var conf struct {
Name *string
Port *int
Delta *float32
DeltaV *float64
Hosts *[]string
Shards *[]*struct {
Name *string
Addr *string
}
Master *struct {
Name *string
Addr *string
}
Timeout *time.Duration
}
os.Setenv("NAME", "foobar")
os.Setenv("PORT", "9000")
os.Setenv("DELTA", "40.01")
os.Setenv("DELTAV", "200.00001")
os.Setenv("HOSTS", "localhost,free.fr")
os.Setenv("SHARDS", "{foobar,localhost:2828},{barbaz,localhost:2929}")
os.Setenv("MASTER_NAME", "master")
os.Setenv("MASTER_ADDR", "localhost:2727")
os.Setenv("TIMEOUT", "1m")
err := envconfig.Init(&conf)
ok(t, err)
equals(t, "foobar", *conf.Name)
equals(t, 9000, *conf.Port)
equals(t, float32(40.01), *conf.Delta)
equals(t, 200.00001, *conf.DeltaV)
equals(t, 2, len(*conf.Hosts))
equals(t, "localhost", (*conf.Hosts)[0])
equals(t, "free.fr", (*conf.Hosts)[1])
equals(t, 2, len(*conf.Shards))
equals(t, "foobar", *(*conf.Shards)[0].Name)
equals(t, "localhost:2828", *(*conf.Shards)[0].Addr)
equals(t, "barbaz", *(*conf.Shards)[1].Name)
equals(t, "localhost:2929", *(*conf.Shards)[1].Addr)
equals(t, "master", *conf.Master.Name)
equals(t, "localhost:2727", *conf.Master.Addr)
equals(t, time.Minute*1, *conf.Timeout)
}
type logMode uint
const (
logFile logMode = iota + 1
logStdout
)
func (m *logMode) Unmarshal(s string) error {
switch strings.ToLower(s) {
case "file":
*m = logFile
case "stdout":
*m = logStdout
default:
return fmt.Errorf("unable to unmarshal %s", s)
}
return nil
}
func TestUnmarshaler(t *testing.T) {
var conf struct {
LogMode logMode
}
os.Setenv("LOGMODE", "file")
err := envconfig.Init(&conf)
ok(t, err)
equals(t, logFile, conf.LogMode)
}
func TestParseOptionalConfig(t *testing.T) {
var conf struct {
Name string `envconfig:"optional"`
}
os.Setenv("NAME", "")
err := envconfig.Init(&conf)
ok(t, err)
equals(t, "", conf.Name)
}
func TestParseCustomNameConfig(t *testing.T) {
var conf struct {
Name string `envconfig:"customName"`
}
os.Setenv("customName", "foobar")
err := envconfig.Init(&conf)
ok(t, err)
equals(t, "foobar", conf.Name)
}
// assert fails the test if the condition is false.
func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
if !condition {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
tb.FailNow()
}
}
// ok fails the test if an err is not nil.
func ok(tb testing.TB, err error) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
tb.FailNow()
}
}
// equals fails the test if exp is not equal to act.
func equals(tb testing.TB, exp, act interface{}) {
if !reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.FailNow()
}
}