aboutsummaryrefslogtreecommitdiff
path: root/test/test-lib.sh
diff options
context:
space:
mode:
authorTomi Ollila <tomi.ollila@iki.fi>2017-05-23 21:54:24 +0300
committerDavid Bremner <david@tethera.net>2017-05-26 07:25:55 -0300
commiteb157f8841b93a0df241b815e30ac54ca81cc83d (patch)
treed0865422441b7bb199018d1c6880fe10c6edc33b /test/test-lib.sh
parent523d2b50fcaae90cbe09975c1f1bc652ff521ec6 (diff)
test-lib.sh: add "atexit" functionality
New function at_exit_function registers given function to be called at script termination. Functions so registered are called in the reverse order of their registration; no arguments are passed. Function is called only once; re-adding with function name already registered will remove previous registration. New function rm_exit_function can be used to remove registration. Modules (and possibly test-lib.sh functions) in future commits will register such functions.
Diffstat (limited to 'test/test-lib.sh')
-rw-r--r--test/test-lib.sh11
1 files changed, 11 insertions, 0 deletions
diff --git a/test/test-lib.sh b/test/test-lib.sh
index 988b00af..37f8ddfa 100644
--- a/test/test-lib.sh
+++ b/test/test-lib.sh
@@ -219,10 +219,21 @@ test_fixed=0
test_broken=0
test_success=0
+declare -a _exit_functions=()
+
+at_exit_function () {
+ _exit_functions=($1 ${_exit_functions[@]/$1})
+}
+
+rm_exit_function () {
+ _exit_functions=(${_exit_functions[@]/$1})
+}
+
_exit_common () {
code=$?
trap - EXIT
set +ex
+ for _fn in ${_exit_functions[@]}; do $_fn; done
rm -rf "$TEST_TMPDIR"
}