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:
		
							parent
							
								
									833b72383d
								
							
						
					
					
						commit
						844726b09b
					
				|  | @ -574,6 +574,7 @@ Helmfile uses some OS environment variables to override default behaviour: | ||||||
| * `HELMFILE_CACHE_HOME` - specify directory to store cached files for remote operations | * `HELMFILE_CACHE_HOME` - specify directory to store cached files for remote operations | ||||||
| * `HELMFILE_FILE_PATH` - specify the path to the helmfile.yaml file | * `HELMFILE_FILE_PATH` - specify the path to the helmfile.yaml file | ||||||
| * `HELMFILE_INTERACTIVE` - enable interactive mode, expecting `true` lower case. The same as `--interactive` CLI flag | * `HELMFILE_INTERACTIVE` - enable interactive mode, expecting `true` lower case. The same as `--interactive` CLI flag | ||||||
|  | * `HELMFILE_ENABLE_GOCCY_GOYAML_JSON_STYLE`: - enable JSON style for *goccy/go-yaml* instead of *gopkg.in/yaml.v2*.  It's `false` by default in Helmfile. it will add quotes to string values. | ||||||
| 
 | 
 | ||||||
| ## CLI Reference | ## CLI Reference | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -6,13 +6,14 @@ const ( | ||||||
| 	// use helm status to check if a release exists before installing it
 | 	// use helm status to check if a release exists before installing it
 | ||||||
| 	UseHelmStatusToCheckReleaseExistence = "HELMFILE_USE_HELM_STATUS_TO_CHECK_RELEASE_EXISTENCE" | 	UseHelmStatusToCheckReleaseExistence = "HELMFILE_USE_HELM_STATUS_TO_CHECK_RELEASE_EXISTENCE" | ||||||
| 
 | 
 | ||||||
| 	DisableRunnerUniqueID = "HELMFILE_DISABLE_RUNNER_UNIQUE_ID" | 	DisableRunnerUniqueID      = "HELMFILE_DISABLE_RUNNER_UNIQUE_ID" | ||||||
| 	Experimental          = "HELMFILE_EXPERIMENTAL" // environment variable for experimental features, expecting "true" lower case
 | 	Experimental               = "HELMFILE_EXPERIMENTAL" // environment variable for experimental features, expecting "true" lower case
 | ||||||
| 	Environment           = "HELMFILE_ENVIRONMENT" | 	Environment                = "HELMFILE_ENVIRONMENT" | ||||||
| 	FilePath              = "HELMFILE_FILE_PATH" | 	FilePath                   = "HELMFILE_FILE_PATH" | ||||||
| 	TempDir               = "HELMFILE_TEMPDIR" | 	TempDir                    = "HELMFILE_TEMPDIR" | ||||||
| 	UpgradeNoticeDisabled = "HELMFILE_UPGRADE_NOTICE_DISABLED" | 	UpgradeNoticeDisabled      = "HELMFILE_UPGRADE_NOTICE_DISABLED" | ||||||
| 	GoccyGoYaml           = "HELMFILE_GOCCY_GOYAML" | 	GoccyGoYaml                = "HELMFILE_GOCCY_GOYAML" | ||||||
| 	CacheHome             = "HELMFILE_CACHE_HOME" | 	CacheHome                  = "HELMFILE_CACHE_HOME" | ||||||
| 	Interactive           = "HELMFILE_INTERACTIVE" | 	Interactive                = "HELMFILE_INTERACTIVE" | ||||||
|  | 	EnableGoccyGoYamlJSONStyle = "HELMFILE_ENABLE_GOCCY_GOYAML_JSON_STYLE" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | @ -4,12 +4,14 @@ import ( | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io/fs" | 	"io/fs" | ||||||
|  | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| 	goruntime "runtime" | 	goruntime "runtime" | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"github.com/stretchr/testify/require" | 	"github.com/stretchr/testify/require" | ||||||
| 
 | 
 | ||||||
|  | 	"github.com/helmfile/helmfile/pkg/envvar" | ||||||
| 	"github.com/helmfile/helmfile/pkg/filesystem" | 	"github.com/helmfile/helmfile/pkg/filesystem" | ||||||
| 	"github.com/helmfile/helmfile/pkg/runtime" | 	"github.com/helmfile/helmfile/pkg/runtime" | ||||||
| ) | ) | ||||||
|  | @ -186,18 +188,80 @@ func TestToYaml_NestedMapInterfaceKey(t *testing.T) { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func TestToYaml(t *testing.T) { | func TestToYaml(t *testing.T) { | ||||||
| 	expected := `foo: | 	tests := []struct { | ||||||
|   bar: BAR | 		name            string | ||||||
| ` | 		input           any | ||||||
| 	// nolint: unconvert
 | 		expected        string | ||||||
| 	vals := Values(map[string]any{ | 		wantErr         bool | ||||||
| 		"foo": map[string]any{ | 		enableJsonStyle bool | ||||||
| 			"bar": "BAR", | 	}{ | ||||||
|  | 		{ | ||||||
|  | 			// 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) | 			name:  "test unmarshalling issue 2024 with int64", | ||||||
| 	require.NoError(t, err) | 			input: map[string]any{"thisShouldBeString": 1234567890123456789}, | ||||||
| 	require.Equal(t, expected, actual) | 			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) { | func testFromYamlObject(t *testing.T) { | ||||||
|  |  | ||||||
|  | @ -3,10 +3,12 @@ package yaml | ||||||
| import ( | import ( | ||||||
| 	"bytes" | 	"bytes" | ||||||
| 	"io" | 	"io" | ||||||
|  | 	"os" | ||||||
| 
 | 
 | ||||||
| 	"github.com/goccy/go-yaml" | 	"github.com/goccy/go-yaml" | ||||||
| 	v2 "gopkg.in/yaml.v2" | 	v2 "gopkg.in/yaml.v2" | ||||||
| 
 | 
 | ||||||
|  | 	"github.com/helmfile/helmfile/pkg/envvar" | ||||||
| 	"github.com/helmfile/helmfile/pkg/runtime" | 	"github.com/helmfile/helmfile/pkg/runtime" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | @ -66,11 +68,19 @@ func NewDecoder(data []byte, strict bool) func(any) error { | ||||||
| func Marshal(v any) ([]byte, error) { | func Marshal(v any) ([]byte, error) { | ||||||
| 	if runtime.GoccyGoYaml { | 	if runtime.GoccyGoYaml { | ||||||
| 		var b bytes.Buffer | 		var b bytes.Buffer | ||||||
| 		yamlEncoder := yaml.NewEncoder( | 		yamlEncoderOpts := []yaml.EncodeOption{ | ||||||
| 			&b, |  | ||||||
| 			yaml.Indent(2), | 			yaml.Indent(2), | ||||||
| 			yaml.UseSingleQuote(true), | 			yaml.UseSingleQuote(true), | ||||||
| 			yaml.UseLiteralStyleIfMultiline(true), | 			yaml.UseLiteralStyleIfMultiline(true), | ||||||
|  | 		} | ||||||
|  | 		// enable JSON style if the envvar is set
 | ||||||
|  | 		if os.Getenv(envvar.EnableGoccyGoYamlJSONStyle) == "true" { | ||||||
|  | 			yamlEncoderOpts = append(yamlEncoderOpts, yaml.JSON(), yaml.Flow(false)) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		yamlEncoder := yaml.NewEncoder( | ||||||
|  | 			&b, | ||||||
|  | 			yamlEncoderOpts..., | ||||||
| 		) | 		) | ||||||
| 		err := yamlEncoder.Encode(v) | 		err := yamlEncoder.Encode(v) | ||||||
| 		defer func() { | 		defer func() { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue