2323from django .core .files .storage import FileSystemStorage , get_storage_class
2424from django .core .files .uploadedfile import UploadedFile
2525from django .test import SimpleTestCase
26- from django .utils import unittest
26+ from django .utils import six , unittest
2727
2828# Try to import PIL in either of the two ways it can end up installed.
2929# Checking for the existence of Image is enough for CPython, but
3737 Image = None
3838
3939
40+ FILE_SUFFIX_REGEX = '[A-Za-z0-9]{7}'
41+
42+
4043class GetStorageClassTests (SimpleTestCase ):
4144
4245 def test_get_filesystem_storage (self ):
@@ -417,10 +420,9 @@ def test_race_condition(self):
417420 self .thread .start ()
418421 name = self .save_file ('conflict' )
419422 self .thread .join ()
420- self .assertTrue (self .storage .exists ('conflict' ))
421- self .assertTrue (self .storage .exists ('conflict_1' ))
422- self .storage .delete ('conflict' )
423- self .storage .delete ('conflict_1' )
423+ files = sorted (os .listdir (self .storage_dir ))
424+ self .assertEqual (files [0 ], 'conflict' )
425+ six .assertRegex (self , files [1 ], 'conflict_%s' % FILE_SUFFIX_REGEX )
424426
425427class FileStoragePermissions (unittest .TestCase ):
426428 def setUp (self ):
@@ -457,9 +459,10 @@ def test_directory_with_dot(self):
457459 self .storage .save ('dotted.path/test' , ContentFile ("1" ))
458460 self .storage .save ('dotted.path/test' , ContentFile ("2" ))
459461
462+ files = sorted (os .listdir (os .path .join (self .storage_dir , 'dotted.path' )))
460463 self .assertFalse (os .path .exists (os .path .join (self .storage_dir , 'dotted_.path' )))
461- self .assertTrue ( os . path . exists ( os . path . join ( self . storage_dir , 'dotted.path/ test' )) )
462- self . assertTrue ( os . path . exists ( os . path . join ( self . storage_dir , 'dotted.path/test_1' )) )
464+ self .assertEqual ( files [ 0 ] , 'test' )
465+ six . assertRegex ( self , files [ 1 ], 'test_%s' % FILE_SUFFIX_REGEX )
463466
464467 def test_first_character_dot (self ):
465468 """
@@ -472,10 +475,12 @@ def test_first_character_dot(self):
472475 self .assertTrue (os .path .exists (os .path .join (self .storage_dir , 'dotted.path/.test' )))
473476 # Before 2.6, a leading dot was treated as an extension, and so
474477 # underscore gets added to beginning instead of end.
478+ files = sorted (os .listdir (os .path .join (self .storage_dir , 'dotted.path' )))
479+ self .assertEqual (files [0 ], '.test' )
475480 if sys .version_info < (2 , 6 ):
476- self . assertTrue ( os . path . exists ( os . path . join ( self . storage_dir , 'dotted.path/_1. test')) )
481+ six . assertRegex ( self , files [ 1 ], '_%s. test' % FILE_SUFFIX_REGEX )
477482 else :
478- self . assertTrue ( os . path . exists ( os . path . join ( self . storage_dir , 'dotted.path/.test_1' )) )
483+ six . assertRegex ( self , files [ 1 ], '.test_%s' % FILE_SUFFIX_REGEX )
479484
480485class DimensionClosingBug (unittest .TestCase ):
481486 """
0 commit comments