From f9a0e425c29abc857ebe5568138654020518a86d Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 24 Jul 2023 00:42:59 +0200 Subject: [PATCH 1/3] Add raw helper function for templates --- libs/template/helpers.go | 3 +++ libs/template/helpers_test.go | 25 +++++++++++++++++++ .../testdata/raw-function/template/hello.tmpl | 1 + 3 files changed, 29 insertions(+) create mode 100644 libs/template/helpers_test.go create mode 100644 libs/template/testdata/raw-function/template/hello.tmpl diff --git a/libs/template/helpers.go b/libs/template/helpers.go index 271fd539bc4..a3afc7107c0 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -17,4 +17,7 @@ var helperFuncs = template.FuncMap{ "fail": func(format string, args ...any) (any, error) { return nil, ErrFail{fmt.Sprintf(format, args...)} }, + "raw": func(s string) string { + return s + }, } diff --git a/libs/template/helpers_test.go b/libs/template/helpers_test.go new file mode 100644 index 00000000000..26c5a2557ad --- /dev/null +++ b/libs/template/helpers_test.go @@ -0,0 +1,25 @@ +package template + +import ( + "context" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestTemplateRawFunction(t *testing.T) { + ctx := context.Background() + tmpDir := t.TempDir() + + r, err := newRenderer(ctx, nil, "./testdata/raw-function/template", "./testdata/raw-function/library", tmpDir) + require.NoError(t, err) + + err = r.walk() + assert.NoError(t, err) + + assert.Len(t, r.files, 1) + cleanContent := strings.Trim(string(r.files[0].content), "\n\r") + assert.Equal(t, `{{ fail "abc" }}`, cleanContent) +} diff --git a/libs/template/testdata/raw-function/template/hello.tmpl b/libs/template/testdata/raw-function/template/hello.tmpl new file mode 100644 index 00000000000..510e0be6191 --- /dev/null +++ b/libs/template/testdata/raw-function/template/hello.tmpl @@ -0,0 +1 @@ +{{raw `{{ fail "abc" }}`}} From bb37f60cf299c24d4b1ce02a53c912b5582dfb79 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 24 Jul 2023 00:44:36 +0200 Subject: [PATCH 2/3] added comment --- libs/template/helpers.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libs/template/helpers.go b/libs/template/helpers.go index a3afc7107c0..0a8d1f0a711 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -17,6 +17,8 @@ var helperFuncs = template.FuncMap{ "fail": func(format string, args ...any) (any, error) { return nil, ErrFail{fmt.Sprintf(format, args...)} }, + // Returns the input string as is. Useful for printing text that would otherwise + // get interpreted as a template. "raw": func(s string) string { return s }, From fe35987d56094130e131ae96b4a66b28987bb88c Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 25 Jul 2023 15:17:51 +0200 Subject: [PATCH 3/3] remove raw function --- libs/template/helpers.go | 5 ----- libs/template/helpers_test.go | 4 ++-- .../testdata/print-without-processing/template/hello.tmpl | 1 + libs/template/testdata/raw-function/template/hello.tmpl | 1 - 4 files changed, 3 insertions(+), 8 deletions(-) create mode 100644 libs/template/testdata/print-without-processing/template/hello.tmpl delete mode 100644 libs/template/testdata/raw-function/template/hello.tmpl diff --git a/libs/template/helpers.go b/libs/template/helpers.go index 0a8d1f0a711..271fd539bc4 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -17,9 +17,4 @@ var helperFuncs = template.FuncMap{ "fail": func(format string, args ...any) (any, error) { return nil, ErrFail{fmt.Sprintf(format, args...)} }, - // Returns the input string as is. Useful for printing text that would otherwise - // get interpreted as a template. - "raw": func(s string) string { - return s - }, } diff --git a/libs/template/helpers_test.go b/libs/template/helpers_test.go index 26c5a2557ad..62c2419c863 100644 --- a/libs/template/helpers_test.go +++ b/libs/template/helpers_test.go @@ -9,11 +9,11 @@ import ( "github.com/stretchr/testify/require" ) -func TestTemplateRawFunction(t *testing.T) { +func TestTemplatePrintStringWithoutProcessing(t *testing.T) { ctx := context.Background() tmpDir := t.TempDir() - r, err := newRenderer(ctx, nil, "./testdata/raw-function/template", "./testdata/raw-function/library", tmpDir) + r, err := newRenderer(ctx, nil, "./testdata/print-without-processing/template", "./testdata/print-without-processing/library", tmpDir) require.NoError(t, err) err = r.walk() diff --git a/libs/template/testdata/print-without-processing/template/hello.tmpl b/libs/template/testdata/print-without-processing/template/hello.tmpl new file mode 100644 index 00000000000..735d02099a4 --- /dev/null +++ b/libs/template/testdata/print-without-processing/template/hello.tmpl @@ -0,0 +1 @@ +{{`{{ fail "abc" }}`}} diff --git a/libs/template/testdata/raw-function/template/hello.tmpl b/libs/template/testdata/raw-function/template/hello.tmpl deleted file mode 100644 index 510e0be6191..00000000000 --- a/libs/template/testdata/raw-function/template/hello.tmpl +++ /dev/null @@ -1 +0,0 @@ -{{raw `{{ fail "abc" }}`}}