From 2521c526d9e40d8a8fee7c4883ac0309026bedb9 Mon Sep 17 00:00:00 2001 From: Lukas Eichler Date: Tue, 2 May 2017 09:41:58 +0200 Subject: [PATCH] - 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 --- pkg/engine/engine.go | 6 +++--- pkg/engine/engine_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index f7fcf632d..ed969aeab 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -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 diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index 461d8a8d5..0c65cdbf9 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -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) + } + }