diff options
| author | Jon Dufresne <jon.dufresne@gmail.com> | 2019-01-28 07:01:35 -0800 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2019-01-28 11:15:06 -0500 |
| commit | 7785e03ba89aafbd949191f126361fb9103cb980 (patch) | |
| tree | 5776f7063604285445cfcebf6efb42fd5d063f60 /tests | |
| parent | 7444f3252757ed4384623e5afd7dcfeef3e0c74e (diff) | |
Fixed #30137 -- Replaced OSError aliases with the canonical OSError.
Used more specific errors (e.g. FileExistsError) as appropriate.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/admin_widgets/tests.py | 2 | ||||
| -rw-r--r-- | tests/cache/tests.py | 4 | ||||
| -rw-r--r-- | tests/csrf_tests/tests.py | 8 | ||||
| -rw-r--r-- | tests/file_storage/tests.py | 4 | ||||
| -rw-r--r-- | tests/file_uploads/tests.py | 7 | ||||
| -rw-r--r-- | tests/files/tests.py | 5 | ||||
| -rw-r--r-- | tests/i18n/tests.py | 7 | ||||
| -rw-r--r-- | tests/mail/tests.py | 3 | ||||
| -rw-r--r-- | tests/requests/tests.py | 8 | ||||
| -rw-r--r-- | tests/responses/tests.py | 6 | ||||
| -rw-r--r-- | tests/servers/tests.py | 4 | ||||
| -rw-r--r-- | tests/sessions_tests/tests.py | 2 | ||||
| -rw-r--r-- | tests/staticfiles_tests/cases.py | 2 | ||||
| -rw-r--r-- | tests/template_tests/test_loaders.py | 4 |
14 files changed, 30 insertions, 36 deletions
diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py index 623fa39bc1..a7335f8f69 100644 --- a/tests/admin_widgets/tests.py +++ b/tests/admin_widgets/tests.py @@ -867,7 +867,7 @@ class DateTimePickerSeleniumTests(AdminWidgetSeleniumTestCase): for language_code, language_name in settings.LANGUAGES: try: catalog = gettext.translation('djangojs', path, [language_code]) - except IOError: + except OSError: continue if month_string in catalog._catalog: month_name = catalog._catalog[month_string] diff --git a/tests/cache/tests.py b/tests/cache/tests.py index 0dd9208710..9f717cfa23 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -1448,8 +1448,8 @@ class FileBasedCacheTests(BaseCacheTests, TestCase): self.assertEqual(cache.get('foo', 'baz'), 'baz') def test_get_does_not_ignore_non_filenotfound_exceptions(self): - with mock.patch('builtins.open', side_effect=IOError): - with self.assertRaises(IOError): + with mock.patch('builtins.open', side_effect=OSError): + with self.assertRaises(OSError): cache.get('foo') def test_empty_cache_file_considered_expired(self): diff --git a/tests/csrf_tests/tests.py b/tests/csrf_tests/tests.py index 2c40e44ae1..59abc6da32 100644 --- a/tests/csrf_tests/tests.py +++ b/tests/csrf_tests/tests.py @@ -441,12 +441,12 @@ class CsrfViewMiddlewareTestMixin: def test_post_data_read_failure(self): """ - #20128 -- IOErrors during POST data reading should be caught and - treated as if the POST data wasn't there. + OSErrors during POST data reading are caught and treated as if the + POST data wasn't there (#20128). """ class CsrfPostRequest(HttpRequest): """ - HttpRequest that can raise an IOError when accessing POST data + HttpRequest that can raise an OSError when accessing POST data """ def __init__(self, token, raise_error): super().__init__() @@ -464,7 +464,7 @@ class CsrfViewMiddlewareTestMixin: self.raise_error = raise_error def _load_post_and_files(self): - raise IOError('error reading input data') + raise OSError('error reading input data') def _get_post(self): if self.raise_error: diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index 8c50abc9b0..991f1ff8cd 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -482,9 +482,9 @@ class FileStorageTests(SimpleTestCase): f1 = ContentFile('chunks fails') def failing_chunks(): - raise IOError + raise OSError f1.chunks = failing_chunks - with self.assertRaises(IOError): + with self.assertRaises(OSError): self.storage.save('error.file', f1) def test_delete_no_name(self): diff --git a/tests/file_uploads/tests.py b/tests/file_uploads/tests.py index fb333dcf13..07a9d9b647 100644 --- a/tests/file_uploads/tests.py +++ b/tests/file_uploads/tests.py @@ -548,16 +548,13 @@ class DirectoryCreationTests(SimpleTestCase): self.obj.testfile.save('foo.txt', SimpleUploadedFile('foo.txt', b'x'), save=False) def test_not_a_directory(self): - """The correct IOError is raised when the upload directory name exists but isn't a directory""" # Create a file with the upload directory name open(UPLOAD_TO, 'wb').close() self.addCleanup(os.remove, UPLOAD_TO) - with self.assertRaises(IOError) as exc_info: + msg = '%s exists and is not a directory.' % UPLOAD_TO + with self.assertRaisesMessage(FileExistsError, msg): with SimpleUploadedFile('foo.txt', b'x') as file: self.obj.testfile.save('foo.txt', file, save=False) - # The test needs to be done on a specific string as IOError - # is raised even without the patch (just not early enough) - self.assertEqual(exc_info.exception.args[0], "%s exists and is not a directory." % UPLOAD_TO) class MultiParserTests(SimpleTestCase): diff --git a/tests/files/tests.py b/tests/files/tests.py index b50061649a..1c005dde57 100644 --- a/tests/files/tests.py +++ b/tests/files/tests.py @@ -355,8 +355,9 @@ class FileMoveSafeTests(unittest.TestCase): handle_a, self.file_a = tempfile.mkstemp() handle_b, self.file_b = tempfile.mkstemp() - # file_move_safe should raise an IOError exception if destination file exists and allow_overwrite is False - with self.assertRaises(IOError): + # file_move_safe() raises OSError if the destination file exists and + # allow_overwrite is False. + with self.assertRaises(FileExistsError): file_move_safe(self.file_a, self.file_b, allow_overwrite=False) # should allow it and continue on if allow_overwrite is True diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 2377c8992e..0f58789237 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -1747,13 +1747,10 @@ class TranslationFilesMissing(SimpleTestCase): gettext_module.find = lambda *args, **kw: None def test_failure_finding_default_mo_files(self): - ''' - Ensure IOError is raised if the default language is unparseable. - Refs: #18192 - ''' + """OSError is raised if the default language is unparseable.""" self.patchGettextFind() trans_real._translations = {} - with self.assertRaises(IOError): + with self.assertRaises(OSError): activate('en') diff --git a/tests/mail/tests.py b/tests/mail/tests.py index f06e053074..e283101b79 100644 --- a/tests/mail/tests.py +++ b/tests/mail/tests.py @@ -4,7 +4,6 @@ import mimetypes import os import shutil import smtpd -import socket import sys import tempfile import threading @@ -1570,7 +1569,7 @@ class SMTPBackendStoppedServerTests(SMTPBackendTestsBase): """ A socket connection error is silenced with fail_silently=True. """ - with self.assertRaises(socket.error): + with self.assertRaises(ConnectionError): self.backend.open() self.backend.fail_silently = True self.backend.open() diff --git a/tests/requests/tests.py b/tests/requests/tests.py index 720178a8b4..9391ca2a08 100644 --- a/tests/requests/tests.py +++ b/tests/requests/tests.py @@ -479,11 +479,11 @@ class RequestsTests(SimpleTestCase): def test_POST_connection_error(self): """ If wsgi.input.read() raises an exception while trying to read() the - POST, the exception should be identifiable (not a generic IOError). + POST, the exception is identifiable (not a generic OSError). """ class ExplodingBytesIO(BytesIO): def read(self, len=0): - raise IOError("kaboom!") + raise OSError('kaboom!') payload = b'name=value' request = WSGIRequest({ @@ -520,11 +520,11 @@ class RequestsTests(SimpleTestCase): def test_FILES_connection_error(self): """ If wsgi.input.read() raises an exception while trying to read() the - FILES, the exception should be identifiable (not a generic IOError). + FILES, the exception is identifiable (not a generic OSError). """ class ExplodingBytesIO(BytesIO): def read(self, len=0): - raise IOError("kaboom!") + raise OSError('kaboom!') payload = b'x' request = WSGIRequest({ diff --git a/tests/responses/tests.py b/tests/responses/tests.py index 8adf092ca0..934e4dfe60 100644 --- a/tests/responses/tests.py +++ b/tests/responses/tests.py @@ -22,14 +22,14 @@ class HttpResponseBaseTests(SimpleTestCase): r = HttpResponseBase() self.assertIs(r.writable(), False) - with self.assertRaisesMessage(IOError, 'This HttpResponseBase instance is not writable'): + with self.assertRaisesMessage(OSError, 'This HttpResponseBase instance is not writable'): r.write('asdf') - with self.assertRaisesMessage(IOError, 'This HttpResponseBase instance is not writable'): + with self.assertRaisesMessage(OSError, 'This HttpResponseBase instance is not writable'): r.writelines(['asdf\n', 'qwer\n']) def test_tell(self): r = HttpResponseBase() - with self.assertRaisesMessage(IOError, 'This HttpResponseBase instance cannot tell its position'): + with self.assertRaisesMessage(OSError, 'This HttpResponseBase instance cannot tell its position'): r.tell() def test_setdefault(self): diff --git a/tests/servers/tests.py b/tests/servers/tests.py index 7f75b85d6c..1f111fb5ae 100644 --- a/tests/servers/tests.py +++ b/tests/servers/tests.py @@ -194,10 +194,10 @@ class LiveServerPort(LiveServerBase): TestCase = type("TestCase", (LiveServerBase,), {}) try: TestCase.setUpClass() - except socket.error as e: + except OSError as e: if e.errno == errno.EADDRINUSE: # We're out of ports, LiveServerTestCase correctly fails with - # a socket error. + # an OSError. return # Unexpected error. raise diff --git a/tests/sessions_tests/tests.py b/tests/sessions_tests/tests.py index 733f5adb1d..0e8cb79fd5 100644 --- a/tests/sessions_tests/tests.py +++ b/tests/sessions_tests/tests.py @@ -532,7 +532,7 @@ class FileSessionTests(SessionTestsMixin, unittest.TestCase): def test_invalid_key_backslash(self): # Ensure we don't allow directory-traversal. # This is tested directly on _key_to_file, as load() will swallow - # a SuspiciousOperation in the same way as an IOError - by creating + # a SuspiciousOperation in the same way as an OSError - by creating # a new session, making it unclear whether the slashes were detected. with self.assertRaises(InvalidSessionKey): self.backend()._key_to_file("a\\b\\c") diff --git a/tests/staticfiles_tests/cases.py b/tests/staticfiles_tests/cases.py index ea746164a6..24de4e029e 100644 --- a/tests/staticfiles_tests/cases.py +++ b/tests/staticfiles_tests/cases.py @@ -23,7 +23,7 @@ class BaseStaticFilesMixin: ) def assertFileNotFound(self, filepath): - with self.assertRaises(IOError): + with self.assertRaises(OSError): self._get_file(filepath) def render_template(self, template, **kwargs): diff --git a/tests/template_tests/test_loaders.py b/tests/template_tests/test_loaders.py index ea69472264..9023457751 100644 --- a/tests/template_tests/test_loaders.py +++ b/tests/template_tests/test_loaders.py @@ -191,11 +191,11 @@ class FileSystemLoaderTests(SimpleTestCase): tmppath = os.path.join(tmpdir, tmpfile.name) os.chmod(tmppath, 0o0222) with self.set_dirs([tmpdir]): - with self.assertRaisesMessage(IOError, 'Permission denied'): + with self.assertRaisesMessage(PermissionError, 'Permission denied'): self.engine.get_template(tmpfile.name) def test_notafile_error(self): - with self.assertRaises(IOError): + with self.assertRaises(IsADirectoryError): self.engine.get_template('first') |
