diff --git a/django/utils/_os.py b/django/utils/_os.py index 8309e08fbf..937ba82fbc 100644 --- a/django/utils/_os.py +++ b/django/utils/_os.py @@ -56,18 +56,13 @@ def symlinks_supported(): host platform and/or if they are allowed to be created (e.g. on Windows it requires admin permissions). """ - tmpdir = tempfile.mkdtemp() - original_path = os.path.join(tmpdir, 'original') - symlink_path = os.path.join(tmpdir, 'symlink') - os.makedirs(original_path) - try: - os.symlink(original_path, symlink_path) - supported = True - except (OSError, NotImplementedError, AttributeError): - supported = False - else: - os.remove(symlink_path) - finally: - os.rmdir(original_path) - os.rmdir(tmpdir) + with tempfile.TemporaryDirectory() as temp_dir: + original_path = os.path.join(temp_dir, 'original') + symlink_path = os.path.join(temp_dir, 'symlink') + os.makedirs(original_path) + try: + os.symlink(original_path, symlink_path) + supported = True + except (OSError, NotImplementedError): + supported = False return supported diff --git a/tests/file_uploads/tests.py b/tests/file_uploads/tests.py index 51e6937598..343d3f2de6 100644 --- a/tests/file_uploads/tests.py +++ b/tests/file_uploads/tests.py @@ -103,21 +103,13 @@ class FileUploadTests(TestCase): self._test_base64_upload("Big data" * 68000, encode=base64.encodebytes) def test_unicode_file_name(self): - tdir = sys_tempfile.mkdtemp() - self.addCleanup(shutil.rmtree, tdir, True) - - # This file contains Chinese symbols and an accented char in the name. - with open(os.path.join(tdir, UNICODE_FILENAME), 'w+b') as file1: - file1.write(b'b' * (2 ** 10)) - file1.seek(0) - - post_data = { - 'file_unicode': file1, - } - - response = self.client.post('/unicode_name/', post_data) - - self.assertEqual(response.status_code, 200) + with sys_tempfile.TemporaryDirectory() as temp_dir: + # This file contains Chinese symbols and an accented char in the name. + with open(os.path.join(temp_dir, UNICODE_FILENAME), 'w+b') as file1: + file1.write(b'b' * (2 ** 10)) + file1.seek(0) + response = self.client.post('/unicode_name/', {'file_unicode': file1}) + self.assertEqual(response.status_code, 200) def test_unicode_file_name_rfc2231(self): """ diff --git a/tests/mail/tests.py b/tests/mail/tests.py index a902e5435c..242bb57915 100644 --- a/tests/mail/tests.py +++ b/tests/mail/tests.py @@ -493,14 +493,11 @@ class MailTests(HeadersCheckMixin, SimpleTestCase): mail.get_connection('django.core.mail.backends.console.EmailBackend'), console.EmailBackend ) - tmp_dir = tempfile.mkdtemp() - try: + with tempfile.TemporaryDirectory() as tmp_dir: self.assertIsInstance( mail.get_connection('django.core.mail.backends.filebased.EmailBackend', file_path=tmp_dir), filebased.EmailBackend ) - finally: - shutil.rmtree(tmp_dir) self.assertIsInstance(mail.get_connection(), locmem.EmailBackend) @override_settings( diff --git a/tests/migrations/test_base.py b/tests/migrations/test_base.py index 007c7c0ba9..d7ed632e51 100644 --- a/tests/migrations/test_base.py +++ b/tests/migrations/test_base.py @@ -98,8 +98,7 @@ class MigrationTestBase(TransactionTestCase): Returns the filesystem path to the temporary migrations module. """ - temp_dir = tempfile.mkdtemp() - try: + with tempfile.TemporaryDirectory() as temp_dir: target_dir = tempfile.mkdtemp(dir=temp_dir) with open(os.path.join(target_dir, '__init__.py'), 'w'): pass @@ -119,6 +118,3 @@ class MigrationTestBase(TransactionTestCase): new_module = os.path.basename(target_dir) + '.migrations' with self.settings(MIGRATION_MODULES={app_label: new_module}): yield target_migrations_dir - - finally: - shutil.rmtree(temp_dir) diff --git a/tests/staticfiles_tests/test_management.py b/tests/staticfiles_tests/test_management.py index ea58f9afb6..4a5963417e 100644 --- a/tests/staticfiles_tests/test_management.py +++ b/tests/staticfiles_tests/test_management.py @@ -218,10 +218,9 @@ class TestInteractiveMessages(CollectionTestCase): def test_no_warning_for_empty_staticdir(self): stdout = StringIO() - static_dir = tempfile.mkdtemp(prefix='collectstatic_empty_staticdir_test') - with override_settings(STATIC_ROOT=static_dir): - call_command('collectstatic', interactive=True, stdout=stdout) - shutil.rmtree(static_dir) + with tempfile.TemporaryDirectory(prefix='collectstatic_empty_staticdir_test') as static_dir: + with override_settings(STATIC_ROOT=static_dir): + call_command('collectstatic', interactive=True, stdout=stdout) output = stdout.getvalue() self.assertNotIn(self.overwrite_warning_msg, output) self.assertNotIn(self.delete_warning_msg, output) @@ -360,24 +359,22 @@ class TestCollectionOverwriteWarning(CollectionTestCase): """ There is a warning when there are duplicate destinations. """ - static_dir = tempfile.mkdtemp() - self.addCleanup(shutil.rmtree, static_dir) + with tempfile.TemporaryDirectory() as static_dir: + duplicate = os.path.join(static_dir, 'test', 'file.txt') + os.mkdir(os.path.dirname(duplicate)) + with open(duplicate, 'w+') as f: + f.write('duplicate of file.txt') - duplicate = os.path.join(static_dir, 'test', 'file.txt') - os.mkdir(os.path.dirname(duplicate)) - with open(duplicate, 'w+') as f: - f.write('duplicate of file.txt') + with self.settings(STATICFILES_DIRS=[static_dir]): + output = self._collectstatic_output(clear=True) + self.assertIn(self.warning_string, output) - with self.settings(STATICFILES_DIRS=[static_dir]): - output = self._collectstatic_output(clear=True) - self.assertIn(self.warning_string, output) + os.remove(duplicate) - os.remove(duplicate) - - # Make sure the warning went away again. - with self.settings(STATICFILES_DIRS=[static_dir]): - output = self._collectstatic_output(clear=True) - self.assertNotIn(self.warning_string, output) + # Make sure the warning went away again. + with self.settings(STATICFILES_DIRS=[static_dir]): + output = self._collectstatic_output(clear=True) + self.assertNotIn(self.warning_string, output) @override_settings(STATICFILES_STORAGE='staticfiles_tests.storage.DummyStorage')