forked from docker/libcompose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvfile_test.go
57 lines (50 loc) · 1.38 KB
/
envfile_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
package lookup
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestEnvfileLookupReturnsEmptyIfError(t *testing.T) {
envfileLookup := &EnvfileLookup{
Path: "anything/file.env",
}
actuals := envfileLookup.Lookup("any", nil)
if len(actuals) != 0 {
t.Fatalf("expected an empty slice, got %v", actuals)
}
}
func TestEnvfileLookupWithGoodFile(t *testing.T) {
content := `foo=bar
baz=quux
# comment
_foobar=foobaz
with.dots=working
and_underscore=working too
`
tmpFolder, err := ioutil.TempDir("", "test-envfile")
if err != nil {
t.Fatal(err)
}
envfile := filepath.Join(tmpFolder, ".env")
if err := ioutil.WriteFile(envfile, []byte(content), 0700); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpFolder)
envfileLookup := &EnvfileLookup{
Path: envfile,
}
validateLookup(t, "baz=quux", envfileLookup.Lookup("baz", nil))
validateLookup(t, "foo=bar", envfileLookup.Lookup("foo", nil))
validateLookup(t, "_foobar=foobaz", envfileLookup.Lookup("_foobar", nil))
validateLookup(t, "with.dots=working", envfileLookup.Lookup("with.dots", nil))
validateLookup(t, "and_underscore=working too", envfileLookup.Lookup("and_underscore", nil))
}
func validateLookup(t *testing.T, expected string, actuals []string) {
if len(actuals) != 1 {
t.Fatalf("expected 1 result, got %v", actuals)
}
if actuals[0] != expected {
t.Fatalf("expected %s, got %s", expected, actuals[0])
}
}