Skip to content

Commit f9609fb

Browse files
committed
Reorganize the test suite using unittest.
1 parent 920c143 commit f9609fb

4 files changed

Lines changed: 100 additions & 59 deletions

File tree

pep8.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,9 +1844,7 @@ def _main():
18441844
pep8style = StyleGuide(parse_argv=True, config_file=True)
18451845
options = pep8style.options
18461846
if options.doctest or options.testsuite:
1847-
sys.path[:0] = [TESTSUITE_PATH]
1848-
from test_pep8 import run_tests
1849-
del sys.path[0]
1847+
from testsuite.support import run_tests
18501848
report = run_tests(pep8style, options.doctest, options.testsuite)
18511849
else:
18521850
report = pep8style.check_files()

testsuite/__init__.py

Whitespace-only changes.
Lines changed: 46 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
#!/usr/bin/env python
21
# -*- coding: utf-8 -*-
3-
import os.path
42
import re
53
import sys
64

7-
import pep8
8-
from pep8 import Checker, StyleGuide, BaseReport, StandardReport, readlines
5+
from pep8 import Checker, BaseReport, StandardReport, readlines
96

107
SELFTEST_REGEX = re.compile(r'\b(Okay|[EW]\d{3}):\s(.*)')
118

@@ -49,6 +46,49 @@ def print_results(self):
4946
print("Test failed." if self.total_errors else "Test passed.")
5047

5148

49+
def selftest(options):
50+
"""
51+
Test all check functions with test cases in docstrings.
52+
"""
53+
count_failed = count_all = 0
54+
report = BaseReport(options)
55+
counters = report.counters
56+
checks = options.physical_checks + options.logical_checks
57+
for name, check, argument_names in checks:
58+
for line in check.__doc__.splitlines():
59+
line = line.lstrip()
60+
match = SELFTEST_REGEX.match(line)
61+
if match is None:
62+
continue
63+
code, source = match.groups()
64+
lines = [part.replace(r'\t', '\t') + '\n'
65+
for part in source.split(r'\n')]
66+
checker = Checker(lines=lines, options=options, report=report)
67+
checker.check_all()
68+
error = None
69+
if code == 'Okay':
70+
if len(counters) > len(options.benchmark_keys):
71+
codes = [key for key in counters
72+
if key not in options.benchmark_keys]
73+
error = "incorrectly found %s" % ', '.join(codes)
74+
elif not counters.get(code):
75+
error = "failed to find %s" % code
76+
# Keep showing errors for multiple tests
77+
for key in set(counters) - set(options.benchmark_keys):
78+
del counters[key]
79+
report.messages = {}
80+
count_all += 1
81+
if not error:
82+
if options.verbose:
83+
print("%s: %s" % (code, source))
84+
else:
85+
count_failed += 1
86+
print("pep8.py: %s:" % error)
87+
for line in checker.lines:
88+
print(line.rstrip())
89+
return count_failed, count_all
90+
91+
5292
def init_tests(pep8style):
5393
"""
5494
Initialize testing framework.
@@ -98,49 +138,7 @@ def run_tests(filename):
98138
return report.counters['failed tests']
99139

100140
pep8style.runner = run_tests
101-
102-
103-
def selftest(options):
104-
"""
105-
Test all check functions with test cases in docstrings.
106-
"""
107-
count_failed = count_all = 0
108-
report = BaseReport(options)
109-
counters = report.counters
110-
checks = options.physical_checks + options.logical_checks
111-
for name, check, argument_names in checks:
112-
for line in check.__doc__.splitlines():
113-
line = line.lstrip()
114-
match = SELFTEST_REGEX.match(line)
115-
if match is None:
116-
continue
117-
code, source = match.groups()
118-
lines = [part.replace(r'\t', '\t') + '\n'
119-
for part in source.split(r'\n')]
120-
checker = Checker(lines=lines, options=options, report=report)
121-
checker.check_all()
122-
error = None
123-
if code == 'Okay':
124-
if len(counters) > len(options.benchmark_keys):
125-
codes = [key for key in counters
126-
if key not in options.benchmark_keys]
127-
error = "incorrectly found %s" % ', '.join(codes)
128-
elif not counters.get(code):
129-
error = "failed to find %s" % code
130-
# Keep showing errors for multiple tests
131-
for key in set(counters) - set(options.benchmark_keys):
132-
del counters[key]
133-
report.messages = {}
134-
count_all += 1
135-
if not error:
136-
if options.verbose:
137-
print("%s: %s" % (code, source))
138-
else:
139-
count_failed += 1
140-
print("%s: %s:" % (pep8.__file__, error))
141-
for line in checker.lines:
142-
print(line.rstrip())
143-
return count_failed, count_all
141+
init_tests.__test__ = False
144142

145143

146144
def run_tests(style, doctest=True, testsuite=False):
@@ -159,12 +157,4 @@ def run_tests(style, doctest=True, testsuite=False):
159157
if testsuite:
160158
init_tests(style)
161159
return style.check_files()
162-
163-
164-
if __name__ == '__main__':
165-
if len(sys.argv) > 1:
166-
sys.exit(pep8._main())
167-
pep8style = StyleGuide(paths=[os.path.dirname(__file__)], ignore=None)
168-
report = run_tests(pep8style, doctest=True, testsuite=True)
169-
report.print_results()
170-
sys.exit(1 if report.total_errors else 0)
160+
run_tests.__test__ = False

testsuite/test_all.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
import os.path
4+
import sys
5+
import unittest
6+
7+
import pep8
8+
from testsuite.support import init_tests, selftest
9+
10+
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
11+
12+
13+
class Pep8TestCase(unittest.TestCase):
14+
15+
def setUp(self):
16+
self._style = pep8.StyleGuide(
17+
paths=[os.path.dirname(__file__)],
18+
ignore=None, quiet=True)
19+
20+
def test_doctest(self):
21+
import doctest
22+
fail_d, done_d = doctest.testmod(pep8, verbose=False, report=False)
23+
self.assertTrue(done_d, msg='tests not found')
24+
self.assertFalse(fail_d, msg='%s failure(s)' % fail_d)
25+
26+
def test_selftest(self):
27+
fail_s, done_s = selftest(self._style.options)
28+
self.assertTrue(done_s, msg='tests not found')
29+
self.assertFalse(fail_s, msg='%s failure(s)' % fail_s)
30+
31+
def test_checkers_testsuite(self):
32+
init_tests(self._style)
33+
report = self._style.check_files()
34+
self.assertFalse(report.total_errors,
35+
msg='%s failure(s)' % report.total_errors)
36+
37+
def test_own_dog_food(self):
38+
files = [pep8.__file__.rstrip('oc'), __file__.rstrip('oc'),
39+
os.path.join(ROOT_DIR, 'setup.py')]
40+
report = self._style.init_report(pep8.StandardReport)
41+
report = self._style.check_files(files)
42+
self.assertFalse(report.total_errors,
43+
msg='Failures: %s' % report.messages)
44+
45+
46+
def _main():
47+
suite = unittest.TestSuite()
48+
suite.addTest(unittest.makeSuite(Pep8TestCase))
49+
runner = unittest.TextTestRunner(verbosity=2)
50+
return runner.run(suite)
51+
52+
if __name__ == '__main__':
53+
sys.exit(not _main())

0 commit comments

Comments
 (0)