Ensure teardown is always executed

This commit is contained in:
Chitoku 2020-02-01 18:49:37 +09:00 committed by James Dinsdale
parent b86c006f62
commit dd69dbaf40
2 changed files with 37 additions and 19 deletions

View file

@ -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

View file

@ -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
}