From 0a5173f2d5391c26bf505f6fcc22182bf7c6d2ac Mon Sep 17 00:00:00 2001 From: "Kai A. Hiller" Date: Mon, 2 Dec 2024 10:57:41 +0100 Subject: [PATCH 1/3] Make includeFun replace , too Signed-off-by: Kai A. Hiller --- pkg/engine/engine.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index df3a600a3..31422fca6 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -139,7 +139,7 @@ func includeFun(t *template.Template, includedNames map[string]int) func(string, } err := t.ExecuteTemplate(&buf, name, data) includedNames[name]-- - return buf.String(), err + return strings.ReplaceAll(buf.String(), "", ""), err } } From bb80e1143b5ae5531fbc3325fca99a47379a3871 Mon Sep 17 00:00:00 2001 From: "Kai A. Hiller" Date: Mon, 2 Dec 2024 11:58:45 +0100 Subject: [PATCH 2/3] Add unittest for nil value rendering Signed-off-by: Kai A. Hiller --- pkg/engine/engine_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index de1896a47..705ee5098 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -1300,3 +1300,34 @@ func TestRenderTplMissingKeyString(t *testing.T) { t.Fatal(err) } } + +func TestRenderNilValue(t *testing.T) { + chart := &chart.Chart{ + Metadata: &chart.Metadata{ + Name: "moby", + Version: "1.2.3", + }, + Templates: []*chart.File{ + {Name: "templates/_helpers.tpl", Data: []byte(`{{- define "noval" -}}{{ and nil }}{{- end -}}`)}, + {Name: "templates/test_include.yaml", Data: []byte(`{{ eq (include "noval" .) "" }}`)}, + {Name: "templates/test_tpl.yaml", Data: []byte(`{{ eq (tpl "{{ and nil }}" .) "" }}`)}, + {Name: "templates/test_render.yaml", Data: []byte(`{{ and nil }}`)}, + }, + } + + out, err := Render(chart, chartutil.Values{}) + if err != nil { + t.Fatalf("Failed to render templates: %s", err) + } + + expect := map[string]string{ + "moby/templates/test_include.yaml": "true", + "moby/templates/test_tpl.yaml": "true", + "moby/templates/test_render.yaml": "", + } + for name, data := range expect { + if out[name] != data { + t.Fatalf("Expected %q, got %q (%q)", data, out[name], name) + } + } +} From aaf0c0974ca677fd9b684bb2b5032985ac89533c Mon Sep 17 00:00:00 2001 From: "Kai A. Hiller" Date: Mon, 2 Dec 2024 12:18:25 +0100 Subject: [PATCH 3/3] Add negative unittests for nil value rendering Signed-off-by: Kai A. Hiller --- pkg/engine/engine_test.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index 705ee5098..7289ea515 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -1308,10 +1308,15 @@ func TestRenderNilValue(t *testing.T) { Version: "1.2.3", }, Templates: []*chart.File{ - {Name: "templates/_helpers.tpl", Data: []byte(`{{- define "noval" -}}{{ and nil }}{{- end -}}`)}, + {Name: "templates/_helpers.tpl", Data: []byte(`` + + `{{- define "noval" -}}{{ and nil }}{{- end -}}` + + `{{- define "noval_str" -}}{{- end -}}`)}, {Name: "templates/test_include.yaml", Data: []byte(`{{ eq (include "noval" .) "" }}`)}, {Name: "templates/test_tpl.yaml", Data: []byte(`{{ eq (tpl "{{ and nil }}" .) "" }}`)}, {Name: "templates/test_render.yaml", Data: []byte(`{{ and nil }}`)}, + {Name: "templates/test_include_str.yaml", Data: []byte(`{{ eq (include "noval_str" .) "" }}`)}, + {Name: "templates/test_tpl_str.yaml", Data: []byte(`{{ eq (tpl "" .) "" }}`)}, + {Name: "templates/test_render_str.yaml", Data: []byte(``)}, }, } @@ -1321,9 +1326,12 @@ func TestRenderNilValue(t *testing.T) { } expect := map[string]string{ - "moby/templates/test_include.yaml": "true", - "moby/templates/test_tpl.yaml": "true", - "moby/templates/test_render.yaml": "", + "moby/templates/test_include.yaml": "true", + "moby/templates/test_tpl.yaml": "true", + "moby/templates/test_render.yaml": "", + "moby/templates/test_include_str.yaml": "true", + "moby/templates/test_tpl_str.yaml": "true", + "moby/templates/test_render_str.yaml": "", } for name, data := range expect { if out[name] != data {