Skip to content

Commit f58c78e

Browse files
author
Aki Arvio
committed
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.
1 parent 650b6da commit f58c78e

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

bash_unit

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# https://github.com/bash-unit/bash_unit
1818

1919
# shellcheck disable=2317 # Ignore unreachable - most function are not called.
20+
# shellcheck disable=2329 # Ignore unreachable - most function are not called.
2021
# shellcheck disable=2155 # Ignore Declare and assign separately
2122
# spellchecker: ignore NOCOLOR SHUF
2223

@@ -413,8 +414,7 @@ text_format() {
413414
}
414415
notify_message() {
415416
local message="$1"
416-
# shellcheck disable=SC2059
417-
[[ -z "$message" ]] || printf -- "$message\n"
417+
[[ -z "$message" ]] || printf -- "%s\n" "$message"
418418
}
419419

420420
notify_stdout() {
@@ -468,7 +468,7 @@ tap_format() {
468468
}
469469
notify_message() {
470470
local message="$1"
471-
[[ -z "$message" ]] || printf -- "%b\n" "$message" | "$SED" -u -e 's/^/# /'
471+
[[ -z "$message" ]] || printf -- "%s\n" "$message" | "$SED" -u -e 's/^/# /'
472472
}
473473
notify_stdout() {
474474
"$SED" 's:^:# out> :' | color "$GREEN"

tests/test_cli.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,30 @@ test_one_test_should_stop_when_assert_fails() {
204204
"$($BASH_UNIT <(echo 'test_fail() { echo "before failure" >&2 ; assert false ; echo "after failure" >&2 ; }') 2>&1 >/dev/null)"
205205
}
206206

207+
test_notify_message_handles_percent_signs_as_literal_text() {
208+
# Test that printf in notify_message doesn't interpret % as placeholder
209+
bash_unit_output=$($BASH_UNIT <(echo 'test_percent() { fail "Expected 100% but got 50%" ; }') 2>&1 | "$GREP" "Expected 100% but got 50%")
210+
assert_equals "Expected 100% but got 50%" "$bash_unit_output"
211+
}
212+
213+
test_notify_message_handles_percent_signs_as_literal_text_tap_format() {
214+
# Test that printf in notify_message doesn't interpret % as placeholder
215+
bash_unit_output=$($BASH_UNIT -f tap <(echo 'test_percent() { fail "Expected 100% but got 50%" ; }') 2>&1 | "$GREP" "Expected 100% but got 50%")
216+
assert_equals "# Expected 100% but got 50%" "$bash_unit_output"
217+
}
218+
219+
test_notify_message_handles_unicode_escape_sequences_as_literal_text() {
220+
# Test that printf in notify_message doesn't interpret escape sequences
221+
bash_unit_output=$($BASH_UNIT <(printf '%s\n' 'test_escape() { fail "Symbol: \u2713\nshould be literal" ; }') 2>&1 | "$GREP" 'Symbol:')
222+
assert_matches '\\u2713\\nshould' "$bash_unit_output"
223+
}
224+
225+
test_notify_message_handles_unicode_escape_sequences_as_literal_text_tap_format() {
226+
# Test that printf in notify_message doesn't interpret escape sequences
227+
bash_unit_output=$($BASH_UNIT -f tap <(printf '%s\n' 'test_escape() { fail "Symbol: \u2713\nshould be literal" ; }') 2>&1 | "$GREP" 'Symbol:')
228+
assert_matches '\\u2713\\nshould' "$bash_unit_output"
229+
}
230+
207231
setup() {
208232
# fake basic unix commands bash_unit relies on so that
209233
# we ensure bash_unit keeps working when people fake

0 commit comments

Comments
 (0)