Skip to content

Commit

Permalink
Merge pull request #157 from pyiron/working_dir
Browse files Browse the repository at this point in the history
Working dir emptiness check
  • Loading branch information
liamhuber authored Jan 15, 2024
2 parents 3f26db0 + 85f153d commit 97d0114
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 6 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 All @@ -71,6 +72,9 @@ def create_subdirectory(self, path):
def create_file(self, file_name):
return FileObject(file_name, self)

def is_empty(self) -> bool:
return len(self) == 0


class FileObject:
def __init__(self, file_name: str, directory: DirectoryObject):
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@ def test_is_file(self):
f.delete()
self.assertFalse(f.is_file())

def test_is_empty(self):
self.assertTrue(self.directory.is_empty())
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 97d0114

Please sign in to comment.