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.

183 lines
4.5 KiB

#!/usr/bin/env bash
# CONSTANTS
TMP_DIRECTORY="/tmp/$(basename "$0")-$UID"
TMP_OUT="$TMP_DIRECTORY/_stdout"
TMP_ERR="$TMP_DIRECTORY/_stderr"
TMP_MIAOU="$TMP_DIRECTORY/_miaou"
readonly TMP_DIRECTORY TMP_OUT TMP_ERR TMP_MIAOU
# functions
function assert {
((test_count++))
cmd="./test/stub/scenario.bash $1"
if $cmd >/dev/null 2>/dev/null 3>"$TMP_MIAOU"; then
if [[ -s "$TMP_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=("$@")
cmd="./test/stub/scenario.bash $script"
if $cmd "${extra_args[@]}" >"$TMP_OUT" 2>/dev/null 3>/dev/null; then
if grep -q "$content" "$TMP_OUT"; then
echo -n .
return
fi
fi
echo -n F
#TODO: quote extra_args
for i in "${extra_args[@]}"; do safe_args+=$(printf '%q ' "$i"); done
failures+=("$cmd $safe_args")
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)"
$cmd >/dev/null 2>"$TMP_ERR" 3>"$TMP_MIAOU"
mapfile -t stderr <"$TMP_ERR"
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}" "$TMP_MIAOU" || success=false
fi
if [[ $success == true ]]; then
echo -n .
return
fi
echo -n F
failures+=("$cmd")
return 1
}
function fail_stderr_grep {
((test_count++))
cmd="./test/stub/scenario.bash $1"
stderr_line="$2"
if ! $cmd 2>"$TMP_ERR" 3>/dev/null && grep -q "$stderr_line" "$TMP_ERR"; then
echo -n .
return
fi
echo -n F
failures+=("$cmd 2>&1 3>/dev/null | grep \"$stderr_line\"")
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>"$TMP_ERR" 3>/dev/null; then
mapfile -t stderr <"$TMP_ERR"
if [[ "${#stderr[@]}" == "$count" ]]; then
echo -n .
return
fi
fi
echo -n F
failures+=("$cmd 2>$TMP_ERR")
return 1
}
function fail_miaou_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>/dev/null 3>"$TMP_MIAOU"; then
mapfile -t miaou <"$TMP_MIAOU"
if [[ "${#miaou[@]}" == "$count" ]]; then
echo -n .
return
fi
fi
echo -n F
failures+=("$cmd 3>$TMP_MIAOU")
return 1
}
# main
set -uo pipefail
test_count=0
failures=()
mkdir -p "$TMP_DIRECTORY"
TIMEFORMAT=$'\nelapsed: %Rs'
time {
assert success
assert exit_0
assert return_conditional
assert_grep success_with_args 'count args: 2' -- 'A B' C
assert heredoc
assert pipe1
assert pipe2
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'
fail_stderr_count unbound_variable 1
fail_stderr_count child_error 4
fail_miaou_count command_error_multiline 7
fail_stderr_grep two_options 'ERROR: too many args 2, either single option or SCRIPT + args accepted!'
fail_stderr_grep script_not_found "ERROR: script 'doesnt_exist' not found!"
fail_stderr_grep no_script 'ERROR: script expected!'
}
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