Browse Source

test 100%

main
pvincent 3 days ago
parent
commit
d536dde018
  1. 30
      bin/miaou-bash
  2. 3
      lib/ansi.bash
  3. 31
      test/miaou-bash.test.bash
  4. 14
      test/stub/ansi_error.bash

30
bin/miaou-bash

@ -3,8 +3,8 @@
# CONSTANTS # CONSTANTS
MIAOU_BASH_ERROR=$(mktemp -t "$(basename "$0").XXXXXXXX" --tmpdir="/run/user/$(id -u)") 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 # FUNCTIONS
@ -89,7 +89,7 @@ function on_exit {
# FIXME: too much tricky! (delayed stderr) # FIXME: too much tricky! (delayed stderr)
if [[ -p /dev/stdout ]]; then if [[ -p /dev/stdout ]]; then
failure_type='SUBSHELL TERM'
failure_type='SUBSHELL_TERM'
failure_payload="$last_error" failure_payload="$last_error"
if [[ "$last_error" =~ $regex1 || "$last_error" =~ $regex2 ]]; then if [[ "$last_error" =~ $regex1 || "$last_error" =~ $regex2 ]]; then
@ -108,7 +108,7 @@ function on_exit {
message=${BASH_REMATCH[2]} message=${BASH_REMATCH[2]}
regex='^(.*): unbound variable$' regex='^(.*): unbound variable$'
if [[ $code == 1 ]] && [[ "$message" =~ $regex ]]; then if [[ $code == 1 ]] && [[ "$message" =~ $regex ]]; then
failure_type='UNBOUND VARIABLE'
failure_type='UNBOUND_VARIABLE'
failure_payload="${BASH_REMATCH[1]}" failure_payload="${BASH_REMATCH[1]}"
stack_lines[0]=$line stack_lines[0]=$line
else else
@ -180,7 +180,7 @@ function on_exit {
else else
regex='^subshell term:' regex='^subshell term:'
if [[ "$last_error" =~ $regex ]]; then if [[ "$last_error" =~ $regex ]]; then
failure_type='SUBSHELL TERM'
failure_type='SUBSHELL_UNKNOWN'
fi fi
fi fi
fi fi
@ -196,8 +196,8 @@ function on_exit {
builtin exit "$code" 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 { function ansi_block_continuation {
@ -217,7 +217,7 @@ function ansi_block_error {
content=$(style_ansi "$1") content=$(style_ansi "$1")
content=$(style_padding "$content") 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") exit_code=$(FG=CYAN style_ansi " $3")
printf "\r" printf "\r"
BLOCK_CONTINUATION_COL=5 style_block "$content$detail$exit_code" 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 if [[ ${failure_type} == 'SUBSHELL SOURCE NOT FOUND' ]]; then
: :
else else
debug '---error---'
miaou_debug '---error---'
if [[ $failure_type == 'SUBSHELL ERROR' ]]; then if [[ $failure_type == 'SUBSHELL ERROR' ]]; then
>&4 echo -e "${failure_payload}" >&4 echo -e "${failure_payload}"
else else
>&4 ansi_block_error "$failure_type" "$failure_payload" "$code" >&4 ansi_block_error "$failure_type" "$failure_payload" "$code"
fi fi
debug '---end of error---'
miaou_debug '---end of error---'
fi fi
debug '---stacktrace---'
miaou_debug '---stacktrace---'
>&4 ansi_traces stack_sources stack_lines stack_functions >&4 ansi_traces stack_sources stack_lines stack_functions
# for i in "${failure_traces[@]}"; do # for i in "${failure_traces[@]}"; do
# >&4 echo -e "$i" # >&4 echo -e "$i"
# done # done
debug '---end of stacktrace---'
miaou_debug '---end of stacktrace---'
cleanup cleanup
} }
@ -273,13 +273,13 @@ function dump_stderr {
mapfile -t tmp_error <"$MIAOU_BASH_ERROR" mapfile -t tmp_error <"$MIAOU_BASH_ERROR"
is_true "$last_trim" && unset 'tmp_error[-1]' && tmp_error=("${tmp_error[@]}") is_true "$last_trim" && unset 'tmp_error[-1]' && tmp_error=("${tmp_error[@]}")
if [[ "${#tmp_error[@]}" -gt 0 ]]; then if [[ "${#tmp_error[@]}" -gt 0 ]]; then
debug '---stderr---'
miaou_debug '---stderr---'
>&4 printf '\r' >&4 printf '\r'
for i in "${tmp_error[@]}"; do for i in "${tmp_error[@]}"; do
>&4 echo -e "$i" >&4 echo -e "$i"
done done
else else
debug '---no stderr---'
miaou_debug '---no stderr---'
fi 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 # 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 exec 3> >(tee "$MIAOU_BASH_ERROR" >/dev/null) 4>/dev/stderr
debug '---stdout---'
miaou_debug '---stdout---'
# shellcheck source=/dev/null # shellcheck source=/dev/null
source "$BASH_ARGV0" 2>&3 source "$BASH_ARGV0" 2>&3
dump_success dump_success

3
lib/ansi.bash

@ -58,7 +58,8 @@ function style_padding {
left=$(for _ in $(seq 1 "${padding_left}"); do builtin echo -n ' '; done) left=$(for _ in $(seq 1 "${padding_left}"); do builtin echo -n ' '; done)
style_ansi "$left" 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) right=$(for _ in $(seq 1 "${padding_right}"); do builtin echo -n ' '; done)
style_ansi "$right" style_ansi "$right"

31
test/miaou-bash.test.bash

@ -6,16 +6,31 @@ function assert {
./test/stub/scenario.bash "$1" >/dev/null || (echo "error in: $1" && exit 1) ./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 { 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 # main
set -euo pipefail
assert success 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!

14
test/stub/ansi_error.bash

@ -14,7 +14,9 @@ stack_functions=('f2' 'f1' '<main>')
# tput rc # tput rc
# FG=RED style_ansi "\t║\n" # 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 { function ansi_block_error {
BG=RED BG=RED
FG=BLACK FG=BLACK
@ -26,7 +28,7 @@ function ansi_block_error {
content=$(style_padding "$content") content=$(style_padding "$content")
detail=$(BG=BLACK style_padding "$2") detail=$(BG=BLACK style_padding "$2")
exit_code=$(FG=CYAN style_ansi " $3") 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 ansi_reset
FG=RED style_ansi " ║" FG=RED style_ansi " ║"
echo echo
@ -59,5 +61,11 @@ function show_error {
declare -F style_ansi >/dev/null || source "$MIAOU_BASH_DIR/lib/ansi.bash" declare -F style_ansi >/dev/null || source "$MIAOU_BASH_DIR/lib/ansi.bash"
echo START 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 "standard flow:" && show_error
echo -n "starting subshell: $(show_error)"
# echo -n "starting subshell: $(show_error)"
Loading…
Cancel
Save