From 75ae1c9d38ab26533838f852c7d9801e6e6af488 Mon Sep 17 00:00:00 2001 From: Techievena Date: Wed, 11 Jan 2017 13:47:49 +0530 Subject: [PATCH] KeywordBear.py: Output appropriate message Output appropriate message if the language given in input is not valid/not supported for KeywordBear Fixes https://github.com/coala/coala-bears/issues/1256 --- bears/general/KeywordBear.py | 6 +++++- tests/general/KeywordBearTest.py | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/bears/general/KeywordBear.py b/bears/general/KeywordBear.py index fabf10843f..172c4fa728 100644 --- a/bears/general/KeywordBear.py +++ b/bears/general/KeywordBear.py @@ -1,4 +1,5 @@ import re +import logging from coalib.bearlib import deprecate_settings from coalib.bears.LocalBear import LocalBear @@ -19,7 +20,10 @@ def _get_comments(dependency_results): return for result in annotation_bear_results: - yield from result.contents.get('comments', []) + if isinstance(result.contents, str): + logging.error(result.contents) + else: + yield from result.contents.get('comments', []) def generate_diff(comments, file, filename, diff --git a/tests/general/KeywordBearTest.py b/tests/general/KeywordBearTest.py index 45c0c98307..139fe0d380 100644 --- a/tests/general/KeywordBearTest.py +++ b/tests/general/KeywordBearTest.py @@ -1,6 +1,7 @@ from collections import namedtuple from queue import Queue import unittest +import logging from bears.general.KeywordBear import KeywordBear from coalib.results.HiddenResult import HiddenResult @@ -214,3 +215,27 @@ def test_keyword_regex(self): self.assertEqual(result[0].message, 'The line contains the keyword' " 'Issue #123' which resulted " 'in a match with given regex.') + + def test_wrong_language(self): + self.section.append(Setting('language', 'anything')) + logger = logging.getLogger() + annotation_bear_result_type = namedtuple('result', 'contents') + dep_results = { + 'AnnotationBear': [ + annotation_bear_result_type( + 'coalang specification for anything not found.') + ] + } + + text = ['# todo 123'] + + with self.assertLogs(logger, 'ERROR') as log: + with execute_bear(self.uut, filename='F', file=text, + dependency_results=dep_results) as result: + self.assertEqual(len(result), 1) + self.assertEqual(result[0].diffs, {}) + self.assertEqual(result[0].affected_code[0].start.line, 1) + self.assertEqual(len(log.output), 1) + self.assertIn(log.output[0], + 'ERROR:root:coalang specification' + ' for anything not found.')