diff --git a/src/commands/run.zsh b/src/commands/run.zsh index f2f8eac..553a0ec 100644 --- a/src/commands/run.zsh +++ b/src/commands/run.zsh @@ -82,9 +82,12 @@ function _zunit_execute_test() { # Add an exit handler which calls the teardown function if it is # defined and the test exits early if (( \$+functions[__zunit_test_teardown] )); then - zshexit() { + TRAPEXIT() { __zunit_test_teardown 2>&1 } + TRAPZERR() { + [[ -o ERR_EXIT ]] && __zunit_test_teardown 2>&1 + } fi # Create some local variables to store test state in @@ -102,14 +105,6 @@ function _zunit_execute_test() { # function it will be read as part of the body of this function ${body} - # If a teardown function is defined, run it now - if (( \$+functions[__zunit_test_teardown] )); then - __zunit_test_teardown 2>&1 - fi - - # Remove the error handler - zshexit() {} - # Check the assertion count, and if it is 0, return # the warning exit code [[ \$_zunit_assertion_count -gt 0 ]] || return 248 diff --git a/tests/setup-and-teardown.zunit b/tests/setup-and-teardown.zunit index 0a04a6a..f8a4cd0 100644 --- a/tests/setup-and-teardown.zunit +++ b/tests/setup-and-teardown.zunit @@ -1,27 +1,50 @@ #!/usr/bin/env zunit @setup { - SOME_VAR='rainbows' + SOME_VAR='rainbows' } @teardown { - unset SOME_VAR + unset SOME_VAR + unset SOME_VAR_TEARDOWN } @test 'Check value of SOME_VAR' { - assert $SOME_VAR same_as 'rainbows' + assert $SOME_VAR same_as 'rainbows' } @test 'Change value of SOME_VAR' { - SOME_VAR='unicorns' - assert $SOME_VAR same_as 'unicorns' + SOME_VAR='unicorns' + assert $SOME_VAR same_as 'unicorns' } @test 'Check value of SOME_VAR again' { - # Check will fail, because the variable was unset in - # the @teardown method, and then reset to 'rainbows' - # when @setup was run again. - run assert $SOME_VAR same_as 'unicorns' + # Check will fail, because the variable was unset in + # the @teardown method, and then reset to 'rainbows' + # when @setup was run again. + run assert $SOME_VAR same_as 'unicorns' - assert $state equals 1 + assert $state equals 1 +} + +@test 'Change value of SOME_VAR_TEARDOWN' { + SOME_VAR_TEARDOWN='rainbows' + run true + + assert $state equals 0 +} + +@test 'Check value of SOME_VAR_TEARDOWN' { + assert $SOME_VAR_TEARDOWN is_empty +} + +@test 'Change value of SOME_VAR_TEARDOWN again' { + SOME_VAR_TEARDOWN='rainbows' + run false + + assert $state equals 1 +} + +@test 'Check value of SOME_VAR_TEARDOWN again' { + assert $SOME_VAR_TEARDOWN is_empty }