Skip to content

Commit 44b6fd3

Browse files
ganpa3timabbott
authored andcommitted
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
1 parent 5f1590f commit 44b6fd3

File tree

8 files changed

+23
-19
lines changed

8 files changed

+23
-19
lines changed

tools/lint

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#! /usr/bin/env python3
22

33
import argparse
4+
import sys
45

56
from zulint.command import add_default_linter_arguments, LinterConfig
67

@@ -21,7 +22,7 @@ def run() -> None:
2122
by_lang = linter_config.list_files(file_types=['py', 'sh', 'json', 'md', 'txt'],
2223
exclude=EXCLUDED_FILES)
2324

24-
linter_config.external_linter('mypy', ['tools/run-mypy'], ['py'], pass_targets=False,
25+
linter_config.external_linter('mypy', [sys.executable, 'tools/run-mypy'], ['py'], pass_targets=False,
2526
description="Static type checker for Python (config: mypy.ini)")
2627
linter_config.external_linter('flake8', ['flake8'], ['py'],
2728
description="Standard Python linter (config: .flake8)")

zulip_bots/zulip_bots/bots/file_uploader/test_file_uploader.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_file_upload_failed(self, is_file: Mock, resolve: Mock) -> None:
2020
server_reply = dict(result='', msg='error')
2121
with patch('zulip_bots.test_lib.StubBotHandler.upload_file_from_path',
2222
return_value=server_reply):
23-
self.verify_reply('file.txt', 'Failed to upload `/file.txt` file: error')
23+
self.verify_reply('file.txt', 'Failed to upload `{}` file: error'.format(Path('file.txt').resolve()))
2424

2525
@patch('pathlib.Path.resolve', return_value=Path('/file.txt'))
2626
@patch('pathlib.Path.is_file', return_value=True)

zulip_bots/zulip_bots/bots/yoda/yoda.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def handle_input(self, message: Dict[str, str], bot_handler: BotHandler) -> None
9898
if len(reply_message) == 0:
9999
reply_message = 'Invalid input, please check the sentence you have entered.'
100100

101-
except ssl.SSLError or TypeError:
101+
except (ssl.SSLError, TypeError):
102102
reply_message = 'The service is temporarily unavailable, please try again.'
103103
logging.error(reply_message)
104104

zulip_bots/zulip_bots/finder.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
import os
3-
from os.path import basename, splitext
43
from typing import Any, Optional, Text, Tuple
4+
from pathlib import Path
55

66
current_dir = os.path.dirname(os.path.abspath(__file__))
77

@@ -30,14 +30,14 @@ def import_module_by_name(name: Text) -> Any:
3030
except ImportError:
3131
return None
3232

33-
def resolve_bot_path(name: Text) -> Optional[Tuple[Text, Text]]:
33+
def resolve_bot_path(name: Text) -> Optional[Tuple[Path, Text]]:
3434
if os.path.isfile(name):
35-
bot_path = os.path.abspath(name)
36-
bot_name = splitext(basename(bot_path))[0]
35+
bot_path = Path(name)
36+
bot_name = Path(bot_path).stem
3737
return (bot_path, bot_name)
3838
else:
3939
bot_name = name
40-
bot_path = os.path.abspath(os.path.join(current_dir, 'bots', bot_name, bot_name + '.py'))
40+
bot_path = Path(current_dir, 'bots', bot_name, bot_name + '.py')
4141
if os.path.isfile(bot_path):
4242
return (bot_path, bot_name)
4343

zulip_bots/zulip_bots/run.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def main() -> None:
110110
provision_bot(os.path.dirname(bot_path), args.force)
111111

112112
try:
113-
lib_module = finder.import_module_from_source(bot_path, bot_name)
113+
lib_module = finder.import_module_from_source(bot_path.as_posix(), bot_name)
114114
except ImportError:
115115
req_path = os.path.join(os.path.dirname(bot_path), "requirements.txt")
116116
with open(req_path) as fp:

zulip_bots/zulip_bots/tests/test_finder.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import os
21
from unittest import TestCase
2+
from pathlib import Path
33

44
from zulip_bots import finder
55

66

77
class FinderTestCase(TestCase):
88

99
def test_resolve_bot_path(self) -> None:
10-
current_directory = os.path.dirname(os.path.abspath(__file__))
11-
expected_bot_path = os.path.abspath(current_directory + '/../bots/helloworld/helloworld.py')
10+
current_directory = Path(__file__).parents[1].as_posix()
11+
expected_bot_path = Path(current_directory + '/bots/helloworld/helloworld.py')
1212
expected_bot_name = 'helloworld'
1313
expected_bot_path_and_name = (expected_bot_path, expected_bot_name)
1414
actual_bot_path_and_name = finder.resolve_bot_path('helloworld')

zulip_bots/zulip_bots/tests/test_run.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python3
2+
from pathlib import Path
23
import os
34
import sys
45
import zulip_bots.run
@@ -43,17 +44,17 @@ def test_argument_parsing_with_bot_path(self, mock_run_message_handler_for_bot:
4344

4445
def test_adding_bot_parent_dir_to_sys_path_when_bot_name_specified(self) -> None:
4546
bot_name = 'helloworld' # existing bot's name
46-
expected_bot_dir_path = os.path.join(
47+
expected_bot_dir_path = Path(
4748
os.path.dirname(zulip_bots.run.__file__),
4849
'bots',
4950
bot_name
50-
)
51+
).as_posix()
5152
self._test_adding_bot_parent_dir_to_sys_path(bot_qualifier=bot_name, bot_dir_path=expected_bot_dir_path)
5253

5354
@patch('os.path.isfile', return_value=True)
5455
def test_adding_bot_parent_dir_to_sys_path_when_bot_path_specified(self, mock_os_path_isfile: mock.Mock) -> None:
5556
bot_path = '/path/to/bot'
56-
expected_bot_dir_path = '/path/to'
57+
expected_bot_dir_path = Path('/path/to').as_posix()
5758
self._test_adding_bot_parent_dir_to_sys_path(bot_qualifier=bot_path, bot_dir_path=expected_bot_dir_path)
5859

5960
def _test_adding_bot_parent_dir_to_sys_path(self, bot_qualifier: str, bot_dir_path: str) -> None:
@@ -63,7 +64,8 @@ def _test_adding_bot_parent_dir_to_sys_path(self, bot_qualifier: str, bot_dir_pa
6364
with patch('zulip_bots.run.exit_gracefully_if_zulip_config_is_missing'):
6465
zulip_bots.run.main()
6566

66-
self.assertIn(bot_dir_path, sys.path)
67+
sys_path = [Path(path).as_posix() for path in sys.path]
68+
self.assertIn(bot_dir_path, sys_path)
6769

6870
@patch('os.path.isfile', return_value=False)
6971
def test_run_bot_by_module_name(self, mock_os_path_isfile: mock.Mock) -> None:

zulip_botserver/tests/test_server.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from collections import OrderedDict
99
from importlib import import_module
1010
from types import ModuleType
11+
from pathlib import Path
1112

1213
from zulip_botserver import server
1314
from zulip_botserver.input_parameters import parse_args
@@ -214,13 +215,13 @@ def test_load_lib_modules(self) -> None:
214215
# restructure zulip_bots, this test would fail and we would also update Botserver
215216
# at the same time.
216217
helloworld = import_module('zulip_bots.bots.{bot}.{bot}'.format(bot='helloworld'))
217-
root_dir = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../'))
218+
root_dir = Path(__file__).parents[2].as_posix()
218219
# load valid module name
219220
module = server.load_lib_modules(['helloworld'])['helloworld']
220221
assert module == helloworld
221222

222223
# load valid file path
223-
path = os.path.join(root_dir, 'zulip_bots/zulip_bots/bots/{bot}/{bot}.py'.format(bot='helloworld'))
224+
path = Path(root_dir, 'zulip_bots/zulip_bots/bots/{bot}/{bot}.py'.format(bot='helloworld')).as_posix()
224225
module = server.load_lib_modules([path])[path]
225226
assert module.__name__ == 'custom_bot_module'
226227
assert module.__file__ == path
@@ -236,7 +237,7 @@ def test_load_lib_modules(self) -> None:
236237
with self.assertRaisesRegexp(SystemExit, # type: ignore
237238
'Error: Bot "{}/zulip_bots/zulip_bots/bots/helloworld.py" doesn\'t exist. '
238239
'Please make sure you have set up the botserverrc file correctly.'.format(root_dir)):
239-
path = os.path.join(root_dir, 'zulip_bots/zulip_bots/bots/{bot}.py'.format(bot='helloworld'))
240+
path = Path(root_dir, 'zulip_bots/zulip_bots/bots/{bot}.py'.format(bot='helloworld')).as_posix()
240241
module = server.load_lib_modules([path])[path]
241242

242243
if __name__ == '__main__':

0 commit comments

Comments
 (0)