From f58c78eaf21910cd6c37e93f312c94e0fe031606 Mon Sep 17 00:00:00 2001 From: Aki Arvio Date: Thu, 15 Jan 2026 08:00:52 +0200 Subject: [PATCH 1/2] Improve notify_message output to print messages literally - Use similar printf formating for both text and TAP formats. Prevent printf from interpreting `%` as format placeholders and backslash escape sequences, preserving exact failure messages. - Add shellcheck disable 2329 (same reasoning as 2317). This ensures notify_message prints messages literally, avoiding misleading or mangled failure output. --- bash_unit | 6 +++--- tests/test_cli.sh | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/bash_unit b/bash_unit index 9318a02..760578c 100755 --- a/bash_unit +++ b/bash_unit @@ -17,6 +17,7 @@ # https://github.com/bash-unit/bash_unit # shellcheck disable=2317 # Ignore unreachable - most function are not called. +# shellcheck disable=2329 # Ignore unreachable - most function are not called. # shellcheck disable=2155 # Ignore Declare and assign separately # spellchecker: ignore NOCOLOR SHUF @@ -413,8 +414,7 @@ text_format() { } notify_message() { local message="$1" - # shellcheck disable=SC2059 - [[ -z "$message" ]] || printf -- "$message\n" + [[ -z "$message" ]] || printf -- "%s\n" "$message" } notify_stdout() { @@ -468,7 +468,7 @@ tap_format() { } notify_message() { local message="$1" - [[ -z "$message" ]] || printf -- "%b\n" "$message" | "$SED" -u -e 's/^/# /' + [[ -z "$message" ]] || printf -- "%s\n" "$message" | "$SED" -u -e 's/^/# /' } notify_stdout() { "$SED" 's:^:# out> :' | color "$GREEN" diff --git a/tests/test_cli.sh b/tests/test_cli.sh index 5959348..70a80ba 100644 --- a/tests/test_cli.sh +++ b/tests/test_cli.sh @@ -204,6 +204,30 @@ test_one_test_should_stop_when_assert_fails() { "$($BASH_UNIT <(echo 'test_fail() { echo "before failure" >&2 ; assert false ; echo "after failure" >&2 ; }') 2>&1 >/dev/null)" } +test_notify_message_handles_percent_signs_as_literal_text() { + # Test that printf in notify_message doesn't interpret % as placeholder + bash_unit_output=$($BASH_UNIT <(echo 'test_percent() { fail "Expected 100% but got 50%" ; }') 2>&1 | "$GREP" "Expected 100% but got 50%") + assert_equals "Expected 100% but got 50%" "$bash_unit_output" +} + +test_notify_message_handles_percent_signs_as_literal_text_tap_format() { + # Test that printf in notify_message doesn't interpret % as placeholder + bash_unit_output=$($BASH_UNIT -f tap <(echo 'test_percent() { fail "Expected 100% but got 50%" ; }') 2>&1 | "$GREP" "Expected 100% but got 50%") + assert_equals "# Expected 100% but got 50%" "$bash_unit_output" +} + +test_notify_message_handles_unicode_escape_sequences_as_literal_text() { + # Test that printf in notify_message doesn't interpret escape sequences + bash_unit_output=$($BASH_UNIT <(printf '%s\n' 'test_escape() { fail "Symbol: \u2713\nshould be literal" ; }') 2>&1 | "$GREP" 'Symbol:') + assert_matches '\\u2713\\nshould' "$bash_unit_output" +} + +test_notify_message_handles_unicode_escape_sequences_as_literal_text_tap_format() { + # Test that printf in notify_message doesn't interpret escape sequences + bash_unit_output=$($BASH_UNIT -f tap <(printf '%s\n' 'test_escape() { fail "Symbol: \u2713\nshould be literal" ; }') 2>&1 | "$GREP" 'Symbol:') + assert_matches '\\u2713\\nshould' "$bash_unit_output" +} + setup() { # fake basic unix commands bash_unit relies on so that # we ensure bash_unit keeps working when people fake From d312f3f257caf428eec82351fffb07790feee55d Mon Sep 17 00:00:00 2001 From: Aki Arvio Date: Sun, 18 Jan 2026 14:25:52 +0200 Subject: [PATCH 2/2] Handle escape sequences in notify_message - To keep it more proper looking handle escape sequences are before. Just ensure % are not interpreted as placeholders. --- bash_unit | 4 ++-- tests/test_cli.sh | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/bash_unit b/bash_unit index 760578c..e9e2288 100755 --- a/bash_unit +++ b/bash_unit @@ -414,7 +414,7 @@ text_format() { } notify_message() { local message="$1" - [[ -z "$message" ]] || printf -- "%s\n" "$message" + [[ -z "$message" ]] || printf -- "%b\n" "$message" } notify_stdout() { @@ -468,7 +468,7 @@ tap_format() { } notify_message() { local message="$1" - [[ -z "$message" ]] || printf -- "%s\n" "$message" | "$SED" -u -e 's/^/# /' + [[ -z "$message" ]] || printf -- "%b\n" "$message" | "$SED" -u -e 's/^/# /' } notify_stdout() { "$SED" 's:^:# out> :' | color "$GREEN" diff --git a/tests/test_cli.sh b/tests/test_cli.sh index 70a80ba..96264aa 100644 --- a/tests/test_cli.sh +++ b/tests/test_cli.sh @@ -216,16 +216,16 @@ test_notify_message_handles_percent_signs_as_literal_text_tap_format() { assert_equals "# Expected 100% but got 50%" "$bash_unit_output" } -test_notify_message_handles_unicode_escape_sequences_as_literal_text() { - # Test that printf in notify_message doesn't interpret escape sequences - bash_unit_output=$($BASH_UNIT <(printf '%s\n' 'test_escape() { fail "Symbol: \u2713\nshould be literal" ; }') 2>&1 | "$GREP" 'Symbol:') - assert_matches '\\u2713\\nshould' "$bash_unit_output" +test_notify_message_renders_unicode_characters_correctly() { + # Test that notify_message properly interprets Unicode escape sequences and newlines + bash_unit_output=$($BASH_UNIT <(printf '%s\n' 'test_escape() { fail "Symbol: \u2713\nMultiline message" ; }') 2>&1 || : ) + assert_matches "Symbol: ✓"$'\n'"Multiline message" "$bash_unit_output" } -test_notify_message_handles_unicode_escape_sequences_as_literal_text_tap_format() { - # Test that printf in notify_message doesn't interpret escape sequences - bash_unit_output=$($BASH_UNIT -f tap <(printf '%s\n' 'test_escape() { fail "Symbol: \u2713\nshould be literal" ; }') 2>&1 | "$GREP" 'Symbol:') - assert_matches '\\u2713\\nshould' "$bash_unit_output" +test_notify_message_renders_unicode_characters_correctly_tap_format() { + # Test that notify_message properly interprets Unicode escape sequences and newlines in TAP format + bash_unit_output=$($BASH_UNIT -f tap <(printf '%s\n' 'test_escape() { fail "Symbol: \u2713\nMultiline message" ; }') 2>&1 || : ) + assert_matches "# Symbol: ✓"$'\n'"# Multiline message" "$bash_unit_output" } setup() {