-
Notifications
You must be signed in to change notification settings - Fork 31
/
env_test.go
151 lines (128 loc) · 3.62 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
150
151
package godo
import (
"fmt"
"os"
"runtime"
"testing"
"gopkg.in/stretchr/testify.v1/assert"
"github.com/mgutz/str"
)
var isWindows = runtime.GOOS == "windows"
func TestEnvironment(t *testing.T) {
var user string
if isWindows {
user = os.Getenv("USERNAME")
os.Setenv("USER", user)
} else {
user = os.Getenv("USER")
}
SetEnviron("USER=$USER:godo", true)
env := EffectiveEnv(nil)
if !sliceContains(env, "USER="+user+":godo") {
t.Error("Environment interpolation failed", env)
}
SetEnviron("USER=$USER:godo", false)
env = EffectiveEnv(nil)
if len(env) != 1 {
t.Error("Disabling parent inheritance failed")
}
if !sliceContains(env, "USER="+user+":godo") {
t.Error("Should have read parent var even if not inheriting")
}
// set back to defaults
SetEnviron("", true)
l := len(os.Environ())
env = EffectiveEnv([]string{"USER=$USER:$USER:func"})
if !sliceContains(env, "USER="+user+":"+user+":func") {
t.Error("Should have been overriden by func environmnt")
}
if len(env) != l {
t.Error("Effective environment length changed")
}
env = EffectiveEnv([]string{"GOSU_NEW_VAR=foo"})
if !sliceContains(env, "GOSU_NEW_VAR=foo") {
t.Error("Should have new var")
}
if len(env) != l+1 {
t.Error("Effective environment length should have increased by 1")
}
SetEnviron(`
USER1=$USER
USER2=$USER1
`, true)
env = EffectiveEnv([]string{"USER3=$USER2"})
if !sliceContains(env, "USER1="+user) {
t.Error("Should have interpolated from parent env")
}
if !sliceContains(env, "USER3="+user) {
t.Error("Should have interpolated from effective env")
}
env = EffectiveEnv([]string{"PATH=foo::bar::bah"})
if !sliceContains(env, "PATH=foo"+string(os.PathListSeparator)+"bar"+string(os.PathListSeparator)+"bah") {
t.Error("Should have replaced PathSeparator, got", env)
}
// set back to defaults
SetEnviron("", true)
}
func TestQuotedVar(t *testing.T) {
// set back to defaults
defer SetEnviron("", true)
env := EffectiveEnv([]string{`FOO="a=bar b=bah c=baz"`})
v := getEnv(env, "FOO", false)
if v != `"a=bar b=bah c=baz"` {
t.Errorf("Quoted var failed %q", v)
}
}
func TestExpansion(t *testing.T) {
SetEnviron(`
FOO=foo
FAIL=$FOObar:godo
OK=${FOO}bar:godo
`, true)
env := EffectiveEnv([]string{})
if !sliceContains(env, "FAIL=:godo") {
t.Error("$FOObar should not have interpolated")
}
if !sliceContains(env, "OK=foobar:godo") {
t.Error("${FOO}bar should have expanded", env)
}
}
func TestInheritedRunEnv(t *testing.T) {
os.Setenv("TEST_RUN_ENV", "fubar")
SetEnviron("", true)
var output string
if isWindows {
output, _ = RunOutput(`FOO=bar BAH=baz cmd /C "echo %TEST_RUN_ENV% %FOO%"`)
} else {
output, _ = RunOutput(`FOO=bar BAH=baz bash -c "echo -n $TEST_RUN_ENV $FOO"`)
}
if str.Clean(output) != "fubar bar" {
t.Error("Environment was not inherited! Got", fmt.Sprintf("%q", output))
}
}
func TestAddToOSEnviron(t *testing.T) {
others := []string{"_foo", "_test_bar=bah", "_test_opts=a=b,c=d,*="}
assert.Equal(t, "", os.Getenv("_foo"))
assert.Equal(t, "", os.Getenv("_test_bar"))
assert.Equal(t, "", os.Getenv("_test_opts"))
addToOSEnviron(others)
assert.Equal(t, "", os.Getenv("_foo"))
assert.Equal(t, "bah", os.Getenv("_test_bar"))
assert.Equal(t, "a=b,c=d,*=", os.Getenv("_test_opts"))
}
func TestEnvFromArgs(t *testing.T) {
tasks := func(p *Project) {
p.Task("foo", nil, func(*Context) {
p.Exit(0)
})
}
argv := []string{"foo", "a=b", "c=", "d=e=f,g=*"}
godoExit(tasks, argv, func(code int) {
assert.Equal(t, "b", os.Getenv("a"))
assert.Equal(t, "", os.Getenv("c"))
assert.Equal(t, "e=f,g=*", os.Getenv("d"))
os.Setenv("a", "")
os.Setenv("c", "")
os.Setenv("d", "")
})
}