Created
March 17, 2021 21:03
-
-
Save zeroSteiner/7e30697827e89e7708144fb3e605831f to your computer and use it in GitHub Desktop.
Python test harness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
py_stdout_fd=1 | |
py_stderr_fd=2 | |
delay=0 | |
timeout=10 | |
params=() | |
show_help () { | |
echo "usage: $(basename $0) [-h] [-d delay] [-t timeout] args" | |
echo "" | |
echo "positional arguments:" | |
echo " args the arguments to pass to Python" | |
echo "" | |
echo "optional arguments:" | |
echo " -d, --delay the amount of time in seconds to wait between tests (default: $delay)" | |
echo " -n, --null redirect Python's stdout and stderr to /dev/null" | |
echo " -t, --timeout the amount of time in seconds to wait for Python to exit (default: $timeout)" | |
echo " --null-err redirect Python's stderr to /dev/null" | |
echo " --null-out redirect Python's stdout to /dev/null" | |
} | |
while (( "$#" )); do | |
case "$1" in | |
-h|--help) | |
show_help | |
exit 0 | |
;; | |
-d|--delay) | |
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then | |
if ! [[ "$2" =~ ^[0-9]+$ ]] ; then | |
echo "Error: Argument for $1 must be a number ($2)" >&2 | |
exit 1 | |
fi | |
delay=$2 | |
shift 2 | |
else | |
echo "Error: Argument for $1 is missing" >&2 | |
exit 1 | |
fi | |
;; | |
-n|--null) | |
(( py_stderr_fd > 2)) && exec {py_stderr_fd}>&- | |
(( py_stdout_fd > 2)) && exec {py_stdout_fd}>&- | |
exec {py_stderr_fd}>/dev/null | |
exec {py_stdout_fd}>/dev/null | |
shift 1 | |
;; | |
--null-err) | |
(( py_stderr_fd > 2)) && exec {py_stderr_fd}>&- | |
exec {py_stderr_fd}>/dev/null | |
shift 1 | |
;; | |
--null-out) | |
(( py_stdout_fd > 2)) && exec {py_stdout_fd}>&- | |
exec {py_stdout_fd}>/dev/null | |
shift 1 | |
;; | |
-t|--timeout) | |
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then | |
if ! [[ "$2" =~ ^[0-9]+$ ]] ; then | |
echo "Error: Argument for $1 must be a number ($2)" >&2 | |
exit 1 | |
fi | |
timeout=$2 | |
shift 2 | |
else | |
echo "Error: Argument for $1 is missing" >&2 | |
exit 1 | |
fi | |
;; | |
--) # end argument parsing | |
shift | |
break | |
;; | |
-*|--*=) # unsupported flags | |
printf "\e[1;31m[-]\e[0m Unsupported flag $1\n" >&2 | |
exit 1 | |
;; | |
*) # preserve positional arguments | |
params+=("$1") | |
shift | |
;; | |
esac | |
done | |
params+=("$@") | |
if ! type "pyenv" &> /dev/null; then | |
printf "\e[1;31m[-]\e[0m Error: pyenv was not found\n" | |
exit 1 | |
fi | |
eval "$(pyenv init -)" | |
set +e | |
for version in $(ls $PYENV_ROOT/versions); do | |
printf "\e[1;34m[*]\e[0m [%-7s] Running...\n" "$version" | |
pyenv shell $version | |
timeout $timeout python "${params[@]}" 1>&"$py_stdout_fd" 2>&"$py_stderr_fd" | |
retval=$? | |
if [ $retval -ne 0 ]; then | |
printf "\e[1;31m[-]\e[0m [%-7s] Status: \e[1;5;31mFAILED WITH STATUS $retval\e[0m\n" "$version" | |
else | |
printf "\e[1;32m[+]\e[0m [%-7s] Status: \e[1;32mSUCCESSFUL\e[0m\n" "$version" | |
fi | |
if [ $delay -ne 0 ]; then | |
sleep $delay | |
fi | |
done | |
(( py_stdout_fd > 2)) && exec {py_stdout_fd}>&- | |
(( py_stderr_fd > 2)) && exec {py_stderr_fd}>&- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment