Skip to content

Commit

Permalink
Extend the delete method so it has an option for safety
Browse files Browse the repository at this point in the history
No change to the default behaviour
  • Loading branch information
liamhuber committed Jan 10, 2024
1 parent adcd1de commit 2e5d985
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pyiron_workflow/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ def __init__(self, directory):
def create(self):
self.path.mkdir(parents=True, exist_ok=True)

def delete(self):
delete_files_and_directories_recursively(self.path)
def delete(self, only_if_empty: bool = False):
if self.is_empty or not only_if_empty:
delete_files_and_directories_recursively(self.path)

def list_content(self):
return categorize_folder_items(self.path)
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ def test_is_empty(self):
self.directory.write(file_name="test.txt", content="something")
self.assertFalse(self.directory.is_empty)

def test_delete(self):
self.assertTrue(
Path("test").exists() and Path("test").is_dir(),
msg="Sanity check on initial state"
)
self.directory.write(file_name="test.txt", content="something")
self.directory.delete(only_if_empty=True)
self.assertFalse(
self.directory.is_empty,
msg="Flag argument on delete should have prevented removal"
)
self.directory.delete()
self.assertFalse(
Path("test").exists(),
msg="Delete should remove the entire directory"
)
self.directory = DirectoryObject("test") # Rebuild it so the tearDown works


if __name__ == '__main__':
unittest.main()

0 comments on commit 2e5d985

Please sign in to comment.