-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmura_test.go
166 lines (126 loc) · 2.96 KB
/
mura_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
package mura
import (
"encoding/json"
"fmt"
"os"
"testing"
)
func print(i interface{}) {
s, _ := json.MarshalIndent(i, "", "\t")
fmt.Println("============= DEBUG =============")
fmt.Printf("%s \n", s)
fmt.Println("=================================")
}
func TestMura(t *testing.T) {
os.Setenv("SERVER_HOST", "localhost")
os.Setenv("SERVER_PORT", "8080")
os.Setenv("SERVER_PRODUCTION", "true")
os.Setenv("PI_CONST", "3.14")
defer func() {
os.Unsetenv("SERVER_HOST")
os.Unsetenv("SERVER_PORT")
os.Unsetenv("SERVER_PRODUCTION")
os.Unsetenv("PI_CONST")
}()
t.Run("success", func(t *testing.T) {
type TestENV struct {
ServerHost string `env:"SERVER_HOST"`
ServerPort int `env:"SERVER_PORT"`
ServerProduction bool `env:"SERVER_PRODUCTION"`
PiConts float64 `env:"PI_CONST"`
}
env := new(TestENV)
err := Unmarshal(env)
print(env)
if err != nil {
t.Error(err)
t.Fail()
}
})
t.Run("success-default", func(t *testing.T) {
type TestENV struct {
ServerHost string `env:"SERVER_HOST"`
DBHost string `env:"DB_HOST" default:"localhost"`
}
env := new(TestENV)
err := Unmarshal(env)
print(env)
if err != nil {
t.Error(err)
t.Fail()
}
})
t.Run("no-env-no-default", func(t *testing.T) {
type TestENV struct {
ServerHost string `env:"SERVER_HOST"`
DBHost string `env:"DB_HOST" default:"localhost"`
}
env := new(TestENV)
err := Unmarshal(env)
print(env)
if err != nil {
t.Error(err)
t.Fail()
}
})
t.Run("error-conversion-type", func(t *testing.T) {
os.Setenv("DB_PORT", "3.14")
type TestENV struct {
ServerHost string `env:"SERVER_HOST"`
DBHost string `env:"DB_HOST" default:"localhost"`
DBSSL bool `default:"it should be boolean"`
PiConts float64 `default:"it should be float"`
DBPort int `env:"DB_PORT"`
}
env := new(TestENV)
err := Unmarshal(env)
print(env)
if err != nil {
t.Error(err)
t.Fail()
}
})
t.Run("error-interface-non-pointer", func(t *testing.T) {
type TestENV struct{}
env := TestENV{}
err := Unmarshal(env)
if err == nil {
t.Error("Expectec error")
t.Fail()
}
})
t.Run("error-unsuported-type", func(t *testing.T) {
type CustomType struct{}
type TestENV struct {
Test CustomType `default:"custom type"`
}
env := new(TestENV)
err := Unmarshal(env)
if err != nil {
t.Error("Expectec error")
t.Fail()
}
})
}
func TestMuraWithEnvFile(t *testing.T) {
t.Run("success", func(t *testing.T) {
defer func() {
os.Remove(".env")
}()
os.WriteFile(".env", []byte("SERVER_PORT=123\nSERVER_PRODUCTION=123"), 0600)
SetENVPath(".env")
type TestENV struct {
ServerHost string `env:"SERVER_HOST"`
ServerPort int `env:"SERVER_PORT"`
ServerProduction bool `env:"SERVER_PRODUCTION"`
PiConts float64 `env:"PI_CONST"`
}
env := new(TestENV)
err := Unmarshal(env)
if err != nil {
t.Error(err)
t.Fail()
}
print(env)
})
}