Skip to content

Instantly share code, notes, and snippets.

@josep11
Created November 23, 2021 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save josep11/0cfaf5892a409cc655f7c87da15deaed to your computer and use it in GitHub Desktop.
Save josep11/0cfaf5892a409cc655f7c87da15deaed to your computer and use it in GitHub Desktop.
pre-commit hook to run unit tests
#!/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
@josep11
Copy link
Author

josep11 commented Nov 23, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment