Repository URL to install this package:
|
Version:
0.16.0 ▾
|
import fnmatch
import os
import re
import unittest
import pep8
from django.conf import settings
class CodeFormat(unittest.TestCase):
"""
common implementation for testing pep8 compliance of a project
"""
def get_files_to_pep8(self):
project_root = getattr(settings, 'PROJECT_ROOT')
exclude_pep8 = getattr(settings, 'EXCLUDE_PEP8', [])
matches = []
if project_root is not None:
for root, dirnames, filenames in os.walk(project_root):
for filename in fnmatch.filter(filenames, '*.py'):
if exclude_pep8:
for exclude_pattern in exclude_pep8:
pattern = re.compile(exclude_pattern)
if not pattern.findall(os.path.join(root, filename)):
matches.append(os.path.join(root, filename))
else:
matches.append(os.path.join(root, filename))
return matches
def pep8_conformance(self):
"""add this to your test to check for comformance"""
pep8style = pep8.StyleGuide(
quiet=False,
config_file=os.path.join(settings.PROJECT_ROOT, 'pep8')
)
files = self.get_files_to_pep8()
result = pep8style.check_files(files)
self.assertEqual(result.total_errors, 0,
"Found code style errors (and warnings).")