Fixed EOF bug when processing empty files

This commit is contained in:
Mike Farah 2021-07-16 20:56:22 +10:00
parent 171ca2e053
commit a13617407e
3 changed files with 24 additions and 3 deletions

View File

@ -1,2 +0,0 @@
# a: apple
# b: cat

View File

@ -15,6 +15,12 @@ func readStream(filename string) (io.Reader, bool, error) {
reader := bufio.NewReader(os.Stdin)
seperatorBytes, err := reader.Peek(3)
if err == io.EOF {
// EOF are handled else where..
return reader, false, nil
}
return reader, string(seperatorBytes) == "---", err
} else {
// ignore CWE-22 gosec issue - that's more targetted for http based apps that run in a public directory,
@ -25,7 +31,10 @@ func readStream(filename string) (io.Reader, bool, error) {
}
seperatorBytes := make([]byte, 3)
_, err = reader.Read(seperatorBytes)
if err != nil {
if err == io.EOF {
// EOF are handled else where..
return reader, false, nil
} else if err != nil {
return nil, false, err
}
_, err = reader.Seek(0, 0)

View File

@ -113,6 +113,20 @@ if [[ $X != $expected ]]; then
exit 1
fi
# handle empty files
./yq e '.' examples/empty.yaml
if [[ $? != 0 ]]; then
echo "Expected no error when processing empty file but got one"
exit 1
fi
cat examples/empty.yaml | ./yq e '.' -
if [[ $? != 0 ]]; then
echo "Expected no error when processing empty stdin but got one"
exit 1
fi
echo "--success"
set -e