-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiger_test.go
243 lines (199 loc) · 5.99 KB
/
configer_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
package configer
import (
"os"
"strings"
"testing"
)
type myconfig struct {
Port int `env:"PORT"`
LogFile string `env:"LOG_FILE"`
}
func (mc myconfig) Validate() error {
return nil
}
func buildFileWithContents(t *testing.T, pattern, contents string) (string, error) {
t.Helper()
f, err := os.CreateTemp("", pattern)
if err != nil {
return "", err
}
defer f.Close()
if _, err := f.WriteString(contents); err != nil {
return "", err
}
return f.Name(), nil
}
func buildConfigFiles(t *testing.T, extensions ...string) ([]string, func(), error) {
t.Helper()
files := make([]string, len(extensions))
for i, ext := range extensions {
fn, err := func(ext string) (string, error) {
var pattern, contents string
switch strings.ToLower(ext) {
case "json":
pattern = "config-*.json"
contents = `{"Port": 3000, "LogFile": "out-json.log"}`
case "yaml":
pattern = "config-*.yaml"
contents = "port: 5000\nlogfile: 'out-yaml.log'"
case "env":
pattern = "config-*.env"
contents = "PORT=8000\nLOG_FILE=out-env.log"
}
f, err := os.CreateTemp("", pattern)
if err != nil {
return "", err
}
defer f.Close()
// write to file with default data
_, err = f.WriteString(contents)
if err != nil {
return "", err
}
return f.Name(), nil
}(ext)
if err != nil {
return nil, nil, err
}
files[i] = fn
}
cleanup := func() {
for _, f := range files {
if err := os.Remove(f); err != nil {
t.Errorf("unable to remove file %q: %v", f, err)
}
}
}
return files, cleanup, nil
}
// TestLoad_filename_dot_env tests that the file ".env" is accepted and read
func TestLoad_filename_dot_env(t *testing.T) {
f, err := os.Create(".env")
if err != nil {
t.Fatalf("unable to create .env: %v", err)
}
f.WriteString("PORT=8000")
f.Close()
t.Cleanup(func() {
if err := os.Remove(".env"); err != nil {
t.Errorf("unable to delete file %q: %v", ".env", err)
}
})
mc := myconfig{}
if err := Load(&mc, ".env"); err != nil {
t.Errorf("expected nil err; got: %v", err)
}
if mc.Port != 8000 {
t.Errorf("mc.Port not loaded correctly; expected: 8000; got: %v", mc.Port)
}
}
func TestLoad_invalid_interface(t *testing.T) {
expected := "invalid struct"
mc := myconfig{}
err := Load(mc)
if err == nil || !strings.Contains(err.Error(), expected) {
t.Errorf("expected err to complain about non-pointer; expected: err contains %q; got %v", expected, err)
}
}
func TestLoad_json_file(t *testing.T) {
files, cleanup, err := buildConfigFiles(t, "JSON")
if err != nil {
t.Fatalf("unable to create prereq files: %v", err)
}
t.Cleanup(cleanup)
mc := myconfig{Port: 1234}
if err := Load(&mc, files...); err != nil {
t.Fatalf("unable to load json file: %v", err)
}
// json defaults
expected := myconfig{Port: 3000, LogFile: "out-json.log"}
if mc.Port != expected.Port {
t.Errorf("Load(&mc, tmp.json) load failure; expected mc.Port=%d; got mc.Port=%d", expected.Port, mc.Port)
}
if mc.LogFile != expected.LogFile {
t.Errorf("Load(&mc, tmp.json) load failure; expected mc.LogFile=%s; got mc.LogFile=%s", expected.LogFile, mc.LogFile)
}
}
func TestLoad_yaml_file(t *testing.T) {
files, cleanup, err := buildConfigFiles(t, "YAML")
if err != nil {
t.Fatalf("unable to create prereq files: %v", err)
}
t.Cleanup(cleanup)
mc := myconfig{Port: 1234}
if err := Load(&mc, files...); err != nil {
t.Fatalf("unable to load yaml file: %v", err)
}
// yaml defaults
expected := myconfig{Port: 5000, LogFile: "out-yaml.log"}
if mc.Port != expected.Port {
t.Errorf("Load(&mc, tmp.yaml) load failure; expected mc.Port=%d; got mc.Port=%d", expected.Port, mc.Port)
}
if mc.LogFile != expected.LogFile {
t.Errorf("Load(&mc, tmp.yaml) load failure; expected mc.LogFile=%s; got mc.LogFile=%s", expected.LogFile, mc.LogFile)
}
}
func TestLoad_env_file(t *testing.T) {
files, cleanup, err := buildConfigFiles(t, "ENV")
if err != nil {
t.Fatalf("unable to create prereq files: %v", err)
}
t.Cleanup(cleanup)
mc := myconfig{Port: 1234}
if err := Load(&mc, files...); err != nil {
t.Fatalf("unable to load env file: %v", err)
}
// env defaults
expected := myconfig{Port: 8000, LogFile: "out-env.log"}
if mc.Port != expected.Port {
t.Errorf("Load(&mc, tmp.env) load failure; expected mc.Port=%d; got mc.Port=%d", expected.Port, mc.Port)
}
if mc.LogFile != expected.LogFile {
t.Errorf("Load(&mc, tmp.env) load failure; expected mc.LogFile=%s; got mc.LogFile=%s", expected.LogFile, mc.LogFile)
}
}
func TestLoad_multi(t *testing.T) {
yamlFile, err := buildFileWithContents(t, "config-*.yaml", "logfile: 'out-yaml.log'")
if err != nil {
t.Fatalf("unable to create yaml file: %v", err)
}
jsonFile, err := buildFileWithContents(t, "config-*.json", `{"Port": 3000}`)
if err != nil {
t.Fatalf("unable to create json file: %v", err)
}
t.Cleanup(func() {
for _, f := range []string{yamlFile, jsonFile} {
if err := os.Remove(f); err != nil {
t.Errorf("unable to delete file %q: %v", f, err)
}
}
})
mc := myconfig{}
if err := Load(&mc, jsonFile, yamlFile); err != nil {
t.Fatalf("unable to load env file: %v", err)
}
expected := myconfig{Port: 3000, LogFile: "out-yaml.log"}
if mc.Port != expected.Port {
t.Errorf("Load(&mc, yamlFile, jsonFile) load failure; expected mc.Port=%d; got mc.Port=%d", expected.Port, mc.Port)
}
if mc.LogFile != expected.LogFile {
t.Errorf("Load(&mc, yamlFile, jsonFile) load failure; expected mc.LogFile=%s; got mc.LogFile=%s", expected.LogFile, mc.LogFile)
}
}
func TestLoad_env_override(t *testing.T) {
if err := os.Setenv("PORT", "443"); err != nil {
t.Fatalf("cannot set PORT env: %v", err)
}
files, cleanup, err := buildConfigFiles(t, "JSON")
if err != nil {
t.Fatalf("unable to create prereq files: %v", err)
}
t.Cleanup(cleanup)
mc := myconfig{Port: 1234}
if err := Load(&mc, files...); err != nil {
t.Fatalf("unable to load json file: %v", err)
}
if mc.Port != 443 {
t.Errorf("Load(&mc, files...) env override load failure; expected mc.Port=%d; got mc.Port=%d", 443, mc.Port)
}
}