Created
November 23, 2021 11:04
-
-
Save josep11/0cfaf5892a409cc655f7c87da15deaed to your computer and use it in GitHub Desktop.
pre-commit hook to run unit tests
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
#!/bin/bash | |
current_branch=`git rev-parse --abbrev-ref HEAD` | |
if [[ $current_branch =~ master|main ]]; then | |
message="Please don't push directly to $current_branch." | |
echo -e "\033[1;31mERROR: $message\033[0m"; | |
exit 1 | |
fi | |
repo_dir=`git rev-parse --show-toplevel` | |
message="[Policy] Doing unit tests ..." | |
echo -e "\033[1;34mInfo: $message\033[0m" | |
# pytest $repo_dir/test.py | |
python -m unittest discover | |
if [ $? -eq 1 ]; then | |
message="[Policy] Unit tests failed, please check and fix your code." | |
echo -e "\033[1;31mERROR: $message\033[0m"; | |
exit 1 | |
else | |
message="[Policy] Passed unit tests." | |
echo -e "\033[1;32mOK: $message\033[0m" | |
exit 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More info on how to install them for new repos:
https://github.com/ddddavidmartin/Pre-commit-hooks/blob/master/pre-commit.example.cfg