Fixed merge new array

This commit is contained in:
Mike Farah 2020-01-06 10:12:30 +13:00
parent 1f7f1b0def
commit 690da9ee74
5 changed files with 35 additions and 11 deletions

View File

@ -1,3 +1,8 @@
# Update doco / notes
- --autocreate=false to turn off default flags
- add comments to test yaml to ensure they are kept on updating.
- update built in command notes to includes quotes around path args.
# New Features
- Keeps comments and formatting (e.g. inline arrays)!
- Handles anchors!

View File

@ -807,6 +807,18 @@ func TestNewCmd(t *testing.T) {
test.AssertResult(t, expectedOutput, result.Output)
}
func TestNewArrayCmd(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "new b[0] 3")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `b:
- 3
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestNewCmd_Error(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "new b.c")
@ -1334,7 +1346,7 @@ func TestMergeOverwriteCmd(t *testing.T) {
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: other
expectedOutput := `a: other # better than the original
b: [3, 4]
c:
test: 1
@ -1348,7 +1360,7 @@ func TestMergeAppendCmd(t *testing.T) {
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: simple
expectedOutput := `a: simple # just the best
b: [1, 2, 3, 4]
c:
test: 1
@ -1366,26 +1378,27 @@ func TestMergeArraysCmd(t *testing.T) {
test.AssertResult(t, expectedOutput, result.Output)
}
func xTestMergeCmd_Multi(t *testing.T) {
func TestMergeCmd_Multi(t *testing.T) {
cmd := getRootCommand()
result := test.RunCmd(cmd, "merge -d1 examples/multiple_docs_small.yaml examples/data2.yaml")
result := test.RunCmd(cmd, "merge -d1 examples/multiple_docs_small.yaml examples/data1.yaml")
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `a: Easy! as one two three
---
a: other
another:
document: here
a: simple # just the best
b:
- 3
- 4
- 1
- 2
c:
test: 1
---
- 1
- 2`
test.AssertResult(t, expectedOutput, strings.Trim(result.Output, "\n "))
- 2
`
test.AssertResult(t, expectedOutput, result.Output)
}
func xTestMergeYamlMultiAllCmd(t *testing.T) {

View File

@ -1,4 +1,4 @@
a: simple
a: simple # just the best
b: [1, 2]
c:
test: 1

View File

@ -1,4 +1,4 @@
a: other
a: other # better than the original
b: [3, 4]
c:
toast: leave

View File

@ -234,7 +234,13 @@ func (n *navigator) recurseArray(value *yaml.Node, head string, tail []string, p
if err != nil {
return errors.Wrapf(err, "Error parsing array index '%v' for '%v'", head, PathStackToString(pathStack))
}
for int64(len(value.Content)) <= index {
value.Content = append(value.Content, &yaml.Node{Kind: guessKind(head, tail, 0)})
}
if index >= int64(len(value.Content)) {
log.Debug("index longer than array length, aborting!")
return nil
}
value.Content[index] = n.getOrReplace(value.Content[index], guessKind(head, tail, value.Content[index].Kind))