@@ -3522,6 +3522,31 @@ def test_compat_makedirs(self):
35223522 py2vs3 .makedirs (name , exist_ok = True ) # No error
35233523 self .assertExists (name )
35243524
3525+ def test_get_first_nonexisting_parent (self ):
3526+ """Test get_first_nonexisting_parent function."""
3527+ test_root = os .path .join (self .test_prefix , 'a' )
3528+
3529+ base_path = os .path .join (self .test_prefix , 'a' )
3530+ target_path = os .path .join (self .test_prefix , 'a' , 'b' , 'c' )
3531+ os .makedirs (base_path )
3532+ first_nonexisting_parent = ft .get_first_nonexisting_parent (target_path )
3533+ self .assertEqual (first_nonexisting_parent , os .path .join (self .test_prefix , 'a' , 'b' ))
3534+ shutil .rmtree (test_root )
3535+
3536+ base_path = os .path .join (self .test_prefix , 'a' , 'b' )
3537+ target_path = os .path .join (self .test_prefix , 'a' , 'b' , 'c' )
3538+ os .makedirs (base_path )
3539+ first_nonexisting_parent = ft .get_first_nonexisting_parent (target_path )
3540+ self .assertEqual (first_nonexisting_parent , os .path .join (self .test_prefix , 'a' , 'b' , 'c' ))
3541+ shutil .rmtree (test_root )
3542+
3543+ base_path = os .path .join (self .test_prefix , 'a' , 'b' , 'c' )
3544+ target_path = os .path .join (self .test_prefix , 'a' , 'b' , 'c' )
3545+ os .makedirs (base_path )
3546+ first_nonexisting_parent = ft .get_first_nonexisting_parent (target_path )
3547+ self .assertEqual (first_nonexisting_parent , None )
3548+ shutil .rmtree (test_root )
3549+
35253550 def test_create_unused_dir (self ):
35263551 """Test create_unused_dir function."""
35273552 path = ft .create_unused_dir (self .test_prefix , 'folder' )
@@ -3560,6 +3585,55 @@ def test_create_unused_dir(self):
35603585 self .assertEqual (path , os .path .join (self .test_prefix , 'file_0' ))
35613586 self .assertExists (path )
35623587
3588+ def test_create_unused_path (self ):
3589+ """Test create_unused_path function."""
3590+ requested_path = os .path .join (self .test_prefix , 'folder' )
3591+ path = ft .create_unused_dir (requested_path )
3592+ self .assertEqual (path , requested_path )
3593+ self .assertExists (path )
3594+
3595+ # Repeat with existing folder(s) should create new ones
3596+ for i in range (10 ):
3597+ requested_path = os .path .join (self .test_prefix , 'folder' )
3598+ path = ft .create_unused_dir (requested_path )
3599+ self .assertEqual (path , requested_path + '_%s' % i )
3600+ self .assertExists (path )
3601+
3602+ # Support creation of parent directories
3603+ requested_path = os .path .join (self .test_prefix , 'parent_folder' , 'folder' )
3604+ path = ft .create_unused_dir (requested_path )
3605+ self .assertEqual (path , requested_path )
3606+ self .assertExists (path )
3607+
3608+ # Not influenced by similar folder
3609+ requested_path = os .path .join (self .test_prefix , 'folder2' )
3610+ path = ft .create_unused_dir (requested_path )
3611+ self .assertEqual (path , requested_path )
3612+ self .assertExists (path )
3613+ for i in range (10 ):
3614+ path = ft .create_unused_dir (requested_path )
3615+ self .assertEqual (path , requested_path + '_%s' % i )
3616+ self .assertExists (path )
3617+
3618+ # Fail cleanly if passed a readonly folder
3619+ readonly_dir = os .path .join (self .test_prefix , 'ro_folder' )
3620+ ft .mkdir (readonly_dir )
3621+ old_perms = os .lstat (readonly_dir )[stat .ST_MODE ]
3622+ ft .adjust_permissions (readonly_dir , stat .S_IREAD | stat .S_IEXEC , relative = False )
3623+ requested_path = os .path .join (readonly_dir , 'new_folder' )
3624+ try :
3625+ self .assertErrorRegex (EasyBuildError , 'Failed to create directory' ,
3626+ ft .create_unused_dir , requested_path )
3627+ finally :
3628+ ft .adjust_permissions (readonly_dir , old_perms , relative = False )
3629+
3630+ # Ignore files same as folders. So first just create a file with no contents
3631+ requested_path = os .path .join (self .test_prefix , 'file' )
3632+ ft .write_file (requested_path , '' )
3633+ path = ft .create_unused_dir (requested_path )
3634+ self .assertEqual (path , requested_path + '_0' )
3635+ self .assertExists (path )
3636+
35633637
35643638def suite ():
35653639 """ returns all the testcases in this module """
0 commit comments