diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-12-31 10:01:31 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-12-31 10:01:31 +0100 |
| commit | d88ec42bd0a37340c8477a6f20bf26e58bd84735 (patch) | |
| tree | abd9422f7fb34a19579a74515ce84d9f472cd226 /tests/file_storage | |
| parent | 81ccf92f154c6d9eac3e30bac0aa67574d0ace15 (diff) | |
Used addCleanup() in tests where appropriate.
Diffstat (limited to 'tests/file_storage')
| -rw-r--r-- | tests/file_storage/tests.py | 46 |
1 files changed, 15 insertions, 31 deletions
diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index 8c47e43742..637de0a3c9 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -71,16 +71,10 @@ class FileStorageTests(SimpleTestCase): def setUp(self): self.temp_dir = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, self.temp_dir) self.storage = self.storage_class( location=self.temp_dir, base_url="/test_media_url/" ) - # Set up a second temporary directory which is ensured to have a mixed - # case name. - self.temp_dir2 = tempfile.mkdtemp(suffix="aBc") - - def tearDown(self): - shutil.rmtree(self.temp_dir) - shutil.rmtree(self.temp_dir2) def test_empty_location(self): """ @@ -414,14 +408,16 @@ class FileStorageTests(SimpleTestCase): """The storage backend should preserve case of filenames.""" # Create a storage backend associated with the mixed case name # directory. - other_temp_storage = self.storage_class(location=self.temp_dir2) + temp_dir2 = tempfile.mkdtemp(suffix="aBc") + self.addCleanup(shutil.rmtree, temp_dir2) + other_temp_storage = self.storage_class(location=temp_dir2) # Ask that storage backend to store a file with a mixed case filename. mixed_case = "CaSe_SeNsItIvE" file = other_temp_storage.open(mixed_case, "w") file.write("storage contents") file.close() self.assertEqual( - os.path.join(self.temp_dir2, mixed_case), + os.path.join(temp_dir2, mixed_case), other_temp_storage.path(mixed_case), ) other_temp_storage.delete(mixed_case) @@ -917,9 +913,7 @@ class FieldCallableFileStorageTests(SimpleTestCase): self.temp_storage_location = tempfile.mkdtemp( suffix="filefield_callable_storage" ) - - def tearDown(self): - shutil.rmtree(self.temp_storage_location) + self.addCleanup(shutil.rmtree, self.temp_storage_location) def test_callable_base_class_error_raises(self): class NotStorage: @@ -993,12 +987,10 @@ class SlowFile(ContentFile): class FileSaveRaceConditionTest(SimpleTestCase): def setUp(self): self.storage_dir = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, self.storage_dir) self.storage = FileSystemStorage(self.storage_dir) self.thread = threading.Thread(target=self.save_file, args=["conflict"]) - def tearDown(self): - shutil.rmtree(self.storage_dir) - def save_file(self, name): name = self.storage.save(name, SlowFile(b"Data")) @@ -1017,12 +1009,10 @@ class FileSaveRaceConditionTest(SimpleTestCase): class FileStoragePermissions(unittest.TestCase): def setUp(self): self.umask = 0o027 - self.old_umask = os.umask(self.umask) + old_umask = os.umask(self.umask) + self.addCleanup(os.umask, old_umask) self.storage_dir = tempfile.mkdtemp() - - def tearDown(self): - shutil.rmtree(self.storage_dir) - os.umask(self.old_umask) + self.addCleanup(shutil.rmtree, self.storage_dir) @override_settings(FILE_UPLOAD_PERMISSIONS=0o654) def test_file_upload_permissions(self): @@ -1059,11 +1049,9 @@ class FileStoragePermissions(unittest.TestCase): class FileStoragePathParsing(SimpleTestCase): def setUp(self): self.storage_dir = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, self.storage_dir) self.storage = FileSystemStorage(self.storage_dir) - def tearDown(self): - shutil.rmtree(self.storage_dir) - def test_directory_with_dot(self): """Regression test for #9610. @@ -1095,11 +1083,9 @@ class FileStoragePathParsing(SimpleTestCase): class ContentFileStorageTestCase(unittest.TestCase): def setUp(self): - self.storage_dir = tempfile.mkdtemp() - self.storage = FileSystemStorage(self.storage_dir) - - def tearDown(self): - shutil.rmtree(self.storage_dir) + storage_dir = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, storage_dir) + self.storage = FileSystemStorage(storage_dir) def test_content_saving(self): """ @@ -1120,11 +1106,9 @@ class FileLikeObjectTestCase(LiveServerTestCase): def setUp(self): self.temp_dir = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, self.temp_dir) self.storage = FileSystemStorage(location=self.temp_dir) - def tearDown(self): - shutil.rmtree(self.temp_dir) - def test_urllib_request_urlopen(self): """ Test the File storage API with a file-like object coming from |
