Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 206 lines (185 sloc) 4.38 KB
#!/bin/bash
export PS4='+${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): } '
function __setup_colors()
{
local -r ncolors=$(tput colors)
[[ -n "$ncolors" && ncolors -ge 8 ]] || return
readonly pgreen=$(printf "\e[32m")
readonly pred=$(printf "\e[31m")
readonly pboldred=$(printf "\e[31;1m")
readonly preset=$(printf "\e[0m")
}
function __print_help {
local -r width=$(( $(tput cols) - 2 ))
cat <<EOH | fold -s -w $width
Usage: $0 [-c|--color][-d|--debug]|[-q|--quiet][-v|--verbose] TEST [TEST...]
Runs BASH test(s).
TEST can be either a single test file or a directory of test path files.
-c, --color Print in color
-d, --debug Print extra output
-q, --quiet Print minimal output
EOH
}
function __enable_debug()
{
export debug=true
export PS4='+${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): } '
set -o pipefail
set -o xtrace
echo "$0: Called with $@"
}
function _register {
case $# in
1 ) local -r name=$1 arity=0 ;;
2 ) local -r name=$1 arity=$2 ;;
esac
case $(type -t $name) in
function ) ;;
* ) echo "$0: $FUNCNAME: Not a function: $name" >&2
exit 3 ;;
esac
read -d '' -r wrapper <<EOF
function $name {
# Original function
$(declare -f $name | sed '1,2d;$d')
__e=\$?
shift $arity
if (( 0 < __e || 0 == \$# ))
then
__tally \$__e
else
true # Reset exit code
"\$@"
__e=\$?
__tally \$__e
fi
}
EOF
eval "$wrapper"
}
function _start {
local __tallied=false
local __e=0
pushd $PWD >/dev/null
"$@"
__tally $?
while popd >/dev/null 2>&1 ; do : ; done
}
let __passed=0 __failed=0 __errored=0
function __tally {
local -r __e=$1
$__tallied && return $__e
__tallied=true
case $__e in
0 ) let ++__passed ;;
1 ) let ++__failed ;;
* ) let ++__errored ;;
esac
_print_result $__e
return $__e
}
[[ -t 1 ]] && _color=true || _color=false
_quiet=false
_verbose=false
while getopts :cdhqv-: opt
do
[[ - == $opt ]] && opt=${OPTARG%%=*} OPTARG=${OPTARG#*=}
case $opt in
c | color ) _color=true ;;
d | debug ) __enable_debug "$@" ;;
h | help ) __print_help ; exit 0 ;;
q | quiet ) _quiet=true ;;
v | verbose ) _verbose=true ;;
* ) cat <<EOE >&2 ; exit 3 ;;
${0##*/}: unrecognized option '-$OPTARG'
Try 'cat --help' for more information.
EOE
esac
done
shift $((OPTIND - 1))
if [[ -t 1 ]] || $_color
then
__setup_colors
fi
case $# in
0 ) cat <<EOE >&2 ; exit 3 ;;
${0##*/}: missing TEST(s)
Try 'cat --help' for more information.
EOE
esac
{
pushd $(dirname $0)
root_dir=$(pwd -P)
popd
} >/dev/null
for f in $root_dir/f/*.sh
do
. $f
done
__tests=()
for t in "$@"
do
if [[ -d "$t" ]]
then
__found_tests+=($(find $t -type f -name \*.sh))
if (( 0 == ${#__found_tests[*]} ))
then
echo "${0##*/}: No tests found in '$t'" >&2
exit 2
fi
__tests+=("${__found_tests[@]}")
elif [[ -f "$t" ]]
then
__tests+=($t)
else
echo "${0##*/}: Not a test or directory: $t" >&2
exit 2
fi
done
set -- $(echo "${__tests[@]}" | sort -u)
if (( 0 == $# ))
then
echo "${0##*/}: No tests" >&2
exit 2
fi
for t in "$@"
do
if ! $_quiet
then
echo "${pbold}Script $t:${preset}"
fi
let __last_passed=$__passed
let __last_failed=$__failed
let __last_errored=$__errored
pushd $PWD >/dev/null
. $t
while popd >/dev/null 2>&1 ; do : ; done
let __t_passed=$((__passed - __last_passed))
let __t_failed=$((__failed - __last_failed))
let __t_errored=$((__errored - __last_errored))
if ! $_quiet
then
__all=()
(( 0 == __t_errored )) || __all+=("$__t_errored errored")
(( 0 == __t_failed )) || __all+=("$__t_failed failed")
(( 0 == __t_passed )) || __all+=("$__t_passed passed")
IFS=, eval '__s="${__all[*]}"'
echo "${__s//,/, }"
fi
done
if ! $_quiet
then
(( 0 == __passed )) && ppassed= || ppassed=${pgreen}
(( 0 == __failed )) && pfailed= || pfailed=${pred}
(( 0 == __errored )) && perrored= || perrored=${pboldred}
cat <<EOS
${pbold}Summary${preset}: ${ppassed}$__passed PASSED${preset}, ${pfailed}$__failed FAILED${preset}, ${perrored}$__errored ERRORED${preset}
EOS
fi
if (( 0 < __errored ))
then
exit 2
elif (( 0 < __failed ))
then
exit 1
fi