Added explode test

This commit is contained in:
Mike Farah 2020-02-05 15:08:13 +11:00
parent dc13fa99f7
commit b7dd3e8e0a
2 changed files with 50 additions and 2 deletions

View File

@ -282,6 +282,47 @@ func TestReadMergeAnchorsOriginalCmd(t *testing.T) {
test.AssertResult(t, "original", result.Output)
}
func TestReadMergeAnchorsExplodeJsonCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -j ../examples/merge-anchor.yaml")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `{"bar":{"b":2,"c":"oldbar","thing":"coconut"},"foo":{"a":"original","thing":"coolasdf","thirsty":"yep"},"foobar":{"a":"original","c":3,"thing":"ice","thirsty":"yep","thirty":"well beyond"},"foobarList":{"a":"original","b":2,"c":"newbar","thing":"coconut","thirsty":"yep"}}
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadMergeAnchorsExplodeCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -X ../examples/merge-anchor.yaml")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `foo:
a: original
thing: coolasdf
thirsty: yep
bar:
b: 2
thing: coconut
c: oldbar
foobarList:
c: newbar
b: 2
thing: coconut
a: original
thirsty: yep
foobar:
thirty: well beyond
thing: ice
c: 3
a: original
thirsty: yep
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadMergeAnchorsOverrideCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read ../examples/merge-anchor.yaml foobar.thing")

View File

@ -103,6 +103,7 @@ func writeString(writer io.Writer, txt string) error {
}
func explode(matchingNodes []*yqlib.NodeContext) error {
for _, nodeContext := range matchingNodes {
var targetNode = yaml.Node{Kind: yaml.MappingNode}
explodedNodes, errorRetrieving := lib.Get(nodeContext.Node, "**")
@ -112,7 +113,10 @@ func explode(matchingNodes []*yqlib.NodeContext) error {
for _, matchingNode := range explodedNodes {
mergePath := lib.MergePathStackToString(matchingNode.PathStack, appendFlag)
updateCommand := yqlib.UpdateCommand{Command: "update", Path: mergePath, Value: matchingNode.Node, Overwrite: overwriteFlag}
lib.Update(&targetNode, updateCommand, true)
errorUpdating := lib.Update(&targetNode, updateCommand, true)
if errorUpdating != nil {
return errorUpdating
}
}
nodeContext.Node = &targetNode
}
@ -126,7 +130,10 @@ func printResults(matchingNodes []*yqlib.NodeContext, writer io.Writer) error {
//always explode anchors when printing json
if explodeAnchors || outputToJSON {
explode(matchingNodes)
errorExploding := explode(matchingNodes)
if errorExploding != nil {
return errorExploding
}
}
bufferedWriter := bufio.NewWriter(writer)