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

AnnotationBear: Allow missing language setting #1100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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
51 changes: 49 additions & 2 deletions tests/general/IndentationBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,72 @@ 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'))
if self.language:
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 MissingLanguageTest(IndentationBearTestBase):

language = None

def test_no_language(self):
self.assertRaisesRegex(IndexError, 'Required index is unavailable',
self.get_results, '')

# Check there are no unexpected messages
mq = self.dep_uut.message_queue
messages = []
while not mq.empty():
messages.append(mq.get(False))

self.assertEqual(len(messages), 1)
self.assertIn('Running bear AnnotationBear', str(messages[0]))


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