forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_test.go
76 lines (63 loc) · 1.77 KB
/
get_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
package envutil
import (
"fmt"
"os"
"testing"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
)
const (
TestEnvName = "TEST_GOUTIL_ENV"
TestNoEnvName = "TEST_GOUTIL_NO_ENV"
TestEnvValue = "1"
defTestEnvValue = "1"
)
func TestGetenv(t *testing.T) {
testutil.MockEnvValues(map[string]string{
TestEnvName: TestEnvValue,
}, func() {
envValue := Getenv(TestEnvName)
assert.Eq(t, TestEnvValue, envValue, "env value not equals")
envValue = Getenv(TestNoEnvName, defTestEnvValue)
assert.Eq(t, defTestEnvValue, envValue, "env value not default")
assert.Eq(t, 1, GetInt(TestEnvName), "int env value not equals")
assert.Eq(t, 0, GetInt(TestNoEnvName))
assert.Eq(t, 2, GetInt(TestNoEnvName, 2))
assert.Len(t, GetMulti(TestEnvName, TestNoEnvName), 1)
})
}
func TestGetBool(t *testing.T) {
testutil.MockEnvValues(map[string]string{
TestEnvName: "on",
}, func() {
assert.True(t, GetBool(TestEnvName))
assert.False(t, GetBool(TestNoEnvName))
assert.True(t, GetBool(TestNoEnvName, true))
})
}
func TestEnviron(t *testing.T) {
assert.NotEmpty(t, EnvPaths())
assert.NotEmpty(t, EnvMap())
testutil.MockOsEnv(map[string]string{
TestEnvName: TestEnvValue,
}, func() {
envValue := Getenv("not_exist")
assert.Eq(t, "", envValue)
fmt.Println("os.Environ:", os.Environ())
fmt.Println("new Environ:", Environ())
assert.Contains(t, Environ(), TestEnvName)
})
}
func TestSearchEnvKeys(t *testing.T) {
testutil.MockOsEnv(map[string]string{
TestEnvName: TestEnvValue,
"APP_NAME": "test",
}, func() {
keys := SearchEnvKeys(TestEnvName)
assert.Contains(t, keys, TestEnvName)
keys = SearchEnvKeys("APP")
assert.Contains(t, keys, "APP_NAME")
keys = SearchEnv("test", true)
assert.Contains(t, keys, "APP_NAME")
})
}