diff --git a/roundup-1-test.sh b/roundup-1-test.sh index 87c5d4f..8122560 100644 --- a/roundup-1-test.sh +++ b/roundup-1-test.sh @@ -46,3 +46,8 @@ it_exits_non_zero() { it_survives_edge_cases() { rup edge } + +it_exits_non_zero_on_broken_before () { + status=$(set +e ; rup roundup-5 >/dev/null ; echo $?) + test 2 -eq $status +} diff --git a/roundup-before-test.sh b/roundup-before-test.sh new file mode 100644 index 0000000..aac7c93 --- /dev/null +++ b/roundup-before-test.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env roundup +# +# This Will Fail +# -------------- +# +# The before function +describe "roundup before trace" + +# This `before` function is run before it_works. The `false` will simulate +# an error. Hence, `it_works` should not be called at all, but it should be +# marked as failed and the `before` trace should be displayed. +before () { + false +} + +# A trivial and correct test case. But because `before` is broken, this +# testcase will fail as well. +it_works () { + true +} diff --git a/roundup.sh b/roundup.sh index 9275a5b..0440271 100755 --- a/roundup.sh +++ b/roundup.sh @@ -242,13 +242,22 @@ do # Any number of things are possible in `before`, `after`, and the # test. Drop into an subshell to contain operations that may throw # off roundup; such as `cd`. + # Momentarily turn off auto-fail to give us access to the tests + # exit status in `$?` for capturing. + set +e ( + # exit subshell with return code of last failing command. This + # is needed to see the return code 253 on failed assumptions. + # But, only do this if the error handling is activated. + set -E + trap 'rc=$?; set +x; set -o | grep "errexit.*on" >/dev/null && exit $rc' ERR + # Output `before` trace to temporary file. If `before` runs cleanly, # the trace will be overwritten by the actual test case below. { # redirect tracing output of `before` into file. { - set -x + set -xe # If `before` wasn't redefined, then this is `:`. before } &>"$roundup_tmp/$roundup_test_name" @@ -256,15 +265,6 @@ do set +x } &>/dev/null - # exit subshell with return code of last failing command. This - # is needed to see the return code 253 on failed assumptions. - # But, only do this if the error handling is activated. - set -E - trap 'rc=$?; set +x; set -o | grep "errexit.*on" >/dev/null && exit $rc' ERR - - # If `before` wasn't redefined, then this is `:`. - before - # Momentarily turn off auto-fail to give us access to the tests # exit status in `$?` for capturing. set +e @@ -284,21 +284,27 @@ do # instead. roundup_result=$? - # It's safe to return to normal operation. - set -e - # If `after` wasn't redefined, then this runs `:`. after - # This is the final step of a test. Print its pass/fail signal - # and name. - if [ "$roundup_result" -ne 0 ] - then printf "f" - else printf "p" - fi - - printf " $roundup_test_name\n" + # Pass roundup return code outside of the subshell + exit $roundup_result ) + + # copy roundup_result from subshell above + roundup_result=$? + + # It's safe to return to normal operation. + set -e + + # This is the final step of a test. Print its pass/fail signal + # and name. + if [ "$roundup_result" -ne 0 ] + then printf "f" + else printf "p" + fi + + printf " $roundup_test_name\n" done ) done |