Repository: mikefarah/yq
Pull request #1241 (2022-06-16 14:04:19)

adding support for --wrapScalar=false in properties encoder
I'm currently running into a scenario where it would be useful to wrap the value side of the output from the Properties encoder.

This PR is a first draft at that with some tests, please let me know if you'd like some more!

mikefarah: This looks really good - you just need to add test(s) into properties_test.go, this will also generate documentation so others know about the feature 👍🏼  
dcarbone: @mikefarah i've updated the test workflow a bit and added one for my added feature.

if you disagree with the changes, i can work on a different solution.
dcarbone: @mikefarah i've updated the test workflow a bit and added one for my added feature.

if you disagree with the changes, i can work on a different solution.
mikefarah: Thank you! There is a problem though in the documentation generated, it doesn't show the updated command to get the wrapping (e..g supplying `--unwrapScalar=false` - if you have a look at xml_test.go, you can see that there are added 'scenarioType's that generate a slightly different documentation on the command, as well us parameterising the encoder/decoder correctly.
dcarbone: @mikefarah sorry it took a few days to get back to you on this!  i reverted my initial pass as it wasn't really in keeping with your original design, and i didn't want to start imposing my own opinions there.

with this update, i've done the following:

1. replaced the various `if .. else ..` cases when processing scenarios with a switch
  * this was mostly to make it a smidge cleaner to read and expand upon, and also forces future `scenarioTypes` to have an explicit handler, rather than letting it succeed with ambiguous correctness.
1. replaced a few `var` definitions with `const` as their values are indeed constant
1. added a new scenario type (`encode-wrapped`) for the properties test suite.

let me know what you think :)

Patch:
diff --git a/.gitignore b/.gitignore
index 53a1f625..8aa98112 100644
--- a/.gitignore
+++ b/.gitignore
@@ -54,3 +54,6 @@ yq.1
 # debian pkg
 _build
 debian/files
+
+# intellij
+/.idea
\ No newline at end of file
diff --git a/cmd/utils.go b/cmd/utils.go
index faec5bed..032d6519 100644
--- a/cmd/utils.go
+++ b/cmd/utils.go
@@ -93,7 +93,7 @@ func configureEncoder(format yqlib.PrinterOutputFormat) yqlib.Encoder {
 	case yqlib.JSONOutputFormat:
 		return yqlib.NewJONEncoder(indent, colorsEnabled)
 	case yqlib.PropsOutputFormat:
-		return yqlib.NewPropertiesEncoder()
+		return yqlib.NewPropertiesEncoder(unwrapScalar)
 	case yqlib.CSVOutputFormat:
 		return yqlib.NewCsvEncoder(',')
 	case yqlib.TSVOutputFormat:
diff --git a/pkg/yqlib/doc/usage/properties.md b/pkg/yqlib/doc/usage/properties.md
index 97d05760..1b0f3904 100644
--- a/pkg/yqlib/doc/usage/properties.md
+++ b/pkg/yqlib/doc/usage/properties.md
@@ -17,7 +17,7 @@ Given a sample.yml file of:
 ```yaml
 # block comments don't come through
 person: # neither do comments on maps
-    name: Mike # comments on values appear
+    name: Mike Wazowski # comments on values appear
     pets: 
     - cat # comments on array values appear
     food: [pizza] # comments on arrays do not
@@ -32,7 +32,36 @@ yq -o=props sample.yml
 will output
 ```properties
 # comments on values appear
-person.name = Mike
+person.name = Mike Wazowski
+
+# comments on array values appear
+person.pets.0 = cat
+person.food.0 = pizza
+```
+
+## Encode properties: scalar encapsulation
+Note that string values with blank characters in them are encapsulated with double quotes
+
+Given a sample.yml file of:
+```yaml
+# block comments don't come through
+person: # neither do comments on maps
+    name: Mike Wazowski # comments on values appear
+    pets: 
+    - cat # comments on array values appear
+    food: [pizza] # comments on arrays do not
+emptyArray: []
+emptyMap: []
+
+```
+then
+```bash
+yq -o=props --unwrapScalar=false sample.yml
+```
+will output
+```properties
+# comments on values appear
+person.name = "Mike Wazowski"
 
 # comments on array values appear
 person.pets.0 = cat
@@ -44,7 +73,7 @@ Given a sample.yml file of:
 ```yaml
 # block comments don't come through
 person: # neither do comments on maps
-    name: Mike # comments on values appear
+    name: Mike Wazowski # comments on values appear
     pets: 
     - cat # comments on array values appear
     food: [pizza] # comments on arrays do not
@@ -58,7 +87,7 @@ yq -o=props '... comments = ""' sample.yml
 ```
 will output
 ```properties
-person.name = Mike
+person.name = Mike Wazowski
 person.pets.0 = cat
 person.food.0 = pizza
 ```
@@ -70,7 +99,7 @@ Given a sample.yml file of:
 ```yaml
 # block comments don't come through
 person: # neither do comments on maps
-    name: Mike # comments on values appear
+    name: Mike Wazowski # comments on values appear
     pets: 
     - cat # comments on array values appear
     food: [pizza] # comments on arrays do not
@@ -85,7 +114,7 @@ yq -o=props '(.. | select( (tag == "!!map" or tag =="!!seq") and length == 0)) =
 will output
 ```properties
 # comments on values appear
-person.name = Mike
+person.name = Mike Wazowski
 
 # comments on array values appear
 person.pets.0 = cat
@@ -98,7 +127,7 @@ emptyMap =
 Given a sample.properties file of:
 ```properties
 # comments on values appear
-person.name = Mike
+person.name = Mike Wazowski
 
 # comments on array values appear
 person.pets.0 = cat
@@ -112,7 +141,7 @@ yq -p=props sample.properties
 will output
 ```yaml
 person:
-    name: Mike # comments on values appear
+    name: Mike Wazowski # comments on values appear
     pets:
         - cat # comments on array values appear
     food:
@@ -123,7 +152,7 @@ person:
 Given a sample.properties file of:
 ```properties
 # comments on values appear
-person.name = Mike
+person.name = Mike Wazowski
 
 # comments on array values appear
 person.pets.0 = cat
@@ -137,7 +166,7 @@ yq -p=props -o=props '.person.pets.0 = "dog"' sample.properties
 will output
 ```properties
 # comments on values appear
-person.name = Mike
+person.name = Mike Wazowski
 
 # comments on array values appear
 person.pets.0 = dog
diff --git a/pkg/yqlib/encoder_properties.go b/pkg/yqlib/encoder_properties.go
index 287b707f..745f1279 100644
--- a/pkg/yqlib/encoder_properties.go
+++ b/pkg/yqlib/encoder_properties.go
@@ -12,10 +12,13 @@ import (
 )
 
 type propertiesEncoder struct {
+	unwrapScalar bool
 }
 
-func NewPropertiesEncoder() Encoder {
-	return &propertiesEncoder{}
+func NewPropertiesEncoder(unwrapScalar bool) Encoder {
+	return &propertiesEncoder{
+		unwrapScalar: unwrapScalar,
+	}
 }
 
 func (pe *propertiesEncoder) CanHandleAliases() bool {
@@ -75,7 +78,13 @@ func (pe *propertiesEncoder) doEncode(p *properties.Properties, node *yaml.Node,
 	p.SetComment(path, headAndLineComment(node))
 	switch node.Kind {
 	case yaml.ScalarNode:
-		_, _, err := p.Set(path, node.Value)
+		var nodeValue string
+		if pe.unwrapScalar || !strings.Contains(node.Value, " ") {
+			nodeValue = node.Value
+		} else {
+			nodeValue = fmt.Sprintf("%q", node.Value)
+		}
+		_, _, err := p.Set(path, nodeValue)
 		return err
 	case yaml.DocumentNode:
 		return pe.doEncode(p, node.Content[0], path)
diff --git a/pkg/yqlib/operator_assign.go b/pkg/yqlib/operator_assign.go
index 1e1058ed..2e8c0e1f 100644
--- a/pkg/yqlib/operator_assign.go
+++ b/pkg/yqlib/operator_assign.go
@@ -22,8 +22,10 @@ func assignUpdateOperator(d *dataTreeNavigator, context Context, expressionNode
 	}
 
 	prefs := assignPreferences{}
-	if expressionNode.Operation.Preferences != nil {
-		prefs = expressionNode.Operation.Preferences.(assignPreferences)
+	// they way *= (multipleAssign) is handled, we set the multiplePrefs
+	// on the node, not assignPrefs. Long story.
+	if p, ok := expressionNode.Operation.Preferences.(assignPreferences); ok {
+		prefs = p
 	}
 
 	if !expressionNode.Operation.UpdateAssign {
diff --git a/pkg/yqlib/operator_encoder_decoder.go b/pkg/yqlib/operator_encoder_decoder.go
index ac4fd73f..dabaf01a 100644
--- a/pkg/yqlib/operator_encoder_decoder.go
+++ b/pkg/yqlib/operator_encoder_decoder.go
@@ -15,7 +15,7 @@ func configureEncoder(format PrinterOutputFormat, indent int) Encoder {
 	case JSONOutputFormat:
 		return NewJONEncoder(indent, false)
 	case PropsOutputFormat:
-		return NewPropertiesEncoder()
+		return NewPropertiesEncoder(true)
 	case CSVOutputFormat:
 		return NewCsvEncoder(',')
 	case TSVOutputFormat:
diff --git a/pkg/yqlib/operator_multiply.go b/pkg/yqlib/operator_multiply.go
index e1dfcd61..ac27ad1f 100644
--- a/pkg/yqlib/operator_multiply.go
+++ b/pkg/yqlib/operator_multiply.go
@@ -27,7 +27,6 @@ func createMultiplyOp(prefs interface{}) func(lhs *ExpressionNode, rhs *Expressi
 
 func multiplyAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
 	var multiplyPrefs = expressionNode.Operation.Preferences
-	expressionNode.Operation.Preferences = nil
 
 	return compoundAssignFunction(d, context, expressionNode, createMultiplyOp(multiplyPrefs))
 }
diff --git a/release_notes.txt b/release_notes.txt
index 3515edc2..5e60603a 100644
--- a/release_notes.txt
+++ b/release_notes.txt
@@ -1,3 +1,6 @@
+4.25.4:
+  - Fixed panic when using multiply assign on multiple documents #1256 Thanks @care0717
+
 4.25.3:
   - xml decoder now maintains namespaces by default. Use new flags to disable if required. Thanks @rndmit
   - Length and other similar operators no longer return comments (#1231)


Test patch:
diff --git a/acceptance_tests/basic.sh b/acceptance_tests/basic.sh
index 597d23be..141122c4 100755
--- a/acceptance_tests/basic.sh
+++ b/acceptance_tests/basic.sh
@@ -331,4 +331,26 @@ EOM
   assertEquals "$expected" "$X"
 }
 
+testBasicMultiplyAssignMultiDoc() {
+      cat >test.yml <<EOL
+a: 1
+---
+b: 2
+EOL
+
+read -r -d '' expected << EOM
+a: 1
+c: 3
+---
+b: 2
+c: 3
+EOM
+
+
+  X=$(./yq '. *= {"c":3}' test.yml)
+  assertEquals "$expected" "$X"
+}
+
+
+
 source ./scripts/shunit2
\ No newline at end of file
diff --git a/pkg/yqlib/encoder_properties_test.go b/pkg/yqlib/encoder_properties_test.go
index 647010a6..2c2b7f58 100644
--- a/pkg/yqlib/encoder_properties_test.go
+++ b/pkg/yqlib/encoder_properties_test.go
@@ -9,11 +9,11 @@ import (
 	"github.com/mikefarah/yq/v4/test"
 )
 
-func yamlToProps(sampleYaml string) string {
+func yamlToProps(sampleYaml string, unwrapScalar bool) string {
 	var output bytes.Buffer
 	writer := bufio.NewWriter(&output)
 
-	var propsEncoder = NewPropertiesEncoder()
+	var propsEncoder = NewPropertiesEncoder(unwrapScalar)
 	inputs, err := readDocuments(strings.NewReader(sampleYaml), "sample.yml", 0, NewYamlDecoder())
 	if err != nil {
 		panic(err)
@@ -28,51 +28,100 @@ func yamlToProps(sampleYaml string) string {
 	return strings.TrimSuffix(output.String(), "\n")
 }
 
-func TestPropertiesEncoderSimple(t *testing.T) {
+func TestPropertiesEncoderSimple_Unwrapped(t *testing.T) {
 	var sampleYaml = `a: 'bob cool'`
 
 	var expectedProps = `a = bob cool`
-	var actualProps = yamlToProps(sampleYaml)
+	var actualProps = yamlToProps(sampleYaml, true)
 	test.AssertResult(t, expectedProps, actualProps)
 }
 
-func TestPropertiesEncoderSimpleWithComments(t *testing.T) {
+func TestPropertiesEncoderSimple_Wrapped(t *testing.T) {
+	var sampleYaml = `a: 'bob cool'`
+
+	var expectedProps = `a = "bob cool"`
+	var actualProps = yamlToProps(sampleYaml, false)
+	test.AssertResult(t, expectedProps, actualProps)
+}
+
+func TestPropertiesEncoderSimpleWithComments_Unwrapped(t *testing.T) {
 	var sampleYaml = `a: 'bob cool' # line`
 
 	var expectedProps = `# line
 a = bob cool`
-	var actualProps = yamlToProps(sampleYaml)
+	var actualProps = yamlToProps(sampleYaml, true)
+	test.AssertResult(t, expectedProps, actualProps)
+}
+
+func TestPropertiesEncoderSimpleWithComments_Wrapped(t *testing.T) {
+	var sampleYaml = `a: 'bob cool' # line`
+
+	var expectedProps = `# line
+a = "bob cool"`
+	var actualProps = yamlToProps(sampleYaml, false)
 	test.AssertResult(t, expectedProps, actualProps)
 }
 
-func TestPropertiesEncoderDeep(t *testing.T) {
+func TestPropertiesEncoderDeep_Unwrapped(t *testing.T) {
 	var sampleYaml = `a: 
   b: "bob cool"
 `
 
 	var expectedProps = `a.b = bob cool`
-	var actualProps = yamlToProps(sampleYaml)
+	var actualProps = yamlToProps(sampleYaml, true)
+	test.AssertResult(t, expectedProps, actualProps)
+}
+
+func TestPropertiesEncoderDeep_Wrapped(t *testing.T) {
+	var sampleYaml = `a: 
+  b: "bob cool"
+`
+
+	var expectedProps = `a.b = "bob cool"`
+	var actualProps = yamlToProps(sampleYaml, false)
 	test.AssertResult(t, expectedProps, actualProps)
 }
 
-func TestPropertiesEncoderDeepWithComments(t *testing.T) {
+func TestPropertiesEncoderDeepWithComments_Unwrapped(t *testing.T) {
 	var sampleYaml = `a:  # a thing
   b: "bob cool" # b thing
 `
 
 	var expectedProps = `# b thing
 a.b = bob cool`
-	var actualProps = yamlToProps(sampleYaml)
+	var actualProps = yamlToProps(sampleYaml, true)
+	test.AssertResult(t, expectedProps, actualProps)
+}
+
+func TestPropertiesEncoderDeepWithComments_Wrapped(t *testing.T) {
+	var sampleYaml = `a:  # a thing
+  b: "bob cool" # b thing
+`
+
+	var expectedProps = `# b thing
+a.b = "bob cool"`
+	var actualProps = yamlToProps(sampleYaml, false)
 	test.AssertResult(t, expectedProps, actualProps)
 }
 
-func TestPropertiesEncoderArray(t *testing.T) {
+func TestPropertiesEncoderArray_Unwrapped(t *testing.T) {
 	var sampleYaml = `a: 
   b: [{c: dog}, {c: cat}]
 `
 
 	var expectedProps = `a.b.0.c = dog
 a.b.1.c = cat`
-	var actualProps = yamlToProps(sampleYaml)
+	var actualProps = yamlToProps(sampleYaml, true)
+	test.AssertResult(t, expectedProps, actualProps)
+}
+
+func TestPropertiesEncoderArray_Wrapped(t *testing.T) {
+	var sampleYaml = `a: 
+  b: [{c: dog named jim}, {c: cat named jam}]
+`
+
+	var expectedProps = `a.b.0.c = "dog named jim"
+a.b.1.c = "cat named jam"`
+	var actualProps = yamlToProps(sampleYaml, false)
 	test.AssertResult(t, expectedProps, actualProps)
 }
diff --git a/pkg/yqlib/json_test.go b/pkg/yqlib/json_test.go
index 0a400434..06958f37 100644
--- a/pkg/yqlib/json_test.go
+++ b/pkg/yqlib/json_test.go
@@ -9,7 +9,7 @@ import (
 	"github.com/mikefarah/yq/v4/test"
 )
 
-var complexExpectYaml = `D0, P[], (!!map)::a: Easy! as one two three
+const complexExpectYaml = `D0, P[], (!!map)::a: Easy! as one two three
 b:
     c: 2
     d:
@@ -96,11 +96,15 @@ func decodeJSON(t *testing.T, jsonString string) *CandidateNode {
 }
 
 func testJSONScenario(t *testing.T, s formatScenario) {
-	if s.scenarioType == "encode" || s.scenarioType == "roundtrip" {
+	switch s.scenarioType {
+	case "encode", "decode":
 		test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewJONEncoder(s.indent, false)), s.description)
-	} else {
+	case "":
 		var actual = resultToString(t, decodeJSON(t, s.input))
 		test.AssertResultWithContext(t, s.expected, actual, s.description)
+
+	default:
+		panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
 	}
 }
 
@@ -135,14 +139,17 @@ func documentJSONDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario)
 
 func documentJSONScenario(t *testing.T, w *bufio.Writer, i interface{}) {
 	s := i.(formatScenario)
-
 	if s.skipDoc {
 		return
 	}
-	if s.scenarioType == "encode" {
-		documentJSONEncodeScenario(w, s)
-	} else {
+	switch s.scenarioType {
+	case "":
 		documentJSONDecodeScenario(t, w, s)
+	case "encode":
+		documentJSONEncodeScenario(w, s)
+
+	default:
+		panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
 	}
 }
 
diff --git a/pkg/yqlib/properties_test.go b/pkg/yqlib/properties_test.go
index 59e01202..3a59fcf3 100644
--- a/pkg/yqlib/properties_test.go
+++ b/pkg/yqlib/properties_test.go
@@ -8,9 +8,9 @@ import (
 	"github.com/mikefarah/yq/v4/test"
 )
 
-var samplePropertiesYaml = `# block comments don't come through
+const samplePropertiesYaml = `# block comments don't come through
 person: # neither do comments on maps
-    name: Mike # comments on values appear
+    name: Mike Wazowski # comments on values appear
     pets: 
     - cat # comments on array values appear
     food: [pizza] # comments on arrays do not
@@ -18,37 +18,45 @@ emptyArray: []
 emptyMap: []
 `
 
-var expectedProperties = `# comments on values appear
-person.name = Mike
+const expectedPropertiesUnwrapped = `# comments on values appear
+person.name = Mike Wazowski
 
 # comments on array values appear
 person.pets.0 = cat
 person.food.0 = pizza
 `
 
-var expectedUpdatedProperties = `# comments on values appear
-person.name = Mike
+const expectedPropertiesWrapped = `# comments on values appear
+person.name = "Mike Wazowski"
+
+# comments on array values appear
+person.pets.0 = cat
+person.food.0 = pizza
+`
+
+const expectedUpdatedProperties = `# comments on values appear
+person.name = Mike Wazowski
 
 # comments on array values appear
 person.pets.0 = dog
 person.food.0 = pizza
 `
 
-var expectedDecodedYaml = `person:
-  name: Mike # comments on values appear
+const expectedDecodedYaml = `person:
+  name: Mike Wazowski # comments on values appear
   pets:
     - cat # comments on array values appear
   food:
     - pizza
 `
 
-var expectedPropertiesNoComments = `person.name = Mike
+const expectedPropertiesNoComments = `person.name = Mike Wazowski
 person.pets.0 = cat
 person.food.0 = pizza
 `
 
-var expectedPropertiesWithEmptyMapsAndArrays = `# comments on values appear
-person.name = Mike
+const expectedPropertiesWithEmptyMapsAndArrays = `# comments on values appear
+person.name = Mike Wazowski
 
 # comments on array values appear
 person.pets.0 = cat
@@ -62,7 +70,14 @@ var propertyScenarios = []formatScenario{
 		description:    "Encode properties",
 		subdescription: "Note that empty arrays and maps are not encoded by default.",
 		input:          samplePropertiesYaml,
-		expected:       expectedProperties,
+		expected:       expectedPropertiesUnwrapped,
+	},
+	{
+		description:    "Encode properties: scalar encapsulation",
+		subdescription: "Note that string values with blank characters in them are encapsulated with double quotes",
+		input:          samplePropertiesYaml,
+		expected:       expectedPropertiesWrapped,
+		scenarioType:   "encode-wrapped",
 	},
 	{
 		description: "Encode properties: no comments",
@@ -79,7 +94,7 @@ var propertyScenarios = []formatScenario{
 	},
 	{
 		description:  "Decode properties",
-		input:        expectedProperties,
+		input:        expectedPropertiesUnwrapped,
 		expected:     expectedDecodedYaml,
 		scenarioType: "decode",
 	},
@@ -92,7 +107,7 @@ var propertyScenarios = []formatScenario{
 	},
 	{
 		description:  "Roundtrip",
-		input:        expectedProperties,
+		input:        expectedPropertiesUnwrapped,
 		expression:   `.person.pets.0 = "dog"`,
 		expected:     expectedUpdatedProperties,
 		scenarioType: "roundtrip",
@@ -106,7 +121,7 @@ var propertyScenarios = []formatScenario{
 	},
 }
 
-func documentEncodePropertyScenario(w *bufio.Writer, s formatScenario) {
+func documentUnwrappedEncodePropertyScenario(w *bufio.Writer, s formatScenario) {
 	writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
 
 	if s.subdescription != "" {
@@ -128,7 +143,32 @@ func documentEncodePropertyScenario(w *bufio.Writer, s formatScenario) {
 	}
 	writeOrPanic(w, "will output\n")
 
-	writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewPropertiesEncoder())))
+	writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewPropertiesEncoder(true))))
+}
+
+func documentWrappedEncodePropertyScenario(w *bufio.Writer, s formatScenario) {
+	writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
+
+	if s.subdescription != "" {
+		writeOrPanic(w, s.subdescription)
+		writeOrPanic(w, "\n\n")
+	}
+
+	writeOrPanic(w, "Given a sample.yml file of:\n")
+	writeOrPanic(w, fmt.Sprintf("```yaml\n%v\n```\n", s.input))
+
+	writeOrPanic(w, "then\n")
+
+	expression := s.expression
+
+	if expression != "" {
+		writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=props --unwrapScalar=false '%v' sample.yml\n```\n", expression))
+	} else {
+		writeOrPanic(w, "```bash\nyq -o=props --unwrapScalar=false sample.yml\n```\n")
+	}
+	writeOrPanic(w, "will output\n")
+
+	writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewPropertiesEncoder(false))))
 }
 
 func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) {
@@ -178,7 +218,7 @@ func documentRoundTripPropertyScenario(w *bufio.Writer, s formatScenario) {
 
 	writeOrPanic(w, "will output\n")
 
-	writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder())))
+	writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder(true))))
 }
 
 func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) {
@@ -186,24 +226,35 @@ func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) {
 	if s.skipDoc {
 		return
 	}
-	if s.scenarioType == "decode" {
+	switch s.scenarioType {
+	case "":
+		documentUnwrappedEncodePropertyScenario(w, s)
+	case "decode":
 		documentDecodePropertyScenario(w, s)
-	} else if s.scenarioType == "roundtrip" {
+	case "encode-wrapped":
+		documentWrappedEncodePropertyScenario(w, s)
+	case "roundtrip":
 		documentRoundTripPropertyScenario(w, s)
-	} else {
-		documentEncodePropertyScenario(w, s)
-	}
 
+	default:
+		panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
+	}
 }
 
 func TestPropertyScenarios(t *testing.T) {
 	for _, s := range propertyScenarios {
-		if s.scenarioType == "decode" {
+		switch s.scenarioType {
+		case "":
+			test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewPropertiesEncoder(true)), s.description)
+		case "decode":
 			test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesDecoder(), NewYamlEncoder(2, false, true, true)), s.description)
-		} else if s.scenarioType == "roundtrip" {
-			test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder()), s.description)
-		} else {
-			test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewPropertiesEncoder()), s.description)
+		case "encode-wrapped":
+			test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewPropertiesEncoder(false)), s.description)
+		case "roundtrip":
+			test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesDecoder(), NewPropertiesEncoder(true)), s.description)
+
+		default:
+			panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
 		}
 	}
 	genericScenarios := make([]interface{}, len(propertyScenarios))
diff --git a/pkg/yqlib/xml_test.go b/pkg/yqlib/xml_test.go
index 276f7006..9058c084 100644
--- a/pkg/yqlib/xml_test.go
+++ b/pkg/yqlib/xml_test.go
@@ -8,7 +8,7 @@ import (
 	"github.com/mikefarah/yq/v4/test"
 )
 
-var inputXMLWithComments = `
+const inputXMLWithComments = `
 <!-- before cat -->
 <cat>
 	<!-- in cat before -->
@@ -26,7 +26,7 @@ for x --></x>
 </cat>
 <!-- after cat -->
 `
-var inputXMLWithCommentsWithSubChild = `
+const inputXMLWithCommentsWithSubChild = `
 <!-- before cat -->
 <cat>
 	<!-- in cat before -->
@@ -45,7 +45,7 @@ for x --></x>
 <!-- after cat -->
 `
 
-var expectedDecodeYamlWithSubChild = `# before cat
+const expectedDecodeYamlWithSubChild = `# before cat
 cat:
     # in cat before
     x: "3" # multi
@@ -65,7 +65,7 @@ cat:
 # after cat
 `
 
-var inputXMLWithCommentsWithArray = `
+const inputXMLWithCommentsWithArray = `
 <!-- before cat -->
 <cat>
 	<!-- in cat before -->
@@ -85,7 +85,7 @@ for x --></x>
 <!-- after cat -->
 `
 
-var expectedDecodeYamlWithArray = `# before cat
+const expectedDecodeYamlWithArray = `# before cat
 cat:
     # in cat before
     x: "3" # multi
@@ -109,7 +109,7 @@ cat:
 # after cat
 `
 
-var expectedDecodeYamlWithComments = `# before cat
+const expectedDecodeYamlWithComments = `# before cat
 cat:
     # in cat before
     x: "3" # multi
@@ -126,7 +126,7 @@ cat:
 # after cat
 `
 
-var expectedRoundtripXMLWithComments = `<!-- before cat --><cat><!-- in cat before -->
+const expectedRoundtripXMLWithComments = `<!-- before cat --><cat><!-- in cat before -->
   <x>3<!-- multi
 line comment 
 for x --></x><!-- before y -->
@@ -137,7 +137,7 @@ in d before -->
 </cat><!-- after cat -->
 `
 
-var yamlWithComments = `# above_cat
+const yamlWithComments = `# above_cat
 cat: # inline_cat
   # above_array
   array: # inline_array
@@ -147,31 +147,31 @@ cat: # inline_cat
 # below_cat
 `
 
-var expectedXMLWithComments = `<!-- above_cat inline_cat --><cat><!-- above_array inline_array -->
+const expectedXMLWithComments = `<!-- above_cat inline_cat --><cat><!-- above_array inline_array -->
   <array>val1<!-- inline_val1 --></array>
   <array><!-- above_val2 -->val2<!-- inline_val2 --></array>
 </cat><!-- below_cat -->
 `
 
-var inputXMLWithNamespacedAttr = `
+const inputXMLWithNamespacedAttr = `
 <?xml version="1.0"?>
 <map xmlns="some-namespace" xmlns:xsi="some-instance" xsi:schemaLocation="some-url">
 </map>
 `
 
-var expectedYAMLWithNamespacedAttr = `map:
+const expectedYAMLWithNamespacedAttr = `map:
   +xmlns: some-namespace
   +xmlns:xsi: some-instance
   +some-instance:schemaLocation: some-url
 `
 
-var expectedYAMLWithRawNamespacedAttr = `map:
+const expectedYAMLWithRawNamespacedAttr = `map:
   +xmlns: some-namespace
   +xmlns:xsi: some-instance
   +xsi:schemaLocation: some-url
 `
 
-var xmlWithCustomDtd = `
+const xmlWithCustomDtd = `
 <?xml version="1.0"?>
 <!DOCTYPE root [
 <!ENTITY writer "Blah.">
@@ -181,7 +181,7 @@ var xmlWithCustomDtd = `
     <item>&writer;&copyright;</item>
 </root>`
 
-var expectedDtd = `root:
+const expectedDtd = `root:
     item: '&writer;&copyright;'
 `
 
@@ -336,6 +336,8 @@ var xmlScenarios = []formatScenario{
 
 func testXMLScenario(t *testing.T, s formatScenario) {
 	switch s.scenarioType {
+	case "", "decode":
+		test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content", false, false, false), NewYamlEncoder(4, false, true, true)), s.description)
 	case "encode":
 		test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewXMLEncoder(2, "+", "+content")), s.description)
 	case "roundtrip":
@@ -344,8 +346,9 @@ func testXMLScenario(t *testing.T, s formatScenario) {
 		test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content", false, true, false), NewYamlEncoder(2, false, true, true)), s.description)
 	case "decode-raw-token":
 		test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content", false, true, true), NewYamlEncoder(2, false, true, true)), s.description)
+
 	default:
-		test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewXMLDecoder("+", "+content", false, false, false), NewYamlEncoder(4, false, true, true)), s.description)
+		panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
 	}
 }
 
@@ -356,6 +359,8 @@ func documentXMLScenario(t *testing.T, w *bufio.Writer, i interface{}) {
 		return
 	}
 	switch s.scenarioType {
+	case "", "decode":
+		documentXMLDecodeScenario(w, s)
 	case "encode":
 		documentXMLEncodeScenario(w, s)
 	case "roundtrip":
@@ -364,10 +369,10 @@ func documentXMLScenario(t *testing.T, w *bufio.Writer, i interface{}) {
 		documentXMLDecodeKeepNsScenario(w, s)
 	case "decode-raw-token":
 		documentXMLDecodeKeepNsRawTokenScenario(w, s)
+
 	default:
-		documentXMLDecodeScenario(w, s)
+		panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
 	}
-
 }
 
 func documentXMLDecodeScenario(w *bufio.Writer, s formatScenario) {