Skip to content

Instantly share code, notes, and snippets.

@alekssamos
Forked from josep11/pre-commit
Created July 19, 2022 16:00
Show Gist options
  • Save alekssamos/ec4ab50d99b586495ee7ce8ffd7b6713 to your computer and use it in GitHub Desktop.
Save alekssamos/ec4ab50d99b586495ee7ce8ffd7b6713 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
@alekssamos
Copy link
Author

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