-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdotenv_test.go
More file actions
224 lines (189 loc) · 4.93 KB
/
dotenv_test.go
File metadata and controls
224 lines (189 loc) · 4.93 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
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
package dotenv_test
import (
"encoding"
"log"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/profclems/go-dotenv"
)
func testReadEnvAndCompare(t *testing.T, envFileName string, expectedValues map[string]string) {
dotenv := dotenv.New()
dotenv.SetConfigFile(envFileName)
err := dotenv.Load()
if err != nil {
t.Error("Error loading config", err)
}
for key, value := range expectedValues {
assert.Equal(t, value, dotenv.GetString(key))
}
}
func TestReadPlainEnv(t *testing.T) {
envFileName := "fixtures/plain.env"
expectedValues := map[string]string{
"OPTION_A": "1",
"OPTION_B": "2",
"OPTION_C": "3",
"OPTION_D": "4",
"OPTION_E": "5",
"OPTION_F": "",
"OPTION_G": "",
"OPTION_H": "my string",
}
testReadEnvAndCompare(t, envFileName, expectedValues)
}
func TestLoadUnquotedEnv(t *testing.T) {
envFileName := "fixtures/unquoted.env"
expectedValues := map[string]string{
"OPTION_A": "some quoted phrase",
"OPTION_B": "first one with an unquoted phrase",
"OPTION_C": "then another one with an unquoted phrase",
"OPTION_D": "then another one with an unquoted phrase special è char",
"OPTION_E": "then another one quoted phrase",
}
testReadEnvAndCompare(t, envFileName, expectedValues)
}
func TestLoadQuotedEnv(t *testing.T) {
//t.Skip()
envFileName := "fixtures/quoted.env"
expectedValues := map[string]string{
"OPTION_A": "1",
"OPTION_B": "2",
"OPTION_C": "",
"OPTION_D": "\\n",
"OPTION_E": "1",
"OPTION_F": "2",
"OPTION_G": "",
"OPTION_H": "\n",
"OPTION_I": "echo 'asd'",
"OPTION_J": `first line
second line
third line
and so on`,
"OPTION_K": "Test#123",
"OPTION_Z": "last value",
}
testReadEnvAndCompare(t, envFileName, expectedValues)
}
func TestLoadExportedEnv(t *testing.T) {
envFileName := "fixtures/exported.env"
expectedValues := map[string]string{
"OPTION_A": "2",
"OPTION_B": "\\n",
}
dotenv := dotenv.New()
dotenv.SetConfigFile(envFileName)
err := dotenv.Load()
if err != nil {
t.Fatal(err)
}
for key, value := range expectedValues {
if dotenv.Get(key) != value {
t.Errorf("Expected: %q got %q", value, dotenv.Get(key))
}
}
}
func TestErrorParsing(t *testing.T) {
envFileName := "fixtures/invalid.env"
dotenv := dotenv.New()
dotenv.SetConfigFile(envFileName)
err := dotenv.Load()
assert.ErrorContains(t, err, "line 7: key cannot contain spaces")
}
func TestUnMarshal(t *testing.T) {
type DB struct {
Host string `env:"DB_HOST" default:"localhost"`
Port int `env:"DB_PORT"`
User string `env:"DB_USERNAME"`
Password string `env:"DB_PASSWORD"`
Database string `env:"DB_DATABASE"`
Driver string `env:"DB_DRIVER"`
}
type Log struct {
Level string `env:"LOG_LEVEL" default:"info"`
Channel string `env:"LOG_CHANNEL" default:"stdout"`
Path string `env:"LOG_PATH" default:"/var/log/app.log"`
}
type Config struct {
APIEndpoint string `env:"API_ENDPOINT" default:"http://localhost:8080"`
AuthEndpoint string `env:"AUTH_ENDPOINT" default:"http://localhost:8080"`
DoesNotExit string `env:"DOES_NOT_EXIT" default:"default"`
SomeDuration time.Duration `env:"SOME_DURATION" default:"1s"`
DB DB
Log Log
}
expectedConfig := Config{
APIEndpoint: "http://localhost:8000/api",
AuthEndpoint: "http://localhost:8000/auth",
DoesNotExit: "default",
SomeDuration: time.Second,
DB: DB{
Host: "localhost",
Port: 3306,
User: "root",
Password: "my-secret-pw",
Database: "app",
Driver: "mysql",
},
Log: Log{
Level: "debug",
Channel: "stack",
Path: "storage/logs/app.log",
},
}
config := Config{}
dotenv := dotenv.New()
dotenv.SetConfigFile("fixtures/test.env")
err := dotenv.Load()
if err != nil {
t.Fatal(err)
}
dotenv.SetPrefix("APP")
err = dotenv.Unmarshal(&config)
if err != nil {
t.Fatal(err)
}
require.Equal(t, expectedConfig, config)
}
type customDuration struct {
value time.Duration
}
// check that it implements encoding.TextUnmarshaler
var _ encoding.TextUnmarshaler = (*customDuration)(nil)
func (c *customDuration) UnmarshalText(text []byte) error {
d, err := time.ParseDuration(string(text))
if err != nil {
return err
}
log.Println(d)
c.value = d
return nil
}
func TestUnMarshal_fieldWithTextUnmarshaler(t *testing.T) {
type config struct {
Interval customDuration `env:"INTERVAL" default:"15m"`
}
expectedConfig := config{
Interval: customDuration{value: 15 * time.Minute},
}
cfg := config{}
dotenv := dotenv.New()
err := dotenv.Unmarshal(&cfg)
require.NoError(t, err)
require.Equal(t, expectedConfig, cfg)
}
func TestGet_NoConfigFile(t *testing.T) {
env := dotenv.New()
val := env.GetString("SOME_KEY")
assert.Equal(t, "", val)
// set os env
err := os.Setenv("SOME_KEY", "some value")
require.NoError(t, err)
val = env.GetString("SOME_KEY")
assert.Equal(t, "some value", val)
// use global instance
val = dotenv.GetString("SOME_KEY")
assert.Equal(t, "some value", val)
}