forked from Mistobaan/env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_test.go
218 lines (176 loc) · 6 KB
/
env_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
package env
import (
"testing"
"time"
"fmt"
"runtime"
"path/filepath"
"reflect"
"os"
)
func TestParseDuration(t *testing.T) {
var cfg = struct {
Duration time.Duration `env:"key=DURATION default=5s"`
}{}
if err := Process(&cfg); err != nil {
t.Fatal(err)
}
if cfg.Duration != time.Second*5 {
t.Fatalf("%v != %f ", cfg.Duration, time.Second*5)
}
}
// Test Slice parsing with YAML value
func TestYamlList(t *testing.T) {
os.Setenv("LIST", "[a,b,c]")
var cfg struct {
List []string `env:"key=LIST decode=yaml default=[]"`
}
err := Process(&cfg);
ok(t, err)
assert(t, cfg.List != nil, "List is nil")
assert(t, len(cfg.List) == 3, "List is not length 3")
assert(t, cfg.List[0] == "a", "List[0] is not a")
assert(t, cfg.List[1] == "b", "List[1] is not b")
assert(t, cfg.List[2] == "c", "List[2] is not c")
}
// Test Map parsing with YAML value
func TestYamlMap(t *testing.T) {
os.Setenv("MAP", "{a: A, b: B}")
var cfg struct {
Map map[string]string `env:"key=MAP decode=yaml default={}"`
}
err := Process(&cfg);
ok(t, err)
assert(t, cfg.Map != nil, "Map is nil")
assert(t, len(cfg.Map) == 2, "Map is not length 2")
assert(t, cfg.Map["a"] == "A", "Map[a] is not A")
assert(t, cfg.Map["b"] == "B", "Map[b] is not B")
}
// Test MapSlice parsing with YAML value
func TestYamlMapList(t *testing.T) {
os.Setenv("MAP_LIST", "{a: [A1, A2], b: [B1, B2]}")
var cfg struct {
MapList map[string][]string `env:"key=MAP_LIST decode=yaml default={}"`
}
err := Process(&cfg);
ok(t, err)
assert(t, cfg.MapList != nil, "MapList is nil")
assert(t, len(cfg.MapList) == 2, "MapList is not length 2")
assert(t, cfg.MapList["a"][0] == "A1", "MapList[a][0] is not A1")
assert(t, cfg.MapList["a"][1] == "A2", "MapList[a][1] is not A2")
assert(t, cfg.MapList["b"][0] == "B1", "MapList[b][0] is not B1")
assert(t, cfg.MapList["b"][1] == "B2", "MapList[b][1] is not B2")
}
// Test Slice parsing with JSON value
func TestJsonList(t *testing.T) {
os.Setenv("LIST", `["a","b","c"]`)
var cfg struct {
List []string `env:"key=LIST decode=yaml default=[]"`
}
err := Process(&cfg);
ok(t, err)
assert(t, cfg.List != nil, "List is nil")
assert(t, len(cfg.List) == 3, "List is not length 3")
assert(t, cfg.List[0] == "a", "List[0] is not a")
assert(t, cfg.List[1] == "b", "List[1] is not b")
assert(t, cfg.List[2] == "c", "List[2] is not c")
}
// Test Map parsing with JSON value
func TestJsonMap(t *testing.T) {
os.Setenv("MAP", `{"a": "A", "b": "B"}`)
var cfg struct {
Map map[string]string `env:"key=MAP decode=yaml default={}"`
}
err := Process(&cfg);
ok(t, err)
assert(t, cfg.Map != nil, "Map is nil")
assert(t, len(cfg.Map) == 2, "Map is not length 2")
assert(t, cfg.Map["a"] == "A", "Map[a] is not A")
assert(t, cfg.Map["b"] == "B", "Map[b] is not B")
}
// Test MapSlice parsing with JSON
func TestJsonMapList(t *testing.T) {
os.Setenv("MAP_LIST", `{"a": ["A1", "A2"], "b": ["B1", "B2"]}`)
var cfg struct {
MapList map[string][]string `env:"key=MAP_LIST decode=yaml default={}"`
}
err := Process(&cfg);
ok(t, err)
assert(t, cfg.MapList != nil, "MapList is nil")
assert(t, len(cfg.MapList) == 2, "MapList is not length 2")
assert(t, cfg.MapList["a"][0] == "A1", "MapList[a][0] is not A1")
assert(t, cfg.MapList["a"][1] == "A2", "MapList[a][1] is not A2")
assert(t, cfg.MapList["b"][0] == "B1", "MapList[b][0] is not B1")
assert(t, cfg.MapList["b"][1] == "B2", "MapList[b][1] is not B2")
}
// Test Slice parsing with no decode specified (default)
// Should accept YAML
func TestDefaultList(t *testing.T) {
os.Setenv("LIST", "[a,b,c]")
var cfg struct {
List []string `env:"key=LIST default=[]"`
}
err := Process(&cfg);
ok(t, err)
assert(t, cfg.List != nil, "List is nil")
assert(t, len(cfg.List) == 3, "List is not length 3")
assert(t, cfg.List[0] == "a", "List[0] is not a")
assert(t, cfg.List[1] == "b", "List[1] is not b")
assert(t, cfg.List[2] == "c", "List[2] is not c")
}
// Test Map parsing with no decode specified (default)
// Should accept YAML
func TestDefaultMap(t *testing.T) {
os.Setenv("MAP", "{a: A, b: B}")
var cfg struct {
Map map[string]string `env:"key=MAP default={}"`
}
err := Process(&cfg);
ok(t, err)
assert(t, cfg.Map != nil, "Map is nil")
assert(t, len(cfg.Map) == 2, "Map is not length 2")
assert(t, cfg.Map["a"] == "A", "Map[a] is not A")
assert(t, cfg.Map["b"] == "B", "Map[b] is not B")
}
// Test MapSlice parsing with no decode specified (default)
// Should accept YAML
func TestDefaultMapList(t *testing.T) {
os.Setenv("MAP_LIST", "{a: [A1, A2], b: [B1, B2]}")
var cfg struct {
MapList map[string][]string `env:"key=MAP_LIST default={}"`
}
err := Process(&cfg);
ok(t, err)
assert(t, cfg.MapList != nil, "MapList is nil")
assert(t, len(cfg.MapList) == 2, "MapList is not length 2")
assert(t, cfg.MapList["a"][0] == "A1", "MapList[a][0] is not A1")
assert(t, cfg.MapList["a"][1] == "A2", "MapList[a][1] is not A2")
assert(t, cfg.MapList["b"][0] == "B1", "MapList[b][0] is not B1")
assert(t, cfg.MapList["b"][1] == "B2", "MapList[b][1] is not B2")
}
// Add some simple funcs to allow quick testing
// copied from: https://github.com/benbjohnson/testing
// 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()
}
}