66
77from collections import defaultdict
88from pathlib import Path
9+ from tempfile import NamedTemporaryFile
910import os
1011import platform
1112import re
@@ -346,16 +347,19 @@ def _test(input, output=None):
346347 _test (u'abcd\xe9 ' , u'abcd\xe9 ' )
347348
348349
349- def trim_trailing_whitespace_in_file (filename , new_file , in_place ):
350+ def trim_trailing_whitespace_in_file (filename , new_file , dry_run ,
351+ add_to_git_index = True ):
350352 '''Remove trailing white spaces in new and modified lines in a filename
351353
352354 :param filename: The file to check
353355 :param new_file: True if whole file is new; False if it's an existing
354356 file that's been modified
355- :param in_place: True if trailing whitespace is to be removed from file in
356- place; False if a value is to be returned to indicate if trailing
357- whitespace is found, instead of updating the file in place.
358- :returns: If in_place=False, 0 if no trailing whitespace is found, 1 if
357+ :param dry_run: True if we don't want to actually update the file - just
358+ return a value to indicate if trailing whitespace is found or not;
359+ False if the file is to be updated if trailing whitespace is found
360+ :param add_to_git_index: If dry_run=False, set to False if we don't want to
361+ automatically add the new file to git index should it be updated
362+ :returns: If dry_run=True, 0 if no trailing whitespace is found, 1 if
359363 trailing whitepsace is found.
360364 '''
361365 try :
@@ -380,18 +384,19 @@ def trim_trailing_whitespace_in_file(filename, new_file, in_place):
380384 continue
381385 after = trim_trailing_whitespace (before )
382386 if before != after :
383- if in_place :
387+ if dry_run :
388+ modified_lines .append (str (line_num ))
389+ else :
384390 print (f' Fixed line { line_num } ' )
385391 modified_file = True
386392 lines [line_num - 1 ] = after
387- else :
388- modified_lines .append (str (line_num ))
389393
390394 if modified_file :
391395 with open (filename , 'wb' ) as fileobj :
392396 lines = '' .join (lines )
393397 fileobj .write (lines .encode ())
394- add_file_to_index (filename )
398+ if add_to_git_index :
399+ add_file_to_index (filename )
395400
396401 if modified_lines :
397402 _fail (f'Found trailing white space in { filename } at lines: ' +
@@ -403,31 +408,34 @@ def trim_trailing_whitespace_in_file(filename, new_file, in_place):
403408
404409class TestTrimTrailingWhitespace (unittest .TestCase ):
405410 def test_trim_trailing_whitespace (self ):
406- test_file = Path .cwd () / 'tttw_testfile.txt'
407411 content = 'first line\n second line \n third line '
408412 trimmed_content = 'first line\n second line\n third line'
409- test_file .write_text (content )
413+ with NamedTemporaryFile () as tmp :
414+ Path (tmp .name ).write_text (content )
415+
416+ # Trailing whitespace found
417+ retval = trim_trailing_whitespace_in_file (tmp .name , True , True )
418+ self .assertEqual (retval , 1 )
419+ self .assertEqual (Path (tmp .name ).read_text (), content )
410420
411- # Trailing whitespace found
412- retval = trim_trailing_whitespace_in_file (test_file , True , False )
413- self .assertEqual (retval , 1 )
414- self .assertEqual (test_file .read_text (), content )
421+ # Now remove the trailing whitespace
422+ trim_trailing_whitespace_in_file (tmp .name , True , False , False )
423+ # Trailing whitespace no longer found
424+ self .assertEqual (Path (tmp .name ).read_text (), trimmed_content )
425+ retval = trim_trailing_whitespace_in_file (tmp .name , True , True )
426+ self .assertEqual (retval , 0 )
415427
416- # Now remove the trailing whitespace
417- trim_trailing_whitespace_in_file (test_file , True , True )
418- # Trailing whitespace no longer found
419- self .assertEqual (test_file .read_text (), trimmed_content )
420- retval = trim_trailing_whitespace_in_file (test_file , True , False )
421- self .assertEqual (retval , 0 )
422- test_file .unlink ()
423428
429+ def remove_trailing_white_space (files , new_files = False , dry_run = False ):
430+ '''Remove trailing white spaces in all new and modified lines
424431
425- def remove_trailing_white_space (files , new_files = False , in_place = True ):
426- '''Remove trailing white spaces in all new and modified lines'''
432+ Set dry_run to True if you just want to check if trailing whitespace exists
433+ in the file instead of actually updating the file.
434+ '''
427435 retval = 0
428436 for filename in files :
429437 retval += trim_trailing_whitespace_in_file (filename , new_files ,
430- in_place )
438+ dry_run )
431439 return retval
432440
433441
0 commit comments