-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure_test.go
120 lines (92 loc) · 2.18 KB
/
configure_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
package configure_test
import (
"os"
"testing"
"github.com/m-porter/configure/v2"
)
func TestDefaults(t *testing.T) {
type Config struct {
HostName string `mapstructure:"host_name"`
}
expected := "my.test.host"
options := configure.Options{
Defaults: Config{
HostName: expected,
},
}
conf := &Config{}
if err := configure.Setup(options, conf); err != nil {
t.Fatalf("error setting up config: %v", err)
}
if conf.HostName != expected {
t.Errorf("expected %s, got %s", expected, conf.HostName)
}
}
func TestFromEnv(t *testing.T) {
type Config struct {
Secret string `mapstructure:"app_secret"`
}
key := "APP_SECRET"
_ = os.Unsetenv(key)
expected := "987xyz"
_ = os.Setenv(key, expected)
options := configure.Options{
EnvPrefix: "",
Defaults: Config{
Secret: "abc123",
},
}
conf := &Config{}
if err := configure.Setup(options, conf); err != nil {
t.Fatalf("error setting up config: %v", err)
}
if conf.Secret != expected {
t.Errorf("expected %s, got %s", expected, conf.Secret)
}
_ = os.Unsetenv(key)
}
func TestFromEnvWithNoEnvPrefix(t *testing.T) {
type Config struct {
AnotherSecret string `mapstructure:"another_secret"`
}
key := "APP_ANOTHER_SECRET"
_ = os.Unsetenv(key)
expected := "987xyz"
_ = os.Setenv(key, expected)
options := configure.Options{
EnvPrefix: "APP",
Defaults: Config{
AnotherSecret: "abc123",
},
}
conf := &Config{}
if err := configure.Setup(options, conf); err != nil {
t.Fatalf("error setting up config: %v", err)
}
if conf.AnotherSecret != expected {
t.Errorf("expected %s, got %s", expected, conf.AnotherSecret)
}
_ = os.Unsetenv(key)
}
func TestFromFile(t *testing.T) {
type Config struct {
SomeValue string `mapstructure:"some_value"`
}
dir, err := os.Getwd()
if err != nil {
t.Fatalf("Unable to get working dir: %v", err)
}
options := configure.Options{
ConfigName: "test_config",
ConfigType: "yaml",
ConfigAbsPath: dir,
}
conf := &Config{}
if err := configure.Setup(options, conf); err != nil {
t.Fatalf("error setting up config: %v", err)
}
expected := "foo foo foo"
if conf.SomeValue != expected {
t.Errorf("expected %s, got %s", expected, conf.SomeValue)
}
}