Skip to content

Commit 73245b3

Browse files
committed
Prevented file_upload tests to leave files behind
Refs django#19206.
1 parent 9a02851 commit 73245b3

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
import tempfile
2-
import os
3-
4-
from django.core.files.storage import FileSystemStorage
51
from django.db import models
62

73

8-
temp_storage = FileSystemStorage(tempfile.mkdtemp())
9-
UPLOAD_TO = os.path.join(temp_storage.location, 'test_upload')
10-
114
class FileModel(models.Model):
12-
testfile = models.FileField(storage=temp_storage, upload_to='test_upload')
5+
testfile = models.FileField(upload_to='test_upload')

tests/regressiontests/file_uploads/tests.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,36 @@
77
import json
88
import os
99
import shutil
10+
import tempfile as sys_tempfile
1011

1112
from django.core.files import temp as tempfile
1213
from django.core.files.uploadedfile import SimpleUploadedFile
1314
from django.http.multipartparser import MultiPartParser
1415
from django.test import TestCase, client
16+
from django.test.utils import override_settings
1517
from django.utils.encoding import force_bytes
1618
from django.utils.six import StringIO
1719
from django.utils import unittest
1820

1921
from . import uploadhandler
20-
from .models import FileModel, temp_storage, UPLOAD_TO
22+
from .models import FileModel
2123

2224

2325
UNICODE_FILENAME = 'test-0123456789_中文_Orléans.jpg'
26+
MEDIA_ROOT = sys_tempfile.mkdtemp()
27+
UPLOAD_TO = os.path.join(MEDIA_ROOT, 'test_upload')
2428

29+
@override_settings(MEDIA_ROOT=MEDIA_ROOT)
2530
class FileUploadTests(TestCase):
31+
@classmethod
32+
def setUpClass(cls):
33+
if not os.path.isdir(MEDIA_ROOT):
34+
os.makedirs(MEDIA_ROOT)
35+
36+
@classmethod
37+
def tearDownClass(cls):
38+
shutil.rmtree(MEDIA_ROOT)
39+
2640
def test_simple_upload(self):
2741
with open(__file__, 'rb') as fp:
2842
post_data = {
@@ -83,7 +97,8 @@ def test_base64_upload(self):
8397
self.assertEqual(received['file'], test_string)
8498

8599
def test_unicode_file_name(self):
86-
tdir = tempfile.gettempdir()
100+
tdir = sys_tempfile.mkdtemp()
101+
self.addCleanup(shutil.rmtree, tdir, True)
87102

88103
# This file contains chinese symbols and an accented char in the name.
89104
with open(os.path.join(tdir, UNICODE_FILENAME), 'w+b') as file1:
@@ -96,11 +111,6 @@ def test_unicode_file_name(self):
96111

97112
response = self.client.post('/file_uploads/unicode_name/', post_data)
98113

99-
try:
100-
os.unlink(file1.name)
101-
except OSError:
102-
pass
103-
104114
self.assertEqual(response.status_code, 200)
105115

106116
def test_dangerous_file_names(self):
@@ -347,26 +357,28 @@ def test_filename_case_preservation(self):
347357
# shouldn't differ.
348358
self.assertEqual(os.path.basename(obj.testfile.path), 'MiXeD_cAsE.txt')
349359

350-
class DirectoryCreationTests(unittest.TestCase):
360+
@override_settings(MEDIA_ROOT=MEDIA_ROOT)
361+
class DirectoryCreationTests(TestCase):
351362
"""
352363
Tests for error handling during directory creation
353364
via _save_FIELD_file (ticket #6450)
354365
"""
366+
@classmethod
367+
def setUpClass(cls):
368+
if not os.path.isdir(MEDIA_ROOT):
369+
os.makedirs(MEDIA_ROOT)
370+
371+
@classmethod
372+
def tearDownClass(cls):
373+
shutil.rmtree(MEDIA_ROOT)
374+
355375
def setUp(self):
356376
self.obj = FileModel()
357-
if not os.path.isdir(temp_storage.location):
358-
os.makedirs(temp_storage.location)
359-
if os.path.isdir(UPLOAD_TO):
360-
os.chmod(UPLOAD_TO, 0o700)
361-
shutil.rmtree(UPLOAD_TO)
362-
363-
def tearDown(self):
364-
os.chmod(temp_storage.location, 0o700)
365-
shutil.rmtree(temp_storage.location)
366377

367378
def test_readonly_root(self):
368379
"""Permission errors are not swallowed"""
369-
os.chmod(temp_storage.location, 0o500)
380+
os.chmod(MEDIA_ROOT, 0o500)
381+
self.addCleanup(os.chmod, MEDIA_ROOT, 0o700)
370382
try:
371383
self.obj.testfile.save('foo.txt', SimpleUploadedFile('foo.txt', b'x'))
372384
except OSError as err:
@@ -378,6 +390,7 @@ def test_not_a_directory(self):
378390
"""The correct IOError is raised when the upload directory name exists but isn't a directory"""
379391
# Create a file with the upload directory name
380392
open(UPLOAD_TO, 'wb').close()
393+
self.addCleanup(os.remove, UPLOAD_TO)
381394
with self.assertRaises(IOError) as exc_info:
382395
self.obj.testfile.save('foo.txt', SimpleUploadedFile('foo.txt', b'x'))
383396
# The test needs to be done on a specific string as IOError

tests/regressiontests/file_uploads/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
from django.utils import six
1010
from django.utils.encoding import force_bytes
1111

12-
from .models import FileModel, UPLOAD_TO
13-
from .tests import UNICODE_FILENAME
12+
from .models import FileModel
13+
from .tests import UNICODE_FILENAME, UPLOAD_TO
1414
from .uploadhandler import QuotaUploadHandler, ErroringUploadHandler
1515

1616

0 commit comments

Comments
 (0)