Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tests/trestle/utils/trash_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,37 @@ def test_trash_recover(tmp_path) -> None:
trash.recover(data_dir)
assert data_dir.exists()
assert readme_file.exists()


def test_trash_recover_dotted_directory_name(tmp_path: pathlib.Path) -> None:
"""Test recover handles directory names containing dots."""
test_utils.ensure_trestle_config_dir(tmp_path)
dotted_dir: pathlib.Path = tmp_path / 'policy.v1'
dotted_dir.mkdir(exist_ok=True, parents=True)
readme_file: pathlib.Path = dotted_dir / 'readme.md'
readme_file.touch()

trash.store(dotted_dir, True)
assert dotted_dir.exists() is False
assert readme_file.exists() is False
assert trash.to_trash_dir_path(dotted_dir).exists()

trash.recover(dotted_dir, True)
assert dotted_dir.exists()
assert readme_file.exists()
assert not trash.to_trash_dir_path(dotted_dir).exists()


def test_trash_recover_dotted_file_name(tmp_path: pathlib.Path) -> None:
"""Test recover still handles dotted file names correctly."""
test_utils.ensure_trestle_config_dir(tmp_path)
dotted_file: pathlib.Path = tmp_path / 'policy.v1.json'
dotted_file.write_text('{}')

trash.store(dotted_file, True)
assert dotted_file.exists() is False
assert trash.to_trash_file_path(dotted_file).exists()

trash.recover(dotted_file, True)
assert dotted_file.exists()
assert not trash.to_trash_file_path(dotted_file).exists()
10 changes: 6 additions & 4 deletions trestle/common/trash.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,13 @@ def recover(dest_content_path: pathlib.Path, delete_trash: bool = False) -> None
"""Recover the specified file or directory from the trash directory.

dest_content_path: destination content path that needs to be recovered from trash
It recovers the latest path content from trash if exists
It recovers the latest path content from trash if exists.
Dispatches to file or directory recovery based on which trash layout exists.
"""
if dest_content_path.suffix != '':
return recover_file(dest_content_path, delete_trash)
return recover_dir(dest_content_path, delete_trash)
trash_dir_path = to_trash_dir_path(dest_content_path)
if trash_dir_path.exists() and trash_dir_path.is_dir():
return recover_dir(dest_content_path, delete_trash)
return recover_file(dest_content_path, delete_trash)


def has_parent_path(sub_path: pathlib.Path, parent_path: pathlib.Path) -> bool:
Expand Down
Loading