-
Notifications
You must be signed in to change notification settings - Fork 30
/
config_test.go
158 lines (120 loc) · 3.58 KB
/
config_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
package config_test
import (
"errors"
"github.com/golobby/config/v3"
"github.com/golobby/config/v3/pkg/feeder"
"github.com/stretchr/testify/assert"
"os"
"syscall"
"testing"
"time"
)
type Region int
const (
Asia Region = iota
Europe
America
Else
)
type FullConfig struct {
App struct {
Name string `env:"APP_NAME"`
Port int `env:"APP_PORT"`
}
Debug bool `env:"DEBUG"`
Production bool `env:"PRODUCTION"`
Pi float64 `env:"PI"`
IPs []string `env:"IPS"`
IDs []int16 `env:"IDS"`
RegionId int `env:"REGION"`
Region Region
}
func (fc *FullConfig) Setup() error {
if fc.RegionId == 0 {
fc.Region = Asia
} else if fc.RegionId == 1 {
fc.Region = Europe
} else if fc.RegionId == 2 {
fc.Region = America
} else if fc.RegionId == 3 {
fc.Region = Else
} else {
return errors.New("invalid region")
}
return nil
}
func TestConfig_Feed_With_No_Data(t *testing.T) {
c := &struct{}{}
err := config.New().AddFeeder(feeder.Env{}).AddStruct(c).Feed()
assert.NoError(t, err)
}
func TestConfig_Feed_With_Invalid_File_It_Should_Fail(t *testing.T) {
s := struct{}{}
c := config.New().AddFeeder(feeder.Json{}).AddStruct(&s)
err := c.Feed()
assert.EqualError(t, err, "config: feeder error: json: read .: is a directory")
}
func TestConfig_Feed(t *testing.T) {
_ = os.Setenv("PRODUCTION", "1")
_ = os.Setenv("APP_PORT", "6969")
c := &FullConfig{}
f1 := feeder.Json{Path: "assets/sample1.json"}
f2 := feeder.DotEnv{Path: "assets/.env.sample2"}
f3 := feeder.Env{}
err := config.New().AddFeeder(f1, f2, f3).AddStruct(c).Feed()
assert.NoError(t, err)
assert.Equal(t, "Blog", c.App.Name)
assert.Equal(t, 6969, c.App.Port)
assert.Equal(t, false, c.Debug)
assert.Equal(t, true, c.Production)
assert.Equal(t, 3.14, c.Pi)
assert.Equal(t, []string{"192.168.0.1", "192.168.0.2"}, c.IPs)
assert.Equal(t, []int16{10, 11, 12, 13}, c.IDs)
assert.Equal(t, Asia, c.Region)
}
func TestConfig_Feed_With_Setup_Returning_Error(t *testing.T) {
_ = os.Setenv("REGION", "4")
c := &FullConfig{}
f1 := feeder.Json{Path: "assets/sample1.json"}
f2 := feeder.Env{}
err := config.New().AddFeeder(f1, f2).AddStruct(c).Feed()
assert.Error(t, err, "invalid region")
}
func TestConfig_ReFeeding(t *testing.T) {
_ = os.Setenv("NAME", "One")
s := &struct {
Name string `env:"NAME"`
}{}
c := config.New().AddFeeder(feeder.Env{}).AddStruct(s)
err := c.Feed()
assert.NoError(t, err)
assert.Equal(t, "One", s.Name)
_ = os.Setenv("NAME", "Two")
err = c.Feed()
assert.NoError(t, err)
assert.Equal(t, "Two", s.Name)
}
func TestConfig_SetupListener(t *testing.T) {
_ = os.Setenv("PI", "3.14")
s := &struct {
Pi float64 `env:"PI"`
}{}
fallbackTested := false
c := config.New().AddFeeder(feeder.Env{}).AddStruct(s).SetupListener(func(err error) {
fallbackTested = true
})
err := c.Feed()
assert.NoError(t, err)
assert.Equal(t, 3.14, s.Pi)
_ = os.Setenv("PI", "3.666")
err = syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
assert.NoError(t, err)
time.Sleep(10 * time.Millisecond)
assert.Equal(t, 3.666, s.Pi)
_ = os.Setenv("PI", "INVALID!")
err = syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
assert.NoError(t, err)
time.Sleep(10 * time.Millisecond)
assert.Equal(t, true, fallbackTested)
assert.Equal(t, 3.666, s.Pi)
}