diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2023-10-19 09:31:33 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2023-10-19 09:31:33 -0400 |
commit | c78e5ddfc8aa0a6373b063dd787f416e9de8002e (patch) | |
tree | e0f9ce4ee5ef0a7a39aaf7388eda5290ebc95a06 /tests/tests.sh | |
parent | b1c6199ad82de2081f18eba931a787a25eb27ea7 (diff) | |
download | bfs-c78e5ddfc8aa0a6373b063dd787f416e9de8002e.tar.xz |
tests: New defer function
Diffstat (limited to 'tests/tests.sh')
-rwxr-xr-x | tests/tests.sh | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/tests/tests.sh b/tests/tests.sh index 282a428..b159ea2 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -260,6 +260,34 @@ if (( ${#TEST_CASES[@]} == 0 )); then exit 1 fi +DEFER=() + +# Run a command when this (sub)shell exits +function defer() { + trap -- KILL + if ! trap -p EXIT | grep -q pop_defers; then + DEFER=() + trap pop_defers EXIT + fi + DEFER+=("$(printf '%q ' "$@")") +} + +function pop_defer() { + local cmd="${DEFER[-1]}" + unset "DEFER[-1]" + eval "$cmd" +} + +function pop_defers() { + local ret=0 + + while ((${#DEFER[@]} > 0)); do + pop_defer || ret=$? + done + + return $ret +} + function bfs_sudo() { if ((${#SUDO[@]})); then "${SUDO[@]}" "$@" @@ -304,7 +332,7 @@ function cleanup() { } if [ "$CLEAN" ]; then - trap cleanup EXIT + defer cleanup else echo "Test files saved to $TMP" fi |