mirror of
https://github.com/mikefarah/yq.git
synced 2025-02-10 13:17:06 +00:00
Added properties round trip test
This commit is contained in:
parent
a5ab9a8a0a
commit
e0d6b45651
@ -1,8 +1,8 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
setUp() {
|
setUp() {
|
||||||
rm test*.yml || true
|
rm test*.yml 2>/dev/null || true
|
||||||
rm .xyz -f || true
|
rm .xyz 2>/dev/null || true
|
||||||
}
|
}
|
||||||
|
|
||||||
testBasicEvalRoundTrip() {
|
testBasicEvalRoundTrip() {
|
||||||
|
@ -1,7 +1,26 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
setUp() {
|
setUp() {
|
||||||
rm test*.yml || true
|
rm test*.yml 2>/dev/null || true
|
||||||
|
rm test*.properties 2>/dev/null || true
|
||||||
|
rm test*.xml 2>/dev/null || true
|
||||||
|
}
|
||||||
|
|
||||||
|
testInputProperties() {
|
||||||
|
cat >test.properties <<EOL
|
||||||
|
mike.things = hello
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
mike:
|
||||||
|
things: hello
|
||||||
|
EOM
|
||||||
|
|
||||||
|
X=$(./yq e -p=props test.properties)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
|
||||||
|
X=$(./yq ea -p=props test.properties)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
}
|
}
|
||||||
|
|
||||||
testInputXml() {
|
testInputXml() {
|
||||||
|
@ -63,7 +63,7 @@ yq -i '.stuff = "foo"' myfile.yml # update myfile.yml inplace
|
|||||||
}
|
}
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVarP(&outputFormat, "output-format", "o", "yaml", "[yaml|y|json|j|props|p|xml|x] output format type.")
|
rootCmd.PersistentFlags().StringVarP(&outputFormat, "output-format", "o", "yaml", "[yaml|y|json|j|props|p|xml|x] output format type.")
|
||||||
rootCmd.PersistentFlags().StringVarP(&inputFormat, "input-format", "p", "yaml", "[yaml|y|xml|x] parse format for input. Note that json is a subset of yaml.")
|
rootCmd.PersistentFlags().StringVarP(&inputFormat, "input-format", "p", "yaml", "[yaml|y|props|p|xml|x] parse format for input. Note that json is a subset of yaml.")
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVar(&xmlAttributePrefix, "xml-attribute-prefix", "+", "prefix for xml attributes")
|
rootCmd.PersistentFlags().StringVar(&xmlAttributePrefix, "xml-attribute-prefix", "+", "prefix for xml attributes")
|
||||||
rootCmd.PersistentFlags().StringVar(&xmlContentName, "xml-content-name", "+content", "name for xml content (if no attribute name is present).")
|
rootCmd.PersistentFlags().StringVar(&xmlContentName, "xml-content-name", "+content", "name for xml content (if no attribute name is present).")
|
||||||
|
@ -39,6 +39,13 @@ func parsePropKey(key string) []interface{} {
|
|||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (dec *propertiesDecoder) processComment(c string) string {
|
||||||
|
if c == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return "# " + c
|
||||||
|
}
|
||||||
|
|
||||||
func (dec *propertiesDecoder) applyProperty(properties *properties.Properties, context Context, key string) error {
|
func (dec *propertiesDecoder) applyProperty(properties *properties.Properties, context Context, key string) error {
|
||||||
value, _ := properties.Get(key)
|
value, _ := properties.Get(key)
|
||||||
path := parsePropKey(key)
|
path := parsePropKey(key)
|
||||||
@ -47,7 +54,7 @@ func (dec *propertiesDecoder) applyProperty(properties *properties.Properties, c
|
|||||||
Value: value,
|
Value: value,
|
||||||
Tag: "!!str",
|
Tag: "!!str",
|
||||||
Kind: yaml.ScalarNode,
|
Kind: yaml.ScalarNode,
|
||||||
LineComment: properties.GetComment(key),
|
LineComment: dec.processComment(properties.GetComment(key)),
|
||||||
}
|
}
|
||||||
|
|
||||||
rhsNode.Tag = guessTagFromCustomType(rhsNode)
|
rhsNode.Tag = guessTagFromCustomType(rhsNode)
|
||||||
|
@ -119,3 +119,28 @@ person:
|
|||||||
- pizza
|
- pizza
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Roundtrip
|
||||||
|
Given a sample.properties file of:
|
||||||
|
```properties
|
||||||
|
# comments on values appear
|
||||||
|
person.name = Mike
|
||||||
|
|
||||||
|
# comments on array values appear
|
||||||
|
person.pets.0 = cat
|
||||||
|
person.food.0 = pizza
|
||||||
|
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq -p=props -o=props '.person.pets.0 = "dog"' sample.properties
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```properties
|
||||||
|
# comments on values appear
|
||||||
|
person.name = Mike
|
||||||
|
|
||||||
|
# comments on array values appear
|
||||||
|
person.pets.0 = dog
|
||||||
|
person.food.0 = pizza
|
||||||
|
```
|
||||||
|
|
||||||
|
@ -26,6 +26,14 @@ person.pets.0 = cat
|
|||||||
person.food.0 = pizza
|
person.food.0 = pizza
|
||||||
`
|
`
|
||||||
|
|
||||||
|
var expectedUpdatedProperties = `# comments on values appear
|
||||||
|
person.name = Mike
|
||||||
|
|
||||||
|
# comments on array values appear
|
||||||
|
person.pets.0 = dog
|
||||||
|
person.food.0 = pizza
|
||||||
|
`
|
||||||
|
|
||||||
var expectedDecodedYaml = `person:
|
var expectedDecodedYaml = `person:
|
||||||
name: Mike # comments on values appear
|
name: Mike # comments on values appear
|
||||||
pets:
|
pets:
|
||||||
@ -70,12 +78,18 @@ var propertyScenarios = []formatScenario{
|
|||||||
expected: expectedPropertiesWithEmptyMapsAndArrays,
|
expected: expectedPropertiesWithEmptyMapsAndArrays,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
skipDoc: true,
|
|
||||||
description: "Decode properties",
|
description: "Decode properties",
|
||||||
input: expectedProperties,
|
input: expectedProperties,
|
||||||
expected: expectedDecodedYaml,
|
expected: expectedDecodedYaml,
|
||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "Roundtrip",
|
||||||
|
input: expectedProperties,
|
||||||
|
expression: `.person.pets.0 = "dog"`,
|
||||||
|
expected: expectedUpdatedProperties,
|
||||||
|
scenarioType: "roundtrip",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func documentEncodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
func documentEncodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
||||||
@ -128,10 +142,37 @@ func documentDecodePropertyScenario(w *bufio.Writer, s formatScenario) {
|
|||||||
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewYamlEncoder(s.indent, false, true, true), NewPropertiesDecoder())))
|
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewYamlEncoder(s.indent, false, true, true), NewPropertiesDecoder())))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func documentRoundTripPropertyScenario(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.properties file of:\n")
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```properties\n%v\n```\n", s.input))
|
||||||
|
|
||||||
|
writeOrPanic(w, "then\n")
|
||||||
|
|
||||||
|
expression := s.expression
|
||||||
|
if expression != "" {
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=props -o=props '%v' sample.properties\n```\n", expression))
|
||||||
|
} else {
|
||||||
|
writeOrPanic(w, "```bash\nyq -p=props -o=props sample.properties\n```\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
writeOrPanic(w, "will output\n")
|
||||||
|
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```properties\n%v```\n\n", processFormatScenario(s, NewPropertiesEncoder(), NewPropertiesDecoder())))
|
||||||
|
}
|
||||||
|
|
||||||
func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
func documentPropertyScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
||||||
s := i.(formatScenario)
|
s := i.(formatScenario)
|
||||||
if s.scenarioType == "decode" {
|
if s.scenarioType == "decode" {
|
||||||
documentDecodePropertyScenario(w, s)
|
documentDecodePropertyScenario(w, s)
|
||||||
|
} else if s.scenarioType == "roundtrip" {
|
||||||
|
documentRoundTripPropertyScenario(w, s)
|
||||||
} else {
|
} else {
|
||||||
documentEncodePropertyScenario(w, s)
|
documentEncodePropertyScenario(w, s)
|
||||||
}
|
}
|
||||||
@ -142,6 +183,8 @@ func TestPropertyScenarios(t *testing.T) {
|
|||||||
for _, s := range propertyScenarios {
|
for _, s := range propertyScenarios {
|
||||||
if s.scenarioType == "decode" {
|
if s.scenarioType == "decode" {
|
||||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlEncoder(2, false, true, true), NewPropertiesDecoder()), s.description)
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlEncoder(2, false, true, true), NewPropertiesDecoder()), s.description)
|
||||||
|
} else if s.scenarioType == "roundtrip" {
|
||||||
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesEncoder(), NewPropertiesDecoder()), s.description)
|
||||||
} else {
|
} else {
|
||||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesEncoder(), NewYamlDecoder()), s.description)
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesEncoder(), NewYamlDecoder()), s.description)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user