-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_test.go
149 lines (125 loc) · 3.01 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
package env
import (
"os"
"reflect"
"testing"
)
var values = map[string]string{
"APP_LOGLEVEL": "TRACE",
"APP_PORT": "12345",
"APP_ENABLED": "True",
"APP_HOMESERVER": "https://example.com",
"APP_LOGIN": "@test:example.com",
"APP_PASSWORD": "password",
"APP_SPAM_EMAILS": "[email protected] [email protected]",
"APP_SPAM_HOSTS": "spamer.com unitedspammers.org",
"APP_BAN_DURATION": "1",
"APP_BAN_SIZE": "invalid",
"APP_LIST": "test1 test2",
"APP_BOOL_TRUE": "True",
"APP_BOOL_YES": "yEs",
"APP_BOOL_TRUEUP": "TRUE",
"APP_TEST1_REDIRECT": "https://example.org",
"APP_TEST1_RATELIMIT": "1r/s",
"APP_TEST1_ROOM": "[email protected]",
"APP_TEST2_REDIRECT": "https://example.com",
"APP_TEST2_RATELIMIT": "1r/m",
"APP_TEST2_ROOM": "[email protected]",
}
func TestMain(m *testing.M) {
for key, value := range values {
os.Setenv(key, value)
}
SetPrefix("app")
exit := m.Run()
for key := range values {
os.Unsetenv(key)
}
os.Exit(exit)
}
func TestString(t *testing.T) {
tests := []struct {
name string
shortkey string
defaultValue string
expected string
}{
{"exists", "login", "", "@test:example.com"},
{"notexists_with_default", "none", "default", "default"},
{"notexists", "none", "", ""},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var actual string
if test.defaultValue == "" {
actual = String(test.shortkey)
} else {
actual = String(test.shortkey, test.defaultValue)
}
if test.expected != actual {
t.Error(test.expected, "is not", actual)
}
})
}
}
func TestInt(t *testing.T) {
tests := []struct {
name string
shortkey string
defaultValue int
expected int
}{
{"exists", "ban.duration", 0, 1},
{"notexists_with_default", "none", 5, 5},
{"invalid", "ban.size", 0, 0},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := Int(test.shortkey, test.defaultValue)
if test.expected != actual {
t.Error(test.expected, "is not", actual)
}
})
}
}
func TestBool(t *testing.T) {
tests := []struct {
name string
shortkey string
expected bool
}{
{"exists", "enabled", true},
{"notexists", "none", false},
{"invalid", "ban.size", false},
{"true", "bool.true", true},
{"trueup", "bool.trueup", true},
{"yes", "bool.yes", true},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := Bool(test.shortkey)
if test.expected != actual {
t.Error(test.expected, "is not", actual)
}
})
}
}
func TestSlice(t *testing.T) {
tests := []struct {
name string
shortkey string
expected []string
}{
{"exists", "SPAM_HOSTS", []string{"spamer.com", "unitedspammers.org"}},
{"one", "ban.size", []string{"invalid"}},
{"notexists", "none", nil},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual := Slice(test.shortkey)
if !reflect.DeepEqual(test.expected, actual) {
t.Error(test.expected, "is not", actual)
}
})
}
}