| 190 | def test_file_save_with_path(self): |
| 191 | """ |
| 192 | Saving a pathname should create intermediate directories as necessary. |
| 193 | """ |
| 194 | self.assertFalse(self.storage.exists('path/to')) |
| 195 | self.storage.save('path/to/test.file', ContentFile('file saved with path')) |
| 196 | |
| 197 | self.assertTrue(self.storage.exists('path/to')) |
| 198 | self.assertEqual(self.storage.open('path/to/test.file').read(), 'file saved with path') |
| 199 | |
| 200 | self.assertTrue(os.path.exists(os.path.join(self.temp_dir, 'path', 'to', 'test.file'))) |
| 201 | |
| 202 | self.storage.delete('path/to/test.file') |
| 203 | |
| 300 | def test_makedirs_race_handling(self): |
| 301 | """ |
| 302 | File storage should be robust against directory creation race conditions. |
| 303 | """ |
| 304 | # Monkey-patch os.makedirs, to simulate a normal call, a raced call, |
| 305 | # and an error. |
| 306 | def fake_makedirs(path): |
| 307 | if path == os.path.join(self.temp_dir, 'normal'): |
| 308 | os.mkdir(path) |
| 309 | elif path == os.path.join(self.temp_dir, 'raced'): |
| 310 | os.mkdir(path) |
| 311 | raise OSError(errno.EEXIST, 'simulated EEXIST') |
| 312 | elif path == os.path.join(self.temp_dir, 'error'): |
| 313 | raise OSError(errno.EACCES, 'simulated EACCES') |
| 314 | else: |
| 315 | self.fail('unexpected argument %r' % (path,)) |
| 316 | |
| 317 | real_makedirs = os.makedirs |
| 318 | try: |
| 319 | os.makedirs = fake_makedirs |
| 320 | |
| 321 | self.storage.save('normal/test.file', ContentFile('saved normally')) |
| 322 | self.assertEqual(self.storage.open('normal/test.file').read(), 'saved normally') |
| 323 | |
| 324 | self.storage.save('raced/test.file', ContentFile('saved with race')) |
| 325 | self.assertEqual(self.storage.open('raced/test.file').read(), 'saved with race') |
| 326 | |
| 327 | # Check that OSErrors aside from EEXIST are still raised. |
| 328 | self.assertRaises(OSError, |
| 329 | lambda: self.storage.save('error/test.file', ContentFile('not saved'))) |
| 330 | finally: |
| 331 | os.makedirs = real_makedirs |
| 332 | |