Added unit test for include in tpl function

reviewable/pr2350/r4
Lukas Eichler 7 years ago
parent ce8e8d6778
commit 439f1b31d1

Binary file not shown.

@ -171,13 +171,13 @@ func (e *Engine) alterFuncMap(t *template.Template) template.FuncMap {
}
templates := map[string]renderable{}
templates["template"] = r
templates["aaa_template"] = r
result, err := e.render(templates)
if err != nil {
return "", fmt.Errorf("Error during tpl function execution for %q: %s", tpl, err.Error())
}
return result["template"], nil
return result["aaa_template"], nil
}
return funcMap
@ -201,15 +201,10 @@ func (e *Engine) render(tpls map[string]renderable) (map[string]string, error) {
t.Option("missingkey=zero")
}
// Adding templates from the engine context but do not overwrite
for k, v := range e.CurrentTemplates {
if _, exists := tpls[k]; exists {
tpls[k] = v
}
}
funcMap := e.alterFuncMap(t)
files := []string{}
for fname, r := range tpls {
t = t.New(fname).Funcs(funcMap)
if _, err := t.Parse(r.tpl); err != nil {
@ -218,6 +213,17 @@ func (e *Engine) render(tpls map[string]renderable) (map[string]string, error) {
files = append(files, fname)
}
// Adding the engine's currentTemplates to the template context
// so they can be referenced in the tpl function
for fname, r := range e.CurrentTemplates {
if t.Lookup(fname) == nil {
t = t.New(fname).Funcs(funcMap)
if _, err := t.Parse(r.tpl); err != nil {
return map[string]string{}, fmt.Errorf("parse error in %q: %s", fname, err)
}
}
}
rendered := make(map[string]string, len(files))
var buf bytes.Buffer
for _, file := range files {

@ -503,4 +503,33 @@ func TestAlterFuncMap(t *testing.T) {
t.Errorf("Expected %q, got %q (%v)", expectTplStrWithFunction, gotStrTplWithFunction, outTplWithFunction)
}
tplChartWithInclude := &chart.Chart{
Metadata: &chart.Metadata{Name: "TplFunction"},
Templates: []*chart.Template{
{Name: "templates/base", Data: []byte(`{{ tpl "{{include ` + "`" + `TplFunction/templates/_partial` + "`" + ` . | quote }}" .}}`)},
{Name: "templates/_partial", Data: []byte(`{{.Release.Name}}`)},
},
Values: &chart.Config{Raw: ``},
Dependencies: []*chart.Chart{},
}
tplValueWithInclude := chartutil.Values{
"Values": chartutil.Values{
"value": "myvalue",
},
"Chart": tplChartWithInclude.Metadata,
"Release": chartutil.Values{
"Name": "TestRelease",
},
}
outTplWithInclude, err := New().Render(tplChartWithInclude, tplValueWithInclude)
if err != nil {
t.Fatal(err)
}
expectedTplStrWithInclude := "\"TestRelease\""
if gotStrTplWithInclude := outTplWithInclude["TplFunction/templates/base"]; gotStrTplWithInclude != expectedTplStrWithInclude {
t.Errorf("Expected %q, got %q (%v)", expectedTplStrWithInclude, gotStrTplWithInclude, outTplWithInclude)
}
}

Loading…
Cancel
Save