From 328830092d391736ebb28230ffa40ab745a8ebe5 Mon Sep 17 00:00:00 2001 From: flintwinters Date: Wed, 17 Dec 2025 00:27:15 -0500 Subject: [PATCH] test: Add tests for UnwrapScalar in shell encoder - Introduce assertEncodesToUnwrapped helper function. - Add TestShellVariablesEncoderUnwrapScalar to verify quote-free output with -r. - Add TestShellVariablesEncoderDefaultQuoting to confirm default quoting behavior without -r. - Ensure comprehensive testing of conditional quoting logic for shell output. --- pkg/yqlib/encoder_shellvariables_test.go | 38 ++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/pkg/yqlib/encoder_shellvariables_test.go b/pkg/yqlib/encoder_shellvariables_test.go index d41ad663..87228d51 100644 --- a/pkg/yqlib/encoder_shellvariables_test.go +++ b/pkg/yqlib/encoder_shellvariables_test.go @@ -135,3 +135,41 @@ func TestShellVariablesEncoderCustomSeparatorArray(t *testing.T) { func TestShellVariablesEncoderCustomSeparatorSingleChar(t *testing.T) { assertEncodesToWithSeparator(t, "a:\n b: value", "aXb=value", "X") } + +func assertEncodesToUnwrapped(t *testing.T, yaml string, shellvars string) { + var output bytes.Buffer + writer := bufio.NewWriter(&output) + + originalUnwrapScalar := ConfiguredShellVariablesPreferences.UnwrapScalar + defer func() { + ConfiguredShellVariablesPreferences.UnwrapScalar = originalUnwrapScalar + }() + + ConfiguredShellVariablesPreferences.UnwrapScalar = true + + var encoder = NewShellVariablesEncoder() + inputs, err := readDocuments(strings.NewReader(yaml), "test.yml", 0, NewYamlDecoder(ConfiguredYamlPreferences)) + if err != nil { + panic(err) + } + node := inputs.Front().Value.(*CandidateNode) + err = encoder.Encode(writer, node) + if err != nil { + panic(err) + } + writer.Flush() + + test.AssertResult(t, shellvars, strings.TrimSuffix(output.String(), "\n")) +} + +func TestShellVariablesEncoderUnwrapScalar(t *testing.T) { + assertEncodesToUnwrapped(t, "a: Lewis Carroll", "a=Lewis Carroll") + assertEncodesToUnwrapped(t, "b: 123", "b=123") + assertEncodesToUnwrapped(t, "c: true", "c=true") + assertEncodesToUnwrapped(t, "d: value with spaces", "d=value with spaces") +} + +func TestShellVariablesEncoderDefaultQuoting(t *testing.T) { + assertEncodesTo(t, "a: Lewis Carroll", "a='Lewis Carroll'") + assertEncodesTo(t, "b: 123 456", "b='123 456'") +}