From d536dde0188f5689c05d6530f7a67d443e2ccfd2 Mon Sep 17 00:00:00 2001 From: pvincent Date: Thu, 23 Jul 2026 09:17:03 +0400 Subject: [PATCH] test 100% --- bin/miaou-bash | 30 +++++++++++++++--------------- lib/ansi.bash | 3 ++- test/miaou-bash.test.bash | 31 +++++++++++++++++++++++-------- test/stub/ansi_error.bash | 14 +++++++++++--- 4 files changed, 51 insertions(+), 27 deletions(-) diff --git a/bin/miaou-bash b/bin/miaou-bash index 4288434..d0d5006 100755 --- a/bin/miaou-bash +++ b/bin/miaou-bash @@ -3,8 +3,8 @@ # CONSTANTS MIAOU_BASH_ERROR=$(mktemp -t "$(basename "$0").XXXXXXXX" --tmpdir="/run/user/$(id -u)") -MIAOU_BASH_DEBUG=${MIAOU_BASH_DEBUG:-false} -readonly MIAOU_BASH_ERROR MIAOU_BASH_DEBUG +MIAOU_DEBUG=${MIAOU_BASH_DEBUG:-false} +readonly MIAOU_BASH_ERROR MIAOU_DEBUG # FUNCTIONS @@ -89,7 +89,7 @@ function on_exit { # FIXME: too much tricky! (delayed stderr) if [[ -p /dev/stdout ]]; then - failure_type='SUBSHELL TERM' + failure_type='SUBSHELL_TERM' failure_payload="$last_error" if [[ "$last_error" =~ $regex1 || "$last_error" =~ $regex2 ]]; then @@ -108,7 +108,7 @@ function on_exit { message=${BASH_REMATCH[2]} regex='^(.*): unbound variable$' if [[ $code == 1 ]] && [[ "$message" =~ $regex ]]; then - failure_type='UNBOUND VARIABLE' + failure_type='UNBOUND_VARIABLE' failure_payload="${BASH_REMATCH[1]}" stack_lines[0]=$line else @@ -180,7 +180,7 @@ function on_exit { else regex='^subshell term:' if [[ "$last_error" =~ $regex ]]; then - failure_type='SUBSHELL TERM' + failure_type='SUBSHELL_UNKNOWN' fi fi fi @@ -196,8 +196,8 @@ function on_exit { builtin exit "$code" } -function debug { - is_true "${MIAOU_BASH_DEBUG}" && >/dev/tty echo "*********** DEBUG: $*" || true +function miaou_debug { + is_true "${MIAOU_DEBUG}" && >/dev/tty echo "*********** DEBUG: $*" || true } function ansi_block_continuation { @@ -217,7 +217,7 @@ function ansi_block_error { content=$(style_ansi "$1") content=$(style_padding "$content") - detail=$(BG=BLACK style_padding "$2") + detail=$(BG=BLACK FG=WHITE style_padding "$2") exit_code=$(FG=CYAN style_ansi " $3") printf "\r" BLOCK_CONTINUATION_COL=5 style_block "$content$detail$exit_code" @@ -249,22 +249,22 @@ function dump_failure { if [[ ${failure_type} == 'SUBSHELL SOURCE NOT FOUND' ]]; then : else - debug '---error---' + miaou_debug '---error---' if [[ $failure_type == 'SUBSHELL ERROR' ]]; then >&4 echo -e "${failure_payload}" else >&4 ansi_block_error "$failure_type" "$failure_payload" "$code" fi - debug '---end of error---' + miaou_debug '---end of error---' fi - debug '---stacktrace---' + miaou_debug '---stacktrace---' >&4 ansi_traces stack_sources stack_lines stack_functions # for i in "${failure_traces[@]}"; do # >&4 echo -e "$i" # done - debug '---end of stacktrace---' + miaou_debug '---end of stacktrace---' cleanup } @@ -273,13 +273,13 @@ function dump_stderr { mapfile -t tmp_error <"$MIAOU_BASH_ERROR" is_true "$last_trim" && unset 'tmp_error[-1]' && tmp_error=("${tmp_error[@]}") if [[ "${#tmp_error[@]}" -gt 0 ]]; then - debug '---stderr---' + miaou_debug '---stderr---' >&4 printf '\r' for i in "${tmp_error[@]}"; do >&4 echo -e "$i" done else - debug '---no stderr---' + miaou_debug '---no stderr---' fi } @@ -307,7 +307,7 @@ BASH_ARGV0="$1" && shift # to pretend script runs by itself # magic FD + delayed tee => /dev/fd/3 means delayed stderr, /dev/fd/4 remains original stderr exec 3> >(tee "$MIAOU_BASH_ERROR" >/dev/null) 4>/dev/stderr -debug '---stdout---' +miaou_debug '---stdout---' # shellcheck source=/dev/null source "$BASH_ARGV0" 2>&3 dump_success diff --git a/lib/ansi.bash b/lib/ansi.bash index 722f830..b512dc3 100644 --- a/lib/ansi.bash +++ b/lib/ansi.bash @@ -58,7 +58,8 @@ function style_padding { left=$(for _ in $(seq 1 "${padding_left}"); do builtin echo -n ' '; done) style_ansi "$left" - builtin echo -n "$1" + style_ansi "$1" + # builtin echo -n "$1" right=$(for _ in $(seq 1 "${padding_right}"); do builtin echo -n ' '; done) style_ansi "$right" diff --git a/test/miaou-bash.test.bash b/test/miaou-bash.test.bash index 689b560..8581df7 100755 --- a/test/miaou-bash.test.bash +++ b/test/miaou-bash.test.bash @@ -6,16 +6,31 @@ function assert { ./test/stub/scenario.bash "$1" >/dev/null || (echo "error in: $1" && exit 1) } +function strip_ansi { + echo "$1" | sed -E 's/\x1b\[[0-9;]*[a-zA-Z]//g' +} + function fail_type { - stderr=$(./test/stub/scenario.bash "$1" 2>&1 >/dev/null) - code="$?" - regex="TYPE: $2" - [[ "$code" != 0 ]] && [[ "$stderr" =~ $regex ]] || (echo "error in: $1" && exit 1) + local i line first error_start_above regex + mapfile -t stderr <<<"$(./test/stub/scenario.bash "$1" 2>&1 >/dev/null)" + # echo "stderr countains ${#stderr[@]}" + error_start_above=false + regex="║[[:blank:]]+([A-Z|_]+)" + for i in "${stderr[@]}"; do + line=$(strip_ansi "$i") + first="${line:1:1}" + [[ $first == ╔ ]] && error_start_above=true && continue + if [[ "$error_start_above" == true ]]; then + [[ $line =~ $regex ]] && [[ ${BASH_REMATCH[1]} == "$2" ]] && return 0 + echo "error in: $1" && return 1 + fi + done } # main - +set -euo pipefail assert success -fail_type subshell_term 'SUBSHELL TERM' -fail_type unbound_variable 'UNBOUND VARIABLE' -fail_type false 'FALSE' +fail_type subshell_term SUBSHELL_TERM +fail_type unbound_variable UNBOUND_VARIABLE +fail_type false FALSE +echo 100% passed! diff --git a/test/stub/ansi_error.bash b/test/stub/ansi_error.bash index 92a3761..c4a3bf4 100755 --- a/test/stub/ansi_error.bash +++ b/test/stub/ansi_error.bash @@ -14,7 +14,9 @@ stack_functions=('f2' 'f1' '
') # tput rc # FG=RED style_ansi "\t║\n" # } - +function strip_ansi { + echo "$1" | sed -E 's/\x1b\[[0-9;]*[a-zA-Z]//g' +} function ansi_block_error { BG=RED FG=BLACK @@ -26,7 +28,7 @@ function ansi_block_error { content=$(style_padding "$content") detail=$(BG=BLACK style_padding "$2") exit_code=$(FG=CYAN style_ansi " $3") - BLOCK_CONTINUATION_COL=5 style_block "$content$detail$exit_code" + BLOCK_CONTINUATION_COL=5 style_block "$content\t$detail$exit_code" ansi_reset FG=RED style_ansi " ║" echo @@ -59,5 +61,11 @@ function show_error { declare -F style_ansi >/dev/null || source "$MIAOU_BASH_DIR/lib/ansi.bash" echo START + +output=$(BG=RED style_ansi "╔") +output=$(strip_ansi "$output") +echo "$output" +# echo "$output" | cat -v + # echo -n "standard flow:" && show_error -echo -n "starting subshell: $(show_error)" +# echo -n "starting subshell: $(show_error)"