From 6ec8386f9e900d086cb95997578e6dd74b8cad1a Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 30 Jan 2020 16:32:28 +1100 Subject: [PATCH] Fixed bad yaml handling --- cmd/commands_test.go | 14 ++++++++++++++ cmd/utils.go | 7 +++++++ examples/bad.yaml | 8 ++++++++ 3 files changed, 29 insertions(+) create mode 100644 examples/bad.yaml diff --git a/cmd/commands_test.go b/cmd/commands_test.go index fdbd4acd..1d96a286 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -539,6 +539,20 @@ func TestReadCmd_Verbose(t *testing.T) { // test.AssertResult(t, "2\n", result.Output) // } +func TestReadBadDataCmd(t *testing.T) { + content := `[!Whatever]` + 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("Expected command to fail due to invalid path") + } + expectedOutput := `yaml: line 1: did not find expected ',' or ']'` + test.AssertResult(t, expectedOutput, result.Error.Error()) +} + func TestReadSplatPrefixCmd(t *testing.T) { content := `a: 2 b: diff --git a/cmd/utils.go b/cmd/utils.go index ebe83f5b..590f49e1 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -25,12 +25,19 @@ func readYamlFile(filename string, path string, updateAll bool, docIndexInt int) if errorReading == io.EOF { return handleEOF(updateAll, docIndexInt, currentIndex) + } else if errorReading != nil { + return errorReading } + var errorParsing error matchingNodes, errorParsing = appendDocument(matchingNodes, dataBucket, path, updateAll, docIndexInt, currentIndex) if errorParsing != nil { return errorParsing } + if !updateAll && currentIndex == docIndexInt { + log.Debug("all done") + return nil + } currentIndex = currentIndex + 1 } }) diff --git a/examples/bad.yaml b/examples/bad.yaml new file mode 100644 index 00000000..427823fc --- /dev/null +++ b/examples/bad.yaml @@ -0,0 +1,8 @@ +b: + d: be gone + c: 2 + e: + - name: Billy Bob # comment over here + +--- +[123123 \ No newline at end of file