github.com/hairyhenderson/templater@v3.5.0+incompatible/context_test.go (about)

     1  package gomplate
     2  
     3  import (
     4  	"net/url"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hairyhenderson/gomplate/data"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestEnvMapifiesEnvironment(t *testing.T) {
    14  	c := &context{}
    15  	env := c.Env()
    16  	assert.Equal(t, env["USER"], os.Getenv("USER"))
    17  }
    18  
    19  func TestEnvGetsUpdatedEnvironment(t *testing.T) {
    20  	c := &context{}
    21  	assert.Empty(t, c.Env()["FOO"])
    22  	assert.NoError(t, os.Setenv("FOO", "foo"))
    23  	assert.Equal(t, c.Env()["FOO"], "foo")
    24  }
    25  
    26  func TestCreateContext(t *testing.T) {
    27  	c, err := createContext(nil, nil)
    28  	assert.NoError(t, err)
    29  	assert.Empty(t, c)
    30  
    31  	fooURL := "env:///foo?type=application/yaml"
    32  	barURL := "env:///bar?type=application/yaml"
    33  	uf, _ := url.Parse(fooURL)
    34  	ub, _ := url.Parse(barURL)
    35  	d := &data.Data{
    36  		Sources: map[string]*data.Source{
    37  			"foo": {URL: uf},
    38  			".":   {URL: ub},
    39  		},
    40  	}
    41  	os.Setenv("foo", "foo: bar")
    42  	defer os.Unsetenv("foo")
    43  	c, err = createContext([]string{"foo=" + fooURL}, d)
    44  	assert.NoError(t, err)
    45  	assert.IsType(t, &context{}, c)
    46  	ctx := c.(*context)
    47  	ds := ((*ctx)["foo"]).(map[string]interface{})
    48  	assert.Equal(t, "bar", ds["foo"])
    49  
    50  	os.Setenv("bar", "bar: baz")
    51  	defer os.Unsetenv("bar")
    52  	c, err = createContext([]string{".=" + barURL}, d)
    53  	assert.NoError(t, err)
    54  	assert.IsType(t, map[string]interface{}{}, c)
    55  	ds = c.(map[string]interface{})
    56  	assert.Equal(t, "baz", ds["bar"])
    57  }
    58  
    59  func TestParseAlias(t *testing.T) {
    60  	testdata := map[string]string{
    61  		"":        "",
    62  		"foo":     "foo",
    63  		"foo.bar": "foo",
    64  		"a=b":     "a",
    65  		".=foo":   ".",
    66  	}
    67  	for k, v := range testdata {
    68  		assert.Equal(t, v, parseAlias(k))
    69  	}
    70  }