Fixed trailing empty docs

This commit is contained in:
Mike Farah 2020-02-21 11:34:26 +11:00
parent 44322f0248
commit ceb76e5c17
2 changed files with 25 additions and 1 deletions

View File

@ -1264,6 +1264,30 @@ func TestWriteEmptyMultiDocCmd(t *testing.T) {
test.AssertResult(t, expectedOutput, result.Output)
}
func TestWriteTrailingEmptyMultiDocCmd(t *testing.T) {
content := `cat: frog
---
# empty
`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("write %s c 7", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := `cat: frog
c: 7
---
# empty
`
test.AssertResult(t, expectedOutput, result.Output)
}
func TestWriteFromFileCmd(t *testing.T) {
content := `b:
c: 3

View File

@ -226,7 +226,7 @@ func mapYamlDecoder(updateData updateDataFn, encoder yqlib.Encoder) yamlDecoderF
dataBucket = yaml.Node{Kind: yaml.DocumentNode, Content: make([]*yaml.Node, 1)}
child := yaml.Node{Kind: yaml.MappingNode}
dataBucket.Content[0] = &child
} else if isNullDocument(&dataBucket) {
} else if isNullDocument(&dataBucket) && (updateAll || docIndexInt == currentIndex) {
child := yaml.Node{Kind: yaml.MappingNode}
dataBucket.Content[0] = &child
} else if errorReading == io.EOF {