Skip to content

Commit 2a15533

Browse files
committed
Limit amount of attempts and test error conditions
1 parent dc9d1d7 commit 2a15533

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

easybuild/tools/filetools.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,14 +2485,11 @@ def create_unused_dir(parent_folder, name):
24852485
parent_folder = os.path.abspath(parent_folder)
24862486

24872487
start_path = os.path.join(parent_folder, name)
2488-
number = None
2489-
while True:
2490-
if number is None:
2488+
for number in range(-1, 10000): # Start with no suffix and limit the number of attempts
2489+
if number < 0:
24912490
path = start_path
2492-
number = 0
24932491
else:
24942492
path = start_path + '_' + str(number)
2495-
number += 1
24962493
try:
24972494
os.mkdir(path)
24982495
break

test/framework/filetools.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,6 +2889,7 @@ def test_locate_files(self):
28892889
self.assertErrorRegex(EasyBuildError, error_pattern, ft.locate_files, ['2.txt'], [])
28902890

28912891
def test_create_unused_dir(self):
2892+
"""Test create_unused_dir function."""
28922893
path = ft.create_unused_dir(self.test_prefix, 'folder')
28932894
self.assertEqual(path, os.path.join(self.test_prefix, 'folder'))
28942895
self.assertTrue(os.path.exists(path))
@@ -2905,6 +2906,21 @@ def test_create_unused_dir(self):
29052906
path = ft.create_unused_dir(self.test_prefix, 'folder2')
29062907
self.assertEqual(path, os.path.join(self.test_prefix, 'folder2_%s' % i))
29072908
self.assertTrue(os.path.exists(path))
2909+
# Fail cleanly if passed a readonly folder
2910+
readonly_dir = os.path.join(self.test_prefix, 'ro_folder')
2911+
os.mkdir(readonly_dir)
2912+
old_perms = os.lstat(readonly_dir)[stat.ST_MODE]
2913+
os.chmod(readonly_dir, stat.S_IREAD | stat.S_IEXEC)
2914+
try:
2915+
self.assertErrorRegex(EasyBuildError, 'Failed to create directory',
2916+
ft.create_unused_dir, readonly_dir, 'new_folder')
2917+
finally:
2918+
os.chmod(readonly_dir, old_perms)
2919+
# Ignore files same as folders. So first just create a file with no contents
2920+
open(os.path.join(self.test_prefix, 'file'), 'w').close()
2921+
path = ft.create_unused_dir(self.test_prefix, 'file')
2922+
self.assertEqual(path, os.path.join(self.test_prefix, 'file_0'))
2923+
self.assertTrue(os.path.exists(path))
29082924

29092925

29102926
def suite():

0 commit comments

Comments
 (0)