Skip to content

Commit

Permalink
AnnotationBear: Allow missing language setting
Browse files Browse the repository at this point in the history
A missing language setting is not any different from
a language name which has a Language definition that
does not contain the necessary information to correctly
parse the file.

Use a special 'UnknownLanguage' in this scenario.

Also upgrades AnnotationBear from LanguageDefinition
to the new Language.

Fixes #1012
Closes #1095
  • Loading branch information
jayvdb committed Dec 7, 2016
1 parent 2759cee commit 029f22d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
49 changes: 37 additions & 12 deletions bears/general/AnnotationBear.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from coalib.bearlib.languages.LanguageDefinition import LanguageDefinition
from coalib.bearlib.languages.Language import Language
from coalib.bears.LocalBear import LocalBear
from coalib.results.HiddenResult import HiddenResult
from coalib.results.Result import Result, RESULT_SEVERITY
Expand All @@ -12,7 +12,8 @@ class AnnotationBear(LocalBear):
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'

def run(self, filename, file, language: str, coalang_dir: str = None):
def run(self, filename, file, language: str='Unknown',
coalang_dir: str=None):
"""
Finds out all the positions of strings and comments in a file.
The Bear searches for valid comments and strings and yields their
Expand All @@ -31,19 +32,43 @@ def run(self, filename, file, language: str, coalang_dir: str = None):
``u"string"``, the ``u`` will not be in the source range).
"""
try:
lang_dict = LanguageDefinition(language, coalang_dir=coalang_dir)
except FileNotFoundError:
lang = Language[language]
except AttributeError:
content = ('coalang specification for ' + language +
' not found.')
yield HiddenResult(self, content)
return

string_delimiters = dict(lang_dict['string_delimiters'])
multiline_string_delimiters = dict(
lang_dict['multiline_string_delimiters'])
multiline_comment_delimiters = dict(
lang_dict['multiline_comment_delimiters'])
comment_delimiter = dict(lang_dict['comment_delimiter'])
lang = Language['Unknown']

lang = lang.get_default_version()

if 'string_delimiters' in lang.attributes:
string_delimiters = lang.string_delimiters
else:
string_delimiters = {}

if 'multiline_string_delimiters' in lang.attributes:
multiline_string_delimiters = lang.multiline_string_delimiters
else:
multiline_string_delimiters = {}

if 'multiline_comment_delimiters' in lang.attributes:
multiline_comment_delimiters = lang.multiline_comment_delimiters
else:
multiline_comment_delimiters = {}

if 'comment_delimiter' in lang.attributes:
if isinstance(lang.comment_delimiter, str):
comment_delimiter = {lang.comment_delimiter: ''}
elif isinstance(lang.comment_delimiter, tuple):
comment_delimiter = {item: ''
for item in lang.comment_delimiter}
else:
raise TypeError('%s.comment_delimiter of unknown type %s'
% (lang.__class__.__qualname__,
lang.comment_delimiter.__class__.__name__))
else:
comment_delimiter = {}

string_ranges = comment_ranges = ()
try:
string_ranges, comment_ranges = self.find_annotation_ranges(
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Use >= for development versions so that source builds always work
coala>=0.9
coala>=0.9.dev20161221104802
munkres3~=1.0
pylint~=1.6
autopep8~=1.2
Expand Down
32 changes: 30 additions & 2 deletions tests/general/IndentationBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,53 @@ class TestLanguage:
encapsulators = {'(': ')', '[': ']'}


class IndentationBearTest(unittest.TestCase):
@Language
class InvalidLanguageSpec:
comment_delimiter = {}


class IndentationBearTestBase(unittest.TestCase):

def setUp(self):
self.section = Section('')
self.section.append(Setting('language', 'TestLanguage'))
self.section.append(Setting('language', self.language))
self.section.append(Setting('use_spaces', False))
self.dep_uut = AnnotationBear(self.section, Queue())

def get_results(self, file, section=None):
if section is None:
section = self.section
dep_results_valid = self.dep_uut.execute('file', file)
if not dep_results_valid:
return

uut = IndentationBear(section, Queue())
arg_dict = {'dependency_results':
{AnnotationBear.__name__:
list(dep_results_valid)},
'file': file}
return list(uut.run_bear_from_section(['file'], arg_dict))


class InvalidLanguageTest(IndentationBearTestBase):

language = 'InvalidLanguageSpec'

def test_wrong_type(self):
self.get_results('')
mq = self.dep_uut.message_queue
last_message = None
while not mq.empty():
last_message = mq.get(False)

self.assertIn(self.language, str(last_message))
self.assertIn('unknown type dict', str(last_message))


class IndentationBearTest(IndentationBearTestBase):

language = 'TestLanguage'

def verify_bear(self,
valid_file=None,
invalid_file=None,
Expand Down
11 changes: 11 additions & 0 deletions tests/general/KeywordBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ def test_empty_keyword(self):
dependency_results=self.dep_results) as result:
self.assertEqual(result, [])

def test_no_language(self):
text = ['# todo 123']
section = Section('')
section.append(Setting('keywords', 'TODO'))

with execute_bear(KeywordBear(section, Queue()), 'F', text,
dependency_results=self.dep_results) as result:
self.assertEqual(result[0].diffs, {})
self.assertEqual(result[0].affected_code[0].start.line, 1)
self.assertEqual(len(result), 1)

def test_keyword_in_comment(self):
dep_results = {
'AnnotationBear': {}
Expand Down

0 comments on commit 029f22d

Please sign in to comment.