forked from msgongora/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
60 lines (52 loc) · 1.39 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
package gomplate
import (
"context"
"net/url"
"os"
"testing"
"github.com/hairyhenderson/gomplate/v3/data"
"github.com/stretchr/testify/assert"
)
func TestEnvMapifiesEnvironment(t *testing.T) {
c := &tmplctx{}
env := c.Env()
assert.Equal(t, env["USER"], os.Getenv("USER"))
}
func TestEnvGetsUpdatedEnvironment(t *testing.T) {
c := &tmplctx{}
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) {
ctx := context.Background()
c, err := createTmplContext(ctx, 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)
//nolint:staticcheck
d := &data.Data{
Sources: map[string]*data.Source{
"foo": {URL: uf},
".": {URL: ub},
},
}
os.Setenv("foo", "foo: bar")
defer os.Unsetenv("foo")
c, err = createTmplContext(ctx, []string{"foo"}, d)
assert.NoError(t, err)
assert.IsType(t, &tmplctx{}, c)
tctx := c.(*tmplctx)
ds := ((*tctx)["foo"]).(map[string]interface{})
assert.Equal(t, "bar", ds["foo"])
os.Setenv("bar", "bar: baz")
defer os.Unsetenv("bar")
c, err = createTmplContext(ctx, []string{"."}, d)
assert.NoError(t, err)
assert.IsType(t, map[string]interface{}{}, c)
ds = c.(map[string]interface{})
assert.Equal(t, "baz", ds["bar"])
}