Skip to content

Commit

Permalink
KeywordBear: Handle empty keywords
Browse files Browse the repository at this point in the history
If setting keyword is used in coafile and not assigned, then keywords
is an empty list, which leads to construction of regex `r'()'` which
matches all the letters, hence yielding false results.

Fixes coala#1689
  • Loading branch information
meetmangukiya committed May 2, 2017
1 parent 197747f commit ec1b330
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
15 changes: 8 additions & 7 deletions bears/general/KeywordBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@ def run(self,
'''
comments = list(_get_comments(dependency_results))

simple_keywords_regex = re.compile(
'(' + '|'.join(re.escape(key) for key in keywords) + ')',
re.IGNORECASE)

message = "The line contains the keyword '{}'."
yield from self.check_keywords(filename, file, comments,
simple_keywords_regex, message)
if keywords:
simple_keywords_regex = re.compile(
'(' + '|'.join(re.escape(key) for key in keywords) + ')',
re.IGNORECASE)

message = "The line contains the keyword '{}'."
yield from self.check_keywords(filename, file, comments,
simple_keywords_regex, message)

if regex_keyword is not '':
regex = re.compile(regex_keyword)
Expand Down
9 changes: 9 additions & 0 deletions tests/general/KeywordBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,12 @@ def test_wrong_language(self):
self.assertIn(log.output[0],
'ERROR:root:coalang specification'
' for anything not found.')

def test_empty_keywords_list(self):
self.section.append(Setting('keywords', ''))

text = ['bears = KeywordBear\n']

with execute_bear(self.uut, filename='F', file=text,
dependency_results=self.dep_results) as result:
self.assertEqual(len(result), 0)

0 comments on commit ec1b330

Please sign in to comment.