-
Notifications
You must be signed in to change notification settings - Fork 0
/
envs_test.go
175 lines (159 loc) · 5.25 KB
/
envs_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
package envs
import (
"os"
"testing"
)
func Test_Get(t *testing.T) {
envs := EnvConfig{}
_ = os.Setenv("TEST_GET_ENVS", "get_envs")
envs.ReadEnvs()
if res := envs.Get("TEST_GET_ENVS"); res != "get_envs" {
t.Errorf("expected 'get_envs', got '%s'", res)
}
if res := envs.Get("TEST_GET_ENVS_DEFAULT", "default_string"); res != "default_string" {
t.Errorf("expected 'default_string', got '%s'", res)
}
}
func Test_GetBool(t *testing.T) {
envs := EnvConfig{}
_ = os.Setenv("TEST_GET_ENVS_BOOL", "true")
envs.ReadEnvs()
if res := envs.GetBool("TEST_GET_ENVS_BOOL"); !res {
t.Errorf("expected true, got %t", res)
}
if res := envs.GetBool("TEST_GET_ENVS_BOOL_DEFAULT", true); !res {
t.Errorf("expected true, got %t", res)
}
}
func Test_GetFloat(t *testing.T) {
envs := EnvConfig{}
_ = os.Setenv("TEST_GET_ENVS_FLOAT", "123.7645")
envs.ReadEnvs()
if res := envs.GetFloat("TEST_GET_ENVS_FLOAT"); res != float32(123.7645) {
t.Errorf("expected 123.7645, got %f", res)
}
if res := envs.GetFloat("TEST_GET_ENVS_FLOAT_DEFAULT", 456.123); res != float32(456.123) {
t.Errorf("expected 456.123, got %f", res)
}
}
func Test_GetInt(t *testing.T) {
envs := EnvConfig{}
_ = os.Setenv("TEST_GET_ENVS_INT", "123")
envs.ReadEnvs()
if res := envs.GetInt("TEST_GET_ENVS_INT"); res != 123 {
t.Errorf("expected 123, got %d", res)
}
if res := envs.GetInt("TEST_GET_ENVS_INT_DEFAULT", 876); res != 876 {
t.Errorf("expected 876, got %d", res)
}
}
func Test_GetMap(t *testing.T) {
envs := EnvConfig{}
_ = os.Setenv("TEST_GET_ENVS_MAP", "key1:value1;key2:value2")
envs.ReadEnvs()
res := envs.GetMap("TEST_GET_ENVS_MAP")
if value := res["key1"]; value != "value1" {
t.Errorf("expected 'value1', got '%s'", value)
}
if value := res["key2"]; value != "value2" {
t.Errorf("expected 'value2', got '%s'", value)
}
res = envs.GetMap("TEST_GET_ENVS_MAP_DEFAULT", map[string]string{"defaultKey": "defaultValue"})
if value := res["defaultKey"]; value != "defaultValue" {
t.Errorf("expected 'defaultValue', got '%s'", value)
}
}
func Test_GetSlice(t *testing.T) {
envs := EnvConfig{}
_ = os.Setenv("TEST_GET_ENVS_SLICE", "first,second,third")
envs.ReadEnvs()
expected := []string{"first", "second", "third"}
for i, value := range envs.GetSlice("TEST_GET_ENVS_SLICE") {
if value != expected[i] {
t.Errorf("expected '%s', got '%s'", expected[i], value)
}
}
if res := envs.GetSlice("TEST_GET_ENVS_SLICE_DEFAULT", []string{"defaultValue"}); res[0] != "defaultValue" {
t.Errorf("expected 'defaultValue', got '%s'", res[0])
}
}
func Test_GetSliceFloat(t *testing.T) {
envs := EnvConfig{}
_ = os.Setenv("TEST_GET_ENVS_SLICE_FLOAT", "123.554,64.23,78.09876")
envs.ReadEnvs()
expected := []float32{123.554, 64.23, 78.09876}
for i, value := range envs.GetSliceFloat("TEST_GET_ENVS_SLICE_FLOAT") {
if value != expected[i] {
t.Errorf("expected %f, got %f", expected[i], value)
}
}
res := envs.GetSliceFloat("TEST_GET_ENVS_SLICE_FLOAT_DEFAULT", []float32{456.235})
if res[0] != float32(456.235) {
t.Errorf("expected 456.235, got %f", res[0])
}
}
func Test_GetSliceInt(t *testing.T) {
envs := EnvConfig{}
_ = os.Setenv("TEST_GET_ENVS_SLICE_INT", "453,8,124567")
envs.ReadEnvs()
expected := []int{453, 8, 124567}
for i, value := range envs.GetSliceInt("TEST_GET_ENVS_SLICE_INT") {
if value != expected[i] {
t.Errorf("expected %d, got %d", expected[i], value)
}
}
if res := envs.GetSliceInt("TEST_GET_ENVS_SLICE_INT_DEFAULT", []int{3576}); res[0] != 3576 {
t.Errorf("expected 3576, got %d", res[0])
}
}
func Test_readEnvs(t *testing.T) {
envs := EnvConfig{}
{ // the .env file exists - envs are empty
_ = os.WriteFile(".env", []byte{}, 0644)
envs.ReadEnvs()
if res := envs.Get("TEST_READ_ENVS_1"); res != "" {
t.Errorf("expected empty value, got %s", res)
}
_ = os.Remove(".env")
}
{ // the .env file exists - envs are read
_ = os.WriteFile(".env", []byte("# Comment\nTEST_READ_ENVS_2=read_envs_2"), 0644)
envs.ReadEnvs()
if res := envs.Get("TEST_READ_ENVS_2"); res != "read_envs_2" {
t.Errorf("expected 'read_envs_2', got %s", res)
}
_ = os.Remove(".env")
}
{ // a file with custom name exists - envs are read
_ = os.WriteFile(".custom_name", []byte("TEST_READ_ENVS_3=read_envs_3"), 0644)
envs.Filepath = ".custom_name"
envs.ReadEnvs()
if res := envs.Get("TEST_READ_ENVS_3"); res != "read_envs_3" {
t.Errorf("expected 'read_envs_3', got %s", res)
}
_ = os.Remove(".custom_name")
envs.Filepath = ""
}
{ // the .env file exists and the same env var was set - envs are read
_ = os.Setenv("TEST_READ_ENVS_4", "read_envs_4_from_env?somePara=value")
_ = os.WriteFile(".env", []byte("TEST_READ_ENVS_4=read_envs_4"), 0644)
envs.ReadEnvs()
if res := envs.Get("TEST_READ_ENVS_4"); res != "read_envs_4_from_env?somePara=value" {
t.Errorf("expected 'read_envs_4_from_env?somePara=value', got %s", res)
}
_ = os.Remove(".env")
}
{ // the .env file doesn't exists - envs are empty
envs.ReadEnvs()
if res := envs.Get("TEST_READ_ENVS_5"); res != "" {
t.Errorf("expected empty value, got %s", res)
}
}
{ // the .env file doesn't exists - envs are read
_ = os.Setenv("TEST_READ_ENVS_6", "read_envs_6")
envs.ReadEnvs()
if res := envs.Get("TEST_READ_ENVS_6"); res != "read_envs_6" {
t.Errorf("expected 'read_envs_6', got %s", res)
}
}
}