summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorent Xicluna <florent.xicluna@gmail.com>2013-03-02 18:32:57 +0100
committerFlorent Xicluna <florent.xicluna@gmail.com>2013-03-02 18:32:57 +0100
commit27cd5cce4741f7f23dd970e8648ace9030b41fbc (patch)
tree0700782e1a53a7faa3276b858781b2da04b245be
parent20346e11223d4ad52a0885fbce0f862d32bd3846 (diff)
downloadpep8-27cd5cce4741f7f23dd970e8648ace9030b41fbc.tar.gz
More API tests
-rw-r--r--testsuite/support.py8
-rw-r--r--testsuite/test_all.py2
-rw-r--r--testsuite/test_api.py215
-rw-r--r--testsuite/test_shell.py12
4 files changed, 225 insertions, 12 deletions
diff --git a/testsuite/support.py b/testsuite/support.py
index 481117a..4bd3067 100644
--- a/testsuite/support.py
+++ b/testsuite/support.py
@@ -9,6 +9,14 @@ SELFTEST_REGEX = re.compile(r'\b(Okay|[EW]\d{3}):\s(.*)')
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
+class PseudoFile(list):
+ """Simplified file interface."""
+ write = list.append
+
+ def getvalue(self):
+ return ''.join(self)
+
+
class TestReport(StandardReport):
"""Collect the results for the tests."""
diff --git a/testsuite/test_all.py b/testsuite/test_all.py
index 9ccf2fe..5160900 100644
--- a/testsuite/test_all.py
+++ b/testsuite/test_all.py
@@ -17,7 +17,7 @@ class Pep8TestCase(unittest.TestCase):
def setUp(self):
self._style = pep8.StyleGuide(
paths=[os.path.join(ROOT_DIR, 'testsuite')],
- ignore=None, quiet=True)
+ select='E,W', quiet=True)
def test_doctest(self):
import doctest
diff --git a/testsuite/test_api.py b/testsuite/test_api.py
index 4fac0f5..8cde061 100644
--- a/testsuite/test_api.py
+++ b/testsuite/test_api.py
@@ -1,21 +1,36 @@
# -*- coding: utf-8 -*-
+import os.path
+import shlex
+import sys
import unittest
import pep8
+from testsuite.support import ROOT_DIR, PseudoFile
+
+E11 = os.path.join(ROOT_DIR, 'testsuite', 'E11.py')
class APITestCase(unittest.TestCase):
"""Test the public methods."""
def setUp(self):
+ self._saved_stdout = sys.stdout
+ self._saved_stderr = sys.stderr
self._saved_checks = pep8._checks
+ sys.stdout = PseudoFile()
+ sys.stderr = PseudoFile()
pep8._checks = dict((k, dict((f, (vals[0][:], vals[1]))
for (f, vals) in v.items()))
for (k, v) in self._saved_checks.items())
def tearDown(self):
+ sys.stdout = self._saved_stdout
+ sys.stderr = self._saved_stderr
pep8._checks = self._saved_checks
+ def reset(self):
+ del sys.stdout[:], sys.stderr[:]
+
def test_register_physical_check(self):
def check_dummy(physical_line, line_number):
if False:
@@ -27,6 +42,10 @@ class APITestCase(unittest.TestCase):
self.assertTrue('Z001' in codes)
self.assertEqual(args, ['physical_line', 'line_number'])
+ options = pep8.StyleGuide().options
+ self.assertTrue(any(func == check_dummy
+ for name, func, args in options.physical_checks))
+
def test_register_logical_check(self):
def check_dummy(logical_line, tokens):
if False:
@@ -44,6 +63,10 @@ class APITestCase(unittest.TestCase):
self.assertEqual(codes, ['Z401', 'Z402', 'Z403'])
self.assertEqual(args, ['logical_line', 'tokens'])
+ options = pep8.StyleGuide().options
+ self.assertTrue(any(func == check_dummy
+ for name, func, args in options.logical_checks))
+
def test_register_ast_check(self):
class DummyChecker(object):
def __init__(self, tree, filename):
@@ -59,6 +82,10 @@ class APITestCase(unittest.TestCase):
self.assertTrue('Z701' in codes)
self.assertTrue(args is None)
+ options = pep8.StyleGuide().options
+ self.assertTrue(any(cls == DummyChecker
+ for name, cls, args in options.ast_checks))
+
def test_register_invalid_check(self):
class DummyChecker(object):
def __init__(self, filename):
@@ -67,11 +94,11 @@ class APITestCase(unittest.TestCase):
def run(self):
if False:
yield
- pep8.register_check(DummyChecker, ['Z741'])
def check_dummy(logical, tokens):
if False:
yield
+ pep8.register_check(DummyChecker, ['Z741'])
pep8.register_check(check_dummy, ['Z441'])
for checkers in pep8._checks.values():
@@ -79,3 +106,189 @@ class APITestCase(unittest.TestCase):
self.assertTrue(check_dummy not in checkers)
self.assertRaises(TypeError, pep8.register_check)
+
+ def test_styleguide(self):
+ report = pep8.StyleGuide().check_files()
+ self.assertEqual(report.total_errors, 0)
+ self.assertFalse(sys.stdout)
+ self.assertFalse(sys.stderr)
+ self.reset()
+
+ report = pep8.StyleGuide().check_files(['missing-file'])
+ stdout = sys.stdout.getvalue().splitlines()
+ self.assertEqual(len(stdout), report.total_errors)
+ self.assertEqual(report.total_errors, 1)
+ self.assertTrue(stdout[0].startswith("missing-file:1:1: E902 IOError"))
+ self.assertFalse(sys.stderr)
+ self.reset()
+
+ report = pep8.StyleGuide().check_files([E11])
+ stdout = sys.stdout.getvalue().splitlines()
+ self.assertEqual(len(stdout), report.total_errors)
+ self.assertEqual(report.total_errors, 4)
+ self.assertFalse(sys.stderr)
+ self.reset()
+
+ # Passing the paths in the constructor gives same result
+ report = pep8.StyleGuide(paths=[E11]).check_files()
+ stdout = sys.stdout.getvalue().splitlines()
+ self.assertEqual(len(stdout), report.total_errors)
+ self.assertEqual(report.total_errors, 4)
+ self.assertFalse(sys.stderr)
+ self.reset()
+
+ def test_styleguide_options(self):
+ # Instanciate a simple checker
+ pep8style = pep8.StyleGuide(paths=[E11])
+
+ # Check style's attributes
+ self.assertEqual(pep8style.checker_class, pep8.Checker)
+ self.assertEqual(pep8style.paths, [E11])
+ self.assertEqual(pep8style.runner, pep8style.input_file)
+ self.assertEqual(pep8style.options.ignore_code, pep8style.ignore_code)
+ self.assertEqual(pep8style.options.paths, pep8style.paths)
+
+ # Check unset options
+ for o in ('benchmark', 'config', 'count', 'diff',
+ 'doctest', 'quiet', 'show_pep8', 'show_source',
+ 'statistics', 'testsuite', 'verbose'):
+ oval = getattr(pep8style.options, o)
+ self.assertTrue(oval in (None, False), msg='%s = %r' % (o, oval))
+
+ # Check default options
+ self.assertTrue(pep8style.options.repeat)
+ self.assertEqual(pep8style.options.benchmark_keys,
+ ['directories', 'files',
+ 'logical lines', 'physical lines'])
+ self.assertEqual(pep8style.options.exclude,
+ ['.svn', 'CVS', '.bzr', '.hg', '.git', '__pycache__'])
+ self.assertEqual(pep8style.options.filename, ['*.py'])
+ self.assertEqual(pep8style.options.format, 'default')
+ self.assertEqual(pep8style.options.select, ())
+ self.assertEqual(pep8style.options.ignore, ('E226', 'E24'))
+ self.assertEqual(pep8style.options.max_line_length, 79)
+
+ def test_styleguide_ignore_code(self):
+ def parse_argv(argstring):
+ _saved_argv = sys.argv
+ sys.argv = shlex.split('pep8 %s /dev/null' % argstring)
+ try:
+ return pep8.StyleGuide(parse_argv=True)
+ finally:
+ sys.argv = _saved_argv
+
+ options = parse_argv('').options
+ self.assertEqual(options.select, ())
+ self.assertEqual(options.ignore, ('E226', 'E24'))
+
+ options = parse_argv('--doctest').options
+ self.assertEqual(options.select, ())
+ self.assertEqual(options.ignore, ())
+
+ options = parse_argv('--ignore E,W').options
+ self.assertEqual(options.select, ())
+ self.assertEqual(options.ignore, ('E', 'W'))
+
+ options = parse_argv('--select E,W').options
+ self.assertEqual(options.select, ('E', 'W'))
+ self.assertEqual(options.ignore, ('',))
+
+ pep8style = pep8.StyleGuide(paths=[E11])
+ self.assertFalse(pep8style.ignore_code('E112'))
+ self.assertFalse(pep8style.ignore_code('W191'))
+ self.assertTrue(pep8style.ignore_code('E241'))
+
+ pep8style = pep8.StyleGuide(select='E', paths=[E11])
+ self.assertFalse(pep8style.ignore_code('E112'))
+ self.assertTrue(pep8style.ignore_code('W191'))
+ self.assertFalse(pep8style.ignore_code('E241'))
+
+ pep8style = pep8.StyleGuide(select='W', paths=[E11])
+ self.assertTrue(pep8style.ignore_code('E112'))
+ self.assertFalse(pep8style.ignore_code('W191'))
+ self.assertTrue(pep8style.ignore_code('E241'))
+
+ def test_styleguide_excluded(self):
+ pep8style = pep8.StyleGuide(paths=[E11])
+
+ self.assertFalse(pep8style.excluded('./foo/bar'))
+ self.assertFalse(pep8style.excluded('./foo/bar/main.py'))
+
+ self.assertTrue(pep8style.excluded('./CVS'))
+ self.assertTrue(pep8style.excluded('./subdir/CVS'))
+ self.assertTrue(pep8style.excluded('__pycache__'))
+ self.assertTrue(pep8style.excluded('./__pycache__'))
+ self.assertTrue(pep8style.excluded('subdir/__pycache__'))
+
+ self.assertFalse(pep8style.excluded('draftCVS'))
+ self.assertFalse(pep8style.excluded('./CVSoup'))
+ self.assertFalse(pep8style.excluded('./CVS/subdir'))
+
+ def test_styleguide_checks(self):
+ pep8style = pep8.StyleGuide(paths=[E11])
+
+ # Default lists of checkers
+ self.assertTrue(len(pep8style.options.physical_checks) > 5)
+ self.assertTrue(len(pep8style.options.logical_checks) > 10)
+ self.assertEqual(len(pep8style.options.ast_checks), 0)
+
+ # Sanity check
+ for name, check, args in pep8style.options.physical_checks:
+ self.assertEqual(check.__name__, name)
+ self.assertEqual(args[0], 'physical_line')
+ for name, check, args in pep8style.options.logical_checks:
+ self.assertEqual(check.__name__, name)
+ self.assertEqual(args[0], 'logical_line')
+
+ # Do run E11 checks
+ options = pep8.StyleGuide().options
+ self.assertTrue(any(func == pep8.indentation
+ for name, func, args in options.logical_checks))
+ options = pep8.StyleGuide(select=['E']).options
+ self.assertTrue(any(func == pep8.indentation
+ for name, func, args in options.logical_checks))
+ options = pep8.StyleGuide(ignore=['W']).options
+ self.assertTrue(any(func == pep8.indentation
+ for name, func, args in options.logical_checks))
+ options = pep8.StyleGuide(ignore=['E12']).options
+ self.assertTrue(any(func == pep8.indentation
+ for name, func, args in options.logical_checks))
+
+ # Do not run E11 checks
+ options = pep8.StyleGuide(select=['W']).options
+ self.assertFalse(any(func == pep8.indentation
+ for name, func, args in options.logical_checks))
+ options = pep8.StyleGuide(ignore=['E']).options
+ self.assertFalse(any(func == pep8.indentation
+ for name, func, args in options.logical_checks))
+ options = pep8.StyleGuide(ignore=['E11']).options
+ self.assertFalse(any(func == pep8.indentation
+ for name, func, args in options.logical_checks))
+
+ def test_styleguide_init_report(self):
+ pep8style = pep8.StyleGuide(paths=[E11])
+
+ self.assertEqual(pep8style.options.reporter, pep8.StandardReport)
+ self.assertEqual(type(pep8style.options.report), pep8.StandardReport)
+
+ class MinorityReport(pep8.BaseReport):
+ pass
+
+ report = pep8style.init_report(MinorityReport)
+ self.assertEqual(pep8style.options.report, report)
+ self.assertEqual(type(report), MinorityReport)
+
+ pep8style = pep8.StyleGuide(paths=[E11], reporter=MinorityReport)
+ self.assertEqual(type(pep8style.options.report), MinorityReport)
+ self.assertEqual(pep8style.options.reporter, MinorityReport)
+
+ def test_styleguide_check_files(self):
+ pep8style = pep8.StyleGuide(paths=[E11])
+
+ report = pep8style.check_files()
+ self.assertTrue(report.total_errors)
+
+ self.assertRaises(TypeError, pep8style.check_files, 42)
+ self.assertRaises(TypeError, pep8style.check_files, [42])
+ # TODO: runner
+ # TODO: input_file
diff --git a/testsuite/test_shell.py b/testsuite/test_shell.py
index 7c88efb..4317bca 100644
--- a/testsuite/test_shell.py
+++ b/testsuite/test_shell.py
@@ -4,15 +4,7 @@ import sys
import unittest
import pep8
-from testsuite.support import ROOT_DIR
-
-
-class PseudoFile(list):
- """Simplified file interface."""
- write = list.append
-
- def __str__(self):
- return ''.join(self)
+from testsuite.support import ROOT_DIR, PseudoFile
class ShellTestCase(unittest.TestCase):
@@ -52,7 +44,7 @@ class ShellTestCase(unittest.TestCase):
errorcode = None
except SystemExit:
errorcode = sys.exc_info()[1].code
- return str(sys.stdout), str(sys.stderr), errorcode
+ return sys.stdout.getvalue(), sys.stderr.getvalue(), errorcode
def test_print_usage(self):
stdout, stderr, errcode = self.pep8('--help')