From 73ebd719b20471e743d7b200c349b8e8901b85bf Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Mon, 20 Nov 2017 14:58:20 -0800 Subject: [PATCH] Handle missing config files gracefully. Before this commit, you would get a traceback if you supplied a non-existent filename for your config file. Now we exit gracefully with a useful error message. --- zulip_bots/zulip_bots/run.py | 14 ++++++++++++++ zulip_bots/zulip_bots/test_run.py | 8 ++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/zulip_bots/zulip_bots/run.py b/zulip_bots/zulip_bots/run.py index de22330..5a944c9 100755 --- a/zulip_bots/zulip_bots/run.py +++ b/zulip_bots/zulip_bots/run.py @@ -75,6 +75,18 @@ def parse_args(): return args +def exit_gracefully_if_config_file_does_not_exist(config_file): + # type: (str) -> None + if not os.path.exists(config_file): + print(''' + ERROR: %s does not exist. + + You may need to download a config file from the Zulip app, or + if you have already done that, you need to specify the file + location correctly. + ''' % (config_file,)) + sys.exit(1) + def main(): # type: () -> None args = parse_args() @@ -91,6 +103,8 @@ def main(): if not args.quiet: logging.basicConfig(stream=sys.stdout, level=logging.INFO) + exit_gracefully_if_config_file_does_not_exist(args.config_file) + run_message_handler_for_bot( lib_module=lib_module, config_file=args.config_file, diff --git a/zulip_bots/zulip_bots/test_run.py b/zulip_bots/zulip_bots/test_run.py index 4b4ed63..b27a1ef 100644 --- a/zulip_bots/zulip_bots/test_run.py +++ b/zulip_bots/zulip_bots/test_run.py @@ -29,7 +29,9 @@ class TestDefaultArguments(TestCase): @patch('zulip_bots.run.run_message_handler_for_bot') def test_argument_parsing_with_bot_name(self, mock_run_message_handler_for_bot): # type: (mock.Mock) -> None - zulip_bots.run.main() + with patch('zulip_bots.run.exit_gracefully_if_config_file_does_not_exist'): + zulip_bots.run.main() + mock_run_message_handler_for_bot.assert_called_with(bot_name='giphy', config_file='/foo/bar/baz.conf', lib_module=mock.ANY, @@ -39,7 +41,9 @@ class TestDefaultArguments(TestCase): @patch('zulip_bots.run.run_message_handler_for_bot') def test_argument_parsing_with_bot_path(self, mock_run_message_handler_for_bot): # type: (mock.Mock) -> None - zulip_bots.run.main() + with patch('zulip_bots.run.exit_gracefully_if_config_file_does_not_exist'): + zulip_bots.run.main() + mock_run_message_handler_for_bot.assert_called_with( bot_name='giphy', config_file='/foo/bar/baz.conf',