From 7c72e2ba3e2a2d8474d5d38c6f175dd3e5690a61 Mon Sep 17 00:00:00 2001 From: yxxhero <11087727+yxxhero@users.noreply.github.com> Date: Fri, 14 Apr 2023 12:03:26 +0800 Subject: [PATCH] fix: ValuesTemplate quote issue (#787) * fix: ValuesTemplate quote issue Signed-off-by: yxxhero * optimize goccy yaml options Signed-off-by: yxxhero * add more options Signed-off-by: yxxhero * fix tests Signed-off-by: yxxhero * fix tests Signed-off-by: yxxhero --------- Signed-off-by: yxxhero --- pkg/state/release_test.go | 45 +++++++++++++++++++++++++++++++++++++++ pkg/yaml/yaml.go | 2 ++ pkg/yaml/yaml_test.go | 16 +++++++++++--- 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 pkg/state/release_test.go diff --git a/pkg/state/release_test.go b/pkg/state/release_test.go new file mode 100644 index 00000000..c209144d --- /dev/null +++ b/pkg/state/release_test.go @@ -0,0 +1,45 @@ +package state + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/helmfile/helmfile/pkg/filesystem" + "github.com/helmfile/helmfile/pkg/runtime" + "github.com/helmfile/helmfile/pkg/tmpl" +) + +func TestExecuteTemplateExpressions(t *testing.T) { + render := tmpl.NewFileRenderer(filesystem.DefaultFileSystem(), "", map[string]interface{}{ + "Values": map[string]interface{}{ + "foo": map[string]interface{}{ + "releaseName": "foo", + }, + }, + "Release": map[string]interface{}{ + "Name": "foo", + }, + }) + + v := runtime.GoccyGoYaml + runtime.GoccyGoYaml = true + t.Cleanup(func() { + runtime.GoccyGoYaml = v + }) + + rs := ReleaseSpec{ + Name: "foo", + Chart: "bar", + Namespace: "baz", + ValuesTemplate: []interface{}{ + map[string]interface{}{ + "fullnameOverride": "{{ .Values | get (printf \"%s.releaseName\" .Release.Name) .Release.Name }}", + }, + }, + } + result, err := rs.ExecuteTemplateExpressions(render) + + require.NoErrorf(t, err, "failed to execute template expressions: %v", err) + require.Equalf(t, result.ValuesTemplate[0].(map[string]interface{})["fullnameOverride"], "foo", "failed to execute template expressions") +} diff --git a/pkg/yaml/yaml.go b/pkg/yaml/yaml.go index 80b48bf9..a58a7d75 100644 --- a/pkg/yaml/yaml.go +++ b/pkg/yaml/yaml.go @@ -67,6 +67,8 @@ func Marshal(v interface{}) ([]byte, error) { yamlEncoder := yaml.NewEncoder( &b, yaml.Indent(2), + yaml.UseSingleQuote(true), + yaml.UseLiteralStyleIfMultiline(true), ) err := yamlEncoder.Encode(v) defer func() { diff --git a/pkg/yaml/yaml_test.go b/pkg/yaml/yaml_test.go index 5bae3393..35e2a3ba 100644 --- a/pkg/yaml/yaml_test.go +++ b/pkg/yaml/yaml_test.go @@ -11,6 +11,13 @@ import ( func testYamlMarshal(t *testing.T, goccyGoYaml bool) { t.Helper() + var yamlLibraryName string + if goccyGoYaml { + yamlLibraryName = "goccy/go-yaml" + } else { + yamlLibraryName = "gopkg.in/yaml.v2" + } + v := runtime.GoccyGoYaml runtime.GoccyGoYaml = goccyGoYaml t.Cleanup(func() { @@ -25,7 +32,7 @@ func testYamlMarshal(t *testing.T, goccyGoYaml bool) { Annotation string `yaml:"annotation"` } `yaml:"info"` - expected string + expected map[string]string }{ { Name: "John", @@ -41,14 +48,17 @@ func testYamlMarshal(t *testing.T, goccyGoYaml bool) { // - https://github.com/helmfile/helmfile/pull/675 Annotation: "on", }}, - expected: "name: John\ninfo:\n- age: 20\n address: New York\n annotation: \"on\"\n", + expected: map[string]string{ + "goccy/go-yaml": "name: John\ninfo:\n- age: 20\n address: New York\n annotation: 'on'\n", + "gopkg.in/yaml.v2": "name: John\ninfo:\n- age: 20\n address: New York\n annotation: \"on\"\n", + }, }, } for _, tt := range tests { actual, err := Marshal(tt) require.NoError(t, err) - require.Equal(t, tt.expected, string(actual)) + require.Equal(t, tt.expected[yamlLibraryName], string(actual)) } }