Skip to content

Commit e6fecb5

Browse files
committed
KeywordBear: Do not run if keywords is empty
If a setting is used in the coafile and nothing is assigned, then the value of that key is an empty string ''. Now, since `keywords` is a `list`, `list('')` returns an empty list: `[]` which leads to construction of regex `r'()'` which matches every character, and hence produces false results. Fixes coala#1689
1 parent 6078e05 commit e6fecb5

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

bears/general/KeywordBear.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,14 @@ def run(self,
9696
'''
9797
comments = list(_get_comments(dependency_results))
9898

99-
simple_keywords_regex = re.compile(
100-
'(' + '|'.join(re.escape(key) for key in keywords) + ')',
101-
re.IGNORECASE)
102-
103-
message = "The line contains the keyword '{}'."
104-
yield from self.check_keywords(filename, file, comments,
105-
simple_keywords_regex, message)
99+
if keywords:
100+
simple_keywords_regex = re.compile(
101+
'(' + '|'.join(re.escape(key) for key in keywords) + ')',
102+
re.IGNORECASE)
103+
104+
message = "The line contains the keyword '{}'."
105+
yield from self.check_keywords(filename, file, comments,
106+
simple_keywords_regex, message)
106107

107108
if regex_keyword is not '':
108109
regex = re.compile(regex_keyword)

tests/general/KeywordBearTest.py

+9
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,12 @@ def test_wrong_language(self):
239239
self.assertIn(log.output[0],
240240
'ERROR:root:coalang specification'
241241
' for anything not found.')
242+
243+
def test_empty_keywords_list(self):
244+
self.section.append(Setting('keywords', ''))
245+
246+
text = ["bears = KeywordBear\n"]
247+
248+
with execute_bear(self.uut, filename='F', file=text,
249+
dependency_results=self.dep_results) as result:
250+
self.assertEqual(len(result), 0)

0 commit comments

Comments
 (0)