|
3 | 3 | import os |
4 | 4 | import sys |
5 | 5 | from pathlib import Path |
6 | | -from typing import TYPE_CHECKING, Any |
| 6 | +from typing import TYPE_CHECKING, Any, Final |
7 | 7 |
|
8 | 8 | import pytest |
9 | 9 |
|
@@ -50,15 +50,15 @@ def home() -> str: |
50 | 50 |
|
51 | 51 | @pytest.fixture |
52 | 52 | def _homebrew_py_prefix(mocker: MockerFixture) -> None: |
53 | | - mocker.patch("sys.prefix", "/opt/homebrew/opt/python@3.13/Frameworks/Python.framework/Versions/3.13") |
| 53 | + mocker.patch("sys.base_prefix", "/opt/homebrew/opt/python@3.13/Frameworks/Python.framework/Versions/3.13") |
54 | 54 |
|
55 | 55 |
|
56 | 56 | @pytest.fixture |
57 | 57 | def _builtin_py_prefix(mocker: MockerFixture) -> None: |
58 | | - """Keep ``sys.prefix`` off the ``/opt/python`` Homebrew heuristic so directories use the system defaults.""" |
| 58 | + """Keep ``sys.base_prefix`` off the ``/opt/python`` Homebrew heuristic so directories use the system defaults.""" |
59 | 59 | py_version = sys.version_info |
60 | 60 | mocker.patch( |
61 | | - "sys.prefix", |
| 61 | + "sys.base_prefix", |
62 | 62 | "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework" |
63 | 63 | f"/Versions/{py_version.major}.{py_version.minor}", |
64 | 64 | ) |
@@ -153,7 +153,7 @@ def test_macos_homebrew( |
153 | 153 | }, |
154 | 154 | ] |
155 | 155 | for prefix in test_data: |
156 | | - mocker.patch("sys.prefix", prefix["sys_prefix"]) |
| 156 | + mocker.patch("sys.base_prefix", prefix["sys_prefix"]) |
157 | 157 |
|
158 | 158 | result = getattr(MacOS(multipath=multipath, **params), site_func) |
159 | 159 |
|
@@ -484,3 +484,60 @@ def test_macos_iter_runtime_dirs_no_duplicate(home: str) -> None: |
484 | 484 | # site_runtime_dir is defined as user_runtime_dir. |
485 | 485 | expected = os.path.join(f"{home}/Library/Caches/TemporaryItems", "foo") # ruff:ignore[os-path-join] |
486 | 486 | assert list(MacOS(appname="foo").iter_runtime_dirs()) == [expected] |
| 487 | + |
| 488 | + |
| 489 | +@pytest.mark.usefixtures("_clear_xdg_env") |
| 490 | +@pytest.mark.parametrize( |
| 491 | + "homebrew_prefix", |
| 492 | + [ |
| 493 | + pytest.param("/opt/homebrew", id="apple-silicon"), |
| 494 | + pytest.param("/usr/local", id="intel"), |
| 495 | + pytest.param("/custom/brew", id="custom-prefix"), |
| 496 | + ], |
| 497 | +) |
| 498 | +@pytest.mark.parametrize("multipath", [pytest.param(True, id="multipath"), pytest.param(False, id="singlepath")]) |
| 499 | +@pytest.mark.parametrize( |
| 500 | + "prop", |
| 501 | + [ |
| 502 | + "site_data_dir", |
| 503 | + "site_config_dir", |
| 504 | + "site_cache_dir", |
| 505 | + "site_state_dir", |
| 506 | + "site_data_path", |
| 507 | + "site_config_path", |
| 508 | + "site_cache_path", |
| 509 | + "site_state_path", |
| 510 | + ], |
| 511 | +) |
| 512 | +def test_homebrew_virtual_environment( |
| 513 | + monkeypatch: pytest.MonkeyPatch, tmp_path: Path, homebrew_prefix: str, prop: str, multipath: bool |
| 514 | +) -> None: |
| 515 | + monkeypatch.setattr(sys, "prefix", str(tmp_path / ".venv")) |
| 516 | + monkeypatch.setattr( |
| 517 | + sys, "base_prefix", f"{homebrew_prefix}/opt/python@3.13/Frameworks/Python.framework/Versions/3.13" |
| 518 | + ) |
| 519 | + suffix: Final = "var/cache" if "cache" in prop else "share" |
| 520 | + expected: str | Path = f"{homebrew_prefix}/{suffix}{os.sep}Example{os.sep}1.0" |
| 521 | + if prop.endswith("_path"): |
| 522 | + expected = Path(expected) |
| 523 | + elif multipath and prop != "site_state_dir": |
| 524 | + fallback: Final = "Caches" if "cache" in prop else "Application Support" |
| 525 | + expected += f":/Library/{fallback}{os.sep}Example{os.sep}1.0" |
| 526 | + assert getattr(MacOS(appname="Example", version="1.0", multipath=multipath), prop) == expected |
| 527 | + |
| 528 | + |
| 529 | +@pytest.mark.usefixtures("_clear_xdg_env", "_builtin_py_prefix") |
| 530 | +@pytest.mark.parametrize( |
| 531 | + ("prop", "expected"), |
| 532 | + [ |
| 533 | + pytest.param("site_data_dir", "/Library/Application Support", id="data"), |
| 534 | + pytest.param("site_config_dir", "/Library/Application Support", id="config"), |
| 535 | + pytest.param("site_cache_dir", "/Library/Caches", id="cache"), |
| 536 | + pytest.param("site_state_dir", "/Library/Application Support", id="state"), |
| 537 | + ], |
| 538 | +) |
| 539 | +def test_non_homebrew_base_ignores_virtual_environment_name( |
| 540 | + monkeypatch: pytest.MonkeyPatch, tmp_path: Path, prop: str, expected: str |
| 541 | +) -> None: |
| 542 | + monkeypatch.setattr(sys, "prefix", (tmp_path / "opt/python/.venv").as_posix()) |
| 543 | + assert getattr(MacOS(), prop) == expected |
0 commit comments