Skip to content

Commit 16ac92e

Browse files
Aki Arviopgrange
authored andcommitted
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 f19cab8 commit 16ac92e

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

@@ -420,8 +421,7 @@ text_format() {
420421
}
421422
notify_message() {
422423
local message="$1"
423-
# shellcheck disable=SC2059
424-
[[ -z "$message" ]] || printf -- "$message\n"
424+
[[ -z "$message" ]] || printf -- "%s\n" "$message"
425425
}
426426

427427
notify_stdout() {
@@ -475,7 +475,7 @@ tap_format() {
475475
}
476476
notify_message() {
477477
local message="$1"
478-
[[ -z "$message" ]] || printf -- "%b\n" "$message" | "$SED" -u -e 's/^/# /'
478+
[[ -z "$message" ]] || printf -- "%s\n" "$message" | "$SED" -u -e 's/^/# /'
479479
}
480480
notify_stdout() {
481481
"$SED" 's:^:# out> :' | color "$GREEN"

tests/test_cli.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,30 @@ test_failure_status_printed_after_test_name() {
209209
"$($BASH_UNIT <(echo 'test_fail_should_print_failure() { false; }'))"
210210
}
211211

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

0 commit comments

Comments
 (0)