Repository navigation
Expand file tree
/
Copy pathtinyviper_test.go
More file actions
148 lines (128 loc) · 3.4 KB
/
Copy pathtinyviper_test.go
File metadata and controls
148 lines (128 loc) · 3.4 KB
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
package tinyviper
import (
"errors"
"os"
"testing"
)
type Config struct {
UserConfig struct {
Email string `env:"MY_APP_EMAIL"`
Password string `env:"MY_APP_PASSWORD"`
}
Endpoint string `env:"MY_APP_ENDPOINT"`
AppUrl string `env:"MY_APP_URL"`
Undefined string `env:"MY_UNDEFINED"`
Optional string `env:"MY_APP_OPTIONAL,omitempty"`
}
type Config2 struct {
UserConfig struct {
Email string `env:"MY_APP_EMAIL"`
Password string `env:"MY_APP_PASSWORD"`
}
Endpoint string `env:"MY_APP_ENDPOINT"`
AppUrl string `env:"MY_APP_URL"`
Undefined string `env:"MY_UNDEFINED"`
Optional string `env:"MY_APP_OPTIONAL"`
}
type testEnvResolver struct{}
func (t testEnvResolver) Get(key string) string {
switch key {
case "MY_APP_EMAIL":
return "someemail@someprovider.org"
case "MY_APP_PASSWORD":
return "somepassword@someprovider.org"
default:
return ""
}
}
func TestConfigErrors(t *testing.T) {
cfg := Config{
Undefined: "foo",
}
err := LoadFromResolver(&cfg, testEnvResolver{})
if err == nil {
t.Fatalf("Expected error, got none")
}
if err.Error() != "missing config variables: MY_APP_ENDPOINT,MY_APP_URL" {
t.Error("Expected error, got wrong one: " + err.Error())
}
}
func TestConfigNew(t *testing.T) {
cfg := Config{
Undefined: "foo",
}
err := LoadFromResolver(&cfg, NewEnvResolver(), NewEnvFileResolver(".env.sample"))
if err != nil {
t.Error(err)
}
if cfg.UserConfig.Email != "someemail@someprovider.org" {
t.Error(errors.New("unexpected email"))
}
if cfg.UserConfig.Password != "password2" {
t.Error(errors.New("unexpected password"))
}
if cfg.Endpoint != "some-endpoint" {
t.Error(errors.New("unexpected endpoint"))
}
if cfg.AppUrl != "https://some.exmpale.org/foo?token=bar" {
t.Error(errors.New("unexpected url"))
}
}
func TestConfigNewWithoutFile(t *testing.T) {
cfg := Config{
Undefined: "foo",
}
err := LoadFromResolver(&cfg, EnvResolver{}, NewEnvFileResolver(".env.sample2"))
if err == nil {
t.Fatalf("Expected error, got none")
}
if err.Error() != "missing config variables: MY_APP_EMAIL,MY_APP_PASSWORD,MY_APP_ENDPOINT,MY_APP_URL" {
t.Error("Expected error, got wrong one: " + err.Error())
}
}
func TestConfigOverride(t *testing.T) {
_ = os.Setenv("MY_APP_EMAIL", "someemail2@someprovider.org")
cfg := Config{
Undefined: "foo",
}
err := LoadFromResolver(&cfg, NewEnvResolver(), NewEnvFileResolver(".env.sample"))
if err != nil {
t.Error(err)
}
if cfg.UserConfig.Email != "someemail2@someprovider.org" {
t.Error(errors.New("unexpected email"))
}
if cfg.Undefined != "foo" {
t.Error(errors.New("unexpected app url"))
}
if cfg.UserConfig.Password != "password2" {
t.Error(errors.New("unexpected password"))
}
if cfg.Optional != "" {
t.Error(errors.New("unexpected optional"))
}
}
func TestConfigNoOmitMissingVariable(t *testing.T) {
cfg := Config2{
Undefined: "foo",
}
err := LoadFromResolver(&cfg, NewEnvResolver(), NewEnvFileResolver(".env.sample"))
if err == nil {
t.Fatalf("Expected error, got none")
}
if err.Error() != "missing config variables: MY_APP_OPTIONAL" {
t.Error("Expected error, got wrong one: " + err.Error())
}
}
func TestConfigOmitVariableDefined(t *testing.T) {
cfg := Config{
Undefined: "foo",
}
err := LoadFromResolver(&cfg, NewEnvResolver(), NewEnvFileResolver(".env.sample3"))
if err != nil {
t.Error(err)
}
if cfg.Optional != "optional" {
t.Error(errors.New("unexpected optional"))
}
}