github.com/hairyhenderson/gomplate/v3@v3.11.7/context_test.go (about)

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