From 201838603a2982909c09c33922d9d847e251cca6 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Mon, 15 Feb 2016 22:18:31 +0200 Subject: [PATCH 1/4] Fix #782 regression: expand ~/path/to for static file serving --- aiohttp/web_urldispatcher.py | 5 ++++- tests/test_urldispatch.py | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index 8a6bcb9b2b6..a5483b2d1db 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -440,7 +440,10 @@ def __init__(self, name, prefix, directory, *, self._prefix = prefix self._prefix_len = len(self._prefix) try: - directory = Path(directory).resolve() + directory = Path(directory) + if str(directory).startswith('~'): + directory = directory.expanduser() + directory = directory.resolve() if not directory.is_dir(): raise ValueError('Not a directory') except (FileNotFoundError, ValueError) as error: diff --git a/tests/test_urldispatch.py b/tests/test_urldispatch.py index 1d60f4113f1..41e9ba611d4 100644 --- a/tests/test_urldispatch.py +++ b/tests/test_urldispatch.py @@ -874,3 +874,12 @@ def test_resources_abc(self): self.assertIsInstance(self.router.resources(), Sized) self.assertIsInstance(self.router.resources(), Iterable) self.assertIsInstance(self.router.resources(), Container) + + def test_static_route_user_home(self): + here = pathlib.Path(aiohttp.__file__).parent + home = pathlib.Path('~').expanduser() + if not str(here).startswith(str(home)): # pragma: no cover + self.skipTest("aiohttp folder is not placed in user's HOME") + static_dir = '~/' + str(here.relative_to(home)) + route = self.router.add_static('/st', static_dir) + self.assertEqual(here, route.get_info()['directory']) From 9b62e305e770ba3bc3604128695447b0c37216cd Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Mon, 15 Feb 2016 23:42:07 +0200 Subject: [PATCH 2/4] Yet another test --- tests/test_urldispatch.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_urldispatch.py b/tests/test_urldispatch.py index 41e9ba611d4..52bb5e65fee 100644 --- a/tests/test_urldispatch.py +++ b/tests/test_urldispatch.py @@ -883,3 +883,8 @@ def test_static_route_user_home(self): static_dir = '~/' + str(here.relative_to(home)) route = self.router.add_static('/st', static_dir) self.assertEqual(here, route.get_info()['directory']) + + def test_static_route_points_to_file(self): + here = pathlib.Path(aiohttp.__file__).parent / '__init__.py' + with self.assertRaises(ValueError): + self.router.add_static('/st', here) From 2f681820c5bd0a2e7b4e981963bc769f8e147282 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Tue, 16 Feb 2016 11:05:54 +0200 Subject: [PATCH 3/4] Python 3.4 has no Path.expanduser --- aiohttp/web_urldispatcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index a5483b2d1db..e9e57ed9ff5 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -442,7 +442,7 @@ def __init__(self, name, prefix, directory, *, try: directory = Path(directory) if str(directory).startswith('~'): - directory = directory.expanduser() + directory = Path(os.path.expanduser(str(directory))) directory = directory.resolve() if not directory.is_dir(): raise ValueError('Not a directory') From ddc135c9d48b54c75a70f53d1c39e97b35594ecf Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Tue, 16 Feb 2016 11:13:22 +0200 Subject: [PATCH 4/4] Fix test --- tests/test_urldispatch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_urldispatch.py b/tests/test_urldispatch.py index 52bb5e65fee..427646a5410 100644 --- a/tests/test_urldispatch.py +++ b/tests/test_urldispatch.py @@ -877,7 +877,7 @@ def test_resources_abc(self): def test_static_route_user_home(self): here = pathlib.Path(aiohttp.__file__).parent - home = pathlib.Path('~').expanduser() + home = pathlib.Path(os.path.expanduser('~')) if not str(here).startswith(str(home)): # pragma: no cover self.skipTest("aiohttp folder is not placed in user's HOME") static_dir = '~/' + str(here.relative_to(home))