Skip to content

Commit b59db40

Browse files
committed
assertions: Fix failure output handling
The `fail` and `assert_line_matches` calls to `printf` were interpolating the `output` string into the format string argument, which meant `printf` got confused when those strings contained `%` characters. This tests for and fixes the bug in each location, and also sets all `printf` format strings to use single-quotes.
1 parent 16ff004 commit b59db40

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

tests/assertions.bash

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ __return_from_bats_assertion() {
2222

2323
fail() {
2424
set +o functrace
25-
printf "STATUS: ${status}\nOUTPUT:\n${output}\n" >&2
25+
printf 'STATUS: %s\nOUTPUT:\n%s\n' "$status" "$output" >&2
2626
__return_from_bats_assertion 1
2727
}
2828

@@ -33,7 +33,7 @@ assert_equal() {
3333
local label="$3"
3434

3535
if [[ "$expected" != "$actual" ]]; then
36-
printf "%s not equal to expected value:\n %s\n %s\n" \
36+
printf '%s not equal to expected value:\n %s\n %s\n' \
3737
"$label" "expected: '$expected'" "actual: '$actual'" >&2
3838
__return_from_bats_assertion 1
3939
else
@@ -48,7 +48,7 @@ assert_matches() {
4848
local label="$3"
4949

5050
if [[ ! "$value" =~ $pattern ]]; then
51-
printf "%s does not match expected pattern:\n %s\n %s\n" \
51+
printf '%s does not match expected pattern:\n %s\n %s\n' \
5252
"$label" "pattern: '$pattern'" "value: '$value'" >&2
5353
__return_from_bats_assertion 1
5454
else
@@ -116,7 +116,7 @@ __assert_line() {
116116
fi
117117

118118
if ! "$assertion" "$constraint" "${lines[$lineno]}" "line $lineno"; then
119-
printf "OUTPUT:\n$output\n" >&2
119+
printf 'OUTPUT:\n%s\n' "$output" >&2
120120
__return_from_bats_assertion 1
121121
else
122122
__return_from_bats_assertion

tests/assertions.bats

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ check_expected_output() {
110110
'Hello, world!'
111111
}
112112

113+
@test "$SUITE: fail handles strings containing percentage signs" {
114+
expect_failure "echo '% not interpreted as a format spec'" \
115+
'fail' \
116+
'STATUS: 0' \
117+
'OUTPUT:' \
118+
'% not interpreted as a format spec'
119+
}
120+
113121
@test "$SUITE: assert_equal success" {
114122
expect_success "echo 'Hello, world!'" \
115123
'assert_equal "Hello, world!" "$output" "echo result"'
@@ -291,3 +299,13 @@ check_expected_output() {
291299
'OUTPUT:' \
292300
'Hello, world!'
293301
}
302+
303+
@test "$SUITE: assert_line_matches failure handles percent signs in output" {
304+
expect_failure "echo '% not interpreted as a format spec'" \
305+
"assert_line_matches 0 '% interpreted as a format spec'" \
306+
'line 0 does not match expected pattern:' \
307+
" pattern: '% interpreted as a format spec'" \
308+
" value: '% not interpreted as a format spec'" \
309+
'OUTPUT:' \
310+
'% not interpreted as a format spec'
311+
}

0 commit comments

Comments
 (0)