Fixed bad yaml handling

This commit is contained in:
Mike Farah 2020-01-30 16:32:28 +11:00
parent 4dbe3636c2
commit 6ec8386f9e
3 changed files with 29 additions and 0 deletions

View File

@ -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:

View File

@ -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
}
})

8
examples/bad.yaml Normal file
View File

@ -0,0 +1,8 @@
b:
d: be gone
c: 2
e:
- name: Billy Bob # comment over here
---
[123123