-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathenviper_test.go
270 lines (226 loc) · 5.62 KB
/
enviper_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
265
266
267
268
269
270
package enviper_test
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"testing"
"github.com/iamolegga/enviper"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
func TestUnmarshal(t *testing.T) {
suite.Run(t, new(UnmarshalSuite))
}
type UnmarshalSuite struct {
suite.Suite
v *viper.Viper
env map[string]string
}
func (s *UnmarshalSuite) SetupSuite() {
s.loadEnvVars()
}
func (s *UnmarshalSuite) SetupTest() {
s.v = viper.New()
}
func (s *UnmarshalSuite) TearDownTest() {}
func (s *UnmarshalSuite) TearDownSuite() {}
func (s *UnmarshalSuite) TestThrowsErrorWhenBrokenConfig() {
cwd, _ := os.Getwd()
p := path.Join(cwd, "test_config.yaml")
ioutil.WriteFile(p, []byte("qwerty"), 0777)
defer os.Remove(p)
var c Config
e := enviper.New(s.v)
e.AddConfigPath(cwd)
e.SetConfigName("test_config")
s.NotNil(e.Unmarshal(&c))
}
func (s *UnmarshalSuite) TestOnlyEnvsByCustomTag() {
s.setupEnvConfig()
defer s.tearDownEnvConfig()
var c Config
e := enviper.New(s.v).WithTagName("custom_tag")
e.SetEnvPrefix("PREF")
s.Nil(e.Unmarshal(&c))
s.Equal("imTheValueByCustomTag", c.TagTest)
}
func (s *UnmarshalSuite) TestOnlyEnvsWithTagValue() {
s.setupEnvConfig()
defer s.tearDownEnvConfig()
var c Config
e := enviper.New(s.v)
e.SetEnvPrefix("PREF")
s.Nil(e.Unmarshal(&c))
s.Equal("imTheValueWithOmitempty", c.TagValueWithOmitempty)
s.Equal("imTheValueWithCustomName", c.TagValueWithNameOmitempty)
s.Equal("", c.TagValueWithDash)
}
func (s *UnmarshalSuite) TestOnlyEnvs() {
s.setupEnvConfig()
defer s.tearDownEnvConfig()
var c Config
e := enviper.New(s.v)
e.SetEnvPrefix("PREF")
s.Nil(e.Unmarshal(&c))
s.Equal("fooooo", c.Foo)
s.Equal(2, c.Bar.BAZ)
s.Equal(false, c.QUX.Quuux)
s.Equal("testptr3", c.QUX.QuuuxPtrUnset.Value)
// TODO: known bug, that maps can not be set when there is no config file. Uncomment and fix
//s.Equal(true, c.QuxMap["key1"].Quuux)
}
func (s *UnmarshalSuite) TestPrimitiveMap() {
s.setupFileConfig()
var c Config
e := enviper.New(s.v)
s.Nil(e.Unmarshal(&c))
s.Equal("val1", c.PrimitiveMap["key1"])
s.Equal("val2", c.PrimitiveMap["key2"])
s.Equal("", c.PrimitiveMap[""])
}
func (s *UnmarshalSuite) TestOnlyConfig() {
s.setupFileConfig()
var c Config
e := enviper.New(s.v)
s.Nil(e.Unmarshal(&c))
s.Equal("foo", c.Foo)
s.Equal(1, c.Bar.BAZ)
s.Equal(true, c.QUX.Quuux)
s.Equal("testptr1", c.FooPtr.Value)
s.Equal("testptr2", c.QUX.QuuuxPtr.Value)
}
func (s *UnmarshalSuite) TestConfigWithEnvs() {
s.setupFileConfig()
s.setupEnvConfig()
defer s.tearDownEnvConfig()
var c Config
e := enviper.New(s.v)
e.SetEnvPrefix("PREF")
s.Nil(e.Unmarshal(&c))
s.Equal("fooooo", c.Foo)
s.Equal(2, c.Bar.BAZ)
s.Equal(false, c.QUX.Quuux)
s.Equal(true, c.QuxMap["key1"].Quuux)
s.Equal(false, c.QuxMap["key2"].Quuux)
s.Equal("testptr1", c.FooPtr.Value)
s.Equal("testptr2", c.QUX.QuuuxPtr.Value)
s.Equal("testptr3", c.QuuuxPtrUnset.Value)
}
func (s *UnmarshalSuite) setupFileConfig() {
cwd, _ := os.Getwd()
s.v.AddConfigPath(cwd)
s.v.SetConfigName("fixture")
}
func (s *UnmarshalSuite) setupEnvConfig() {
for k, v := range s.env {
if err := os.Setenv(k, v); err != nil {
s.T().Error(err)
}
}
}
func (s *UnmarshalSuite) tearDownEnvConfig() {
for k := range s.env {
if err := os.Unsetenv(k); err != nil {
s.T().Error(err)
}
}
}
func (s *UnmarshalSuite) loadEnvVars() {
cwd, _ := os.Getwd()
p := path.Join(cwd, "fixture_env")
bytes, err := ioutil.ReadFile(p)
if err != nil {
s.T().Error(err)
}
content := string(bytes)
raws := strings.Split(content, "\n")
s.env = make(map[string]string, len(raws))
for _, raw := range raws {
if len(raw) == 0 {
continue
}
pair := strings.Split(raw, "=")
if len(pair) != 2 {
s.T().Error(errors.New("invalid env fixtures"))
}
k := pair[0]
v := pair[1]
s.env[k] = v
}
}
type Config struct {
Foo string
FooPtr *PtrTest
Bar struct {
BAZ int `mapstructure:"baz"`
} `mapstructure:"bar"`
QuxMap map[string]struct {
Quuux bool
}
PrimitiveMap map[string]string
QUX `mapstructure:",squash"`
TagTest string `custom_tag:"TAG_TEST"`
TagValueWithOmitempty string `mapstructure:",omitempty"`
TagValueWithNameOmitempty string `mapstructure:"tag_custom_name,omitempty"`
TagValueWithDash string `mapstructure:"-"`
}
type QUX struct {
Quuux bool
QuuuxPtr *PtrTest `mapstructure:"quuux_ptr"`
QuuuxPtrUnset *PtrTest `mapstructure:"quuux_ptr_unset"`
}
type PtrTest struct {
Value string
}
func TestNew(t *testing.T) {
v := viper.New()
assert.Exactly(t, &enviper.Enviper{Viper: v}, enviper.New(v))
}
func ExampleEnviper_Unmarshal() {
// describe config structure
type barry struct {
Bar int `mapstructure:"bar"`
}
type bazzy struct {
Baz bool
}
type config struct {
Foo string
Barry barry
Bazzy bazzy `mapstructure:",squash"`
}
// write config file
dir := os.TempDir()
defer os.RemoveAll(dir)
p := path.Join(dir, "config.yaml")
ioutil.WriteFile(p, []byte(`
Foo: foo
Barry:
bar: 1
`), 0777)
// write env vars that could override values from config file
os.Setenv("MYAPP_BARRY_BAR", "2") // override value from file
os.Setenv("MYAPP_BAZ", "false")
defer os.Unsetenv("MYAPP_BARRY_BAR")
defer os.Unsetenv("MYAPP_BAZ")
// setup viper and enviper
var c config
e := enviper.New(viper.New())
e.SetEnvPrefix("MYAPP")
e.AddConfigPath(dir)
e.SetConfigName("config")
if err := e.Unmarshal(&c); err != nil {
fmt.Printf("%+v\n", err)
}
fmt.Println(c.Foo) // file only
fmt.Println(c.Barry.Bar) // file & env, take env
fmt.Println(c.Bazzy.Baz) // env only
// Output:
// foo
// 2
// false
}