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
2 changes: 1 addition & 1 deletion src/pyproject_api/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def ensure_empty_dir(path: Path) -> None:
if path.exists():
if path.is_dir():
for sub_path in path.iterdir():
if sub_path.is_dir():
if sub_path.is_dir() and not sub_path.is_symlink():
rmtree(sub_path, ignore_errors=True)
else:
sub_path.unlink()
Expand Down
23 changes: 23 additions & 0 deletions tests/test_util_regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from pyproject_api._util import ensure_empty_dir

if TYPE_CHECKING:
from pathlib import Path


def test_ensure_empty_dir_on_path_symlink_to_dir(tmp_path: Path) -> None:
"""A symlink pointing to a directory must be removed, not followed."""
target = tmp_path / "target"
target.mkdir()
(target / "c").write_text("")
work = tmp_path / "work"
work.mkdir()
link = work / "link"
link.symlink_to(target, target_is_directory=True)
ensure_empty_dir(work)
assert list(work.iterdir()) == []
# the symlink target itself must not be traversed and deleted
assert list(target.iterdir()) == [target / "c"]