-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathPEP8_PyUnit_pre-commit_hook
More file actions
33 lines (28 loc) · 1.14 KB
/
PEP8_PyUnit_pre-commit_hook
File metadata and controls
33 lines (28 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python3
#
# A pre-commit hook to verify PEP8 conformity using flake8
# and to run all available unit tests
import subprocess
from subprocess import CalledProcessError
import sys
import os
import glob
# Try to read the GIT_DIR envvar, which is set if this is being called as a git pre-commit hook.
try:
path, _ = os.path.split(os.path.abspath(os.environ['GIT_DIR']))
# If it is not set, a KeyError is raised, which is taken as if this is invoked from the command line, using the cwd instead.
# This requires calling this script from the correct directory!
except KeyError:
path = os.getcwd()
print("Checking " + path)
try:
subprocess.check_call(["flake8", path])
except CalledProcessError as e:
print("Found PEP8 non-conformities! Aborting commit...")
sys.exit(1)
for test_file in glob.glob(path + "/tests/*_tests.py"):
print("Running " + test_file)
if subprocess.call(["python3", "-m", "unittest", test_file], stdin=None, stdout=None, stderr=subprocess.DEVNULL) != 0:
print("Unit tests failed in " + test_file + "! Aborting commit...")
sys.exit(1)
print("Style check and unit tests successful, committing!")