@@ -180,32 +180,42 @@ def print_output(self, old_text, new_text, filename, equal):
180180 def check_file_refactoring (self , test_file , fixers = _2TO3_FIXERS ,
181181 options = None , mock_log_debug = None ,
182182 actually_write = True ):
183- tmpdir = tempfile .mkdtemp (prefix = "2to3-test_refactor" )
184- self .addCleanup (shutil .rmtree , tmpdir )
185- # make a copy of the tested file that we can write to
186- shutil .copy (test_file , tmpdir )
187- test_file = os .path .join (tmpdir , os .path .basename (test_file ))
188- os .chmod (test_file , 0o644 )
189-
190- def read_file ():
191- with open (test_file , "rb" ) as fp :
192- return fp .read ()
193-
194- old_contents = read_file ()
183+ test_file = self .init_test_file (test_file )
184+ old_contents = self .read_file (test_file )
195185 rt = self .rt (fixers = fixers , options = options )
196186 if mock_log_debug :
197187 rt .log_debug = mock_log_debug
198188
199189 rt .refactor_file (test_file )
200- self .assertEqual (old_contents , read_file ())
190+ self .assertEqual (old_contents , self . read_file (test_file ))
201191
202192 if not actually_write :
203193 return
204194 rt .refactor_file (test_file , True )
205- new_contents = read_file ()
195+ new_contents = self . read_file (test_file )
206196 self .assertNotEqual (old_contents , new_contents )
207197 return new_contents
208198
199+ def init_test_file (self , test_file ):
200+ tmpdir = tempfile .mkdtemp (prefix = "2to3-test_refactor" )
201+ self .addCleanup (shutil .rmtree , tmpdir )
202+ shutil .copy (test_file , tmpdir )
203+ test_file = os .path .join (tmpdir , os .path .basename (test_file ))
204+ os .chmod (test_file , 0o644 )
205+ return test_file
206+
207+ def read_file (self , test_file ):
208+ with open (test_file , "rb" ) as fp :
209+ return fp .read ()
210+
211+ def refactor_file (self , test_file , fixers = _2TO3_FIXERS ):
212+ test_file = self .init_test_file (test_file )
213+ old_contents = self .read_file (test_file )
214+ rt = self .rt (fixers = fixers )
215+ rt .refactor_file (test_file , True )
216+ new_contents = self .read_file (test_file )
217+ return old_contents , new_contents
218+
209219 def test_refactor_file (self ):
210220 test_file = os .path .join (FIXER_DIR , "parrot_example.py" )
211221 self .check_file_refactoring (test_file , _DEFAULT_FIXERS )
@@ -285,6 +295,12 @@ def test_crlf_newlines(self):
285295 finally :
286296 os .linesep = old_sep
287297
298+ def test_crlf_unchanged (self ):
299+ fn = os .path .join (TEST_DATA_DIR , "crlf.py" )
300+ old , new = self .refactor_file (fn )
301+ self .assertIn (b"\r \n " , old )
302+ self .assertIn (b"\r \n " , new )
303+
288304 def test_refactor_docstring (self ):
289305 rt = self .rt ()
290306
0 commit comments