-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetting_test.go
60 lines (46 loc) · 1.13 KB
/
setting_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
package setting
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
type SimpleConfig struct {
AAA string `default:"A"`
BBB string `env:"BBB"`
CCC string `env:"CCC" default:"C"`
}
type NestedConfig struct {
Service struct {
Port string `default:"1234"`
Auth struct {
Key string `default:"key"`
Secret string `env:"SERVICE_SECRET"`
}
}
}
func TestSettingFromDefault(t *testing.T) {
config := new(SimpleConfig)
Load(config)
assert.Equal(t, config.AAA, "A")
}
func TestSettingFromEnv(t *testing.T) {
os.Setenv("BBB", "three b's")
defer os.Unsetenv("BBB")
config := new(SimpleConfig)
Load(config)
assert.Equal(t, config.BBB, "three b's")
}
func TestSettingFromEnvDefaultFallback(t *testing.T) {
config := new(SimpleConfig)
Load(config)
assert.Equal(t, config.CCC, "C")
}
func TestSettingNestedConfig(t *testing.T) {
os.Setenv("SERVICE_SECRET", "super secret")
defer os.Unsetenv("SERVICE_SECRET")
config := new(NestedConfig)
Load(config)
assert.Equal(t, config.Service.Port, "1234")
assert.Equal(t, config.Service.Auth.Key, "key")
assert.Equal(t, config.Service.Auth.Secret, "super secret")
}