You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
206 lines
3.6 KiB
206 lines
3.6 KiB
#!/usr/bin/env miaou-bash
|
|
|
|
# CONSTANTS
|
|
|
|
BASEDIR=$(dirname "$0")
|
|
readonly BASEDIR
|
|
|
|
# SCENARII
|
|
|
|
function do_subshell_false {
|
|
echo 'about to subshell error'
|
|
result=$(test ! 1 == 1)
|
|
result=$(false)
|
|
}
|
|
|
|
function do_subshell_term {
|
|
|
|
echo first line.
|
|
# echo -n second line...
|
|
# >&2 echo stderr1
|
|
# grep none none 2>/dev/null
|
|
# echo 'toto' | grep none
|
|
# echo 'toto' | return 3
|
|
|
|
echo -n "found1=$(grep nope nope)"
|
|
echo -n "found1=$(false)"
|
|
# echo "found1=$(grep nope nope | grep none)"
|
|
# echo "found2=$(command_not_found)"
|
|
# echo SHOULD NOT SHOW UP!
|
|
}
|
|
|
|
# unable to catch up error when the last command is `!` statement
|
|
function do_last_false {
|
|
echo 'last false'
|
|
! true
|
|
}
|
|
|
|
function do_command_not_found {
|
|
echo START
|
|
command_not_found A B C
|
|
echo SHOULD NOT APPEAR
|
|
}
|
|
|
|
function do_exit {
|
|
echo START
|
|
call_function_which_will_exit
|
|
echo SHOULD NOT APPEAR
|
|
}
|
|
|
|
function do_return {
|
|
should_return
|
|
}
|
|
|
|
function do_unbound_associative {
|
|
declare -A associative
|
|
associative['A']='Abc'
|
|
|
|
# this is fine
|
|
[[ -v associative['A'] ]] && echo "${associative['A']}"
|
|
[[ -v associative['B'] ]] && echo "${associative['B']}"
|
|
|
|
# this fails
|
|
echo "${associative['B']}"
|
|
}
|
|
|
|
function do_false {
|
|
echo before false
|
|
echo -n 'paragraph...'
|
|
false
|
|
echo after false
|
|
}
|
|
|
|
function do_success {
|
|
[[ true==true ]] && echo true
|
|
! false && echo unfalse
|
|
return
|
|
}
|
|
|
|
function do_unbound_variable {
|
|
a=5
|
|
echo "a=${a}"
|
|
echo "b=${b}"
|
|
}
|
|
|
|
function do_source_not_found {
|
|
source "./lib/fake/nope.bash"
|
|
}
|
|
|
|
function do_file_not_found {
|
|
./lib/fake/nope.bash
|
|
}
|
|
|
|
function do_child_error {
|
|
"$BASEDIR/extra/library.bash"
|
|
echo SHOULD NOT APPEAR
|
|
. "$BASEDIR/extra/library.bash"
|
|
}
|
|
|
|
function do_command_error {
|
|
>&2 echo nope is not defined!
|
|
grep nope nope
|
|
}
|
|
|
|
# FUNCTIONS
|
|
|
|
function call_function_which_will_exit {
|
|
exit 99
|
|
}
|
|
|
|
function call_function_which_will_return {
|
|
return 199
|
|
}
|
|
|
|
function F2 {
|
|
echo "stdout A"
|
|
# echo toto >>/tmp/toto
|
|
return
|
|
>&2 echo "This is stderr"
|
|
|
|
blabla
|
|
$nope
|
|
exit 6
|
|
grep titi toto
|
|
echo "stdout B"
|
|
echo "stdout C"
|
|
# command_not_found
|
|
}
|
|
|
|
function F1 {
|
|
echo F1
|
|
F2
|
|
}
|
|
|
|
function main {
|
|
F1
|
|
F3
|
|
echo before subshell
|
|
grep nope nope 2>/dev/null
|
|
echo "here in subshell $(grep titi $0)"
|
|
echo NO MORE
|
|
|
|
result=$(grep titi $0)
|
|
[[ "$result" -ne 0 ]] && echo true
|
|
# grep titi nope
|
|
>&2 echo some trace in stderr1
|
|
"$BASEDIR/extra/library.bash"
|
|
. "$BASEDIR/lib/utility.bash"
|
|
# . not_found/not_found.bash
|
|
# echo "here in subshell $(grep titi nope)"
|
|
# ls -l nope
|
|
>&2 echo some trace in stderr2
|
|
}
|
|
|
|
function should_return {
|
|
echo A
|
|
if call_function_which_will_return; then
|
|
echo SUCCESS
|
|
else
|
|
echo FAILURE
|
|
fi
|
|
call_function_which_will_return
|
|
echo SHOULD NOT APPEAR
|
|
}
|
|
|
|
function F3 {
|
|
echo F3
|
|
return 0
|
|
}
|
|
|
|
function usage {
|
|
introspect_scenarii
|
|
(
|
|
IFS='|'
|
|
echo "USAGE: $0 <case_scenario>"
|
|
)
|
|
echo -e "\trun a case scenario built from various bash statements and mostly ends up on failure"
|
|
echo -e "\tthese case scenarii provide the help suite needed for \`miaou-bash\`"
|
|
echo -e "\there is the list of available case scenario:"
|
|
local i
|
|
for i in "${SCENARII[@]}"; do
|
|
echo -e "\t- $i"
|
|
done
|
|
}
|
|
|
|
function introspect_scenarii {
|
|
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 '<case_scenario> required!' && usage && exit 1
|
|
|
|
scenario="$1"
|
|
introspect_scenarii
|
|
if test_contain_item SCENARII "$scenario"; then
|
|
shift
|
|
"do_${scenario}" "$@"
|
|
else
|
|
(>&2 echo "scenario <${scenario}> not found!" && usage && exit 2)
|
|
fi
|