Don't fail when reading an empty file

This commit is contained in:
Mike Farah 2020-02-03 09:15:16 +11:00
parent b7554e6e76
commit f52de57652
2 changed files with 22 additions and 4 deletions

View File

@ -342,6 +342,20 @@ func TestReadCmd_ArrayYaml(t *testing.T) {
test.AssertResult(t, "false", result.Output) 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) { func TestReadPrettyPrintCmd(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := test.RunCmd(cmd, "read -P ../examples/sample.json") result := test.RunCmd(cmd, "read -P ../examples/sample.json")
@ -1636,9 +1650,13 @@ c:
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }
func TestMergeDontAllowEmptyCmd(t *testing.T) { func TestMergeAllowEmptyMergeCmd(t *testing.T) {
cmd := getRootCommand() cmd := getRootCommand()
result := test.RunCmd(cmd, "merge ../examples/data1.yaml ../examples/empty.yaml") 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)` expectedOutput := `a: simple # just the best
test.AssertResult(t, expectedOutput, result.Error.Error()) b: [1, 2]
c:
test: 1
`
test.AssertResult(t, expectedOutput, result.Output)
} }

View File

@ -46,7 +46,7 @@ func readYamlFile(filename string, path string, updateAll bool, docIndexInt int)
func handleEOF(updateAll bool, docIndexInt int, currentIndex int) error { func handleEOF(updateAll bool, docIndexInt int, currentIndex int) error {
log.Debugf("done %v / %v", currentIndex, docIndexInt) 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 fmt.Errorf("Could not process document index %v as there are only %v document(s)", docIndex, currentIndex)
} }
return nil return nil