Browse Source

success_with_args

main
pvincent 1 week ago
parent
commit
2e595ef217
  1. 45
      bin/miaou-bash
  2. 24
      test/miaou-bash.test.bash
  3. 8
      test/stub/scenario.bash

45
bin/miaou-bash

@ -581,33 +581,48 @@ function cleanup {
rm -f "$MIAOU_BASH_ERROR" rm -f "$MIAOU_BASH_ERROR"
} }
## main
function usage {
echo "$(basename "$0") <SCRIPT> [...SCRIPT_ARGS] | --version | --help"
}
[[ $# -lt 1 ]] && >&2 echo 'ERROR: script expected!' && builtin exit 1
function parse_options {
[[ $# == 0 ]] && >&2 echo 'ERROR: script expected!' && builtin exit 2
[[ $# == 1 ]] && case "$1" in
--help) usage && builtin exit ;;
--version) cat "$MIAOU_BASH_DIR/.semver_git_tag" && builtin exit ;;
-*) >&2 echo "ERROR: option '$1' unknown!" && usage && builtin exit 2 ;;
esac
[[ $1 =~ ^- ]] && >&2 echo "ERROR: too many args $#, either single option or SCRIPT + args accepted!" && builtin exit 2
SCRIPT="$1"
[[ ! -v SCRIPT ]] && >&2 echo 'ERROR: script expected!' && builtin exit 2
[[ ! -f "$SCRIPT" ]] && >&2 echo "ERROR: script '$SCRIPT' not found!" && builtin exit 2
true
}
## main
set -euo pipefail set -euo pipefail
set -E # HALT on subshell error
set -T # DEBUG RETURN
set -E # HALT on pipe error
set -T # RETURN
parse_options "$@"
build_miaou_bash_error build_miaou_bash_error
BASH_ARGV0="$1" && shift # to pretend script runs by itself
trap 'on_error $?' ERR trap 'on_error $?' ERR
trap 'on_exit $?' EXIT trap 'on_exit $?' EXIT
trap 'on_pipe' PIPE trap 'on_pipe' PIPE
trap 'on_return' RETURN trap 'on_return' RETURN
if ! is_true "$CHILD_PROCESS"; then
# main
[[ -e /proc/$$/fd/3 ]] && exec 4>&3 3>&- || exec 4>/dev/tty # swap fd 3 4 when fd 3 sent off
exec 3>/dev/stderr 2>"$MIAOU_BASH_ERROR"
else
# any children
exec 4>/dev/tty 2>"$MIAOU_BASH_ERROR"
fi
BASH_ARGV0="$SCRIPT" && shift
if is_true "$CHILD_PROCESS"; then if is_true "$CHILD_PROCESS"; then
# any nested children
exec 4>/dev/tty 2>"$MIAOU_BASH_ERROR"
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
[[ -e /proc/$$/fd/3 ]] && exec 4>&3 3>&- || exec 4>/dev/tty # swap fd 3 4 when fd 3 sent off
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
# shellcheck source=/dev/null # shellcheck source=/dev/null
source "$BASH_ARGV0"
source "$SCRIPT"

24
test/miaou-bash.test.bash

@ -13,9 +13,29 @@ function assert {
failures+=("$1") failures+=("$1")
return 1 return 1
fi fi
} }
# assert performing '$1:script' successfully + output must contain '$2:content'
# in case of extra args (4th position or more) must be provided after -- (3rd position)
# 1: script
# 2: content
# 3: --
# 4..: extra args
function assert_grep {
((test_count++))
local script="$1" content="$2" extra_args=()
[[ $# -gt 3 ]] && shift 3 && extra_args=("$@")
if miaou-bash ./test/stub/scenario.bash "$script" "${extra_args[@]}" >_stdout 2>/dev/null 3>/dev/null; then
if grep -q "$content" _stdout; then
echo -n .
return
fi
fi
echo -n F
failures+=("$script")
return 1
}
function fail_type { function fail_type {
((test_count++)) ((test_count++))
local success regex hint_message local success regex hint_message
@ -52,6 +72,8 @@ test_count=0
failures=() failures=()
assert success assert success
assert_grep success_with_args 'count args: 2' -- 'A B' C
fail_type subshell_term SUBSHELL_ERROR fail_type subshell_term SUBSHELL_ERROR
fail_type unbound_variable UNBOUND_VARIABLE fail_type unbound_variable UNBOUND_VARIABLE
fail_type unbound_associative UNBOUND_VARIABLE fail_type unbound_associative UNBOUND_VARIABLE

8
test/stub/scenario.bash

@ -3,7 +3,6 @@
# CONSTANTS # CONSTANTS
BASEDIR=$(dirname "$0") BASEDIR=$(dirname "$0")
# readonly BASEDIR
# SCENARII # SCENARII
@ -71,11 +70,16 @@ function do_false {
} }
function do_success { function do_success {
echo "count args: $#"
[[ true==true ]] && echo true [[ true==true ]] && echo true
! false && echo unfalse ! false && echo unfalse
return return
} }
function do_success_with_args {
echo "count args: $#"
}
function do_unbound_variable { function do_unbound_variable {
a=5 a=5
echo "a=${a}" echo "a=${a}"
@ -203,5 +207,5 @@ if test_contain_item SCENARII "$scenario"; then
shift shift
"do_${scenario}" "$@" "do_${scenario}" "$@"
else else
(>&2 echo "scenario <${scenario}> not found!" && usage && exit 2)
(>&2 echo "scenario '${scenario}' not found!" && usage && exit 2)
fi fi
Loading…
Cancel
Save