MIAOU-BASH is a collection of settings and helpers for leveraging BASH. Developer-friendly, it may be used as solo package with or without the miaou project.
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.

109 lines
2.5 KiB

#!/usr/bin/env bash
# CONSTANTS
# functions
function assert {
((test_count++))
cmd="./test/stub/scenario.bash $1"
if eval "$cmd" >/dev/null 2>/dev/null 3>_miaou; then
if [[ -s _miaou ]]; then
echo -n .
return
fi
fi
echo -n F
failures+=("$cmd")
return 1
}
# 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 ./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 {
((test_count++))
local success regex hint_message
success=false
hint_message="${3:-}"
cmd="./test/stub/scenario.bash $1"
# mapfile -t stderr <<<"$(miaou-bash ./test/stub/scenario.bash "$1" 2>&1 >/dev/null 3>/dev/null)"
eval "$cmd" >/dev/null 2>_stderr 3>_miaou
mapfile -t stderr <_stderr
last_error="${stderr[-1]}"
regex="^(.*): line ([0-9]+): miaou_error: $2 ([0-9]+)"
[[ "${last_error}" =~ $regex ]] && success=true
if [[ $success == true && -n "${hint_message}" ]]; then
grep -q "${hint_message}" _miaou || success=false
fi
if [[ $success == true ]]; then
echo -n .
return
fi
echo -n F
failures+=("$cmd")
return 1
}
# main
set -uo pipefail
test_count=0
failures=()
TIMEFORMAT=$'\nelapsed: %Rs'
time {
assert success
assert exit_0
assert_grep success_with_args 'count args: 2' -- 'A B' C
fail_type subshell_term SUBSHELL_ERROR
fail_type unbound_variable UNBOUND_VARIABLE
fail_type unbound_associative UNBOUND_VARIABLE
fail_type false FALSE
fail_type last_false COMMAND_ERROR
fail_type command_error COMMAND_ERROR
fail_type subshell_term SUBSHELL_ERROR
fail_type exit EXIT
fail_type command_not_found COMMAND_NOT_FOUND
fail_type source_not_found SOURCE_NOT_FOUND
fail_type file_not_found FILE_NOT_FOUND
fail_type child_error CHILD_ERROR 'prefer source than child process'
}
failed=${#failures[@]}
success=$((test_count - failed))
if [[ $failed -gt 0 ]]; then
ratio=$((100 * success / test_count))
echo "${ratio}% passed, ${test_count} tests, ${failed} failed!"
echo failures:
for failure in "${failures[@]}"; do
echo -e "\t- $failure"
done
exit 1
else
echo "100% passed, ${test_count} tests"
fi