Skip to content

Instantly share code, notes, and snippets.

@praveenc
Last active December 30, 2018 15:42
Show Gist options
  • Save praveenc/b3ff4655404e0670695f7a5ae6157fff to your computer and use it in GitHub Desktop.
Save praveenc/b3ff4655404e0670695f7a5ae6157fff to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#coding=utf-8
# --------------------------------------------------------------------
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
#
# Author:
# Praveen Chamarthi
# --------------------------------------------------------------------
import argparse
import subprocess
def parse_args():
pass
def yapf_lint():
print('------ [YAPF] --------')
try:
result = subprocess.run(['yapf', '--diff', '-r', 'src', 'tests'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).stdout.decode('utf-8')
if result:
print(result)
subprocess.run(['yapf', '-i', '-r', 'src', 'tests'],
stdout=subprocess.PIPE,
check=True).stdout.decode('utf-8')
print('ERROR: Lint Errors found')
print('INFO: Applied Lint fixes .. commit linted files')
raise SystemExit('commit failed')
else:
print('INFO: Lint checks OK! **')
except subprocess.CalledProcessError:
raise SystemExit('ERROR: Failed to execute YAPF, activate venv')
def run_tests():
print('------ [PYTEST]--------')
try:
result = subprocess.run(
['pytest', 'tests', '--cov=src', '--cov-fail-under=75'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).stdout.decode('utf-8')
if 'FAIL' in result:
print(result)
print('ERROR: Test coverage not reached (75%) **')
raise SystemExit('commit failed')
if not result.strip():
print('ERROR: Failed to execute PYTEST **')
print(' is your venv active?')
print(' Ensure pytest is available to execute')
raise SystemExit('commit failed')
else:
print(result)
print('INFO: Test coverage OK! **')
except subprocess.CalledProcessError:
print(result)
raise SystemExit('ERROR: Failed to execute PYTEST, activate venv')
def main(args=None):
yapf_lint()
run_tests()
if __name__ == "__main__":
args = parse_args()
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment