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.
This commit is contained in:
Abhijeet Kaur 2017-08-26 05:11:57 +05:30 committed by Tim Abbott
parent dd13d0314b
commit fec14ca286
2 changed files with 28 additions and 3 deletions

View file

@ -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)

View file

@ -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)