Fixed explode for aliases to scalars

This commit is contained in:
Mike Farah 2020-02-07 10:09:20 +11:00
parent 63313ebb02
commit d40ad9649d
2 changed files with 39 additions and 3 deletions

View File

@ -307,6 +307,21 @@ foobar:
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }
func TestReadMergeAnchorsExplodeSimpleValueCmd(t *testing.T) {
content := `value: &value-pointer the value
pointer: *value-pointer`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("read -X %s pointer", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `the value`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadMergeAnchorsExplodeCmd(t *testing.T) { func TestReadMergeAnchorsExplodeCmd(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := test.RunCmd(cmd, "read -X ../examples/merge-anchor.yaml") result := test.RunCmd(cmd, "read -X ../examples/merge-anchor.yaml")
@ -337,6 +352,21 @@ foobar:
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }
func TestReadMergeAnchorsExplodeDeepCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "read -X ../examples/merge-anchor.yaml foobar")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `thirty: well beyond
thing: ice
c: 3
a: original
thirsty: yep
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestReadMergeAnchorsOverrideCmd(t *testing.T) { func TestReadMergeAnchorsOverrideCmd(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := test.RunCmd(cmd, "read ../examples/merge-anchor.yaml foobar.thing") result := test.RunCmd(cmd, "read ../examples/merge-anchor.yaml foobar.thing")

View File

@ -63,7 +63,7 @@ func (n *navigator) getOrReplace(original *yaml.Node, expectedKind yaml.Kind) *y
} }
func (n *navigator) recurse(value *yaml.Node, head string, tail []string, pathStack []interface{}) error { func (n *navigator) recurse(value *yaml.Node, head string, tail []string, pathStack []interface{}) error {
log.Debug("recursing, processing %v", head) log.Debug("recursing, processing %v, pathStack %v", head, pathStackToString(pathStack))
switch value.Kind { switch value.Kind {
case yaml.MappingNode: case yaml.MappingNode:
log.Debug("its a map with %v entries", len(value.Content)/2) log.Debug("its a map with %v entries", len(value.Content)/2)
@ -83,9 +83,15 @@ func (n *navigator) recurse(value *yaml.Node, head string, tail []string, pathSt
log.Debug("its an alias!") log.Debug("its an alias!")
DebugNode(value.Alias) DebugNode(value.Alias)
if n.navigationStrategy.FollowAlias(NewNodeContext(value, head, tail, pathStack)) { if n.navigationStrategy.FollowAlias(NewNodeContext(value, head, tail, pathStack)) {
if value.Alias.Kind == yaml.ScalarNode {
log.Debug("alias to a scalar")
return n.navigationStrategy.Visit(NewNodeContext(value.Alias, head, tail, pathStack))
} else {
log.Debug("following the alias") log.Debug("following the alias")
return n.recurse(value.Alias, head, tail, pathStack) return n.recurse(value.Alias, head, tail, pathStack)
} }
}
return nil return nil
default: default:
return nil return nil