-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathenv_test.go
83 lines (67 loc) · 1.5 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
package procs_test
import (
"fmt"
"os"
"os/exec"
"strings"
"testing"
"github.com/ionrock/procs"
)
func TestParseEnv(t *testing.T) {
env := []string{
"FOO=bar",
"BAZ=`echo 'hello=world'`",
}
m := procs.ParseEnv(env)
v, ok := m["FOO"]
if !ok {
t.Errorf("error missing FOO from env: %#v", m)
}
if v != "bar" {
t.Errorf("error FOO != bar: %s", v)
}
v, ok = m["BAZ"]
if !ok {
t.Errorf("error missing BAZ from env: %#v", m)
}
expectBaz := "`echo 'hello=world'`"
if v != expectBaz {
t.Errorf("error BAZ != %s: %s", expectBaz, v)
}
}
func TestEnvBuilder(t *testing.T) {
env := procs.Env(map[string]string{
"FOO": "bar",
"BAZ": "hello world",
}, false)
if len(env) != 2 {
t.Errorf("error loading env: %s", env)
}
}
func helperEnvCommand(env map[string]string) *exec.Cmd {
cmd := exec.Command(os.Args[0], "-test.run=TestEnvBuilderOverrides")
cmd.Env = procs.Env(env, false)
return cmd
}
func TestEnvBuilderOverrides(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
}
for _, envvar := range procs.Env(map[string]string{"FOO": "override"}, true) {
fmt.Println(envvar)
}
}
func TestEnvBuilderWithEnv(t *testing.T) {
cmd := helperEnvCommand(map[string]string{
"GO_WANT_HELPER_PROCESS": "1",
"FOO": "default",
})
out, err := cmd.Output()
if err != nil {
t.Fatalf("error running helper: %s", err)
}
env := procs.ParseEnv(strings.Split(string(out), "\n"))
if env["FOO"] != "override" {
t.Errorf("error overriding envvar: %s", string(out))
}
}