mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-27 00:47:56 +00:00
Added properties round trip test
This commit is contained in:
parent
a5ab9a8a0a
commit
e0d6b45651
@ -1,8 +1,8 @@
|
||||
#!/bin/bash
|
||||
|
||||
setUp() {
|
||||
rm test*.yml || true
|
||||
rm .xyz -f || true
|
||||
rm test*.yml 2>/dev/null || true
|
||||
rm .xyz 2>/dev/null || true
|
||||
}
|
||||
|
||||
testBasicEvalRoundTrip() {
|
||||
|
@ -1,7 +1,26 @@
|
||||
#!/bin/bash
|
||||
|
||||
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() {
|
||||
|
@ -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(&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(&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
|
||||
}
|
||||
|
||||
func (dec *propertiesDecoder) processComment(c string) string {
|
||||
if c == "" {
|
||||
return ""
|
||||
}
|
||||
return "# " + c
|
||||
}
|
||||
|
||||
func (dec *propertiesDecoder) applyProperty(properties *properties.Properties, context Context, key string) error {
|
||||
value, _ := properties.Get(key)
|
||||
path := parsePropKey(key)
|
||||
@ -47,7 +54,7 @@ func (dec *propertiesDecoder) applyProperty(properties *properties.Properties, c
|
||||
Value: value,
|
||||
Tag: "!!str",
|
||||
Kind: yaml.ScalarNode,
|
||||
LineComment: properties.GetComment(key),
|
||||
LineComment: dec.processComment(properties.GetComment(key)),
|
||||
}
|
||||
|
||||
rhsNode.Tag = guessTagFromCustomType(rhsNode)
|
||||
|
@ -119,3 +119,28 @@ person:
|
||||
- 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
|
||||
`
|
||||
|
||||
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:
|
||||
name: Mike # comments on values appear
|
||||
pets:
|
||||
@ -70,12 +78,18 @@ var propertyScenarios = []formatScenario{
|
||||
expected: expectedPropertiesWithEmptyMapsAndArrays,
|
||||
},
|
||||
{
|
||||
skipDoc: true,
|
||||
description: "Decode properties",
|
||||
input: expectedProperties,
|
||||
expected: expectedDecodedYaml,
|
||||
scenarioType: "decode",
|
||||
},
|
||||
{
|
||||
description: "Roundtrip",
|
||||
input: expectedProperties,
|
||||
expression: `.person.pets.0 = "dog"`,
|
||||
expected: expectedUpdatedProperties,
|
||||
scenarioType: "roundtrip",
|
||||
},
|
||||
}
|
||||
|
||||
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())))
|
||||
}
|
||||
|
||||
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{}) {
|
||||
s := i.(formatScenario)
|
||||
if s.scenarioType == "decode" {
|
||||
documentDecodePropertyScenario(w, s)
|
||||
} else if s.scenarioType == "roundtrip" {
|
||||
documentRoundTripPropertyScenario(w, s)
|
||||
} else {
|
||||
documentEncodePropertyScenario(w, s)
|
||||
}
|
||||
@ -142,6 +183,8 @@ func TestPropertyScenarios(t *testing.T) {
|
||||
for _, s := range propertyScenarios {
|
||||
if s.scenarioType == "decode" {
|
||||
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 {
|
||||
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewPropertiesEncoder(), NewYamlDecoder()), s.description)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user