From 3b91ad576431d48f3be01ca6af67e0d45d123538 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Tue, 20 Jul 2021 11:01:56 +1000 Subject: [PATCH] Handle leading comment with no new-line --- acceptance_tests/empty.sh | 20 ++++++++++++++++++++ pkg/yqlib/printer.go | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/acceptance_tests/empty.sh b/acceptance_tests/empty.sh index a88a6f4b..d7f344df 100755 --- a/acceptance_tests/empty.sh +++ b/acceptance_tests/empty.sh @@ -8,7 +8,27 @@ EOL testEmptyEval() { X=$(./yq e test.yml) + expected=$(cat test.yml) assertEquals 0 $? + assertEquals "$expected" "$X" +} + +testEmptyEvalNoNewLine() { + echo -n "#comment" >test.yml + X=$(./yq e test.yml) + expected=$(cat test.yml) + assertEquals 0 $? + assertEquals "$expected" "$X" +} + +testEmptyEvalNoNewLineWithExpression() { + echo -n "# comment" >test.yml + X=$(./yq e '.apple = "tree"' test.yml) + read -r -d '' expected << EOM +# comment +apple: tree +EOM + assertEquals "$expected" "$X" } testEmptyEvalPipe() { diff --git a/pkg/yqlib/printer.go b/pkg/yqlib/printer.go index 64c477d6..89a0bec9 100644 --- a/pkg/yqlib/printer.go +++ b/pkg/yqlib/printer.go @@ -147,6 +147,12 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error { } if errReading == io.EOF { + if readline != "" { + // the last comment we read didn't have a new line, put one in + if err := p.writeString(bufferedWriter, "\n"); err != nil { + return err + } + } break } }