2020-04-03 05:20:20 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2017-09-04 20:19:39 +02:00
|
|
|
import argparse
|
2020-04-03 05:23:36 -04:00
|
|
|
import io
|
2021-05-28 17:00:04 +08:00
|
|
|
import os
|
2017-09-04 20:19:39 +02:00
|
|
|
import unittest
|
|
|
|
from unittest import TestCase
|
2020-04-03 05:23:36 -04:00
|
|
|
from unittest.mock import patch
|
2017-09-04 20:19:39 +02:00
|
|
|
|
2021-05-28 17:00:04 +08:00
|
|
|
import zulip
|
|
|
|
from zulip import ZulipError
|
|
|
|
|
|
|
|
|
2017-09-04 20:19:39 +02:00
|
|
|
class TestDefaultArguments(TestCase):
|
2020-04-18 15:59:12 -07:00
|
|
|
def test_invalid_arguments(self) -> None:
|
2017-09-04 20:19:39 +02:00
|
|
|
parser = zulip.add_default_arguments(argparse.ArgumentParser(usage="lorem ipsum"))
|
2021-03-04 15:09:27 -08:00
|
|
|
with self.assertRaises(SystemExit) as cm:
|
2021-05-28 17:05:11 +08:00
|
|
|
with patch("sys.stderr", new=io.StringIO()) as mock_stderr:
|
|
|
|
parser.parse_args(["invalid argument"])
|
2017-09-04 20:19:39 +02:00
|
|
|
self.assertEqual(cm.exception.code, 2)
|
|
|
|
# Assert that invalid arguments exit with printing the full usage (non-standard behavior)
|
2021-08-26 16:48:02 -07:00
|
|
|
self.assertRegex(
|
|
|
|
mock_stderr.getvalue(),
|
|
|
|
r"""^usage: lorem ipsum
|
2017-09-04 20:19:39 +02:00
|
|
|
|
2021-08-26 16:48:02 -07:00
|
|
|
(optional arguments|options):
|
2017-09-04 20:19:39 +02:00
|
|
|
-h, --help show this help message and exit
|
|
|
|
|
|
|
|
Zulip API configuration:
|
|
|
|
--site ZULIP_SITE Zulip server URI
|
2021-08-26 16:48:02 -07:00
|
|
|
""",
|
2021-05-28 17:03:46 +08:00
|
|
|
)
|
2017-09-04 20:19:39 +02:00
|
|
|
|
2021-05-28 17:05:11 +08:00
|
|
|
@patch("os.path.exists", return_value=False)
|
2020-04-18 15:59:12 -07:00
|
|
|
def test_config_path_with_tilde(self, mock_os_path_exists: bool) -> None:
|
2017-09-04 20:19:39 +02:00
|
|
|
parser = zulip.add_default_arguments(argparse.ArgumentParser(usage="lorem ipsum"))
|
2021-05-28 17:05:11 +08:00
|
|
|
test_path = "~/zuliprc"
|
|
|
|
args = parser.parse_args(["--config-file", test_path])
|
2018-01-28 01:51:49 +05:30
|
|
|
with self.assertRaises(ZulipError) as cm:
|
2017-09-04 20:19:39 +02:00
|
|
|
zulip.init_from_options(args)
|
|
|
|
expanded_test_path = os.path.abspath(os.path.expanduser(test_path))
|
2021-05-28 17:03:46 +08:00
|
|
|
self.assertEqual(
|
|
|
|
str(cm.exception),
|
2023-10-27 22:00:51 -07:00
|
|
|
"api_key or email not specified and " f"file {expanded_test_path} does not exist",
|
2021-05-28 17:03:46 +08:00
|
|
|
)
|
|
|
|
|
2017-09-04 20:19:39 +02:00
|
|
|
|
2021-05-28 17:05:11 +08:00
|
|
|
if __name__ == "__main__":
|
2017-09-04 20:19:39 +02:00
|
|
|
unittest.main()
|