2020-04-03 05:20:20 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2017-09-04 14:19:39 -04:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import six
|
|
|
|
import unittest
|
|
|
|
import zulip
|
|
|
|
|
|
|
|
from unittest import TestCase
|
2018-01-27 15:21:49 -05:00
|
|
|
from zulip import ZulipError
|
2017-09-04 14:19:39 -04:00
|
|
|
if six.PY2:
|
|
|
|
from mock import patch
|
|
|
|
else:
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
class TestDefaultArguments(TestCase):
|
|
|
|
|
|
|
|
def test_invalid_arguments(self):
|
2017-10-11 20:54:09 -04:00
|
|
|
# type: () -> None
|
2017-09-04 14:19:39 -04:00
|
|
|
parser = zulip.add_default_arguments(argparse.ArgumentParser(usage="lorem ipsum"))
|
2017-10-11 20:54:09 -04:00
|
|
|
with self.assertRaises(SystemExit) as cm: # type: ignore # error: "assertRaises" doesn't match argument types
|
|
|
|
with patch('sys.stderr', new=six.StringIO()) as mock_stderr:
|
|
|
|
parser.parse_args(['invalid argument'])
|
2017-09-04 14:19:39 -04:00
|
|
|
self.assertEqual(cm.exception.code, 2)
|
|
|
|
# Assert that invalid arguments exit with printing the full usage (non-standard behavior)
|
|
|
|
self.assertTrue(mock_stderr.getvalue().startswith("""usage: lorem ipsum
|
|
|
|
|
|
|
|
optional arguments:
|
|
|
|
-h, --help show this help message and exit
|
|
|
|
|
|
|
|
Zulip API configuration:
|
|
|
|
--site ZULIP_SITE Zulip server URI
|
|
|
|
"""))
|
|
|
|
|
|
|
|
@patch('os.path.exists', return_value=False)
|
|
|
|
def test_config_path_with_tilde(self, mock_os_path_exists):
|
2017-10-11 20:54:09 -04:00
|
|
|
# type: (bool) -> None
|
2017-09-04 14:19:39 -04:00
|
|
|
parser = zulip.add_default_arguments(argparse.ArgumentParser(usage="lorem ipsum"))
|
|
|
|
test_path = '~/zuliprc'
|
|
|
|
args = parser.parse_args(['--config-file', test_path])
|
2018-01-27 15:21:49 -05:00
|
|
|
with self.assertRaises(ZulipError) as cm:
|
2017-09-04 14:19:39 -04:00
|
|
|
zulip.init_from_options(args)
|
|
|
|
expanded_test_path = os.path.abspath(os.path.expanduser(test_path))
|
|
|
|
self.assertEqual(str(cm.exception), 'api_key or email not specified and '
|
|
|
|
'file {} does not exist'.format(expanded_test_path))
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|