From ceb76e5c1743b62021d09ce408a164fb9f0fc523 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 21 Feb 2020 11:34:26 +1100 Subject: [PATCH] Fixed trailing empty docs --- cmd/commands_test.go | 24 ++++++++++++++++++++++++ cmd/utils.go | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index d526713..8729015 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -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 diff --git a/cmd/utils.go b/cmd/utils.go index 5a5800a..257edd9 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -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 {