python-mylib/tests.sh

78 lines
1.6 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
QUIET_ARG=""
NO_VENV=0
function usage() {
[ -n "$1" ] && echo -e "$1\n" > /dev/stderr
echo "Usage: $0 [-x] [-q|--quiet] [--no-venv]"
echo " -h/--help Show usage message"
echo " -q/--quiet Enable quiet mode"
echo " -n/--no-venv Disable venv creation and run tests on system environment"
echo " -x Enable debug mode"
[ -n "$1" ] && exit 1
exit 0
}
idx=1
while [ $idx -le $# ]
do
OPT=${!idx}
case $OPT in
-h|--help)
usage
;;
-q|--quiet)
QUIET_ARG="--quiet"
;;
-n|--no-venv)
NO_VENV=1
;;
-x)
set -x
;;
*)
2024-03-15 09:52:23 +01:00
usage "Unknown parameter '$OPT'"
esac
let idx=idx+1
done
[ "$1" == "--quiet" ] && QUIET_ARG="--quiet"
# Enter source directory
cd $( dirname $0 )
TEMP_VENV=0
VENV=""
if [ $NO_VENV -eq 1 ]
then
echo "Run tests in system environment..."
elif [ -d venv ]
then
VENV=$( realpath venv )
echo "Using existing virtualenv ($VENV)..."
else
# Create a temporary venv
VENV=$(mktemp -d)
echo "Create a temporary virtualenv in $VENV..."
TEMP_VENV=1
python3 -m venv $VENV
fi
if [ -n "$VENV" ]
then
echo "Install package with dev dependencies using pip in virtualenv..."
$VENV/bin/python3 -m pip install -e ".[dev]" $QUIET_ARG
source $VENV/bin/activate
fi
2023-01-06 22:18:46 +01:00
# Run pre-commit
2023-01-16 13:37:53 +01:00
RES=0
echo "Run pre-commit..."
pre-commit run --all-files
2023-01-06 22:18:46 +01:00
[ $? -ne 0 ] && RES=1
# Clean temporary venv
[ $TEMP_VENV -eq 1 ] && rm -fr $VENV
exit $RES