3030from .models import Storage , temp_storage , temp_storage_location
3131
3232
33+ FILE_SUFFIX_REGEX = '[A-Za-z0-9]{7}'
34+
35+
3336class GetStorageClassTests (SimpleTestCase ):
3437
3538 def test_get_filesystem_storage (self ):
@@ -457,14 +460,16 @@ def test_files(self):
457460 # Save another file with the same name.
458461 obj2 = Storage ()
459462 obj2 .normal .save ("django_test.txt" , ContentFile ("more content" ))
460- self .assertEqual (obj2 .normal .name , "tests/django_test_1.txt" )
463+ obj2_name = obj2 .normal .name
464+ six .assertRegex (self , obj2_name , "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX )
461465 self .assertEqual (obj2 .normal .size , 12 )
462466 obj2 .normal .close ()
463467
464468 # Deleting an object does not delete the file it uses.
465469 obj2 .delete ()
466470 obj2 .normal .save ("django_test.txt" , ContentFile ("more content" ))
467- self .assertEqual (obj2 .normal .name , "tests/django_test_2.txt" )
471+ self .assertNotEqual (obj2_name , obj2 .normal .name )
472+ six .assertRegex (self , obj2 .normal .name , "tests/django_test_%s.txt" % FILE_SUFFIX_REGEX )
468473 obj2 .normal .close ()
469474
470475 def test_filefield_read (self ):
@@ -477,17 +482,18 @@ def test_filefield_read(self):
477482 self .assertEqual (list (obj .normal .chunks (chunk_size = 2 )), [b"co" , b"nt" , b"en" , b"t" ])
478483 obj .normal .close ()
479484
480- def test_file_numbering (self ):
481- # Multiple files with the same name get _N appended to them.
482- objs = [Storage () for i in range (3 )]
485+ def test_duplicate_filename (self ):
486+ # Multiple files with the same name get _(7 random chars) appended to them.
487+ objs = [Storage () for i in range (2 )]
483488 for o in objs :
484489 o .normal .save ("multiple_files.txt" , ContentFile ("Same Content" ))
485- self .assertEqual (
486- [o .normal .name for o in objs ],
487- ["tests/multiple_files.txt" , "tests/multiple_files_1.txt" , "tests/multiple_files_2.txt" ]
488- )
489- for o in objs :
490- o .delete ()
490+ try :
491+ names = [o .normal .name for o in objs ]
492+ self .assertEqual (names [0 ], "tests/multiple_files.txt" )
493+ six .assertRegex (self , names [1 ], "tests/multiple_files_%s.txt" % FILE_SUFFIX_REGEX )
494+ finally :
495+ for o in objs :
496+ o .delete ()
491497
492498 def test_filefield_default (self ):
493499 # Default values allow an object to access a single file.
@@ -579,10 +585,9 @@ def test_race_condition(self):
579585 self .thread .start ()
580586 self .save_file ('conflict' )
581587 self .thread .join ()
582- self .assertTrue (self .storage .exists ('conflict' ))
583- self .assertTrue (self .storage .exists ('conflict_1' ))
584- self .storage .delete ('conflict' )
585- self .storage .delete ('conflict_1' )
588+ files = sorted (os .listdir (self .storage_dir ))
589+ self .assertEqual (files [0 ], 'conflict' )
590+ six .assertRegex (self , files [1 ], 'conflict_%s' % FILE_SUFFIX_REGEX )
586591
587592
588593@unittest .skipIf (sys .platform .startswith ('win' ), "Windows only partially supports umasks and chmod." )
@@ -643,9 +648,10 @@ def test_directory_with_dot(self):
643648 self .storage .save ('dotted.path/test' , ContentFile ("1" ))
644649 self .storage .save ('dotted.path/test' , ContentFile ("2" ))
645650
651+ files = sorted (os .listdir (os .path .join (self .storage_dir , 'dotted.path' )))
646652 self .assertFalse (os .path .exists (os .path .join (self .storage_dir , 'dotted_.path' )))
647- self .assertTrue ( os . path . exists ( os . path . join ( self . storage_dir , 'dotted.path/ test' )) )
648- self . assertTrue ( os . path . exists ( os . path . join ( self . storage_dir , 'dotted.path/test_1' )) )
653+ self .assertEqual ( files [ 0 ] , 'test' )
654+ six . assertRegex ( self , files [ 1 ], 'test_%s' % FILE_SUFFIX_REGEX )
649655
650656 def test_first_character_dot (self ):
651657 """
@@ -655,8 +661,10 @@ def test_first_character_dot(self):
655661 self .storage .save ('dotted.path/.test' , ContentFile ("1" ))
656662 self .storage .save ('dotted.path/.test' , ContentFile ("2" ))
657663
658- self .assertTrue (os .path .exists (os .path .join (self .storage_dir , 'dotted.path/.test' )))
659- self .assertTrue (os .path .exists (os .path .join (self .storage_dir , 'dotted.path/.test_1' )))
664+ files = sorted (os .listdir (os .path .join (self .storage_dir , 'dotted.path' )))
665+ self .assertFalse (os .path .exists (os .path .join (self .storage_dir , 'dotted_.path' )))
666+ self .assertEqual (files [0 ], '.test' )
667+ six .assertRegex (self , files [1 ], '.test_%s' % FILE_SUFFIX_REGEX )
660668
661669
662670class ContentFileStorageTestCase (unittest .TestCase ):
0 commit comments