From d9c47bb4afa040e5eaab2dcc5f34923f9499f678 Mon Sep 17 00:00:00 2001 From: Castulo Martinez Date: Wed, 15 Aug 2018 11:35:17 -0700 Subject: [PATCH] Print the function stack on param error in testlib Most of the functions of the test library include some type of parameter validation, when a function is called and one of its parameters don't comply with the expected parameters it terminates the whole script. This is expected, but sometimes it can be difficult to figure out what was the function that received the incorrect parameters in the script. This commit adds a function that prints a stack of the function calls so when a script is terminated it is easier to figure out the culprit. --- test/functional/testlib.bash | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/test/functional/testlib.bash b/test/functional/testlib.bash index 8eb3933b..3a05d3ad 100644 --- a/test/functional/testlib.bash +++ b/test/functional/testlib.bash @@ -58,6 +58,18 @@ generate_random_name() { } +print_stack() { + + echo "An error occurred" + echo "Function stack (most recent on top):" + for func in ${FUNCNAME[*]}; do + if [ "$func" != "print_stack" ] && [ "$func" != "terminate" ]; then + echo -e "\\t$func" + fi + done + +} + terminate() { # since the library could be sourced and run from an interactive shell @@ -67,8 +79,10 @@ terminate() { local msg=$1 local param case "$-" in - *i*) : ${param:?"$msg, exiting..."} ;; - *) echo "$msg, exiting..." + *i*) print_stack + : "${param:?"$msg, exiting..."}" ;; + *) print_stack + echo "$msg, exiting..." exit 1;; esac