mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #36298 -- Truncated the overwritten file content in file_move_safe().
Regression in 58cd4902a7
.
Thanks Baptiste Mispelon for the report.
This commit is contained in:
parent
d4a2809c2b
commit
8ad3e80e88
6 changed files with 59 additions and 0 deletions
|
@ -496,6 +496,27 @@ class FileMoveSafeTests(unittest.TestCase):
|
|||
os.close(handle_b)
|
||||
os.close(handle_c)
|
||||
|
||||
def test_file_move_ensure_truncation(self):
|
||||
with tempfile.NamedTemporaryFile(delete=False) as src:
|
||||
src.write(b"content")
|
||||
src_name = src.name
|
||||
self.addCleanup(
|
||||
lambda: os.remove(src_name) if os.path.exists(src_name) else None
|
||||
)
|
||||
|
||||
with tempfile.NamedTemporaryFile(delete=False) as dest:
|
||||
dest.write(b"This is a longer content.")
|
||||
dest_name = dest.name
|
||||
self.addCleanup(os.remove, dest_name)
|
||||
|
||||
with mock.patch("django.core.files.move.os.rename", side_effect=OSError()):
|
||||
file_move_safe(src_name, dest_name, allow_overwrite=True)
|
||||
|
||||
with open(dest_name, "rb") as f:
|
||||
content = f.read()
|
||||
|
||||
self.assertEqual(content, b"content")
|
||||
|
||||
|
||||
class SpooledTempTests(unittest.TestCase):
|
||||
def test_in_memory_spooled_temp(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue