diff --git a/securesystemslib/util.py b/securesystemslib/util.py index 1c6f12b7..2fd772d4 100755 --- a/securesystemslib/util.py +++ b/securesystemslib/util.py @@ -254,7 +254,10 @@ def ensure_parent_dir(filename, storage_backend=None): # Split 'filename' into head and tail, check if head exists. directory = os.path.split(filename)[0] - storage_backend.create_folder(directory) + # Check for cases where filename is without directory like 'file.txt' + # and as a result directory is an empty string + if directory: + storage_backend.create_folder(directory) def file_in_confined_directories(filepath, confined_directories): diff --git a/tests/test_util.py b/tests/test_util.py index 6cf38997..287d8f25 100755 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -153,6 +153,13 @@ def test_B4_ensure_parent_dir(self): self.assertRaises(securesystemslib.exceptions.FormatError, securesystemslib.util.ensure_parent_dir, parent_dir) + # When we call ensure_parent_dir with filepath arg like "a.txt", + # then the directory of that filepath will be an empty string. + # We want to make sure that securesyslib.storage.create_folder() + # won't be called with an empty string and thus raise an exception. + # If an exception is thrown the test will fail. + securesystemslib.util.ensure_parent_dir('a.txt') + def test_B5_file_in_confined_directories(self):