From 360fe2c1346517443a315b2e36bd64f191f899ff Mon Sep 17 00:00:00 2001 From: pvincent Date: Thu, 16 Jul 2026 21:20:42 +0400 Subject: [PATCH] miaou-bash.test.bash --- bin/miaou-bash | 3 +- test/miaou-bash.test.bash | 3 ++ test/stub/{script.bash => scenario.bash} | 49 +++++++++++------------- 3 files changed, 28 insertions(+), 27 deletions(-) create mode 100644 test/miaou-bash.test.bash rename test/stub/{script.bash => scenario.bash} (68%) diff --git a/bin/miaou-bash b/bin/miaou-bash index 4f4a8db..ab13bae 100755 --- a/bin/miaou-bash +++ b/bin/miaou-bash @@ -23,7 +23,7 @@ function on_return { function exit { code=${1:-0} # >&2 echo "${BASH_SOURCE[2]}: line ${BASH_LINENO[0]}: exit $@" - >&2 echo "${BASH_SOURCE[2]}:${BASH_LINENO[0]} exit $code" + >&2 echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]} exit $code" debug "overridden exit $code" builtin exit "$code" } @@ -134,6 +134,7 @@ function on_exit { fi else failure_type='UNDEFINED' + debug "last error=${last_error}" failure_payload="${last_error}" regex="^ .*:[0-9]* .*\$" if [[ "$last_error" =~ $regex ]]; then diff --git a/test/miaou-bash.test.bash b/test/miaou-bash.test.bash new file mode 100644 index 0000000..c8f0d20 --- /dev/null +++ b/test/miaou-bash.test.bash @@ -0,0 +1,3 @@ +#!/usr/bin/env miaou-bash + +[[ $(./test/stub/scenario.bash unbound_variable 2>&1 1>/dev/null) =~ 'TYPE: UNBOUND VARIABLE' ]] diff --git a/test/stub/script.bash b/test/stub/scenario.bash similarity index 68% rename from test/stub/script.bash rename to test/stub/scenario.bash index 0b726f7..8275af0 100755 --- a/test/stub/script.bash +++ b/test/stub/scenario.bash @@ -52,13 +52,22 @@ function F3 { } function usage { - echo "USAGE: $0 " + introspect_scenarii + ( + IFS='|' + echo "USAGE: $0 <${SCENARII[*]}>" + ) echo -e "\trun case scenario which performs various bash statements and mostly fails" echo -e "\tthese case scenarii helps testing the miaou-bash script" } function do_success { - echo SUCCESS + echo -n 'SUCCESS args=[' + ( + IFS='|' + echo -n "$*" + ) + echo ']' } function do_unbound_variable { @@ -68,35 +77,23 @@ function do_unbound_variable { } function introspect_scenarii { - mapfile -t SCENARII <<<"$(compgen -A function | grep ^do_)" + mapfile -t SCENARII <<<"$(compgen -A function | grep ^do_ | sed 's/...//')" +} +function test_contain_item { + local -n array=$1 + for item in "${array[@]}"; do [[ "$item" == "$2" ]] && return 0; done + return 1 } # MAIN + [[ $# -eq 0 ]] && >&2 echo 'arg1 expected!' && usage && exit 1 scenario="$1" -shift -mapfile -t scenarii <<<"$(compgen -A function | grep ^do_)" - -args=("$@") - -echo "scenarii = ${scenarii[@]}" -exit 0 - -echt "start of useless $0 $* ($# args)" -if grep titi nope 2>/dev/null; then - echo OK -else - echo not found but ok -fi -if grep titi "$BASEDIR/lib/utility.bash"; then - echo OK +introspect_scenarii +if test_contain_item SCENARII "$scenario"; then + shift + "do_${scenario}" "$@" else - echo not found but ok + (>&2 echo "scenario <${scenario}> not found!" && usage && exit 2) fi -echo question?: -# read -r answer -# echo "$answer" -echo suite -main -echo end of useless