From fec14ca286c155cc262784379cb570bd69d69b28 Mon Sep 17 00:00:00 2001 From: Abhijeet Kaur Date: Sat, 26 Aug 2017 05:11:57 +0530 Subject: [PATCH] bots: Add additional check to avoid ambiguous case. If a user provides both the name and the path to the bot as input then we either will have to chose one of them or alert the user to check the input. Selecting the latter by sending an error message to the user. --- zulip_bots/zulip_bots/run.py | 17 ++++++++++++++++- zulip_bots/zulip_bots/zulip_bot_output.py | 14 ++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/zulip_bots/zulip_bots/run.py b/zulip_bots/zulip_bots/run.py index e762ef7..5df6da7 100755 --- a/zulip_bots/zulip_bots/run.py +++ b/zulip_bots/zulip_bots/run.py @@ -15,7 +15,6 @@ import six from zulip_bots.lib import run_message_handler_for_bot from zulip_bots.provision import provision_bot - def import_module_from_source(path, name=None): if not name: name = splitext(basename(path))[0] @@ -31,6 +30,12 @@ def import_module_from_source(path, name=None): return module +def name_and_patch_match(given_name, path_to_bot): + if given_name and path_to_bot: + name_by_path = os.path.splitext(os.path.basename(path_to_bot))[0] + if (given_name != name_by_path): + return False + return True def parse_args(): usage = ''' @@ -78,6 +83,16 @@ def parse_args(): You must either specify the name of an existing bot or specify a path to the file (--path-to-bot) that contains the bot handler class. +""" + parser.error(error_message) + # Checks if both name and path to bots are provided: + # checks if both of these are in sync, otherwise we'll + # have to be bias towards one and the user may get incorrect + # result. + elif not name_and_path_match(options.name, options.path_to_bot): + error_message = """ +Please make sure that the given name of the bot and the +given path to the bot are same and valid. """ parser.error(error_message) diff --git a/zulip_bots/zulip_bots/zulip_bot_output.py b/zulip_bots/zulip_bots/zulip_bot_output.py index 064a762..4ca9538 100644 --- a/zulip_bots/zulip_bots/zulip_bot_output.py +++ b/zulip_bots/zulip_bots/zulip_bot_output.py @@ -16,8 +16,7 @@ from mock import MagicMock, patch from zulip_bots.lib import run_message_handler_for_bot, StateHandler from zulip_bots.provision import provision_bot -from zulip_bots.run import import_module_from_source - +from zulip_bots.run import import_module_from_source, name_and_patch_match def parse_args(): usage = ''' @@ -53,11 +52,22 @@ def parse_args(): help='Install dependencies for the bot.') options = parser.parse_args() + if not options.name and not options.path_to_bot: error_message = """ You must either specify the name of an existing bot or specify a path to the file (--path-to-bot) that contains the bot handler class. +""" + parser.error(error_message) + # Checks if both name and path to bots are provided: + # checks if both of these are in sync, otherwise we'll + # have to be bias towards one and the user may get incorrect + # result. + elif not name_and_patch_match(options.name, options.path_to_bot): + error_message = """ +Please make sure that the given name of the bot and the +given path to the bot are same and valid. """ parser.error(error_message)