-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathenv_test.go
68 lines (59 loc) · 1.21 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
package starter
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestEnvdir(t *testing.T) {
dir, err := ioutil.TempDir("", "starter_test")
if err != nil {
t.Errorf("Failed to create tempdir: %s", err)
return
}
defer os.RemoveAll(dir)
files := []string{"FOO", "BAR", "BAZ"}
for _, fn := range files {
longFn := filepath.Join(dir, fn)
f, err := os.OpenFile(longFn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
t.Errorf("Failed to create file '%s': %s", fn, err)
return
}
closed := false
defer func() {
if !closed {
f.Close()
}
}()
io.WriteString(f, fn)
f.Close()
closed = true
// save old values and restore later, if any
if old := os.Getenv(fn); old != "" {
os.Setenv(fn, "")
defer os.Setenv(fn, old)
}
}
if old := os.Getenv("ENVDIR"); old != "" {
defer os.Setenv("ENVDIR", old)
}
os.Setenv("ENVDIR", dir)
m, err := reloadEnv()
if err != nil {
t.Errorf("reloadEnv failed: %s", err)
return
}
for _, fn := range files {
v, ok := m[fn]
if !ok {
t.Errorf("Expected environment variable '%s' to exist", fn)
return
}
if v != fn {
t.Errorf("Expected environment variable '%s' to be '%s'", fn, fn)
return
}
}
}