Skip to content

Commit

Permalink
Inaccessible lock files imply temporary directories can't be removed
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Jul 14, 2020
1 parent 358150c commit 97f8a95
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions changelog/7491.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:fixture:`tmpdir` and :fixture:`tmp_path` no longer raise an error if the lock to check for
stale temporary directories is not accessible.
11 changes: 8 additions & 3 deletions src/_pytest/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,17 @@ def maybe_delete_a_numbered_dir(path: Path) -> None:


def ensure_deletable(path: Path, consider_lock_dead_if_created_before: float) -> bool:
"""checks if a lock exists and breaks it if its considered dead"""
"""checks if `path` is deletable based if the lock file is expired"""
if path.is_symlink():
return False
lock = get_lock_path(path)
if not lock.exists():
return True
try:
if not lock.is_file():
return True
except OSError:
# we might not have access to the lock file at all, in this case assume
# we don't have access to the entire directory (#7491).
return False
try:
lock_time = lock.stat().st_mtime
except Exception:
Expand Down
8 changes: 7 additions & 1 deletion testing/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def test_get_extended_length_path_str():


def test_suppress_error_removing_lock(tmp_path):
"""ensure_deletable should not raise an exception if the lock file cannot be removed (#5456)"""
"""ensure_deletable should not be resilient if lock file cannot be removed (#5456, #7491)"""
path = tmp_path / "dir"
path.mkdir()
lock = get_lock_path(path)
Expand All @@ -371,6 +371,12 @@ def test_suppress_error_removing_lock(tmp_path):
)
assert lock.is_file()

with unittest.mock.patch.object(Path, "is_file", side_effect=OSError):
assert not ensure_deletable(
path, consider_lock_dead_if_created_before=mtime + 30
)
assert lock.is_file()

# check now that we can remove the lock file in normal circumstances
assert ensure_deletable(path, consider_lock_dead_if_created_before=mtime + 30)
assert not lock.is_file()

0 comments on commit 97f8a95

Please sign in to comment.