1- #!/usr/bin/env python
21# -*- coding: utf-8 -*-
3- import os .path
42import re
53import sys
64
7- import pep8
8- from pep8 import Checker , StyleGuide , BaseReport , StandardReport , readlines
5+ from pep8 import Checker , BaseReport , StandardReport , readlines
96
107SELFTEST_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+
5292def 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
146144def 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
0 commit comments