Skip to content

Commit

Permalink
FilenameBear.py: Enable default naming convention
Browse files Browse the repository at this point in the history
Add default naming conventions that are widely accepted for
languages like java, javascript and python. Add 'auto' option
to predict the file naming convention.

Closes coala#1083
  • Loading branch information
yash-nisar committed Mar 25, 2017
1 parent 63d31e1 commit fc06dd3
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
23 changes: 22 additions & 1 deletion bears/general/FilenameBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ class FilenameBear(LocalBear):
'snake': to_snakecase,
'space': to_spacecase}

_language_naming_convention = {'.java': 'pascal',
'.js': 'kebab',
'.py': 'snake'}

def run(self, filename, file,
file_naming_convention: str='snake',
file_naming_convention: str=None,
ignore_uppercase_filenames: bool=True):
"""
Checks whether the filename follows a certain naming-convention.
:param file_naming_convention:
The naming-convention. Supported values are:
- ``auto`` to guess the right convention. Defaults to
``snake`` if the right convention cannot be guessed.
- ``camel`` (``thisIsCamelCase``)
- ``kebab`` (``this-is-kebab-case``)
- ``pascal`` (``ThisIsPascalCase``)
Expand All @@ -38,6 +44,21 @@ def run(self, filename, file,
"""
head, tail = os.path.split(filename)
filename_without_extension, extension = os.path.splitext(tail)

if file_naming_convention is None:
self.err('Please specify a file naming convention explicitly. '
'Using the default "snake" naming convention...')
file_naming_convention = 'snake'

if file_naming_convention.lower() == 'auto':
if extension not in self._language_naming_convention.keys():
self.err('The file naming convention could not be guessed. '
'Using the default "snake" naming convention...')
file_naming_convention = 'snake'
else:
file_naming_convention = self._language_naming_convention[
extension]

try:
new_name = self._naming_convention[file_naming_convention](
filename_without_extension)
Expand Down
55 changes: 52 additions & 3 deletions tests/general/FilenameBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from coalib.settings.Section import Section


class SpaceConsistencyBearTest(LocalBearTestHelper):
class FilenameBearTest(LocalBearTestHelper):

def setUp(self):
self.section = Section('test section')
Expand All @@ -25,10 +25,9 @@ def test_invalid_naming_convention(self):
self.assertEqual(log.log_level, LOG_LEVEL.ERROR)

def test_snake_case(self):
self.section['file_naming_convention'] = 'snake'
self.check_validity(self.uut, [''], filename='/Home/xyz/x_y.py')
self.check_validity(self.uut, [''], filename='XYZ/__init__.py')

self.section['file_naming_convention'] = 'snake'
self.check_validity(self.uut, [''], filename='/a/camCase', valid=False)

def test_camel_case(self):
Expand Down Expand Up @@ -63,8 +62,58 @@ def test_space_case(self):
self.check_validity(self.uut, [''], filename='/a/Space Case')

def test_ignore_upper(self):
self.section['file_naming_convention'] = 'auto'
self.check_validity(self.uut, [''], filename='/LICENSE')

self.section['ignore_uppercase_filenames'] = 'nope'

self.check_validity(self.uut, [''], filename='/LICENSE', valid=False)

def test_default_naming_java(self):
self.section['file_naming_convention'] = 'auto'
self.check_validity(self.uut, [''], filename='/Home/xyz/x_y.java',
valid=False)
self.check_validity(self.uut, [''], filename='/Home/xyz/x-y.java',
valid=False)
self.check_validity(self.uut, [''], filename='/Home/xyz/x y.java',
valid=False)
self.check_validity(self.uut, [''], filename='/Home/xyz/XY.java')

def test_default_naming_javascript(self):
self.section['file_naming_convention'] = 'auto'
self.check_validity(self.uut, [''], filename='/Home/xyz/x_y.js',
valid=False)
self.check_validity(self.uut, [''], filename='/Home/xyz/x y.js',
valid=False)
self.check_validity(self.uut, [''], filename='/Home/xyz/XY.js')
self.check_validity(self.uut, [''], filename='/Home/xyz/x-y.js')

def test_default_file_naming_python(self):
self.section['file_naming_convention'] = 'auto'
self.check_validity(self.uut, [''], filename='/Home/xyz/x_y.py')
self.check_validity(self.uut, [''], filename='XYZ/__init__.py')
self.check_validity(self.uut, [''], filename='/a/x y.py', valid=False)

def test_none_file_naming_convention_warning(self):
self.uut.execute('filename', [])

# Automatic bear messages we want to ignore.
self.uut.message_queue.get(timeout=1)

log = self.uut.message_queue.get(timeout=1)
self.assertEqual(log.message,
'Please specify a file naming convention explicitly. '
'Using the default "snake" naming convention...')
self.check_validity(self.uut, [''], filename='/Home/xyz/x_y.py')

def test_auto_file_naming_convention_warning(self):
self.section['file_naming_convention'] = 'auto'
self.uut.execute('filename.cpp', [])

# Automatic bear messages we want to ignore.
self.uut.message_queue.get(timeout=1)

log = self.uut.message_queue.get(timeout=1)
self.assertEqual(log.message,
'The file naming convention could not be guessed. '
'Using the default "snake" naming convention...')

0 comments on commit fc06dd3

Please sign in to comment.