forked from caarlos0/env
/
env_test.go
195 lines (166 loc) · 4.41 KB
/
env_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
package env_test
import (
"fmt"
"os"
"testing"
"github.com/richleigh/env"
"github.com/stretchr/testify/assert"
)
type Config struct {
Some string `env:"somevar"`
Other bool `env:"othervar"`
Port int `env:"PORT"`
NotAnEnv string
DatabaseURL string `env:"DATABASE_URL,optional"`
Password string `env:"PASSWORD,optional,sensitive"`
}
func TestParsesEnv(t *testing.T) {
os.Setenv("somevar", "somevalue")
os.Setenv("othervar", "true")
os.Setenv("PORT", "8080")
defer os.Setenv("somevar", "")
defer os.Setenv("othervar", "")
defer os.Setenv("PORT", "")
cfg := Config{}
assert.NoError(t, env.Parse(&cfg))
assert.Equal(t, "somevalue", cfg.Some)
assert.Equal(t, true, cfg.Other)
assert.Equal(t, 8080, cfg.Port)
}
func TestEmptyVars(t *testing.T) {
cfg := Config{}
assert.Error(t, env.Parse(&cfg))
}
func TestPassAnInvalidPtr(t *testing.T) {
var thisShouldBreak int
assert.Error(t, env.Parse(&thisShouldBreak))
}
func TestPassReference(t *testing.T) {
cfg := Config{}
assert.Error(t, env.Parse(cfg))
}
func TestInvalidBool(t *testing.T) {
os.Setenv("somevar", "somevalue")
os.Setenv("PORT", "8080")
defer os.Setenv("somevar", "")
defer os.Setenv("PORT", "")
os.Setenv("othervar", "should-be-a-bool")
defer os.Setenv("othervar", "")
cfg := Config{}
assert.Error(t, env.Parse(&cfg))
}
func TestInvalidInt(t *testing.T) {
os.Setenv("somevar", "somevalue")
os.Setenv("othervar", "true")
defer os.Setenv("somevar", "")
defer os.Setenv("othervar", "")
os.Setenv("PORT", "should-be-an-int")
defer os.Setenv("PORT", "")
cfg := Config{}
assert.Error(t, env.Parse(&cfg))
}
func TestParsesOptionalMissing(t *testing.T) {
os.Setenv("somevar", "somevalue")
os.Setenv("othervar", "true")
os.Setenv("PORT", "8080")
defer os.Setenv("somevar", "")
defer os.Setenv("othervar", "")
defer os.Setenv("PORT", "")
cfg := Config{}
assert.NoError(t, env.Parse(&cfg))
assert.Equal(t, "", cfg.DatabaseURL)
}
func TestParsesOptionalPresent(t *testing.T) {
os.Setenv("somevar", "somevalue")
os.Setenv("othervar", "true")
os.Setenv("PORT", "8080")
defer os.Setenv("somevar", "")
defer os.Setenv("othervar", "")
defer os.Setenv("PORT", "")
cfg := Config{}
db := "postgres://localhost:5432/db"
os.Setenv("DATABASE_URL", db)
assert.NoError(t, env.Parse(&cfg))
assert.Equal(t, db, cfg.DatabaseURL)
}
func TestParsesSensitive(t *testing.T) {
os.Setenv("somevar", "somevalue")
os.Setenv("othervar", "true")
os.Setenv("PORT", "8080")
defer os.Setenv("somevar", "")
defer os.Setenv("othervar", "")
defer os.Setenv("PORT", "")
cfg := Config{}
pw := "MrGoodbytes"
os.Setenv("PASSWORD", pw)
assert.NoError(t, env.Parse(&cfg))
assert.Equal(t, pw, cfg.Password)
assert.Equal(t, "", os.Getenv("PASSWORD"))
}
func TestClearSensitiveOnError(t *testing.T) {
os.Setenv("somevar", "somevalue")
os.Setenv("othervar", "true")
defer os.Setenv("somevar", "")
defer os.Setenv("othervar", "")
cfg := Config{}
pw := "MrGoodbytes"
os.Setenv("PASSWORD", pw)
// Should get an error, since we didn't pass in PORT
assert.Error(t, env.Parse(&cfg))
assert.Equal(t, "", os.Getenv("PASSWORD"))
}
func TestParseStructWithoutEnvTag(t *testing.T) {
os.Setenv("somevar", "somevalue")
os.Setenv("othervar", "true")
os.Setenv("PORT", "8080")
defer os.Setenv("somevar", "")
defer os.Setenv("othervar", "")
defer os.Setenv("PORT", "")
cfg := Config{}
assert.NoError(t, env.Parse(&cfg))
assert.Empty(t, cfg.NotAnEnv)
}
func TestParseStructWithInvalidFieldKind(t *testing.T) {
type config struct {
WontWork int64 `env:"BLAH"`
}
os.Setenv("BLAH", "10")
cfg := config{}
assert.Error(t, env.Parse(&cfg))
}
func ExampleParse() {
type config struct {
Home string `env:"HOME"`
Port int `env:"PORT"`
IsProduction bool `env:"PRODUCTION,optional"`
}
os.Setenv("HOME", "/tmp/fakehome")
os.Setenv("PORT", "3000")
defer os.Setenv("HOME", "")
defer os.Setenv("PORT", "")
cfg := config{}
err := env.Parse(&cfg)
if err == nil {
fmt.Println(cfg)
} else {
fmt.Println(err)
}
// Output: {/tmp/fakehome 3000 false}
}
func ExampleFail() {
type config struct {
Home string `env:"HOME"`
Port int `env:"PORT"`
IsProduction bool `env:"PRODUCTION,optional"`
}
os.Setenv("HOME", "/tmp/fakehome")
defer os.Setenv("HOME", "")
cfg := config{}
err := env.Parse(&cfg)
if err == nil {
fmt.Println(cfg)
} else {
fmt.Println(err)
}
// Output: Missing config environment variable 'PORT'
}