forked from go-godo/godo
/
env_test.go
120 lines (101 loc) · 2.79 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
package godo
import (
"fmt"
"os"
"runtime"
"testing"
"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))
}
}