diff --git a/celery/backends/filesystem.py b/celery/backends/filesystem.py index ab1a46132cf..d0aee69efc0 100644 --- a/celery/backends/filesystem.py +++ b/celery/backends/filesystem.py @@ -20,6 +20,10 @@ default_encoding = locale.getpreferredencoding(False) +E_NO_PATH_SET = 'You need to configure a path for the file-system backend' +E_PATH_NON_CONFORMING_SCHEME = ( + 'A path for the file-system backend should conform to the file URI scheme' +) E_PATH_INVALID = """\ The configured path for the file-system backend does not work correctly, please make sure that it exists and has @@ -56,10 +60,12 @@ def __init__(self, url=None, open=open, unlink=os.unlink, sep=os.sep, def _find_path(self, url): if not url: - raise ImproperlyConfigured( - 'You need to configure a path for the File-system backend') - if url is not None and url.startswith('file:///'): + raise ImproperlyConfigured(E_NO_PATH_SET) + if url.startswith('file:///'): return url[7:] + if url.startswith('file://localhost/'): + return url[16:] + raise ImproperlyConfigured(E_PATH_NON_CONFORMING_SCHEME) def _do_directory_test(self, key): try: diff --git a/t/unit/backends/test_filesystem.py b/t/unit/backends/test_filesystem.py index 7b755d95229..8a5df5f6e6f 100644 --- a/t/unit/backends/test_filesystem.py +++ b/t/unit/backends/test_filesystem.py @@ -8,6 +8,7 @@ from case import skip from celery import states, uuid +from celery.backends import filesystem from celery.backends.filesystem import FilesystemBackend from celery.exceptions import ImproperlyConfigured @@ -28,9 +29,26 @@ def test_a_path_in_url(self): tb = FilesystemBackend(app=self.app, url=self.url) assert tb.path == self.path - def test_path_is_incorrect(self): - with pytest.raises(ImproperlyConfigured): - FilesystemBackend(app=self.app, url=self.url + '-incorrect') + @pytest.mark.parametrize("url,expected_error_message", [ + ('file:///non-existing', filesystem.E_PATH_INVALID), + ('url://non-conforming', filesystem.E_PATH_NON_CONFORMING_SCHEME), + (None, filesystem.E_NO_PATH_SET) + ]) + def test_raises_meaningful_errors_for_invalid_urls( + self, + url, + expected_error_message + ): + with pytest.raises( + ImproperlyConfigured, + match=expected_error_message + ): + FilesystemBackend(app=self.app, url=url) + + def test_localhost_is_removed_from_url(self): + url = 'file://localhost' + self.directory + tb = FilesystemBackend(app=self.app, url=url) + assert tb.path == self.path def test_missing_task_is_PENDING(self): tb = FilesystemBackend(app=self.app, url=self.url)