Skip to content

Instantly share code, notes, and snippets.

@Madalosso
Created November 28, 2016 15:38
Show Gist options
  • Save Madalosso/d06fd7bd7bfeaeffb6364d4d50fc9fb6 to your computer and use it in GitHub Desktop.
Save Madalosso/d06fd7bd7bfeaeffb6364d4d50fc9fb6 to your computer and use it in GitHub Desktop.

git-pre-push-tests

Just a thought about test execution and git hooks

I’m creating this post to share a practice that I decided to adopt recently: ###Bind test execution to git pushes.

#####First of all: Git Hooks. Git hooks are a way to execute scripts every time some important action is executed on git. (git hooks). Today we will use the hook pre-push to execute a project's tests to make sure that everything is ok before actually execute the push to the repository.

#####Git Hook Configuration. When you create a git repository, git creates a directory named ".git", inside this directory there's another one called "hooks" which have some git hooks samples. All you need to do is give permission to execute and rename the hook that you want to use removing the ".sample" from the filename and the hook will be activated.

$ mv project_root/.git/hooks/pre-push.sample project_root/.git/hooks/pre-push
$ chmod +x project_root/.git/hooks/pre-push

#####The script. In the following script, I access my virtual environment and then execute the comand to run my tests

#! /bin/bash
VENV="source ../virtual-env-ecommerce/bin/activate"
CMD="./manage.py test --settings=adrecommerce.settings.local --keepdb"
$VENV
$CMD
RESULT=$?
echo $RESULT
if [ $RESULT -ne 0 ]; then
    echo "TEST FAIL! => $CMD"
    exit 1
fi
exit 0

*Remember to give the pre-push file permition to execute.

This way, after every git push command, the script will be executed and will proced only if the tests pass.

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