From fb2aff46111ed62ef857f53b619facd1006b3175 Mon Sep 17 00:00:00 2001 From: derAnfaenger Date: Mon, 4 Sep 2017 20:19:39 +0200 Subject: [PATCH] zulip package: Add unittest As the first unittest, this creates a test directory and abnd adds it tho the excluded pip package files. There are two `tests` directories now, one in zulip_botserver and one in zulip. This confuses the unittest runner, leading to failed test imports. Therefore, we need to tell the package importer that there are multiple tests directories, all of which should be considered for a search. --- zulip/setup.py | 2 +- zulip/tests/__init__.py | 2 ++ zulip/tests/test_default_arguments.py | 45 +++++++++++++++++++++++++++ zulip_botserver/tests/__init__.py | 2 ++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 zulip/tests/__init__.py create mode 100755 zulip/tests/test_default_arguments.py diff --git a/zulip/setup.py b/zulip/setup.py index 1932074..42f179b 100755 --- a/zulip/setup.py +++ b/zulip/setup.py @@ -74,7 +74,7 @@ setuptools_info = dict( try: from setuptools import setup, find_packages package_info.update(setuptools_info) - package_info['packages'] = find_packages() + package_info['packages'] = find_packages(exclude=['tests']) except ImportError: from distutils.core import setup diff --git a/zulip/tests/__init__.py b/zulip/tests/__init__.py new file mode 100644 index 0000000..bb61062 --- /dev/null +++ b/zulip/tests/__init__.py @@ -0,0 +1,2 @@ +import pkgutil +__path__ = pkgutil.extend_path(__path__, __name__) diff --git a/zulip/tests/test_default_arguments.py b/zulip/tests/test_default_arguments.py new file mode 100755 index 0000000..659b397 --- /dev/null +++ b/zulip/tests/test_default_arguments.py @@ -0,0 +1,45 @@ +from __future__ import absolute_import + +import argparse +import os +import six +import unittest +import zulip + +from unittest import TestCase + +if six.PY2: + from mock import patch +else: + from unittest.mock import patch + +class TestDefaultArguments(TestCase): + + def test_invalid_arguments(self): + parser = zulip.add_default_arguments(argparse.ArgumentParser(usage="lorem ipsum")) + with self.assertRaises(SystemExit) as cm, patch('sys.stderr', new=six.StringIO()) as mock_stderr: + parser.parse_args(['invalid argument']) + 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): + parser = zulip.add_default_arguments(argparse.ArgumentParser(usage="lorem ipsum")) + test_path = '~/zuliprc' + args = parser.parse_args(['--config-file', test_path]) + with self.assertRaises(RuntimeError) as cm: + 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() diff --git a/zulip_botserver/tests/__init__.py b/zulip_botserver/tests/__init__.py index e69de29..bb61062 100644 --- a/zulip_botserver/tests/__init__.py +++ b/zulip_botserver/tests/__init__.py @@ -0,0 +1,2 @@ +import pkgutil +__path__ = pkgutil.extend_path(__path__, __name__)