-
Notifications
You must be signed in to change notification settings - Fork 190
/
envmock_test.go
79 lines (63 loc) · 1.6 KB
/
envmock_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
package testutil_test
import (
"os"
"testing"
"github.com/gookit/goutil/internal/comfunc"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
)
func TestMockEnvValue(t *testing.T) {
is := assert.New(t)
is.Eq("", os.Getenv("APP_COMMAND"))
testutil.MockEnvValue("APP_COMMAND", "new val", func(nv string) {
is.Eq("new val", nv)
})
shellVal := "custom-value"
testutil.MockEnvValue("SHELL", shellVal, func(newVal string) {
is.Eq(shellVal, newVal)
})
is.Eq("", os.Getenv("APP_COMMAND"))
is.Panics(func() {
testutil.MockEnvValue("invalid=", "value", nil)
})
}
func TestMockEnvValues(t *testing.T) {
is := assert.New(t)
is.Eq("", os.Getenv("APP_COMMAND"))
testutil.MockEnvValues(map[string]string{
"APP_COMMAND": "new val",
}, func() {
is.Eq("new val", os.Getenv("APP_COMMAND"))
})
is.Eq("", os.Getenv("APP_COMMAND"))
}
func TestMockOsEnv(t *testing.T) {
is := assert.New(t)
is.Eq("", os.Getenv("APP_COMMAND"))
testutil.MockOsEnv(map[string]string{
"APP_COMMAND": "new val",
}, func() {
is.Eq("new val", os.Getenv("APP_COMMAND"))
})
is.Eq("", os.Getenv("APP_COMMAND"))
}
func TestClearOSEnv(t *testing.T) {
testutil.ClearOSEnv()
assert.Empty(t, os.Environ())
testutil.RevertOSEnv()
assert.NotEmpty(t, os.Environ())
}
func TestMockOsEnvByText(t *testing.T) {
envStr := `
APP_COMMAND = login
APP_ENV = dev
APP_DEBUG = true
APP_PWD=
`
testutil.MockOsEnvByText(envStr, func() {
assert.Len(t, comfunc.Environ(), 4)
assert.Eq(t, "true", os.Getenv("APP_DEBUG"))
assert.Eq(t, "login", os.Getenv("APP_COMMAND"))
assert.Eq(t, "", os.Getenv("APP_PWD"))
})
}