Refs #23919 -- Replaced tempfile.mkdtemp() with TemporaryDirectory() context manager.
This commit is contained in:
parent
fee42fd99e
commit
6478e07a62
|
@ -56,18 +56,13 @@ def symlinks_supported():
|
||||||
host platform and/or if they are allowed to be created (e.g.
|
host platform and/or if they are allowed to be created (e.g.
|
||||||
on Windows it requires admin permissions).
|
on Windows it requires admin permissions).
|
||||||
"""
|
"""
|
||||||
tmpdir = tempfile.mkdtemp()
|
with tempfile.TemporaryDirectory() as temp_dir:
|
||||||
original_path = os.path.join(tmpdir, 'original')
|
original_path = os.path.join(temp_dir, 'original')
|
||||||
symlink_path = os.path.join(tmpdir, 'symlink')
|
symlink_path = os.path.join(temp_dir, 'symlink')
|
||||||
os.makedirs(original_path)
|
os.makedirs(original_path)
|
||||||
try:
|
try:
|
||||||
os.symlink(original_path, symlink_path)
|
os.symlink(original_path, symlink_path)
|
||||||
supported = True
|
supported = True
|
||||||
except (OSError, NotImplementedError, AttributeError):
|
except (OSError, NotImplementedError):
|
||||||
supported = False
|
supported = False
|
||||||
else:
|
|
||||||
os.remove(symlink_path)
|
|
||||||
finally:
|
|
||||||
os.rmdir(original_path)
|
|
||||||
os.rmdir(tmpdir)
|
|
||||||
return supported
|
return supported
|
||||||
|
|
|
@ -103,21 +103,13 @@ class FileUploadTests(TestCase):
|
||||||
self._test_base64_upload("Big data" * 68000, encode=base64.encodebytes)
|
self._test_base64_upload("Big data" * 68000, encode=base64.encodebytes)
|
||||||
|
|
||||||
def test_unicode_file_name(self):
|
def test_unicode_file_name(self):
|
||||||
tdir = sys_tempfile.mkdtemp()
|
with sys_tempfile.TemporaryDirectory() as temp_dir:
|
||||||
self.addCleanup(shutil.rmtree, tdir, True)
|
# 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:
|
||||||
# This file contains Chinese symbols and an accented char in the name.
|
file1.write(b'b' * (2 ** 10))
|
||||||
with open(os.path.join(tdir, UNICODE_FILENAME), 'w+b') as file1:
|
file1.seek(0)
|
||||||
file1.write(b'b' * (2 ** 10))
|
response = self.client.post('/unicode_name/', {'file_unicode': file1})
|
||||||
file1.seek(0)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
post_data = {
|
|
||||||
'file_unicode': file1,
|
|
||||||
}
|
|
||||||
|
|
||||||
response = self.client.post('/unicode_name/', post_data)
|
|
||||||
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
|
|
||||||
def test_unicode_file_name_rfc2231(self):
|
def test_unicode_file_name_rfc2231(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -493,14 +493,11 @@ class MailTests(HeadersCheckMixin, SimpleTestCase):
|
||||||
mail.get_connection('django.core.mail.backends.console.EmailBackend'),
|
mail.get_connection('django.core.mail.backends.console.EmailBackend'),
|
||||||
console.EmailBackend
|
console.EmailBackend
|
||||||
)
|
)
|
||||||
tmp_dir = tempfile.mkdtemp()
|
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||||
try:
|
|
||||||
self.assertIsInstance(
|
self.assertIsInstance(
|
||||||
mail.get_connection('django.core.mail.backends.filebased.EmailBackend', file_path=tmp_dir),
|
mail.get_connection('django.core.mail.backends.filebased.EmailBackend', file_path=tmp_dir),
|
||||||
filebased.EmailBackend
|
filebased.EmailBackend
|
||||||
)
|
)
|
||||||
finally:
|
|
||||||
shutil.rmtree(tmp_dir)
|
|
||||||
self.assertIsInstance(mail.get_connection(), locmem.EmailBackend)
|
self.assertIsInstance(mail.get_connection(), locmem.EmailBackend)
|
||||||
|
|
||||||
@override_settings(
|
@override_settings(
|
||||||
|
|
|
@ -98,8 +98,7 @@ class MigrationTestBase(TransactionTestCase):
|
||||||
|
|
||||||
Returns the filesystem path to the temporary migrations module.
|
Returns the filesystem path to the temporary migrations module.
|
||||||
"""
|
"""
|
||||||
temp_dir = tempfile.mkdtemp()
|
with tempfile.TemporaryDirectory() as temp_dir:
|
||||||
try:
|
|
||||||
target_dir = tempfile.mkdtemp(dir=temp_dir)
|
target_dir = tempfile.mkdtemp(dir=temp_dir)
|
||||||
with open(os.path.join(target_dir, '__init__.py'), 'w'):
|
with open(os.path.join(target_dir, '__init__.py'), 'w'):
|
||||||
pass
|
pass
|
||||||
|
@ -119,6 +118,3 @@ class MigrationTestBase(TransactionTestCase):
|
||||||
new_module = os.path.basename(target_dir) + '.migrations'
|
new_module = os.path.basename(target_dir) + '.migrations'
|
||||||
with self.settings(MIGRATION_MODULES={app_label: new_module}):
|
with self.settings(MIGRATION_MODULES={app_label: new_module}):
|
||||||
yield target_migrations_dir
|
yield target_migrations_dir
|
||||||
|
|
||||||
finally:
|
|
||||||
shutil.rmtree(temp_dir)
|
|
||||||
|
|
|
@ -218,10 +218,9 @@ class TestInteractiveMessages(CollectionTestCase):
|
||||||
|
|
||||||
def test_no_warning_for_empty_staticdir(self):
|
def test_no_warning_for_empty_staticdir(self):
|
||||||
stdout = StringIO()
|
stdout = StringIO()
|
||||||
static_dir = tempfile.mkdtemp(prefix='collectstatic_empty_staticdir_test')
|
with tempfile.TemporaryDirectory(prefix='collectstatic_empty_staticdir_test') as static_dir:
|
||||||
with override_settings(STATIC_ROOT=static_dir):
|
with override_settings(STATIC_ROOT=static_dir):
|
||||||
call_command('collectstatic', interactive=True, stdout=stdout)
|
call_command('collectstatic', interactive=True, stdout=stdout)
|
||||||
shutil.rmtree(static_dir)
|
|
||||||
output = stdout.getvalue()
|
output = stdout.getvalue()
|
||||||
self.assertNotIn(self.overwrite_warning_msg, output)
|
self.assertNotIn(self.overwrite_warning_msg, output)
|
||||||
self.assertNotIn(self.delete_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.
|
There is a warning when there are duplicate destinations.
|
||||||
"""
|
"""
|
||||||
static_dir = tempfile.mkdtemp()
|
with tempfile.TemporaryDirectory() as static_dir:
|
||||||
self.addCleanup(shutil.rmtree, 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')
|
with self.settings(STATICFILES_DIRS=[static_dir]):
|
||||||
os.mkdir(os.path.dirname(duplicate))
|
output = self._collectstatic_output(clear=True)
|
||||||
with open(duplicate, 'w+') as f:
|
self.assertIn(self.warning_string, output)
|
||||||
f.write('duplicate of file.txt')
|
|
||||||
|
|
||||||
with self.settings(STATICFILES_DIRS=[static_dir]):
|
os.remove(duplicate)
|
||||||
output = self._collectstatic_output(clear=True)
|
|
||||||
self.assertIn(self.warning_string, output)
|
|
||||||
|
|
||||||
os.remove(duplicate)
|
# Make sure the warning went away again.
|
||||||
|
with self.settings(STATICFILES_DIRS=[static_dir]):
|
||||||
# Make sure the warning went away again.
|
output = self._collectstatic_output(clear=True)
|
||||||
with self.settings(STATICFILES_DIRS=[static_dir]):
|
self.assertNotIn(self.warning_string, output)
|
||||||
output = self._collectstatic_output(clear=True)
|
|
||||||
self.assertNotIn(self.warning_string, output)
|
|
||||||
|
|
||||||
|
|
||||||
@override_settings(STATICFILES_STORAGE='staticfiles_tests.storage.DummyStorage')
|
@override_settings(STATICFILES_STORAGE='staticfiles_tests.storage.DummyStorage')
|
||||||
|
|
Loading…
Reference in New Issue