refactor(yaml): upgrade from gopkg.in/yaml.v2 to v3 (#2039)

* refactor(yaml): upgrade from gopkg.in/yaml.v2 to v3

Signed-off-by: yxxhero <aiopsclub@163.com>

* refactor(yaml): enhance yaml encoding with consistent formatting and quotes

Signed-off-by: yxxhero <aiopsclub@163.com>

* optimize code

Signed-off-by: yxxhero <aiopsclub@163.com>

* fix tests

Signed-off-by: yxxhero <aiopsclub@163.com>

* fix more issues

Signed-off-by: yxxhero <aiopsclub@163.com>

* fix tests

Signed-off-by: yxxhero <aiopsclub@163.com>

---------

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero
2025-05-15 10:21:37 -04:00
committed by GitHub
parent 867bef0f03
commit b52ca9ae04
13 changed files with 140 additions and 147 deletions
+17 -27
View File
@@ -4,14 +4,12 @@ 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"
)
@@ -162,9 +160,9 @@ func TestReadFile_PassAbsPath(t *testing.T) {
}
func TestToYaml_NestedMapInterfaceKey(t *testing.T) {
v := runtime.GoccyGoYaml
v := runtime.GoYamlV3
t.Cleanup(func() {
runtime.GoccyGoYaml = v
runtime.GoYamlV3 = v
})
// nolint: unconvert
@@ -174,13 +172,13 @@ func TestToYaml_NestedMapInterfaceKey(t *testing.T) {
},
})
runtime.GoccyGoYaml = true
runtime.GoYamlV3 = true
actual, err := ToYaml(vals)
require.Equal(t, "foo:\n bar: BAR\n", actual)
require.NoError(t, err, "expected nil, but got: %v, when type: map[interface {}]interface {}", err)
runtime.GoccyGoYaml = false
runtime.GoYamlV3 = false
actual, err = ToYaml(vals)
require.Equal(t, "foo:\n bar: BAR\n", actual)
@@ -189,18 +187,16 @@ func TestToYaml_NestedMapInterfaceKey(t *testing.T) {
func TestToYaml(t *testing.T) {
tests := []struct {
name string
input any
expected string
wantErr bool
enableJsonStyle bool
name string
input any
expected string
wantErr bool
}{
{
// https://github.com/helmfile/helmfile/issues/2024
name: "test unmarshalling issue 2024",
enableJsonStyle: true,
input: map[string]any{"thisShouldBeString": "01234567890123456789"},
expected: `'thisShouldBeString': '01234567890123456789'
name: "test unmarshalling issue 2024",
input: map[string]any{"thisShouldBeString": "01234567890123456789"},
expected: `thisShouldBeString: "01234567890123456789"
`,
},
{
@@ -247,12 +243,6 @@ func TestToYaml(t *testing.T) {
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)
@@ -345,13 +335,13 @@ func testFromYamlNull(t *testing.T) {
require.Equal(t, nil, actual)
}
func testFromYaml(t *testing.T, goccyGoYaml bool) {
func testFromYaml(t *testing.T, GoYamlV3 bool) {
t.Helper()
v := runtime.GoccyGoYaml
runtime.GoccyGoYaml = goccyGoYaml
v := runtime.GoYamlV3
runtime.GoYamlV3 = GoYamlV3
t.Cleanup(func() {
runtime.GoccyGoYaml = v
runtime.GoYamlV3 = v
})
t.Run("test unmarshalling object", testFromYamlObject)
@@ -368,11 +358,11 @@ func testFromYaml(t *testing.T, goccyGoYaml bool) {
}
func TestFromYaml(t *testing.T) {
t.Run("with goccy/go-yaml", func(t *testing.T) {
t.Run("with gopkg.in/yaml.v2", func(t *testing.T) {
testFromYaml(t, true)
})
t.Run("with gopkg.in/yaml.v2", func(t *testing.T) {
t.Run("with gopkg.in/yaml.v3", func(t *testing.T) {
testFromYaml(t, false)
})
}