2121# along with this program. If not, see <http://www.gnu.org/licenses/>.
2222#
2323
24-
2524__author__ = "Hartmut Goebel <h.goebel@crazy-compilers.com>"
2625__copyright__ = "Copyright 2015-2019 by Hartmut Goebel"
2726__licence__ = "GNU General Public License version 3 or later (GPLv3+)"
2827
2928
30- import pytest
31-
32-
29+ import glob
30+ import logging
3331import os
34- from os .path import join , abspath
3532import re
36- import glob
37- import shutil
38- from difflib import unified_diff
3933import unittest
40- import logging
34+ from difflib import unified_diff
35+ from os .path import abspath , join
4136
37+ import pytest
4238from fissix .main import main
4339
4440# make logging less verbose
45- logging .getLogger ('fissix.main' ).setLevel (logging .WARN )
46- logging .getLogger ('RefactoringTool' ).setLevel (logging .WARN )
41+ logging .getLogger ("fissix.main" ).setLevel (logging .WARN )
42+ logging .getLogger ("RefactoringTool" ).setLevel (logging .WARN )
43+
44+ FIXTURE_PATH = os .path .join (os .path .dirname (__file__ ), "fixtures" )
4745
48- FIXTURE_PATH = os .path .join (os .path .dirname (__file__ ), 'fixtures' )
4946
5047def requiredTestMethod (name ):
5148 # skip if TestCase does not have this method
5249 is_missing = getattr (unittest .TestCase , name , None ) is None
53- return pytest .mark .skipif (is_missing ,
54- reason = "unittest does not have TestCase.%s " % name )
50+ return pytest .mark .skipif (
51+ is_missing , reason = "unittest does not have TestCase.%s " % name
52+ )
5553
5654
5755def _collect_in_files_from_directory (directory ):
58- fixture_files = glob .glob (abspath (join (directory , ' *_in.py' )))
56+ fixture_files = glob .glob (abspath (join (directory , " *_in.py" )))
5957 for fixture_file in fixture_files :
6058 with open (fixture_file ) as fh :
6159 text = fh .read (200 )
62- l = re .findall (r' ^# required-method: (\S+)' , text )
60+ l = re .findall (r" ^# required-method: (\S+)" , text )
6361 method = l [0 ] if l else None
6462 yield fixture_file , method
6563
@@ -70,7 +68,7 @@ def collect_all_test_fixtures():
7068 # subdirectory, only run the fixer of the subdirectory name, else run
7169 # all fixers.
7270 for in_file , method in _collect_in_files_from_directory (root ):
73- fixer_to_run = root [len (FIXTURE_PATH )+ 1 :] or None
71+ fixer_to_run = root [len (FIXTURE_PATH ) + 1 :] or None
7472 marks = []
7573 if method :
7674 marks .append (requiredTestMethod (method ))
@@ -82,20 +80,39 @@ def _get_id(argvalue):
8280 return os .path .basename (argvalue ).replace ("_in.py" , "" )
8381
8482
85- @pytest .mark .parametrize ("fixer, in_file" ,
86- collect_all_test_fixtures (), ids = _get_id )
83+ @pytest .mark .parametrize ("fixer, in_file" , collect_all_test_fixtures (), ids = _get_id )
8784def test_check_fixture (in_file , fixer , tmpdir ):
8885 if fixer :
89- main ("unittest2pytest.fixes" ,
90- args = ['--no-diffs' , '--fix' , fixer , '-w' , in_file ,
91- '--nobackups' , '--output-dir' , str (tmpdir )])
86+ main (
87+ "unittest2pytest.fixes" ,
88+ args = [
89+ "--no-diffs" ,
90+ "--fix" ,
91+ fixer ,
92+ "-w" ,
93+ in_file ,
94+ "--nobackups" ,
95+ "--output-dir" ,
96+ str (tmpdir ),
97+ ],
98+ )
9299 else :
93- main ("unittest2pytest.fixes" ,
94- args = ['--no-diffs' , '--fix' , 'all' , '-w' , in_file ,
95- '--nobackups' , '--output-dir' , str (tmpdir )])
100+ main (
101+ "unittest2pytest.fixes" ,
102+ args = [
103+ "--no-diffs" ,
104+ "--fix" ,
105+ "all" ,
106+ "-w" ,
107+ in_file ,
108+ "--nobackups" ,
109+ "--output-dir" ,
110+ str (tmpdir ),
111+ ],
112+ )
96113
97114 result_file_name = tmpdir .join (os .path .basename (in_file ))
98- assert result_file_name .exists (), ' %s is missing' % result_file_name
115+ assert result_file_name .exists (), " %s is missing" % result_file_name
99116 result_file_contents = result_file_name .readlines ()
100117
101118 expected_file = in_file .replace ("_in.py" , "_out.py" )
@@ -104,13 +121,15 @@ def test_check_fixture(in_file, fixer, tmpdir):
104121
105122 # ensure the expected code is actually correct and compiles
106123 try :
107- compile ('' .join (expected_contents ), expected_file , ' exec' )
124+ compile ("" .join (expected_contents ), expected_file , " exec" )
108125 except Exception as e :
109- pytest .fail (f"FATAL: { expected_file } does not compile: { e } " ,
110- False )
126+ pytest .fail (f"FATAL: { expected_file } does not compile: { e } " , False )
111127
112128 if result_file_contents != expected_contents :
113129 text = "Refactured code doesn't match expected outcome\n "
114- text += '' .join (unified_diff (expected_contents , result_file_contents ,
115- 'expected' , 'refactured result' ))
130+ text += "" .join (
131+ unified_diff (
132+ expected_contents , result_file_contents , "expected" , "refactured result"
133+ )
134+ )
116135 pytest .fail (text , False )
0 commit comments