@@ -180,32 +180,42 @@ def print_output(self, old_text, new_text, filename, equal):
180
180
def check_file_refactoring (self , test_file , fixers = _2TO3_FIXERS ,
181
181
options = None , mock_log_debug = None ,
182
182
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 )
195
185
rt = self .rt (fixers = fixers , options = options )
196
186
if mock_log_debug :
197
187
rt .log_debug = mock_log_debug
198
188
199
189
rt .refactor_file (test_file )
200
- self .assertEqual (old_contents , read_file ())
190
+ self .assertEqual (old_contents , self . read_file (test_file ))
201
191
202
192
if not actually_write :
203
193
return
204
194
rt .refactor_file (test_file , True )
205
- new_contents = read_file ()
195
+ new_contents = self . read_file (test_file )
206
196
self .assertNotEqual (old_contents , new_contents )
207
197
return new_contents
208
198
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
+
209
219
def test_refactor_file (self ):
210
220
test_file = os .path .join (FIXER_DIR , "parrot_example.py" )
211
221
self .check_file_refactoring (test_file , _DEFAULT_FIXERS )
@@ -285,6 +295,12 @@ def test_crlf_newlines(self):
285
295
finally :
286
296
os .linesep = old_sep
287
297
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
+
288
304
def test_refactor_docstring (self ):
289
305
rt = self .rt ()
290
306
0 commit comments