Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FilenameBear.py: Provide multiple patches #2948

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
FilenameBear.py: Provide multiple patches
This will provide multiple patches to the user when the file
naming convention cannot be guessed.
akshatkarani committed Aug 25, 2019
commit 0d33ba6ebfb36ba62c56d35ced8f90699835251b
20 changes: 20 additions & 0 deletions bears/general/FilenameBear.py
Original file line number Diff line number Diff line change
@@ -58,6 +58,7 @@ def run(self, filename, file,
"""
head, tail = os.path.split(filename)
filename_without_extension, extension = os.path.splitext(tail)
alternate_conventions = []

if file_naming_convention is None:
self.warn('Please specify a file naming convention explicitly'
@@ -74,12 +75,20 @@ def run(self, filename, file,
self.warn('The file naming convention could not be guessed. '
'Using the default "snake" naming convention.')
file_naming_convention = 'snake'
alternate_conventions += ['camel', 'kebab', 'pascal', 'space']

messages = []

try:
new_name = self._naming_convention[file_naming_convention](
filename_without_extension)
alternate_names = [self._naming_convention[alternate_convention](
filename_without_extension)
for alternate_convention in
alternate_conventions]
alternate_names = [alternate_name
for alternate_name in alternate_names
if alternate_name != filename_without_extension]
except KeyError:
self.err('Invalid file-naming-convention provided: ' +
file_naming_convention)
@@ -92,12 +101,16 @@ def run(self, filename, file,

if not filename_without_extension.startswith(filename_prefix):
new_name = filename_prefix + new_name
alternate_names = [filename_prefix + alternate_name
for alternate_name in alternate_names]
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
alternate_names = [alternate_name + filename_suffix
for alternate_name in alternate_names]
messages.append(
'Filename does not use the suffix {!r}.'.format(
filename_suffix))
@@ -121,4 +134,11 @@ def run(self, filename, file,
rename=os.path.join(head, new_name + extension))
result_kwargs['diffs'] = {filename: diff}

if alternate_names:
alternate_diffs = [{filename: Diff(
file, rename=os.path.join(
head, alternate_name + extension))}
for alternate_name in alternate_names]
result_kwargs['alternate_diffs'] = alternate_diffs

yield Result.from_values(self, **result_kwargs)
24 changes: 24 additions & 0 deletions tests/general/FilenameBearTest.py
Original file line number Diff line number Diff line change
@@ -110,6 +110,30 @@ def test_auto_file_naming_convention_warning(self):
'The file naming convention could not be guessed. '
'Using the default "snake" naming convention.')

def test_multiple_patches(self):
self.section['file_naming_convention'] = 'auto'

filename_test1 = 'FileName.xyz'
msg = 'Filename does not follow snake naming-convention.'
expected_diffs = {filename_test1: Diff(['\n'], rename='file_name.xyz')}

alternate_diffs = [{filename_test1: Diff(
['\n'], rename='fileName.xyz')},
{filename_test1: Diff(
['\n'], rename='file-name.xyz')},
{filename_test1: Diff(
['\n'], rename='File Name.xyz')}]

expected_result = Result.from_values('FilenameBear',
msg,
severity=RESULT_SEVERITY.NORMAL,
file=filename_test1,
diffs=expected_diffs,
alternate_diffs=alternate_diffs)

self.check_results(
self.uut, [''], [expected_result], filename=filename_test1)

def test_file_prefix(self):
self.section['filename_prefix'] = 'pre'
self.check_invalidity(