From f52de576526adcbcf4c18651af5eea301ba420fa Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 3 Feb 2020 09:15:16 +1100 Subject: [PATCH] Don't fail when reading an empty file --- cmd/commands_test.go | 24 +++++++++++++++++++++--- cmd/utils.go | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index 73d8dbd1..7be6ebf4 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -342,6 +342,20 @@ func TestReadCmd_ArrayYaml(t *testing.T) { test.AssertResult(t, "false", result.Output) } +func TestReadEmptyContentCmd(t *testing.T) { + content := `` + filename := test.WriteTempYamlFile(content) + defer test.RemoveTempYamlFile(filename) + + cmd := getRootCommand() + result := test.RunCmd(cmd, fmt.Sprintf("read %s", filename)) + if result.Error != nil { + t.Error(result.Error) + } + expectedOutput := `` + test.AssertResult(t, expectedOutput, result.Output) +} + func TestReadPrettyPrintCmd(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "read -P ../examples/sample.json") @@ -1636,9 +1650,13 @@ c: test.AssertResult(t, expectedOutput, result.Output) } -func TestMergeDontAllowEmptyCmd(t *testing.T) { +func TestMergeAllowEmptyMergeCmd(t *testing.T) { cmd := getRootCommand() result := test.RunCmd(cmd, "merge ../examples/data1.yaml ../examples/empty.yaml") - expectedOutput := `Could not process document index 0 as there are only 0 document(s)` - test.AssertResult(t, expectedOutput, result.Error.Error()) + expectedOutput := `a: simple # just the best +b: [1, 2] +c: + test: 1 +` + test.AssertResult(t, expectedOutput, result.Output) } diff --git a/cmd/utils.go b/cmd/utils.go index 1b51992d..aa6de02f 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -46,7 +46,7 @@ func readYamlFile(filename string, path string, updateAll bool, docIndexInt int) func handleEOF(updateAll bool, docIndexInt int, currentIndex int) error { log.Debugf("done %v / %v", currentIndex, docIndexInt) - if !updateAll && currentIndex <= docIndexInt { + if !updateAll && currentIndex <= docIndexInt && docIndexInt != 0 { return fmt.Errorf("Could not process document index %v as there are only %v document(s)", docIndex, currentIndex) } return nil