tests: Fix tests failing on Windows.
Tests were failing on Windows since paths are case-insensitive on it. This uses pathlib library to compare paths on all platforms. Fixes #651
This commit is contained in:
parent
5f1590f12a
commit
44b6fd395a
|
@ -1,6 +1,7 @@
|
||||||
#! /usr/bin/env python3
|
#! /usr/bin/env python3
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
import sys
|
||||||
|
|
||||||
from zulint.command import add_default_linter_arguments, LinterConfig
|
from zulint.command import add_default_linter_arguments, LinterConfig
|
||||||
|
|
||||||
|
@ -21,7 +22,7 @@ def run() -> None:
|
||||||
by_lang = linter_config.list_files(file_types=['py', 'sh', 'json', 'md', 'txt'],
|
by_lang = linter_config.list_files(file_types=['py', 'sh', 'json', 'md', 'txt'],
|
||||||
exclude=EXCLUDED_FILES)
|
exclude=EXCLUDED_FILES)
|
||||||
|
|
||||||
linter_config.external_linter('mypy', ['tools/run-mypy'], ['py'], pass_targets=False,
|
linter_config.external_linter('mypy', [sys.executable, 'tools/run-mypy'], ['py'], pass_targets=False,
|
||||||
description="Static type checker for Python (config: mypy.ini)")
|
description="Static type checker for Python (config: mypy.ini)")
|
||||||
linter_config.external_linter('flake8', ['flake8'], ['py'],
|
linter_config.external_linter('flake8', ['flake8'], ['py'],
|
||||||
description="Standard Python linter (config: .flake8)")
|
description="Standard Python linter (config: .flake8)")
|
||||||
|
|
|
@ -20,7 +20,7 @@ class TestFileUploaderBot(BotTestCase, DefaultTests):
|
||||||
server_reply = dict(result='', msg='error')
|
server_reply = dict(result='', msg='error')
|
||||||
with patch('zulip_bots.test_lib.StubBotHandler.upload_file_from_path',
|
with patch('zulip_bots.test_lib.StubBotHandler.upload_file_from_path',
|
||||||
return_value=server_reply):
|
return_value=server_reply):
|
||||||
self.verify_reply('file.txt', 'Failed to upload `/file.txt` file: error')
|
self.verify_reply('file.txt', 'Failed to upload `{}` file: error'.format(Path('file.txt').resolve()))
|
||||||
|
|
||||||
@patch('pathlib.Path.resolve', return_value=Path('/file.txt'))
|
@patch('pathlib.Path.resolve', return_value=Path('/file.txt'))
|
||||||
@patch('pathlib.Path.is_file', return_value=True)
|
@patch('pathlib.Path.is_file', return_value=True)
|
||||||
|
|
|
@ -98,7 +98,7 @@ class YodaSpeakHandler:
|
||||||
if len(reply_message) == 0:
|
if len(reply_message) == 0:
|
||||||
reply_message = 'Invalid input, please check the sentence you have entered.'
|
reply_message = 'Invalid input, please check the sentence you have entered.'
|
||||||
|
|
||||||
except ssl.SSLError or TypeError:
|
except (ssl.SSLError, TypeError):
|
||||||
reply_message = 'The service is temporarily unavailable, please try again.'
|
reply_message = 'The service is temporarily unavailable, please try again.'
|
||||||
logging.error(reply_message)
|
logging.error(reply_message)
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
from os.path import basename, splitext
|
|
||||||
from typing import Any, Optional, Text, Tuple
|
from typing import Any, Optional, Text, Tuple
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
@ -30,14 +30,14 @@ def import_module_by_name(name: Text) -> Any:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def resolve_bot_path(name: Text) -> Optional[Tuple[Text, Text]]:
|
def resolve_bot_path(name: Text) -> Optional[Tuple[Path, Text]]:
|
||||||
if os.path.isfile(name):
|
if os.path.isfile(name):
|
||||||
bot_path = os.path.abspath(name)
|
bot_path = Path(name)
|
||||||
bot_name = splitext(basename(bot_path))[0]
|
bot_name = Path(bot_path).stem
|
||||||
return (bot_path, bot_name)
|
return (bot_path, bot_name)
|
||||||
else:
|
else:
|
||||||
bot_name = name
|
bot_name = name
|
||||||
bot_path = os.path.abspath(os.path.join(current_dir, 'bots', bot_name, bot_name + '.py'))
|
bot_path = Path(current_dir, 'bots', bot_name, bot_name + '.py')
|
||||||
if os.path.isfile(bot_path):
|
if os.path.isfile(bot_path):
|
||||||
return (bot_path, bot_name)
|
return (bot_path, bot_name)
|
||||||
|
|
||||||
|
|
|
@ -110,7 +110,7 @@ def main() -> None:
|
||||||
provision_bot(os.path.dirname(bot_path), args.force)
|
provision_bot(os.path.dirname(bot_path), args.force)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
lib_module = finder.import_module_from_source(bot_path, bot_name)
|
lib_module = finder.import_module_from_source(bot_path.as_posix(), bot_name)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
req_path = os.path.join(os.path.dirname(bot_path), "requirements.txt")
|
req_path = os.path.join(os.path.dirname(bot_path), "requirements.txt")
|
||||||
with open(req_path) as fp:
|
with open(req_path) as fp:
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import os
|
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from zulip_bots import finder
|
from zulip_bots import finder
|
||||||
|
|
||||||
|
@ -7,8 +7,8 @@ from zulip_bots import finder
|
||||||
class FinderTestCase(TestCase):
|
class FinderTestCase(TestCase):
|
||||||
|
|
||||||
def test_resolve_bot_path(self) -> None:
|
def test_resolve_bot_path(self) -> None:
|
||||||
current_directory = os.path.dirname(os.path.abspath(__file__))
|
current_directory = Path(__file__).parents[1].as_posix()
|
||||||
expected_bot_path = os.path.abspath(current_directory + '/../bots/helloworld/helloworld.py')
|
expected_bot_path = Path(current_directory + '/bots/helloworld/helloworld.py')
|
||||||
expected_bot_name = 'helloworld'
|
expected_bot_name = 'helloworld'
|
||||||
expected_bot_path_and_name = (expected_bot_path, expected_bot_name)
|
expected_bot_path_and_name = (expected_bot_path, expected_bot_name)
|
||||||
actual_bot_path_and_name = finder.resolve_bot_path('helloworld')
|
actual_bot_path_and_name = finder.resolve_bot_path('helloworld')
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
from pathlib import Path
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import zulip_bots.run
|
import zulip_bots.run
|
||||||
|
@ -43,17 +44,17 @@ class TestDefaultArguments(TestCase):
|
||||||
|
|
||||||
def test_adding_bot_parent_dir_to_sys_path_when_bot_name_specified(self) -> None:
|
def test_adding_bot_parent_dir_to_sys_path_when_bot_name_specified(self) -> None:
|
||||||
bot_name = 'helloworld' # existing bot's name
|
bot_name = 'helloworld' # existing bot's name
|
||||||
expected_bot_dir_path = os.path.join(
|
expected_bot_dir_path = Path(
|
||||||
os.path.dirname(zulip_bots.run.__file__),
|
os.path.dirname(zulip_bots.run.__file__),
|
||||||
'bots',
|
'bots',
|
||||||
bot_name
|
bot_name
|
||||||
)
|
).as_posix()
|
||||||
self._test_adding_bot_parent_dir_to_sys_path(bot_qualifier=bot_name, bot_dir_path=expected_bot_dir_path)
|
self._test_adding_bot_parent_dir_to_sys_path(bot_qualifier=bot_name, bot_dir_path=expected_bot_dir_path)
|
||||||
|
|
||||||
@patch('os.path.isfile', return_value=True)
|
@patch('os.path.isfile', return_value=True)
|
||||||
def test_adding_bot_parent_dir_to_sys_path_when_bot_path_specified(self, mock_os_path_isfile: mock.Mock) -> None:
|
def test_adding_bot_parent_dir_to_sys_path_when_bot_path_specified(self, mock_os_path_isfile: mock.Mock) -> None:
|
||||||
bot_path = '/path/to/bot'
|
bot_path = '/path/to/bot'
|
||||||
expected_bot_dir_path = '/path/to'
|
expected_bot_dir_path = Path('/path/to').as_posix()
|
||||||
self._test_adding_bot_parent_dir_to_sys_path(bot_qualifier=bot_path, bot_dir_path=expected_bot_dir_path)
|
self._test_adding_bot_parent_dir_to_sys_path(bot_qualifier=bot_path, bot_dir_path=expected_bot_dir_path)
|
||||||
|
|
||||||
def _test_adding_bot_parent_dir_to_sys_path(self, bot_qualifier: str, bot_dir_path: str) -> None:
|
def _test_adding_bot_parent_dir_to_sys_path(self, bot_qualifier: str, bot_dir_path: str) -> None:
|
||||||
|
@ -63,7 +64,8 @@ class TestDefaultArguments(TestCase):
|
||||||
with patch('zulip_bots.run.exit_gracefully_if_zulip_config_is_missing'):
|
with patch('zulip_bots.run.exit_gracefully_if_zulip_config_is_missing'):
|
||||||
zulip_bots.run.main()
|
zulip_bots.run.main()
|
||||||
|
|
||||||
self.assertIn(bot_dir_path, sys.path)
|
sys_path = [Path(path).as_posix() for path in sys.path]
|
||||||
|
self.assertIn(bot_dir_path, sys_path)
|
||||||
|
|
||||||
@patch('os.path.isfile', return_value=False)
|
@patch('os.path.isfile', return_value=False)
|
||||||
def test_run_bot_by_module_name(self, mock_os_path_isfile: mock.Mock) -> None:
|
def test_run_bot_by_module_name(self, mock_os_path_isfile: mock.Mock) -> None:
|
||||||
|
|
|
@ -8,6 +8,7 @@ import json
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from zulip_botserver import server
|
from zulip_botserver import server
|
||||||
from zulip_botserver.input_parameters import parse_args
|
from zulip_botserver.input_parameters import parse_args
|
||||||
|
@ -214,13 +215,13 @@ class BotServerTests(BotServerTestCase):
|
||||||
# restructure zulip_bots, this test would fail and we would also update Botserver
|
# restructure zulip_bots, this test would fail and we would also update Botserver
|
||||||
# at the same time.
|
# at the same time.
|
||||||
helloworld = import_module('zulip_bots.bots.{bot}.{bot}'.format(bot='helloworld'))
|
helloworld = import_module('zulip_bots.bots.{bot}.{bot}'.format(bot='helloworld'))
|
||||||
root_dir = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))
|
root_dir = Path(__file__).parents[2].as_posix()
|
||||||
# load valid module name
|
# load valid module name
|
||||||
module = server.load_lib_modules(['helloworld'])['helloworld']
|
module = server.load_lib_modules(['helloworld'])['helloworld']
|
||||||
assert module == helloworld
|
assert module == helloworld
|
||||||
|
|
||||||
# load valid file path
|
# load valid file path
|
||||||
path = os.path.join(root_dir, 'zulip_bots/zulip_bots/bots/{bot}/{bot}.py'.format(bot='helloworld'))
|
path = Path(root_dir, 'zulip_bots/zulip_bots/bots/{bot}/{bot}.py'.format(bot='helloworld')).as_posix()
|
||||||
module = server.load_lib_modules([path])[path]
|
module = server.load_lib_modules([path])[path]
|
||||||
assert module.__name__ == 'custom_bot_module'
|
assert module.__name__ == 'custom_bot_module'
|
||||||
assert module.__file__ == path
|
assert module.__file__ == path
|
||||||
|
@ -236,7 +237,7 @@ class BotServerTests(BotServerTestCase):
|
||||||
with self.assertRaisesRegexp(SystemExit, # type: ignore
|
with self.assertRaisesRegexp(SystemExit, # type: ignore
|
||||||
'Error: Bot "{}/zulip_bots/zulip_bots/bots/helloworld.py" doesn\'t exist. '
|
'Error: Bot "{}/zulip_bots/zulip_bots/bots/helloworld.py" doesn\'t exist. '
|
||||||
'Please make sure you have set up the botserverrc file correctly.'.format(root_dir)):
|
'Please make sure you have set up the botserverrc file correctly.'.format(root_dir)):
|
||||||
path = os.path.join(root_dir, 'zulip_bots/zulip_bots/bots/{bot}.py'.format(bot='helloworld'))
|
path = Path(root_dir, 'zulip_bots/zulip_bots/bots/{bot}.py'.format(bot='helloworld')).as_posix()
|
||||||
module = server.load_lib_modules([path])[path]
|
module = server.load_lib_modules([path])[path]
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue