/
env_test.go
86 lines (68 loc) · 2.63 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
package run
import (
"os"
"runtime"
"testing"
"github.com/Fiery/testify/assert"
)
var user string
func init(){
if runtime.GOOS == "windows" {
user = os.Getenv("USERNAME")
os.Setenv("USER", user)
} else {
os.Setenv("USER", "user")
user = os.Getenv("USER")
}
}
func TestEnvInitialization(t *testing.T) {
// set to os defaults
Env = makeEnvMap(os.Environ(), false)
Env.Set("USER=$USER:run")
assert.Equal(t, user+":run", (*Env)["USER"], "Environment interpretation failed")
}
func TestEnvCombination(t *testing.T){
// set to os defaults
Env = makeEnvMap(os.Environ(), false)
Env = Env.combine([]string{"USER=$USER:$USER:func"})
assert.Equal(t, user+":"+user+":func", (*Env)["USER"], "Should have been overriden by func environment")
assert.Equal(t, len(os.Environ()), len(*Env), "Consolidated environment length changed.")
Env = Env.combine([]string{"GOSU_NEW_VAR=foo"})
assert.Equal(t, "foo", (*Env)["GOSU_NEW_VAR"] , "Should have conslidated Env set")
assert.Equal(t, len(os.Environ())+1, len(*Env), "Consolidated environment length should have increased by 1")
}
func TestEnvQuote(t *testing.T) {
Env = Env.combine([]string{`FOO="a=bar b=bah c=baz"`})
if val, ok:= (*Env)["FOO"]; !ok{
t.Error("Key insertion failed", Env)
}else if val != `"a=bar b=bah c=baz"` {
t.Errorf("Quoted var failed %q", val)
}
}
func TestEnvInterpretation(t *testing.T) {
// set back to default
Env = makeEnvMap(os.Environ(), false)
Env.Set(`USER1=$USER`,`USER2=$USER1`)
Env = Env.combine([]string{"USER3=$USER2"})
assert.Equal(t, user, (*Env)["USER1"], "Should have been evaluated")
assert.Equal(t, user, (*Env)["USER3"], "Should have been evaluated during consolidation")
Env = Env.combine([]string{"PATH=foo::bar::bah"})
assert.Equal(t, "foo"+string(os.PathListSeparator)+"bar"+string(os.PathListSeparator)+"bah", (*Env)["PATH"], "Should have replaced run.PathSeparator")
// set back to defaults
Env = makeEnvMap(os.Environ(),false)
Env.Set(`FOO=foo`,`FAIL=$FOObar:run`,`PASS=${FOO}bar:run`)
assert.Equal(t, ":run", (*Env)["FAIL"], "$FOObar should have been interpreted as empty string.")
assert.Equal(t, "foobar:run", (*Env)["PASS"], "${FOO}bar should have been interpreted accordingly.")
}
func TestPromotion(t *testing.T) {
assert.Equal(t, "", os.Getenv("_foo"))
assert.Equal(t, "", os.Getenv("_test_bar"))
assert.Equal(t, "", os.Getenv("_test_opts"))
Env = makeEnvMap([]string{"_foo", "_test_bar=bah", `_test_opts="a=b,c=d,*="`}, false)
for key,_:= range *Env{
Env.promote(key)
}
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"))
}