Skip to content

Commit

Permalink
FilenameBear.py: Add filename length check
Browse files Browse the repository at this point in the history
Adds the check if filename has length more than 260, so minimum of
maximum filename length on Windows and maximum filename length
on Unix-like systems. In that case, user will get message about it.

Closes coala#2003
  • Loading branch information
damian1996 authored and gitmate-bot committed Jan 7, 2018
1 parent b496c33 commit 56091d0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
9 changes: 9 additions & 0 deletions bears/general/FilenameBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class FilenameBear(LocalBear):
}

def run(self, filename, file,
max_filename_length: int=260,
file_naming_convention: str=None,
ignore_uppercase_filenames: bool=True,
filename_prefix: str='',
Expand All @@ -42,6 +43,8 @@ def run(self, filename, file,
- ``pascal`` (``ThisIsPascalCase``)
- ``snake`` (``this_is_snake_case``)
- ``space`` (``This Is Space Case``)
:param max_filename_length:
Maximum filename length on both Windows and Unix-like systems.
:param ignore_uppercase_filenames:
Whether or not to ignore fully uppercase filenames completely,
e.g. COPYING, LICENSE etc.
Expand Down Expand Up @@ -98,6 +101,12 @@ def run(self, filename, file,
'Filename does not use the suffix {!r}.'.format(
filename_suffix))

if len(filename) > max_filename_length:
messages.append(
'Filename is too long ({} > {}).'.format(
len(filename),
max_filename_length))

if ignore_uppercase_filenames and filename_without_extension.isupper():
return

Expand Down
39 changes: 39 additions & 0 deletions tests/general/FilenameBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,42 @@ def test_file_prefix_suffix(self):
severity=RESULT_SEVERITY.NORMAL,
file='filename.xyz')],
filename='filename.xyz')

def test_file_with_too_long_filename(self):
msg = 'Filename is too long ({} > {}).'
filename_test1 = '_filenamewhichistoolong'*15 + '.xyz'
filename_test2 = '_filenamewhichistoolong'*10 + '.xyz'
filename_test3 = '_validfilenamelength'*13
max_filename_length = 260
self.check_invalidity(
self.uut, [''], filename=filename_test1)
self.check_validity(
self.uut, [''], filename=filename_test2)
self.check_validity(
self.uut, [''], filename=filename_test3)
self.check_results(
self.uut,
[''],
[Result.from_values('FilenameBear',
msg.format(len(filename_test1),
max_filename_length),
severity=RESULT_SEVERITY.NORMAL,
file=filename_test1)],
filename=filename_test1)

def test_message_too_long_file_with_other_errors(self):
self.section['filename_prefix'] = 'pre'
filename_test1 = '_filenamewhichistoolong'*15 + '.xyz'
max_filename_length = 260
msg = ("- Filename does not use the prefix 'pre'.\n"
'- Filename is too long ({} > {}).'
)
self.check_results(
self.uut,
[''],
[Result.from_values('FilenameBear',
msg.format(len(filename_test1),
max_filename_length),
severity=RESULT_SEVERITY.NORMAL,
file=filename_test1)],
filename=filename_test1)

0 comments on commit 56091d0

Please sign in to comment.