utils: write atomic temporary file on default storage location (#32543)
This commit is contained in:
parent
beb4f0e2e3
commit
765166fa6d
|
@ -18,7 +18,7 @@ import contextlib
|
|||
import tempfile
|
||||
import errno
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.files.storage import default_storage
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
|
@ -34,7 +34,7 @@ def atomic_write(filepath, **kwargs):
|
|||
|
||||
tmp_dir = kwargs.pop('dir', None)
|
||||
if not tmp_dir:
|
||||
tmp_dir = os.path.join(settings.MEDIA_ROOT, 'tmp')
|
||||
tmp_dir = default_storage.path('tmp')
|
||||
if not os.path.exists(tmp_dir):
|
||||
try:
|
||||
os.makedirs(tmp_dir)
|
||||
|
|
|
@ -18,21 +18,26 @@ import os
|
|||
import pytest
|
||||
|
||||
from passerelle.utils.files import atomic_write
|
||||
from django.core.files.storage import default_storage
|
||||
|
||||
|
||||
def test_atomic_write(settings, tmpdir):
|
||||
def test_atomic_write(tmpdir):
|
||||
target_dir = tmpdir.mkdir('target')
|
||||
filepath = str(target_dir.join('test'))
|
||||
|
||||
assert not os.path.exists(os.path.join(settings.MEDIA_ROOT, 'tmp'))
|
||||
with pytest.raises(Exception):
|
||||
with atomic_write(filepath) as fd:
|
||||
fd.write('coucou')
|
||||
raise Exception()
|
||||
assert os.path.exists(os.path.join(settings.MEDIA_ROOT, 'tmp'))
|
||||
|
||||
assert not os.path.exists(filepath)
|
||||
assert os.listdir(str(target_dir)) == []
|
||||
|
||||
target_dir = default_storage.path('target')
|
||||
if not os.path.exists(target_dir):
|
||||
os.makedirs(target_dir)
|
||||
filepath = os.path.join(target_dir, 'test')
|
||||
|
||||
with atomic_write(filepath) as fd:
|
||||
fd.write('coucou')
|
||||
|
||||
|
|
Loading…
Reference in New Issue