Skip to content

Commit

Permalink
FilenameBear.py: Add (pre/suf)fix matching feature
Browse files Browse the repository at this point in the history
FilenameBearTest.py: Add tests

Add a new feature to the FilenameBear.
The bear can check if the filename matches a certain prefix/suffix.
The bear can suggest a rename patch to the file such that the new
filename matches the chosen naming convention, prefix and suffix.

Closes coala#1817
  • Loading branch information
AMR-KELEG committed Jul 11, 2017
1 parent d9fca90 commit 404db94
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
36 changes: 32 additions & 4 deletions bears/general/FilenameBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class FilenameBear(LocalBear):

def run(self, filename, file,
file_naming_convention: str=None,
ignore_uppercase_filenames: bool=True):
ignore_uppercase_filenames: bool=True,
filename_prefix: str='',
filename_suffix: str=''):
"""
Checks whether the filename follows a certain naming-convention.
Expand All @@ -43,6 +45,12 @@ def run(self, filename, file,
:param ignore_uppercase_filenames:
Whether or not to ignore fully uppercase filenames completely,
e.g. COPYING, LICENSE etc.
:param filename_prefix:
Check whether the filename uses a certain prefix.
The file's extension is ignored.
:param filename_suffix:
Check whether the filename uses a certain suffix.
The file's extension is ignored.
"""
head, tail = os.path.split(filename)
filename_without_extension, extension = os.path.splitext(tail)
Expand All @@ -63,6 +71,8 @@ def run(self, filename, file,
'Using the default "snake" naming convention.')
file_naming_convention = 'snake'

messages = []

try:
new_name = self._naming_convention[file_naming_convention](
filename_without_extension)
Expand All @@ -71,15 +81,33 @@ def run(self, filename, file,
file_naming_convention)
return

if new_name != filename_without_extension:
messages.append(
'Filename does not follow {} naming-convention.'.format(
file_naming_convention))

if not filename_without_extension.startswith(filename_prefix):
new_name = filename_prefix + new_name
messages.append(
'Filename does not use the prefix {!r}.'.format(
filename_prefix))

if not filename_without_extension.endswith(filename_suffix):
new_name = new_name + filename_suffix
messages.append(
'Filename does not use the suffix {!r}.'.format(
filename_suffix))

if ignore_uppercase_filenames and filename_without_extension.isupper():
return

if new_name != filename_without_extension:
if messages:
diff = Diff(file, rename=os.path.join(head, new_name + extension))
message = ('\n'.join('- ' + mes for mes in messages)
if len(messages) > 1 else messages[0])

yield Result(
self,
'Filename does not follow {} naming-convention.'.format(
file_naming_convention),
message,
diff.affected_code(filename),
diffs={filename: diff})
44 changes: 44 additions & 0 deletions tests/general/FilenameBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from bears.general.FilenameBear import FilenameBear
from coalib.testing.LocalBearTestHelper import LocalBearTestHelper
from coalib.results.Result import RESULT_SEVERITY, Result
from coalib.output.printers.LOG_LEVEL import LOG_LEVEL
from coalib.settings.Section import Section

Expand Down Expand Up @@ -107,3 +108,46 @@ def test_auto_file_naming_convention_warning(self):
self.assertEqual(log.message,
'The file naming convention could not be guessed. '
'Using the default "snake" naming convention.')

def test_file_prefix(self):
self.section['filename_prefix'] = 'pre'
self.check_invalidity(
self.uut, [''], filename='filename.xyz')
self.check_validity(
self.uut, [''], filename='prefilename.xyz')
self.check_results(
self.uut,
[''],
[Result.from_values('FilenameBear',
"Filename does not use the prefix 'pre'.",
severity=RESULT_SEVERITY.NORMAL,
file='filename.xyz')],
filename='filename.xyz')

def test_file_suffix(self):
self.section['filename_suffix'] = 'fix'
self.check_invalidity(
self.uut, [''], filename='filename.xyz')
self.check_validity(
self.uut, [''], filename='filenamesuffix.xyz')
self.check_results(
self.uut,
[''],
[Result.from_values('FilenameBear',
"Filename does not use the suffix 'fix'.",
severity=RESULT_SEVERITY.NORMAL,
file='filename.xyz')],
filename='filename.xyz')

def test_file_prefix_suffix(self):
self.section['filename_prefix'] = 'pre'
self.section['filename_suffix'] = 'fix'
self.check_results(
self.uut,
[''],
[Result.from_values('FilenameBear',
"- Filename does not use the prefix 'pre'.\n"
"- Filename does not use the suffix 'fix'.",
severity=RESULT_SEVERITY.NORMAL,
file='filename.xyz')],
filename='filename.xyz')

0 comments on commit 404db94

Please sign in to comment.