Debian Codegraph @

env_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package godo
2
3 import (
4 "fmt"
5 "os"
6 "runtime"
7 "testing"
8
•9 "github.com/stretchr/testify/assert"
10
11 "github.com/mgutz/str"
12 )
13
14 var isWindows = runtime.GOOS == "windows"
15
16 func TestEnvironment(t *testing.T) {
17 var user string
18 if isWindows {
19 user = os.Getenv("USERNAME")
20 os.Setenv("USER", user)
21 } else {
22 user = os.Getenv("USER")
23 }
24
25 SetEnviron("USER=$USER:godo", true)
26 env := EffectiveEnv(nil)
27 if !sliceContains(env, "USER="+user+":godo") {
28 t.Error("Environment interpolation failed", env)
29 }
30
31 SetEnviron("USER=$USER:godo", false)
32 env = EffectiveEnv(nil)
33 if len(env) != 1 {
34 t.Error("Disabling parent inheritance failed")
35 }
36 if !sliceContains(env, "USER="+user+":godo") {
37 t.Error("Should have read parent var even if not inheriting")
38 }
39
40 // set back to defaults
41 SetEnviron("", true)
42 l := len(os.Environ())
43 env = EffectiveEnv([]string{"USER=$USER:$USER:func"})
44 if !sliceContains(env, "USER="+user+":"+user+":func") {
45 t.Error("Should have been overriden by func environmnt")
46 }
47 if len(env) != l {
48 t.Error("Effective environment length changed")
49 }
50
51 env = EffectiveEnv([]string{"GOSU_NEW_VAR=foo"})
52 if !sliceContains(env, "GOSU_NEW_VAR=foo") {
53 t.Error("Should have new var")
54 }
55 if len(env) != l+1 {
56 t.Error("Effective environment length should have increased by 1")
57 }
58
59 SetEnviron(`
60 USER1=$USER
61 USER2=$USER1
62 `, true)
63 env = EffectiveEnv([]string{"USER3=$USER2"})
64 if !sliceContains(env, "USER1="+user) {
65 t.Error("Should have interpolated from parent env")
66 }
67 if !sliceContains(env, "USER3="+user) {
68 t.Error("Should have interpolated from effective env")
69 }
70
71 env = EffectiveEnv([]string{"PATH=foo::bar::bah"})
72 if !sliceContains(env, "PATH=foo"+string(os.PathListSeparator)+"bar"+string(os.PathListSeparator)+"bah") {
73 t.Error("Should have replaced PathSeparator, got", env)
74 }
75
76 // set back to defaults
77 SetEnviron("", true)
78 }
79
80 func TestQuotedVar(t *testing.T) {
81 // set back to defaults
82 defer SetEnviron("", true)
83 env := EffectiveEnv([]string{`FOO="a=bar b=bah c=baz"`})
84 v := getEnv(env, "FOO", false)
85 if v != `"a=bar b=bah c=baz"` {
86 t.Errorf("Quoted var failed %q", v)
87 }
88 }
89
90 func TestExpansion(t *testing.T) {
91 SetEnviron(`
92 FOO=foo
93 FAIL=$FOObar:godo
94 OK=${FOO}bar:godo
95 `, true)
96
97 env := EffectiveEnv([]string{})
98 if !sliceContains(env, "FAIL=:godo") {
99 t.Error("$FOObar should not have interpolated")
100 }
101 if !sliceContains(env, "OK=foobar:godo") {
102 t.Error("${FOO}bar should have expanded", env)
103 }
104 }
105
106 func TestInheritedRunEnv(t *testing.T) {
107 os.Setenv("TEST_RUN_ENV", "fubar")
108 SetEnviron("", true)
109
110 var output string
111
112 if isWindows {
113 output, _ = RunOutput(`FOO=bar BAH=baz cmd /C "echo %TEST_RUN_ENV% %FOO%"`)
114 } else {
115 output, _ = RunOutput(`FOO=bar BAH=baz bash -c "echo -n $TEST_RUN_ENV $FOO"`)
116 }
117
118 if str.Clean(output) != "fubar bar" {
119 t.Error("Environment was not inherited! Got", fmt.Sprintf("%q", output))
120 }
121 }
122
123 func TestAddToOSEnviron(t *testing.T) {
124 others := []string{"_foo", "_test_bar=bah", "_test_opts=a=b,c=d,*="}
125 assert.Equal(t, "", os.Getenv("_foo"))
126 assert.Equal(t, "", os.Getenv("_test_bar"))
127 assert.Equal(t, "", os.Getenv("_test_opts"))
128 addToOSEnviron(others)
129 assert.Equal(t, "", os.Getenv("_foo"))
130 assert.Equal(t, "bah", os.Getenv("_test_bar"))
131 assert.Equal(t, "a=b,c=d,*=", os.Getenv("_test_opts"))
132 }
133
134 func TestEnvFromArgs(t *testing.T) {
135 tasks := func(p *Project) {
136 p.Task("foo", nil, func(*Context) {
137 p.Exit(0)
138 })
139 }
140
141 argv := []string{"foo", "a=b", "c=", "d=e=f,g=*"}
142 godoExit(tasks, argv, func(code int) {
143 assert.Equal(t, "b", os.Getenv("a"))
144 assert.Equal(t, "", os.Getenv("c"))
145 assert.Equal(t, "e=f,g=*", os.Getenv("d"))
146
147 os.Setenv("a", "")
148 os.Setenv("c", "")
149 os.Setenv("d", "")
150 })
151 }