Skip to content
Merged
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: 2 additions & 0 deletions docs/changelog/+case-probe-private-dir.security.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Discovery no longer fails with :exc:`PermissionError`, or misdetects file-system case sensitivity, when the shared
temporary directory contains entries created by another user.
7 changes: 5 additions & 2 deletions src/python_discovery/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ def fs_path_id(path: str) -> str:

@functools.lru_cache(maxsize=1)
def fs_is_case_sensitive() -> bool:
with tempfile.NamedTemporaryFile(prefix="TmP") as tmp_file:
result = not pathlib.Path(tmp_file.name.lower()).exists()
# Probe in a private directory: the shared temp dir may hold another user's lower-cased twin of the name.
with tempfile.TemporaryDirectory() as folder:
probe = pathlib.Path(folder, "TmP")
probe.touch()
result = not probe.with_name(probe.name.lower()).exists()
_LOGGER.debug("filesystem is %scase-sensitive", "" if result else "not ")
return result

Expand Down
36 changes: 36 additions & 0 deletions tests/python_discovery/test_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import annotations

import tempfile
from typing import TYPE_CHECKING

import pytest

from python_discovery._compat import fs_is_case_sensitive

if TYPE_CHECKING:
from collections.abc import Generator
from pathlib import Path

from pytest_mock import MockerFixture


@pytest.fixture
def _fresh_case_sensitivity_probe() -> Generator[None]:
fs_is_case_sensitive.cache_clear()
yield
fs_is_case_sensitive.cache_clear()


@pytest.mark.usefixtures("_fresh_case_sensitivity_probe")
def test_fs_is_case_sensitive_ignores_lower_cased_twin_in_shared_temp_dir(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, mocker: MockerFixture
) -> None:
(case_probe := tmp_path / "CaSe").touch()
expected = not case_probe.with_name("case").exists()
shared = tmp_path / "shared"
shared.mkdir()
(shared / "tmpabcdef").touch()
monkeypatch.setattr(tempfile, "tempdir", str(shared))
mocker.patch("tempfile._get_candidate_names", autospec=True, return_value=iter(["AbCdEf", "GhIjKl"]))

assert fs_is_case_sensitive() is expected