summaryrefslogtreecommitdiffstats
path: root/tests/tests.sh
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-10-19 09:31:33 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-10-19 09:31:33 -0400
commitc78e5ddfc8aa0a6373b063dd787f416e9de8002e (patch)
treee0f9ce4ee5ef0a7a39aaf7388eda5290ebc95a06 /tests/tests.sh
parentb1c6199ad82de2081f18eba931a787a25eb27ea7 (diff)
downloadbfs-c78e5ddfc8aa0a6373b063dd787f416e9de8002e.tar.xz
tests: New defer function
Diffstat (limited to 'tests/tests.sh')
-rwxr-xr-xtests/tests.sh30
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