Git Nosetest Pre-commit Hook
I wrote a simple little git pre-comit hook today (inspired by Cosmin Stejerean’s hook).
It gets a list of directories from a git config setting tests.directories and runs nosetests against all directories.
Predictably, it doesn’t commit if any tests fail, and carries on if they all pass.
To enable, drop it into .git/hooks/, name it pre-commit and make it executable.
You can disable it by running git commit with the --no-verify option.
I think running all my tests before commit is a bit overkill, but as far as I’m aware there is no pre-push hook in git as yet.
Here it is…
#!/bin/bash
echo -e "======================================================================================\n"
echo " >>> Running pre-commit hook..."
dirs=`git-config --get tests.directories`
if [ -z "$dirs" ]
then
echo -e " >>> You have no tests.directories setting. Tests will not be run.\n >>> To enable tests on pre-commit run:\n\tgit config --add tests.directories <space separated list of test dirs>\n"
echo -e "======================================================================================\n"
exit 0
fi
echo -e " >>> Running tests ('nosetests $dirs')\n"
echo -e "======================================================================================\n"
nosetests $dirs
code=$?
echo -e "======================================================================================\n"
if [ "$code" != "0" ]
then
echo -e " >>> Tests failed. Commit aborted. To force the commit, run: \n\tgit commit --no-verify\n"
else
echo -e " >>> Tests passed. Committing...\n"
fi
echo -e "======================================================================================\n"
exit $code

Comments
Subscribe Make comment