-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv-test.go
66 lines (54 loc) · 1.2 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
package testutils
import (
"os"
"strings"
)
type testEnvironment struct {
envMap map[string]string
}
func NewTestEnvironment() *testEnvironment {
te := &testEnvironment{
envMap: make(map[string]string),
}
for _,pair := range os.Environ() {
parts := strings.SplitN(pair, "=", 2)
te.envMap[parts[0]] = parts[1]
}
return te
}
func (te *testEnvironment) Getenv(key string) string {
return te.envMap[key]
}
func (te *testEnvironment) Setenv(key, val string) (err error) {
if strings.Contains(key, "=") {
err = os.ErrInvalid
return
}
te.envMap[key] = val
return
}
func (te *testEnvironment) Unsetenv(key string) (err error) {
if strings.Contains(key, "=") {
err = os.ErrInvalid
return
}
delete(te.envMap, key)
return
}
func (te *testEnvironment) Environ() (list []string) {
list = make([]string, 0, len(te.envMap))
for k,v := range te.envMap {
list = append(list, k + "=" + v)
}
return
}
func (te *testEnvironment) Clearenv() {
te.envMap = make(map[string]string)
}
func (te *testEnvironment) LookupEnv(key string) (string, bool) {
val, exists := te.envMap[key]
return val, exists
}
func (te *testEnvironment) ExpandEnv(input string) string {
return os.Expand(input, te.Getenv)
}