-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathext_test.go
More file actions
81 lines (66 loc) · 1.6 KB
/
ext_test.go
File metadata and controls
81 lines (66 loc) · 1.6 KB
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
package assert_test
import (
"os"
"testing"
"github.com/gookit/assert"
)
func TestBuffer(t *testing.T) {
// SafeBuffer
sb := assert.NewSafeBuffer()
_, err := sb.Write([]byte("hello,"))
assert.NoErr(t, err)
err = sb.WriteByte('a')
assert.NoErr(t, err)
_, err = sb.WriteRune('b')
assert.NoErr(t, err)
s := sb.ResetGet()
assert.Eq(t, "hello,ab", s)
_, err = sb.WriteString("hello")
assert.NoErr(t, err)
// Buffer
b := assert.NewBuffer()
_, err = sb.WriteTo(b)
assert.NoErr(t, err)
assert.Eq(t, "hello", b.ResetGet())
}
func TestMockEnvValue(t *testing.T) {
is := assert.New(t)
is.Eq("", os.Getenv("APP_COMMAND"))
assert.MockEnvValue("APP_COMMAND", "new val", func(nv string) {
is.Eq("new val", nv)
})
shellVal := "custom-value"
assert.MockEnvValue("SHELL", shellVal, func(newVal string) {
is.Eq(shellVal, newVal)
})
is.Eq("", os.Getenv("APP_COMMAND"))
is.Panics(func() {
assert.MockEnvValue("invalid=", "value", nil)
})
}
func TestMockOsEnv(t *testing.T) {
is := assert.New(t)
is.Eq("", os.Getenv("APP_COMMAND"))
assert.MockOsEnv(map[string]string{
"APP_COMMAND": "new val",
}, func() {
is.Eq("new val", os.Getenv("APP_COMMAND"))
})
is.Eq("", os.Getenv("APP_COMMAND"))
}
func TestMockOsEnvByText(t *testing.T) {
envStr := `
APP_COMMAND = login
APP_ENV = dev
APP_DEBUG = true
APP_PWD=
// ENV_NOT_EXIST=comment line
`
assert.MockOsEnvByText(envStr, func() {
assert.Len(t, os.Environ(), 4)
assert.Eq(t, "true", os.Getenv("APP_DEBUG"))
assert.Eq(t, "login", os.Getenv("APP_COMMAND"))
assert.Empty(t, os.Getenv("APP_PWD"))
assert.Empty(t, os.Getenv("ENV_NOT_EXIST"))
})
}