Skip to content

Commit

Permalink
Fixing issue #56
Browse files Browse the repository at this point in the history
  • Loading branch information
h-0-0 committed Oct 18, 2024
1 parent e05a93f commit be83d74
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/slune/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,19 @@ def is_numeric(s):
equiv = next_dir
continue
# If the directory doesn't exist, check if there's a directory with the same numerical value
dir_value = d.split('=')[1]
check, dir_value = d.split('=')
if (check == '') or (dir_value == ''):
raise ValueError("'=' cannot be at the beginning or end of a directory name.")
if is_numeric(dir_value):
dir_value = float(dir_value)
if os.path.exists(equiv):
existing_dirs = [entry.name for entry in os.scandir(equiv) if entry.is_dir()]
for existing_dir in existing_dirs:
if not '=' in existing_dir: # We only consider directories with the form '--string=value'
continue
existing_dir_value = existing_dir.split('=')[1]
check, existing_dir_value = existing_dir.split('=')
if check == '' or existing_dir_value == '':
raise ValueError("'=' cannot be at the beginning of a directory name.")
if is_numeric(existing_dir_value) and float(existing_dir_value) == dir_value:
equiv = os.path.join(equiv, existing_dir)
break
Expand Down
43 changes: 35 additions & 8 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,36 +86,38 @@ def setUp(self):

def tearDown(self):
os.rmdir(os.path.join(self.test_dir, '--dir1=1', '--dir2=2', '--dir3=3'))
if os.path.exists(os.path.join(self.test_dir, '--dir1=1', '--dir2=2', '--dir3_3.0')):
os.rmdir(os.path.join(self.test_dir, '--dir1=1', '--dir2=2', '--dir3_3.0'))
os.rmdir(os.path.join(self.test_dir, '--dir1=1', '--dir2=2'))
os.rmdir(os.path.join(self.test_dir, '--dir1=1'))
os.rmdir(self.test_dir)

def test_get_numeric_equiv_full_path_exist(self):
def test_equiv_full_path_exist(self):
path = os.path.join('--dir1=1','--dir2=2')
expected = os.path.join(self.test_dir, '--dir1=1', '--dir2=2')
self.assertEqual(get_numeric_equiv(path, self.test_dir), expected)

def test_get_numeric_equiv_path_longer_than_existing(self):
def test_path_longer_than_existing(self):
path = os.path.join('--dir1=1','--dir2=2','--dir4=4')
expected = os.path.join(self.test_dir, '--dir1=1', '--dir2=2', '--dir4=4')
self.assertEqual(get_numeric_equiv(path, self.test_dir), expected)

def test_get_numeric_equiv_full_path_exist_diff_numeric_value(self):
def test_full_path_exist_diff_numeric_value(self):
path = os.path.join('--dir1=1','--dir2=2.00')
expected = os.path.join(self.test_dir, '--dir1=1', '--dir2=2')
self.assertEqual(get_numeric_equiv(path, self.test_dir), expected)

def test_get_numeric_equiv_none_of_path_exist(self):
def test_none_of_path_exist(self):
path = os.path.join('--dir5=5','--dir6=6')
expected = os.path.join(self.test_dir, '--dir5=5', '--dir6=6')
self.assertEqual(get_numeric_equiv(path, self.test_dir), expected)

def test_get_numeric_equiv_numeric_equiv_exist_then_none(self):
def test_numeric_equiv_exist_then_none(self):
path = os.path.join('--dir1=1.0','--dir4=4')
expected = os.path.join(self.test_dir, '--dir1=1', '--dir4=4')
self.assertEqual(get_numeric_equiv(path, self.test_dir), expected)

def test_get_numeric_equiv_exists_then_numeric_equiv(self):
def test_exists_then_numeric_equiv(self):
path = os.path.join('--dir1=1','--dir2=2.0')
expected = os.path.join(self.test_dir, '--dir1=1', '--dir2=2')
self.assertEqual(get_numeric_equiv(path, self.test_dir), expected)
Expand All @@ -124,7 +126,7 @@ def test_get_numeric_equiv_exists_then_numeric_equiv(self):
expected = os.path.join(self.test_dir, '--dir1=1', '--dir2=2', '--dir3=3')
self.assertEqual(get_numeric_equiv(path, self.test_dir), expected)

def test_get_numerc_equiv_numeric_equiv_then_exists(self):
def test_numeric_equiv_then_exists(self):
path = os.path.join('--dir1=1.0','--dir2=2')
expected = os.path.join(self.test_dir, '--dir1=1', '--dir2=2')
self.assertEqual(get_numeric_equiv(path, self.test_dir), expected)
Expand All @@ -133,7 +135,7 @@ def test_get_numerc_equiv_numeric_equiv_then_exists(self):
expected = os.path.join(self.test_dir, '--dir1=1', '--dir2=2', '--dir3=3')
self.assertEqual(get_numeric_equiv(path, self.test_dir), expected)

def test_get_numeric_directory_with_no_equals(self):
def test_directory_with_no_equals(self):
os.makedirs(os.path.join(self.test_dir, '--dir1=1', '--dir2=2', '--dir_no_equals'))

path = os.path.join('--dir1=1.0','--dir2=2', '--dir_no_equals')
Expand All @@ -142,6 +144,31 @@ def test_get_numeric_directory_with_no_equals(self):

os.rmdir(os.path.join(self.test_dir, '--dir1=1', '--dir2=2', '--dir_no_equals'))

def test_number_no_equals(self):
os.makedirs(os.path.join(self.test_dir, '--dir1=1', '--dir2=2', '--dir3_3.0'))
path = os.path.join('--dir1=1','--dir2=2','--dir3_3.0')
expected = os.path.join(self.test_dir, '--dir1=1', '--dir2=2', '--dir3_3.0')
self.assertEqual(get_numeric_equiv(path, self.test_dir), expected)
os.rmdir(os.path.join(self.test_dir, '--dir1=1', '--dir2=2', '--dir3_3.0'))

def test_equals_at_begining(self):
path = os.path.join('--dir1=1','--dir2=2','=--dir3_3.0')
# Expect an error to be raised
with self.assertRaises(ValueError):
get_numeric_equiv(path, self.test_dir)

def test_equals_at_end(self):
path = os.path.join('--dir1=1','--dir2=2','--dir3_3.0=')
# Expect an error to be raised
with self.assertRaises(ValueError):
get_numeric_equiv(path, self.test_dir)

def test_two_equals(self):
path = os.path.join('--dir1=1','--dir2=2','--dir3=3=3')
# Expect an error to be raised
with self.assertRaises(ValueError):
get_numeric_equiv(path, self.test_dir)


class TestDictToStrings(unittest.TestCase):

Expand Down

0 comments on commit be83d74

Please sign in to comment.