From e9d3143fdbaa0fb867cf4be7bcb7226a2c50324f Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Wed, 28 Dec 2011 23:50:49 -0600 Subject: [PATCH] Capture stdout/stderr during tests and display it for failures --- libexec/bats-exec | 35 +++++++++++++++++++++++------------ test/bats.bats | 8 ++++++++ test/fixtures/output.bats | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/output.bats diff --git a/libexec/bats-exec b/libexec/bats-exec index d5eae1c..b7e84fa 100755 --- a/libexec/bats-exec +++ b/libexec/bats-exec @@ -29,11 +29,6 @@ run() { [ -z "$e" ] || set -e } -abort() { - [ "$#" -eq 0 ] || echo error: "$@" >&2 - exit 1 -} - setup() { true } @@ -54,19 +49,25 @@ bats_test_function() { bats_teardown_trap() { trap bats_exit_trap err exit - teardown + teardown >>"$BATS_OUT" 2>&1 bats_exit_trap } bats_exit_trap() { + local status trap - err exit + if [ -z "$BATS_TEST_COMPLETED" ]; then - echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" - exit 1 + echo "not ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 + sed -e "s/^/ /" < "$BATS_OUT" >&3 + status=1 else - echo "ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" - exit 0 + echo "ok $BATS_TEST_NUMBER $BATS_TEST_DESCRIPTION" >&3 + status=0 fi + + rm -f "$BATS_OUT" + exit "$status" } bats_perform_tests() { @@ -91,8 +92,9 @@ bats_perform_test() { BATS_TEST_COMPLETED="" trap bats_teardown_trap err exit - setup - "$BATS_TEST_NAME" + { setup + "$BATS_TEST_NAME" + } >>"$BATS_OUT" 2>&1 BATS_TEST_COMPLETED=1 else @@ -101,6 +103,15 @@ bats_perform_test() { fi } +if [ -z "$TMPDIR" ]; then + BATS_TMPDIR="/tmp" +else + BATS_TMPDIR="${TMPDIR%/}" +fi +BATS_OUT="$BATS_TMPDIR/bats.$(date "+%Y%m%d%H%M%S").$$" + +exec 3<&1 + eval "$(bats-preprocess < "$BATS_TEST_FILENAME")" if [ "$#" -eq 0 ]; then diff --git a/test/bats.bats b/test/bats.bats index 74a3f73..8a57b55 100755 --- a/test/bats.bats +++ b/test/bats.bats @@ -72,3 +72,11 @@ teardown() { run bats "$FIXTURE_ROOT/load.bats" [ $status -eq 0 ] } + +@test "output is discarded for passing tests and printed for failing tests" { + run bats "$FIXTURE_ROOT/output.bats" + [ $status -eq 1 ] + [ "${lines[4]}" = " failure stdout 1" ] + [ "${lines[5]}" = " failure stdout 2" ] + [ "${lines[7]}" = " failure stderr" ] +} diff --git a/test/fixtures/output.bats b/test/fixtures/output.bats new file mode 100644 index 0000000..66b1543 --- /dev/null +++ b/test/fixtures/output.bats @@ -0,0 +1,19 @@ +@test "success writing to stdout" { + echo "success stdout 1" + echo "success stdout 2" +} + +@test "success writing to stderr" { + echo "success stderr" >&2 +} + +@test "failure writing to stdout" { + echo "failure stdout 1" + echo "failure stdout 2" + false +} + +@test "failure writing to stderr" { + echo "failure stderr" >&2 + false +}