diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2018-11-12 23:22:06 -0500 |
|---|---|---|
| committer | Dave Henderson <dhenderson@gmail.com> | 2018-11-15 13:40:17 -0500 |
| commit | 6056ca97d889fb268cc287334bc644e6b6d487d7 (patch) | |
| tree | 70c877188224219bb94d6b1c98894e53daf02497 /context_test.go | |
| parent | 79b79c1db7652ba763f0186360e92da7f4b30fbd (diff) | |
New --context flag for adding datasources to context
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'context_test.go')
| -rw-r--r-- | context_test.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/context_test.go b/context_test.go index 16492e9a..f2780356 100644 --- a/context_test.go +++ b/context_test.go @@ -1,9 +1,12 @@ package gomplate import ( + "net/url" "os" "testing" + "github.com/hairyhenderson/gomplate/data" + "github.com/stretchr/testify/assert" ) @@ -19,3 +22,49 @@ func TestEnvGetsUpdatedEnvironment(t *testing.T) { 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)) + } +} |
