.githooks/pre-commit/unit-tests
#!/bin/sh
# run the tests
echo "> Running the unit tests ..."
HAS_DOCKER=$(docker version > /dev/null 2>&1; echo $?)
if [ -z "$SKIP_DOCKER" ] && [ "$HAS_DOCKER" -eq "0" ]; then
case $(arch) in
arm*|aarch64) FROM="arm32v6/python:2.7-alpine3.6" ;;
*) FROM="python:2.7-alpine" ;;
esac
docker build -t demo-site-test-runner -f - . << EOF > /dev/null
FROM ${FROM}
ADD requirements.txt /tmp/
RUN pip install -r /tmp/requirements.txt
CMD PYTHONPATH=src python -m unittest discover -s tests -v
EOF
if [ "$?" -ne "0" ]; then
exit 1
fi
TEST_OUTPUT=$(docker run --rm -v ${PWD}:/app:ro -w /app demo-site-test-runner 2>&1)
if [ "$?" -ne "0" ]; then
echo "! The tests have failed, output:"
echo "$TEST_OUTPUT"
exit 1
fi
else
# try running the test locally with Python
TEST_OUTPUT=$(PYTHONPATH=src python -m unittest discover -s tests -v 2>&1)
if [ "$?" -ne "0" ]; then
echo "! The tests have failed, output:"
echo "$TEST_OUTPUT"
exit 1
fi
fi
echo "* The unit tests have successfully finished"