feat(tmpl): enhance ToYaml test with multiple scenarios (#2031)

* feat(tmpl): enhance ToYaml test with multiple scenarios

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero
2025-05-09 11:56:54 +08:00
committed by GitHub
parent 833b72383d
commit 844726b09b
4 changed files with 98 additions and 22 deletions
+75 -11
View File
@@ -4,12 +4,14 @@ import (
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
goruntime "runtime"
"testing"
"github.com/stretchr/testify/require"
"github.com/helmfile/helmfile/pkg/envvar"
"github.com/helmfile/helmfile/pkg/filesystem"
"github.com/helmfile/helmfile/pkg/runtime"
)
@@ -186,18 +188,80 @@ func TestToYaml_NestedMapInterfaceKey(t *testing.T) {
}
func TestToYaml(t *testing.T) {
expected := `foo:
bar: BAR
`
// nolint: unconvert
vals := Values(map[string]any{
"foo": map[string]any{
"bar": "BAR",
tests := []struct {
name string
input any
expected string
wantErr bool
enableJsonStyle bool
}{
{
// https://github.com/helmfile/helmfile/issues/2024
name: "test unmarshalling issue 2024",
enableJsonStyle: true,
input: map[string]any{"thisShouldBeString": "01234567890123456789"},
expected: `'thisShouldBeString': '01234567890123456789'
`,
},
})
actual, err := ToYaml(vals)
require.NoError(t, err)
require.Equal(t, expected, actual)
{
name: "test unmarshalling issue 2024 with int64",
input: map[string]any{"thisShouldBeString": 1234567890123456789},
expected: `thisShouldBeString: 1234567890123456789
`,
},
{
name: "test unmarshalling object",
input: map[string]any{"foo": map[string]any{"bar": "BAR"}},
expected: `foo:
bar: BAR
`,
},
{
name: "test unmarshalling array",
input: []any{"foo", map[string]any{"bar": "BAR"}},
expected: `- foo
- bar: BAR
`,
},
{
name: "test unmarshalling string",
input: "foo",
expected: "foo\n",
},
{
name: "test unmarshalling number",
input: 1234,
expected: "1234\n",
},
{
name: "test unmarshalling boolean",
input: true,
expected: "true\n",
},
{
name: "test unmarshalling null",
input: nil,
expected: "null\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.enableJsonStyle {
_ = os.Setenv(envvar.EnableGoccyGoYamlJSONStyle, "true")
}
defer func() {
_ = os.Unsetenv(envvar.EnableGoccyGoYamlJSONStyle)
}()
actual, err := ToYaml(tt.input)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
require.Equal(t, tt.expected, actual)
})
}
}
func testFromYamlObject(t *testing.T) {