Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Made a set of small test changes to avoid leaving temp files …

…hanging around after running the test suite. First, fixed a couple of places where temp dirs were (or could be) created without later being deleted. Second, added a missing close() before unlink() since Windows raises an error on an attempt to remove an open file. Finally, in the file_uploads tests, avoided opening-by-name temporary files that we already have a descriptor for. Doing additional opens seems to run afoul of the Windows issue with deleting open files, so it generally works better to just seek back to 0 instead of calling open multiple times.

Backport/merge of r10406.  Also updated svnmerge metadata.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10407 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit bb558539ad7e3b439997d53b8521e50f7e4624e3 1 parent 520c670
Karen Tracey authored April 05, 2009
3  tests/regressiontests/bug639/models.py
@@ -4,7 +4,8 @@
4 4
 from django.core.files.storage import FileSystemStorage
5 5
 from django.forms import ModelForm
6 6
 
7  
-temp_storage = FileSystemStorage(tempfile.gettempdir())
  7
+temp_storage_dir = tempfile.mkdtemp()
  8
+temp_storage = FileSystemStorage(temp_storage_dir)
8 9
 
9 10
 class Photo(models.Model):
10 11
     title = models.CharField(max_length=30)
4  tests/regressiontests/bug639/tests.py
@@ -5,10 +5,11 @@
5 5
 """
6 6
 
7 7
 import os
  8
+import shutil
8 9
 import unittest
9 10
 
10 11
 from django.core.files.uploadedfile import SimpleUploadedFile
11  
-from regressiontests.bug639.models import Photo, PhotoForm
  12
+from regressiontests.bug639.models import Photo, PhotoForm, temp_storage_dir
12 13
 
13 14
 class Bug639Test(unittest.TestCase):
14 15
 
@@ -37,3 +38,4 @@ def tearDown(self):
37 38
         """
38 39
         p = Photo.objects.get()
39 40
         p.image.delete(save=False)
  41
+        shutil.rmtree(temp_storage_dir)
6  tests/regressiontests/file_storage/models.py
@@ -5,9 +5,6 @@
5 5
 from django.core.files.storage import FileSystemStorage
6 6
 from django.core.files.base import ContentFile
7 7
 
8  
-temp_storage_dir = tempfile.mkdtemp()
9  
-temp_storage = FileSystemStorage(temp_storage_dir)
10  
-
11 8
 # Test for correct behavior of width_field/height_field.
12 9
 # Of course, we can't run this without PIL.
13 10
 
@@ -20,6 +17,9 @@
20 17
 
21 18
 # If we have PIL, do these tests
22 19
 if Image:
  20
+    temp_storage_dir = tempfile.mkdtemp()
  21
+    temp_storage = FileSystemStorage(temp_storage_dir)
  22
+
23 23
     class Person(models.Model):
24 24
         name = models.CharField(max_length=50)
25 25
         mugshot = models.ImageField(storage=temp_storage, upload_to='tests',
28  tests/regressiontests/file_uploads/tests.py
@@ -37,8 +37,8 @@ def test_large_upload(self):
37 37
 
38 38
         post_data = {
39 39
             'name': 'Ringo',
40  
-            'file_field1': open(file1.name),
41  
-            'file_field2': open(file2.name),
  40
+            'file_field1': file1,
  41
+            'file_field2': file2,
42 42
             }
43 43
 
44 44
         for key in post_data.keys():
@@ -66,6 +66,7 @@ def test_unicode_file_name(self):
66 66
 
67 67
         response = self.client.post('/file_uploads/unicode_name/', post_data)
68 68
 
  69
+        file1.close()
69 70
         try:
70 71
             os.unlink(file1.name)
71 72
         except:
@@ -150,51 +151,57 @@ def test_custom_upload_handler(self):
150 151
         # A small file (under the 5M quota)
151 152
         smallfile = tempfile.NamedTemporaryFile()
152 153
         smallfile.write('a' * (2 ** 21))
  154
+        smallfile.seek(0)
153 155
 
154 156
         # A big file (over the quota)
155 157
         bigfile = tempfile.NamedTemporaryFile()
156 158
         bigfile.write('a' * (10 * 2 ** 20))
  159
+        bigfile.seek(0)
157 160
 
158 161
         # Small file posting should work.
159  
-        response = self.client.post('/file_uploads/quota/', {'f': open(smallfile.name)})
  162
+        response = self.client.post('/file_uploads/quota/', {'f': smallfile})
160 163
         got = simplejson.loads(response.content)
161 164
         self.assert_('f' in got)
162 165
 
163 166
         # Large files don't go through.
164  
-        response = self.client.post("/file_uploads/quota/", {'f': open(bigfile.name)})
  167
+        response = self.client.post("/file_uploads/quota/", {'f': bigfile})
165 168
         got = simplejson.loads(response.content)
166 169
         self.assert_('f' not in got)
167 170
 
168 171
     def test_broken_custom_upload_handler(self):
169 172
         f = tempfile.NamedTemporaryFile()
170 173
         f.write('a' * (2 ** 21))
  174
+        f.seek(0)
171 175
 
172 176
         # AttributeError: You cannot alter upload handlers after the upload has been processed.
173 177
         self.assertRaises(
174 178
             AttributeError,
175 179
             self.client.post,
176 180
             '/file_uploads/quota/broken/',
177  
-            {'f': open(f.name)}
  181
+            {'f': f}
178 182
         )
179 183
 
180 184
     def test_fileupload_getlist(self):
181 185
         file1 = tempfile.NamedTemporaryFile()
182 186
         file1.write('a' * (2 ** 23))
  187
+        file1.seek(0)
183 188
 
184 189
         file2 = tempfile.NamedTemporaryFile()
185 190
         file2.write('a' * (2 * 2 ** 18))
  191
+        file2.seek(0)
186 192
 
187 193
         file2a = tempfile.NamedTemporaryFile()
188 194
         file2a.write('a' * (5 * 2 ** 20))
  195
+        file2a.seek(0)
189 196
 
190 197
         response = self.client.post('/file_uploads/getlist_count/', {
191  
-            'file1': open(file1.name),
  198
+            'file1': file1,
192 199
             'field1': u'test',
193 200
             'field2': u'test3',
194 201
             'field3': u'test5',
195 202
             'field4': u'test6',
196 203
             'field5': u'test7',
197  
-            'file2': (open(file2.name), open(file2a.name))
  204
+            'file2': (file2, file2a)
198 205
         })
199 206
         got = simplejson.loads(response.content)
200 207
 
@@ -242,6 +249,13 @@ def handle_uncaught_exception(self, request, resolver, exc_info):
242 249
             # CustomUploadError is the error that should have been raised
243 250
             self.assertEqual(err.__class__, uploadhandler.CustomUploadError)
244 251
 
  252
+    def setUp(self):
  253
+        if not os.path.isdir(temp_storage.location):
  254
+            os.makedirs(temp_storage.location)
  255
+
  256
+    def tearDown(self):
  257
+        shutil.rmtree(temp_storage.location)
  258
+
245 259
 class DirectoryCreationTests(unittest.TestCase):
246 260
     """
247 261
     Tests for error handling during directory creation

0 notes on commit bb55853

Please sign in to comment.
Something went wrong with that request. Please try again.