- Changed error behaviour to returning an error instead of writing the error in the template

- Added tests for using a function inside a template that is evaluated using the "tpl" function
reviewable/pr2350/r2
Lukas Eichler 7 years ago
parent d01f7978d2
commit 2521c526d9

@ -162,7 +162,7 @@ func (e *Engine) alterFuncMap(t *template.Template) template.FuncMap {
}
// Add the 'tpl' function here
funcMap["tpl"] = func(tpl string, vals chartutil.Values) string {
funcMap["tpl"] = func(tpl string, vals chartutil.Values) (string, error) {
r := renderable{
tpl: tpl,
vals: vals,
@ -173,9 +173,9 @@ func (e *Engine) alterFuncMap(t *template.Template) template.FuncMap {
result, err := e.render(templates)
if err != nil {
return fmt.Errorf("Error during tpl function execution for %q", tpl).Error()
return "", fmt.Errorf("Error during tpl function execution for %q: %s", tpl, err.Error())
}
return result["template"]
return result["template"], nil
}
return funcMap

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

Loading…
Cancel
Save