Skip to content

Instantly share code, notes, and snippets.

@callmewhy
Forked from swenson/test_pep8.py
Last active September 22, 2016 15:27
Show Gist options
  • Save callmewhy/a97175b451381dc6a895c235a0b625d1 to your computer and use it in GitHub Desktop.
Save callmewhy/a97175b451381dc6a895c235a0b625d1 to your computer and use it in GitHub Desktop.
Test for running PEP8 against all Python files. Useful to hook up to nose and as part of your CI.I modify PEP8 in two ways by default: don't enforce 4-spacing (I prefer 2-spacing), and use 100 for the default line length, because we don't use 80-column punch cards anymore.Licensed under CC0 (public domain): do what you want. http://creativecommo…
import os
import os.path
import unittest
import pep8
ignore_patterns = ('.git',)
def ignore(check_dir):
for pattern in ignore_patterns:
if pattern in check_dir:
return True
return False
class TestPep8(unittest.TestCase):
def test_pep8(self):
style = pep8.StyleGuide(config_file=os.path.join('.', 'setup.cfg'))
errors = 0
for root, _, files in os.walk('.'):
if ignore(root):
continue
python_files = [os.path.join(root, f) for f in files if f.endswith('.py')]
check = style.check_files(python_files)
errors = check.total_errors
self.assertEqual(errors, 0, 'PEP8 style errors')
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment