11# -*- coding: utf-8 -*-
2+ import os .path
3+ import shlex
4+ import sys
25import unittest
36
47import pep8
8+ from testsuite .support import ROOT_DIR , PseudoFile
9+
10+ E11 = os .path .join (ROOT_DIR , 'testsuite' , 'E11.py' )
511
612
713class APITestCase (unittest .TestCase ):
814 """Test the public methods."""
915
1016 def setUp (self ):
17+ self ._saved_stdout = sys .stdout
18+ self ._saved_stderr = sys .stderr
1119 self ._saved_checks = pep8 ._checks
20+ sys .stdout = PseudoFile ()
21+ sys .stderr = PseudoFile ()
1222 pep8 ._checks = dict ((k , dict ((f , (vals [0 ][:], vals [1 ]))
1323 for (f , vals ) in v .items ()))
1424 for (k , v ) in self ._saved_checks .items ())
1525
1626 def tearDown (self ):
27+ sys .stdout = self ._saved_stdout
28+ sys .stderr = self ._saved_stderr
1729 pep8 ._checks = self ._saved_checks
1830
31+ def reset (self ):
32+ del sys .stdout [:], sys .stderr [:]
33+
1934 def test_register_physical_check (self ):
2035 def check_dummy (physical_line , line_number ):
2136 if False :
@@ -27,6 +42,10 @@ def check_dummy(physical_line, line_number):
2742 self .assertTrue ('Z001' in codes )
2843 self .assertEqual (args , ['physical_line' , 'line_number' ])
2944
45+ options = pep8 .StyleGuide ().options
46+ self .assertTrue (any (func == check_dummy
47+ for name , func , args in options .physical_checks ))
48+
3049 def test_register_logical_check (self ):
3150 def check_dummy (logical_line , tokens ):
3251 if False :
@@ -44,6 +63,10 @@ def check_dummy(logical_line, tokens):
4463 self .assertEqual (codes , ['Z401' , 'Z402' , 'Z403' ])
4564 self .assertEqual (args , ['logical_line' , 'tokens' ])
4665
66+ options = pep8 .StyleGuide ().options
67+ self .assertTrue (any (func == check_dummy
68+ for name , func , args in options .logical_checks ))
69+
4770 def test_register_ast_check (self ):
4871 class DummyChecker (object ):
4972 def __init__ (self , tree , filename ):
@@ -59,6 +82,10 @@ def run(self):
5982 self .assertTrue ('Z701' in codes )
6083 self .assertTrue (args is None )
6184
85+ options = pep8 .StyleGuide ().options
86+ self .assertTrue (any (cls == DummyChecker
87+ for name , cls , args in options .ast_checks ))
88+
6289 def test_register_invalid_check (self ):
6390 class DummyChecker (object ):
6491 def __init__ (self , filename ):
@@ -67,15 +94,201 @@ def __init__(self, filename):
6794 def run (self ):
6895 if False :
6996 yield
70- pep8 .register_check (DummyChecker , ['Z741' ])
7197
7298 def check_dummy (logical , tokens ):
7399 if False :
74100 yield
101+ pep8 .register_check (DummyChecker , ['Z741' ])
75102 pep8 .register_check (check_dummy , ['Z441' ])
76103
77104 for checkers in pep8 ._checks .values ():
78105 self .assertTrue (DummyChecker not in checkers )
79106 self .assertTrue (check_dummy not in checkers )
80107
81108 self .assertRaises (TypeError , pep8 .register_check )
109+
110+ def test_styleguide (self ):
111+ report = pep8 .StyleGuide ().check_files ()
112+ self .assertEqual (report .total_errors , 0 )
113+ self .assertFalse (sys .stdout )
114+ self .assertFalse (sys .stderr )
115+ self .reset ()
116+
117+ report = pep8 .StyleGuide ().check_files (['missing-file' ])
118+ stdout = sys .stdout .getvalue ().splitlines ()
119+ self .assertEqual (len (stdout ), report .total_errors )
120+ self .assertEqual (report .total_errors , 1 )
121+ self .assertTrue (stdout [0 ].startswith ("missing-file:1:1: E902 IOError" ))
122+ self .assertFalse (sys .stderr )
123+ self .reset ()
124+
125+ report = pep8 .StyleGuide ().check_files ([E11 ])
126+ stdout = sys .stdout .getvalue ().splitlines ()
127+ self .assertEqual (len (stdout ), report .total_errors )
128+ self .assertEqual (report .total_errors , 4 )
129+ self .assertFalse (sys .stderr )
130+ self .reset ()
131+
132+ # Passing the paths in the constructor gives same result
133+ report = pep8 .StyleGuide (paths = [E11 ]).check_files ()
134+ stdout = sys .stdout .getvalue ().splitlines ()
135+ self .assertEqual (len (stdout ), report .total_errors )
136+ self .assertEqual (report .total_errors , 4 )
137+ self .assertFalse (sys .stderr )
138+ self .reset ()
139+
140+ def test_styleguide_options (self ):
141+ # Instanciate a simple checker
142+ pep8style = pep8 .StyleGuide (paths = [E11 ])
143+
144+ # Check style's attributes
145+ self .assertEqual (pep8style .checker_class , pep8 .Checker )
146+ self .assertEqual (pep8style .paths , [E11 ])
147+ self .assertEqual (pep8style .runner , pep8style .input_file )
148+ self .assertEqual (pep8style .options .ignore_code , pep8style .ignore_code )
149+ self .assertEqual (pep8style .options .paths , pep8style .paths )
150+
151+ # Check unset options
152+ for o in ('benchmark' , 'config' , 'count' , 'diff' ,
153+ 'doctest' , 'quiet' , 'show_pep8' , 'show_source' ,
154+ 'statistics' , 'testsuite' , 'verbose' ):
155+ oval = getattr (pep8style .options , o )
156+ self .assertTrue (oval in (None , False ), msg = '%s = %r' % (o , oval ))
157+
158+ # Check default options
159+ self .assertTrue (pep8style .options .repeat )
160+ self .assertEqual (pep8style .options .benchmark_keys ,
161+ ['directories' , 'files' ,
162+ 'logical lines' , 'physical lines' ])
163+ self .assertEqual (pep8style .options .exclude ,
164+ ['.svn' , 'CVS' , '.bzr' , '.hg' , '.git' , '__pycache__' ])
165+ self .assertEqual (pep8style .options .filename , ['*.py' ])
166+ self .assertEqual (pep8style .options .format , 'default' )
167+ self .assertEqual (pep8style .options .select , ())
168+ self .assertEqual (pep8style .options .ignore , ('E226' , 'E24' ))
169+ self .assertEqual (pep8style .options .max_line_length , 79 )
170+
171+ def test_styleguide_ignore_code (self ):
172+ def parse_argv (argstring ):
173+ _saved_argv = sys .argv
174+ sys .argv = shlex .split ('pep8 %s /dev/null' % argstring )
175+ try :
176+ return pep8 .StyleGuide (parse_argv = True )
177+ finally :
178+ sys .argv = _saved_argv
179+
180+ options = parse_argv ('' ).options
181+ self .assertEqual (options .select , ())
182+ self .assertEqual (options .ignore , ('E226' , 'E24' ))
183+
184+ options = parse_argv ('--doctest' ).options
185+ self .assertEqual (options .select , ())
186+ self .assertEqual (options .ignore , ())
187+
188+ options = parse_argv ('--ignore E,W' ).options
189+ self .assertEqual (options .select , ())
190+ self .assertEqual (options .ignore , ('E' , 'W' ))
191+
192+ options = parse_argv ('--select E,W' ).options
193+ self .assertEqual (options .select , ('E' , 'W' ))
194+ self .assertEqual (options .ignore , ('' ,))
195+
196+ pep8style = pep8 .StyleGuide (paths = [E11 ])
197+ self .assertFalse (pep8style .ignore_code ('E112' ))
198+ self .assertFalse (pep8style .ignore_code ('W191' ))
199+ self .assertTrue (pep8style .ignore_code ('E241' ))
200+
201+ pep8style = pep8 .StyleGuide (select = 'E' , paths = [E11 ])
202+ self .assertFalse (pep8style .ignore_code ('E112' ))
203+ self .assertTrue (pep8style .ignore_code ('W191' ))
204+ self .assertFalse (pep8style .ignore_code ('E241' ))
205+
206+ pep8style = pep8 .StyleGuide (select = 'W' , paths = [E11 ])
207+ self .assertTrue (pep8style .ignore_code ('E112' ))
208+ self .assertFalse (pep8style .ignore_code ('W191' ))
209+ self .assertTrue (pep8style .ignore_code ('E241' ))
210+
211+ def test_styleguide_excluded (self ):
212+ pep8style = pep8 .StyleGuide (paths = [E11 ])
213+
214+ self .assertFalse (pep8style .excluded ('./foo/bar' ))
215+ self .assertFalse (pep8style .excluded ('./foo/bar/main.py' ))
216+
217+ self .assertTrue (pep8style .excluded ('./CVS' ))
218+ self .assertTrue (pep8style .excluded ('./subdir/CVS' ))
219+ self .assertTrue (pep8style .excluded ('__pycache__' ))
220+ self .assertTrue (pep8style .excluded ('./__pycache__' ))
221+ self .assertTrue (pep8style .excluded ('subdir/__pycache__' ))
222+
223+ self .assertFalse (pep8style .excluded ('draftCVS' ))
224+ self .assertFalse (pep8style .excluded ('./CVSoup' ))
225+ self .assertFalse (pep8style .excluded ('./CVS/subdir' ))
226+
227+ def test_styleguide_checks (self ):
228+ pep8style = pep8 .StyleGuide (paths = [E11 ])
229+
230+ # Default lists of checkers
231+ self .assertTrue (len (pep8style .options .physical_checks ) > 5 )
232+ self .assertTrue (len (pep8style .options .logical_checks ) > 10 )
233+ self .assertEqual (len (pep8style .options .ast_checks ), 0 )
234+
235+ # Sanity check
236+ for name , check , args in pep8style .options .physical_checks :
237+ self .assertEqual (check .__name__ , name )
238+ self .assertEqual (args [0 ], 'physical_line' )
239+ for name , check , args in pep8style .options .logical_checks :
240+ self .assertEqual (check .__name__ , name )
241+ self .assertEqual (args [0 ], 'logical_line' )
242+
243+ # Do run E11 checks
244+ options = pep8 .StyleGuide ().options
245+ self .assertTrue (any (func == pep8 .indentation
246+ for name , func , args in options .logical_checks ))
247+ options = pep8 .StyleGuide (select = ['E' ]).options
248+ self .assertTrue (any (func == pep8 .indentation
249+ for name , func , args in options .logical_checks ))
250+ options = pep8 .StyleGuide (ignore = ['W' ]).options
251+ self .assertTrue (any (func == pep8 .indentation
252+ for name , func , args in options .logical_checks ))
253+ options = pep8 .StyleGuide (ignore = ['E12' ]).options
254+ self .assertTrue (any (func == pep8 .indentation
255+ for name , func , args in options .logical_checks ))
256+
257+ # Do not run E11 checks
258+ options = pep8 .StyleGuide (select = ['W' ]).options
259+ self .assertFalse (any (func == pep8 .indentation
260+ for name , func , args in options .logical_checks ))
261+ options = pep8 .StyleGuide (ignore = ['E' ]).options
262+ self .assertFalse (any (func == pep8 .indentation
263+ for name , func , args in options .logical_checks ))
264+ options = pep8 .StyleGuide (ignore = ['E11' ]).options
265+ self .assertFalse (any (func == pep8 .indentation
266+ for name , func , args in options .logical_checks ))
267+
268+ def test_styleguide_init_report (self ):
269+ pep8style = pep8 .StyleGuide (paths = [E11 ])
270+
271+ self .assertEqual (pep8style .options .reporter , pep8 .StandardReport )
272+ self .assertEqual (type (pep8style .options .report ), pep8 .StandardReport )
273+
274+ class MinorityReport (pep8 .BaseReport ):
275+ pass
276+
277+ report = pep8style .init_report (MinorityReport )
278+ self .assertEqual (pep8style .options .report , report )
279+ self .assertEqual (type (report ), MinorityReport )
280+
281+ pep8style = pep8 .StyleGuide (paths = [E11 ], reporter = MinorityReport )
282+ self .assertEqual (type (pep8style .options .report ), MinorityReport )
283+ self .assertEqual (pep8style .options .reporter , MinorityReport )
284+
285+ def test_styleguide_check_files (self ):
286+ pep8style = pep8 .StyleGuide (paths = [E11 ])
287+
288+ report = pep8style .check_files ()
289+ self .assertTrue (report .total_errors )
290+
291+ self .assertRaises (TypeError , pep8style .check_files , 42 )
292+ self .assertRaises (TypeError , pep8style .check_files , [42 ])
293+ # TODO: runner
294+ # TODO: input_file
0 commit comments