From 36f4982e540abd79c3de5d1376d41f10d2200bd6 Mon Sep 17 00:00:00 2001 From: Abhijeet Kaur Date: Tue, 22 Aug 2017 22:21:11 +0530 Subject: [PATCH] bots: Mock get_config_info function for get-bot-output. Since ExternalBotHandler class is mocked, few bots that require specific configurations require to run get_config_info. --- zulip_bots/zulip_bots/zulip_bot_output.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/zulip_bots/zulip_bots/zulip_bot_output.py b/zulip_bots/zulip_bots/zulip_bot_output.py index bae241a..c9a5796 100644 --- a/zulip_bots/zulip_bots/zulip_bot_output.py +++ b/zulip_bots/zulip_bots/zulip_bot_output.py @@ -10,6 +10,7 @@ from importlib import import_module from os.path import basename, splitext import six +from six.moves import configparser import mock from mock import MagicMock, patch @@ -83,7 +84,26 @@ def main(): message_handler = lib_module.handler_class() with patch('zulip_bots.lib.ExternalBotHandler') as mock_bot_handler: - mock_bot_handler.send_reply = MagicMock() + def get_config_info(bot_name, section=None, optional=False): + # type: (str, Optional[str], Optional[bool]) -> Dict[str, Any] + conf_file_path = os.path.realpath(os.path.join( + 'zulip_bots', 'bots', bot_name, bot_name + '.conf')) + section = section or bot_name + config = configparser.ConfigParser() + try: + with open(conf_file_path) as conf: + config.readfp(conf) # type: ignore + except IOError: + if optional: + return dict() + raise + return dict(config.items(section)) + + mock_bot_handler.get_config_info = get_config_info + if (hasattr(message_handler, 'initialize') and callable(message_handler.initialize)): + message_handler.initialize(mock_bot_handler) + + mock_bot_handler.send_reply = MagicMock() message_handler.handle_message( message=message, bot_handler=mock_bot_handler,