Skip to content

Instantly share code, notes, and snippets.

@motleytech
Last active April 14, 2016 03:22
Show Gist options
  • Save motleytech/178f2cfe2feb5f023221c55eee28b0f9 to your computer and use it in GitHub Desktop.
Save motleytech/178f2cfe2feb5f023221c55eee28b0f9 to your computer and use it in GitHub Desktop.
Git pre-commit hook to verify tests updated for python files
#!/usr/bin/env python2.7
"""
Git pre-commit hook to check if tests have been added
or modified for staged python files.
__author__ = "Motleytech http://github.com/motleytech"
"""
# pylint: disable=C0103
import subprocess
import os
from colorama import Fore, Style
def run_command_and_get_output(command):
"""
As the name describes, this function runs a command
and returns its output back to the caller as a string.
"""
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
(stdoutdata, _) = proc.communicate()
return stdoutdata
def check_for_tests():
"""
Checks whether test files have been modified for any
modified and staged python files.
"""
root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))
os.chdir(root_dir)
result = run_command_and_get_output("git diff --name-only --cached")
tests_updated = True
changed_files = [res.strip() for res in result.split("\n")]
for fpath in changed_files:
if not fpath.endswith('.py'):
continue
fpath = fpath.strip()
dirpath, fname = os.path.split(fpath)
if fname.startswith('test_'):
continue
testdir = os.path.join(dirpath, 'tests')
testfilepath = os.path.join(testdir, 'test_' + fname)
if testfilepath not in changed_files:
if tests_updated is True:
tests_updated = False
print Fore.RED + 'Error: pre-commit check failed.' + Style.RESET_ALL
print 'Tests for following files missing (or not updated)...'
print fpath
if not tests_updated:
exit(1)
exit(0)
if __name__ == '__main__':
check_for_tests()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment