Skip to content

Commit 035e99c

Browse files
Merge pull request #75 from jakub-bochenski/patch-1
feat: Print stderr in assert_success/failure
2 parents b93143a + c5ea35f commit 035e99c

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

src/assert_failure.bash

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ assert_failure() {
6161

6262
(( $# > 0 )) && local -r expected="$1"
6363
if (( status == 0 )); then
64-
batslib_print_kv_single_or_multi 6 'output' "$output" \
64+
{ local -ir width=6
65+
batslib_print_kv_single_or_multi "$width" 'output' "$output"
66+
if [[ -n "$stderr" ]]; then
67+
batslib_print_kv_single_or_multi "$width" 'stderr' "$stderr"
68+
fi
69+
} \
6570
| batslib_decorate 'command succeeded, but it was expected to fail' \
6671
| fail
6772
elif (( $# > 0 )) && (( status != expected )); then
@@ -71,6 +76,9 @@ assert_failure() {
7176
'actual' "$status"
7277
batslib_print_kv_single_or_multi "$width" \
7378
'output' "$output"
79+
if [[ -n "$stderr" ]]; then
80+
batslib_print_kv_single_or_multi "$width" 'stderr' "$stderr"
81+
fi
7482
} \
7583
| batslib_decorate 'command failed as expected, but status differs' \
7684
| fail

src/assert_success.bash

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ assert_success() {
3737
{ local -ir width=6
3838
batslib_print_kv_single "$width" 'status' "$status"
3939
batslib_print_kv_single_or_multi "$width" 'output' "$output"
40+
if [[ -n "$stderr" ]]; then
41+
batslib_print_kv_single_or_multi "$width" 'stderr' "$stderr"
42+
fi
4043
} \
4144
| batslib_decorate 'command failed' \
4245
| fail

test/assert_failure.bats

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ output : a
2121
ERR_MSG
2222
}
2323

24+
@test "assert_failure(): returns 1 and displays \`\$stderr' if it is set" {
25+
run --separate-stderr \
26+
bash -c 'echo "a"
27+
echo "b" >&2
28+
exit 0'
29+
run assert_failure
30+
31+
echo "Stderr: $stderr" >&3
32+
33+
assert_test_fail <<'ERR_MSG'
34+
35+
-- command succeeded, but it was expected to fail --
36+
output : a
37+
stderr : b
38+
--
39+
ERR_MSG
40+
}
41+
2442
@test "assert_failure(): displays \`\$output' in multi-line format if it is longer then one line" {
2543
run bash -c 'printf "a 0\na 1"
2644
exit 0'
@@ -73,3 +91,4 @@ output (2 lines):
7391
--
7492
ERR_MSG
7593
}
94+

test/assert_success.bats

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,20 @@ output (2 lines):
3838
--
3939
ERR_MSG
4040
}
41+
42+
@test "assert_success(): displays \`\$stderr' if it is set" {
43+
run --separate-stderr \
44+
bash -c 'echo "a"
45+
echo "b" >&2
46+
exit 1'
47+
run assert_success
48+
49+
assert_test_fail <<'ERR_MSG'
50+
51+
-- command failed --
52+
status : 1
53+
output : a
54+
stderr : b
55+
--
56+
ERR_MSG
57+
}

0 commit comments

Comments
 (0)