Skip to content

Commit f205963

Browse files
committed
🧪 test(lease): cover removed Windows junction
An open target marker remains linked when its Windows junction is removed. That reaches the parent probe even though Windows refuses to unlink the open file. Exercise both missing-parent and parent-I/O-error outcomes. Refs #1013
1 parent 53dc1d6 commit f205963

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

‎tests/test_soft_lease.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,43 @@ def lstat(path: str | os.PathLike[str]) -> os.stat_result:
310310
assert (reports.seen[0].reason, error.errno) == ("refresh-failed", EIO)
311311

312312

313+
@pytest.mark.skipif(sys.platform != "win32", reason="requires a Windows directory junction") # pragma: win32 cover
314+
@pytest.mark.parametrize(
315+
"parent_errno", [pytest.param(None, id="missing-parent"), pytest.param(EIO, id="unreadable-parent")]
316+
)
317+
def test_lease_reports_refresh_failure_after_junction_removed(
318+
tmp_path: Path, create_junction: Callable[[Path, Path], None], mocker: MockerFixture, parent_errno: int | None
319+
) -> None:
320+
target = tmp_path / "target"
321+
target.mkdir()
322+
junction = tmp_path / "junction"
323+
create_junction(junction, target)
324+
marker = junction / "a.lock"
325+
reports = _Reports()
326+
lease = _lease(marker, on_compromise=reports)
327+
lease.acquire()
328+
real_lstat = os.lstat
329+
330+
def lstat(path: str | os.PathLike[str]) -> os.stat_result:
331+
if os.fspath(path) == str(junction) and parent_errno is not None:
332+
raise OSError(parent_errno, os.strerror(parent_errno), str(junction))
333+
return real_lstat(path)
334+
335+
mocker.patch("filelock._lease.os.lstat", autospec=True, side_effect=lstat)
336+
337+
try:
338+
junction.rmdir()
339+
assert (target / marker.name).exists()
340+
assert reports.landed.wait(_DEADLINE)
341+
finally:
342+
mocker.stopall()
343+
lease.release()
344+
345+
assert len(reports.seen) == 1
346+
assert isinstance((error := reports.seen[0].error), OSError)
347+
assert (reports.seen[0].reason, error.errno) == ("refresh-failed", parent_errno or ENOENT)
348+
349+
313350
@NEEDS_UNLINK_OPEN_FILE
314351
def test_lease_reports_compromise_when_a_peer_takes_over(marker: Path) -> None: # pragma: needs unlink-open-file
315352
reports = _Reports()

0 commit comments

Comments
 (0)