2>_stderr
This commit is contained in:
+27
-8
@@ -4,6 +4,7 @@
|
|||||||
# constants
|
# constants
|
||||||
|
|
||||||
MIAOU_DEBUG=${MIAOU_DEBUG:-false}
|
MIAOU_DEBUG=${MIAOU_DEBUG:-false}
|
||||||
|
MIAOU_ERROR_REGEX='^(.*): line ([0-9]+): miaou_error:'
|
||||||
|
|
||||||
declare -A ANSI
|
declare -A ANSI
|
||||||
ANSI[RESET]=0
|
ANSI[RESET]=0
|
||||||
@@ -36,7 +37,7 @@ ANSI[BG_WHITE]=47
|
|||||||
ANSI[BG_GRAY]=90
|
ANSI[BG_GRAY]=90
|
||||||
ANSI[BG_MAGENTA]=95
|
ANSI[BG_MAGENTA]=95
|
||||||
|
|
||||||
readonly MIAOU_DEBUG ANSI
|
readonly MIAOU_DEBUG MIAOU_ERROR_REGEX ANSI
|
||||||
|
|
||||||
## functions
|
## functions
|
||||||
|
|
||||||
@@ -139,8 +140,6 @@ function on_error {
|
|||||||
trap - RETURN
|
trap - RETURN
|
||||||
failure_code="${1:-200}"
|
failure_code="${1:-200}"
|
||||||
|
|
||||||
# >&4 printf '\r' # TODO: force carriage return explicitly, is it really useful?
|
|
||||||
|
|
||||||
miaou_debug "<on_error code=${failure_code} command={${BASH_COMMAND}}>"
|
miaou_debug "<on_error code=${failure_code} command={${BASH_COMMAND}}>"
|
||||||
|
|
||||||
if [[ $$ == "$BASHPID" ]]; then
|
if [[ $$ == "$BASHPID" ]]; then
|
||||||
@@ -310,7 +309,22 @@ function on_pipe {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function dump_stderr {
|
function dump_stderr {
|
||||||
miaou_debug "<stderr count=${#tmp_error[@]}>"
|
local tmp_error_count=${#tmp_error[@]}
|
||||||
|
miaou_debug "<stderr count=${tmp_error_count}>"
|
||||||
|
if [[ -v MAIN_FD_MIAOU && $tmp_error_count -gt 0 && "$MAIN_FD_STDERR" == false ]]; then
|
||||||
|
miaou_debug "<main_fd_miaou/>"
|
||||||
|
local last_error="${tmp_error[-1]}"
|
||||||
|
if [[ "$last_error" =~ $MIAOU_ERROR_REGEX ]]; then
|
||||||
|
tmp_error_count=$((tmp_error_count - 1))
|
||||||
|
miaou_debug "<hide_latest_miaou_error line=${last_error} count=$tmp_error_count/>"
|
||||||
|
unset 'tmp_error[-1]'
|
||||||
|
if [[ $tmp_error_count == 0 ]]; then
|
||||||
|
# TODO: grab last_stdout and test no carriage_return, cf: ../test/stub/tee_last_line.bash
|
||||||
|
miaou_debug "<prevent_stdout_no_carriage_return />"
|
||||||
|
printf "\r"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
tty_ansi FG_LIGHTBLUE
|
tty_ansi FG_LIGHTBLUE
|
||||||
for i in "${tmp_error[@]}"; do
|
for i in "${tmp_error[@]}"; do
|
||||||
is_true "$CHILD_PROCESS" && miaou_debug "<childerr content={$i}/>"
|
is_true "$CHILD_PROCESS" && miaou_debug "<childerr content={$i}/>"
|
||||||
@@ -431,13 +445,12 @@ function load_tmp_error {
|
|||||||
miaou_debug "<tmp_error empty/>"
|
miaou_debug "<tmp_error empty/>"
|
||||||
else
|
else
|
||||||
miaou_debug "<tmp_error count=${count}>"
|
miaou_debug "<tmp_error count=${count}>"
|
||||||
local i miaou_error_regex last_miaou_error_index=-1 content
|
local i last_miaou_error_index=-1 content
|
||||||
local multiple_errors=()
|
local multiple_errors=()
|
||||||
miaou_error_regex='^(.*): line ([0-9]+): miaou_error:'
|
|
||||||
for i in "${!tmp_error[@]}"; do
|
for i in "${!tmp_error[@]}"; do
|
||||||
content="${tmp_error[i]}"
|
content="${tmp_error[i]}"
|
||||||
# miaou_debug "<tmperr index=$i content={$content}/>"
|
# miaou_debug "<tmperr index=$i content={$content}/>"
|
||||||
if [[ "$content" =~ $miaou_error_regex ]]; then
|
if [[ "$content" =~ $MIAOU_ERROR_REGEX ]]; then
|
||||||
if [[ $last_miaou_error_index -ge 0 ]]; then
|
if [[ $last_miaou_error_index -ge 0 ]]; then
|
||||||
multiple_errors+=("$last_miaou_error_index")
|
multiple_errors+=("$last_miaou_error_index")
|
||||||
fi
|
fi
|
||||||
@@ -623,7 +636,13 @@ if is_true "$CHILD_PROCESS"; then
|
|||||||
miaou_debug "<child file=$BASH_ARGV0 pid=${BASHPID} parent=${PPID}>"
|
miaou_debug "<child file=$BASH_ARGV0 pid=${BASHPID} parent=${PPID}>"
|
||||||
else
|
else
|
||||||
# the MAIN Process
|
# the MAIN Process
|
||||||
[[ -e /proc/$$/fd/3 ]] && exec 4>&3 3>&- || exec 4>/dev/tty # swap fd 3 4 when fd 3 sent off
|
if [[ -e /proc/$$/fd/3 ]]; then
|
||||||
|
exec 4>&3 3>&- # swap fd 3 4 when fd 3 sent off
|
||||||
|
else
|
||||||
|
MAIN_FD_MIAOU=true
|
||||||
|
[[ -t 2 ]] && MAIN_FD_STDERR=false || MAIN_FD_STDERR=true
|
||||||
|
exec 4>/dev/tty
|
||||||
|
fi
|
||||||
exec 3>/dev/stderr 2>"$MIAOU_BASH_ERROR"
|
exec 3>/dev/stderr 2>"$MIAOU_BASH_ERROR"
|
||||||
miaou_debug "<main file=$BASH_ARGV0 pid=${BASHPID}/>"
|
miaou_debug "<main file=$BASH_ARGV0 pid=${BASHPID}/>"
|
||||||
fi
|
fi
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ function assert_grep {
|
|||||||
failures+=("$cmd $safe_args")
|
failures+=("$cmd $safe_args")
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
function fail_type {
|
function fail_type {
|
||||||
((test_count++))
|
((test_count++))
|
||||||
local success regex hint_message
|
local success regex hint_message
|
||||||
@@ -68,7 +69,24 @@ function fail_type {
|
|||||||
failures+=("$cmd")
|
failures+=("$cmd")
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
function fail_stderr_count {
|
||||||
|
((test_count++))
|
||||||
|
local regex count="$2"
|
||||||
|
|
||||||
|
cmd="./test/stub/scenario.bash $1"
|
||||||
|
# mapfile -t stderr <<<"$(miaou-bash ./test/stub/scenario.bash "$1" 2>&1 >/dev/null 3>/dev/null)"
|
||||||
|
if ! $cmd >/dev/null 2>_stderr 3>/dev/null; then
|
||||||
|
mapfile -t stderr <_stderr
|
||||||
|
if [[ "${#stderr[@]}" == "$count" ]]; then
|
||||||
|
echo -n .
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -n F
|
||||||
|
failures+=("$cmd 2>_stderr")
|
||||||
|
return 1
|
||||||
|
}
|
||||||
# main
|
# main
|
||||||
|
|
||||||
set -uo pipefail
|
set -uo pipefail
|
||||||
@@ -95,6 +113,9 @@ time {
|
|||||||
fail_type source_not_found SOURCE_NOT_FOUND
|
fail_type source_not_found SOURCE_NOT_FOUND
|
||||||
fail_type file_not_found FILE_NOT_FOUND
|
fail_type file_not_found FILE_NOT_FOUND
|
||||||
fail_type child_error CHILD_ERROR 'prefer source than child process'
|
fail_type child_error CHILD_ERROR 'prefer source than child process'
|
||||||
|
|
||||||
|
fail_stderr_count unbound_variable 1
|
||||||
|
fail_stderr_count child_error 4
|
||||||
}
|
}
|
||||||
failed=${#failures[@]}
|
failed=${#failures[@]}
|
||||||
success=$((test_count - failed))
|
success=$((test_count - failed))
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
#!/usr/bin/env miaou-bash
|
#!/usr/bin/env miaou-bash
|
||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
function onemore {
|
function onemore {
|
||||||
echo onemore loaded
|
echo onemore loaded
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ function do_false {
|
|||||||
|
|
||||||
function do_success {
|
function do_success {
|
||||||
echo "count args: $#"
|
echo "count args: $#"
|
||||||
[[ true==true ]] && echo true
|
[[ true != 'false' ]] && echo true
|
||||||
! false && echo unfalse
|
! false && echo unfalse
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -89,6 +89,8 @@ function do_success_with_args {
|
|||||||
function do_unbound_variable {
|
function do_unbound_variable {
|
||||||
a=5
|
a=5
|
||||||
echo -n "content: "
|
echo -n "content: "
|
||||||
|
# >&2 echo err1
|
||||||
|
# echo "content: "
|
||||||
echo -n "a=${a} b=${b}"
|
echo -n "a=${a} b=${b}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user