forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
70 lines (61 loc) · 1.55 KB
/
context_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
package gomplate
import (
"net/url"
"os"
"testing"
"github.com/hairyhenderson/gomplate/data"
"github.com/stretchr/testify/assert"
)
func TestEnvMapifiesEnvironment(t *testing.T) {
c := &context{}
env := c.Env()
assert.Equal(t, env["USER"], os.Getenv("USER"))
}
func TestEnvGetsUpdatedEnvironment(t *testing.T) {
c := &context{}
assert.Empty(t, c.Env()["FOO"])
assert.NoError(t, os.Setenv("FOO", "foo"))
assert.Equal(t, c.Env()["FOO"], "foo")
}
func TestCreateContext(t *testing.T) {
c, err := createContext(nil, nil)
assert.NoError(t, err)
assert.Empty(t, c)
fooURL := "env:///foo?type=application/yaml"
barURL := "env:///bar?type=application/yaml"
uf, _ := url.Parse(fooURL)
ub, _ := url.Parse(barURL)
d := &data.Data{
Sources: map[string]*data.Source{
"foo": {URL: uf},
".": {URL: ub},
},
}
os.Setenv("foo", "foo: bar")
defer os.Unsetenv("foo")
c, err = createContext([]string{"foo=" + fooURL}, d)
assert.NoError(t, err)
assert.IsType(t, &context{}, c)
ctx := c.(*context)
ds := ((*ctx)["foo"]).(map[string]interface{})
assert.Equal(t, "bar", ds["foo"])
os.Setenv("bar", "bar: baz")
defer os.Unsetenv("bar")
c, err = createContext([]string{".=" + barURL}, d)
assert.NoError(t, err)
assert.IsType(t, map[string]interface{}{}, c)
ds = c.(map[string]interface{})
assert.Equal(t, "baz", ds["bar"])
}
func TestParseAlias(t *testing.T) {
testdata := map[string]string{
"": "",
"foo": "foo",
"foo.bar": "foo",
"a=b": "a",
".=foo": ".",
}
for k, v := range testdata {
assert.Equal(t, v, parseAlias(k))
}
}