diff --git a/src/parser.rs b/src/parser.rs index 8c1a7536..3173e453 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -187,6 +187,9 @@ pub fn read_lines( if !item.tags.is_empty() && !item.comment.is_empty() {} // blank if line.is_empty() { + if !(&item.snippet).is_empty() { + item.snippet.push_str(writer::LINE_SEPARATOR); + } } // tag else if line.starts_with('%') { diff --git a/src/writer.rs b/src/writer.rs index 163e2c37..1600706e 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -46,7 +46,7 @@ pub fn write(item: &Item) -> String { tags = item.tags, comment = item.comment, delimiter = DELIMITER, - snippet = &item.snippet, + snippet = &item.snippet.trim_end_matches(LINE_SEPARATOR), file_index = item.file_index, ) } diff --git a/tests/core.bash b/tests/core.bash index 865c1faf..e4d9dc91 100644 --- a/tests/core.bash +++ b/tests/core.bash @@ -3,6 +3,8 @@ source "${NAVI_HOME}/scripts/install" +NEWLINE_CHAR="\036" + PASSED=0 FAILED=0 SKIPPED=0 @@ -38,12 +40,17 @@ test::run() { "$@" && test::success || test::fail } +test::_escape() { + tr '\n' "$NEWLINE_CHAR" | sed -E "s/[\s$(printf "$NEWLINE_CHAR") ]+$//g" +} + test::equals() { local -r actual="$(cat)" local -r expected="$(echo "${1:-}")" + - local -r actual2="$(echo "$actual" | xargs | sed -E 's/\s/ /g')" - local -r expected2="$(echo "$expected" | xargs | sed -E 's/\s/ /g')" + local -r actual2="$(echo "$actual" | test::_escape)" + local -r expected2="$(echo "$expected" | test::_escape)" if [[ "$actual2" != "$expected2" ]]; then log::error "Expected '${expected}' but got '${actual}'" diff --git a/tests/no_prompt_cheats/cases.cheat b/tests/no_prompt_cheats/cases.cheat index 0b341a1f..8ecd64aa 100644 --- a/tests/no_prompt_cheats/cases.cheat +++ b/tests/no_prompt_cheats/cases.cheat @@ -8,14 +8,23 @@ echo "foo" # map -> "_foo_" echo "" +# expand -> "foo" +echo "" + # duplicated lines -> "foo\nlorem ipsum\nlorem ipsum\nbaz" echo foo echo lorem ipsum echo lorem ipsum echo baz -# expand -> "foo" -echo "" +# empty line -> "foo\n\n\nbar" +echo "$(cat < "172.17.0.2" echo "8.8.8.8 via 172.17.0.1 dev eth0 src 172.17.0.2" | sed -E 's/.*src ([0-9.]+).*/\1/p' | head -n1 diff --git a/tests/run b/tests/run index b8e801b0..7f49e2fa 100755 --- a/tests/run +++ b/tests/run @@ -36,7 +36,7 @@ _get_all_tests() { cat "${TEST_CHEAT_PATH}/cases.cheat" \ | grep '^#' \ | grep ' ->' \ - | sed 's/\\n/ /g' \ + | sed 's/\\n/'"$(printf "$NEWLINE_CHAR")"'/g' \ | sed -E 's/# (.*) -> "(.*)"/\1|\2/g' } @@ -144,7 +144,7 @@ IFS=$'\n' for i in $(_get_tests "$filter"); do IFS="$ifs" query="$(echo "$i" | cut -d'|' -f1)" - expected="$(echo "$i" | cut -d'|' -f2)" + expected="$(echo "$i" | tr "$NEWLINE_CHAR" '\n' | cut -d'|' -f2)" test::run "$query" _navi_cases_test "$query" "$expected" done