Skip to content

Commit 2df7a04

Browse files
committed
Unittests for a couple of functions
1 parent d194a3a commit 2df7a04

1 file changed

Lines changed: 47 additions & 2 deletions

File tree

main/githooks.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,17 @@ def _test(input, output=None):
347347

348348

349349
def trim_trailing_whitespace_in_file(filename, new_file, in_place):
350-
'''Remove trailing white spaces in new and modified lines in a filename'''
350+
'''Remove trailing white spaces in new and modified lines in a filename
351+
352+
:param filename: The file to check
353+
:param new_file: True if whole file is new; False if it's an existing
354+
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
359+
trailing whitepsace is found.
360+
'''
351361
try:
352362
with open(filename, 'rb') as fileobj:
353363
lines = fileobj.read().decode().splitlines(True)
@@ -391,6 +401,27 @@ def trim_trailing_whitespace_in_file(filename, new_file, in_place):
391401
return 0
392402

393403

404+
class TestTrimTrailingWhitespace(unittest.TestCase):
405+
def test_trim_trailing_whitespace(self):
406+
test_file = Path.cwd() / 'tttw_testfile.txt'
407+
content = 'first line\nsecond line \nthird line '
408+
trimmed_content = 'first line\nsecond line\nthird line'
409+
test_file.write_text(content)
410+
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)
415+
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()
423+
424+
394425
def remove_trailing_white_space(files, new_files=False, in_place=True):
395426
'''Remove trailing white spaces in all new and modified lines'''
396427
retval = 0
@@ -439,7 +470,7 @@ def check_filename(filepath):
439470

440471
try:
441472
filepath.encode('ascii')
442-
except UnicodeDecodeError:
473+
except UnicodeEncodeError:
443474
_fail(f'Illegal path "{filepath}" - '
444475
'only ASCII characters are permitted.')
445476
return 1
@@ -452,6 +483,20 @@ def check_filename(filepath):
452483
return 0
453484

454485

486+
class TestCheckFileName(unittest.TestCase):
487+
def test_various_strings(self):
488+
def _test(input, output):
489+
self.assertEqual(output, check_filename(input))
490+
_test('good/some.txt', 0)
491+
_test('bad/illegal/star*star.txt', 1)
492+
_test('bad/reserved/device/con.txt', 1)
493+
_test('bad/end/period.txt.', 1)
494+
_test('bad/end/space.txt ', 1)
495+
_test('bad/ascii/你好.txt', 1)
496+
_test('long/path/'*20 + 'l208.txt', 0)
497+
_test('long/path/'*20 + 'll209.txt', 1)
498+
499+
455500
def check_filenames(files):
456501
'''Check file path and name meet requirement.
457502

0 commit comments

Comments
 (0)