|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import sys |
4 | 5 | from pathlib import Path |
5 | | -from typing import Any |
| 6 | +from typing import TYPE_CHECKING, Any |
6 | 7 |
|
7 | 8 | import pytest |
8 | 9 |
|
9 | 10 | from platformdirs.macos import MacOS |
10 | 11 |
|
| 12 | +if TYPE_CHECKING: |
| 13 | + from pytest_mock import MockerFixture |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(autouse=True) |
| 17 | +def _fix_os_pathsep(mocker: MockerFixture) -> None: |
| 18 | + """ |
| 19 | + If we're not actually running on macOS, set `os.pathsep` to what it should be on macOS. |
| 20 | + """ |
| 21 | + if sys.platform != "darwin": # pragma: darwin no cover |
| 22 | + mocker.patch("os.pathsep", ":") |
| 23 | + mocker.patch("os.path.pathsep", ":") |
| 24 | + |
11 | 25 |
|
12 | 26 | @pytest.mark.parametrize( |
13 | 27 | "params", |
|
17 | 31 | pytest.param({"appname": "foo", "version": "v1.0"}, id="app_name_version"), |
18 | 32 | ], |
19 | 33 | ) |
20 | | -def test_macos(params: dict[str, Any], func: str) -> None: |
| 34 | +def test_macos(mocker: MockerFixture, params: dict[str, Any], func: str) -> None: |
| 35 | + # Make sure we are not in Homebrew |
| 36 | + py_version = sys.version_info |
| 37 | + builtin_py_prefix = ( |
| 38 | + "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework" |
| 39 | + f"/Versions/{py_version.major}.{py_version.minor}" |
| 40 | + ) |
| 41 | + mocker.patch("sys.prefix", builtin_py_prefix) |
| 42 | + |
21 | 43 | result = getattr(MacOS(**params), func) |
22 | 44 |
|
23 | 45 | home = str(Path("~").expanduser()) |
@@ -45,3 +67,45 @@ def test_macos(params: dict[str, Any], func: str) -> None: |
45 | 67 | expected = expected_map[func] |
46 | 68 |
|
47 | 69 | assert result == expected |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize( |
| 73 | + "params", |
| 74 | + [ |
| 75 | + pytest.param({}, id="no_args"), |
| 76 | + pytest.param({"appname": "foo"}, id="app_name"), |
| 77 | + pytest.param({"appname": "foo", "version": "v1.0"}, id="app_name_version"), |
| 78 | + ], |
| 79 | +) |
| 80 | +@pytest.mark.parametrize( |
| 81 | + "site_func", |
| 82 | + [ |
| 83 | + "site_data_dir", |
| 84 | + "site_config_dir", |
| 85 | + "site_cache_dir", |
| 86 | + "site_runtime_dir", |
| 87 | + ], |
| 88 | +) |
| 89 | +@pytest.mark.parametrize("multipath", [pytest.param(True, id="multipath"), pytest.param(False, id="singlepath")]) |
| 90 | +def test_macos_homebrew(mocker: MockerFixture, params: dict[str, Any], multipath: bool, site_func: str) -> None: |
| 91 | + mocker.patch("sys.prefix", "/opt/homebrew/opt/python") |
| 92 | + |
| 93 | + result = getattr(MacOS(multipath=multipath, **params), site_func) |
| 94 | + |
| 95 | + home = str(Path("~").expanduser()) |
| 96 | + suffix_elements = tuple(params[i] for i in ("appname", "version") if i in params) |
| 97 | + suffix = os.sep.join(("", *suffix_elements)) if suffix_elements else "" # noqa: PTH118 |
| 98 | + |
| 99 | + expected_map = { |
| 100 | + "site_data_dir": f"/opt/homebrew/share{suffix}", |
| 101 | + "site_config_dir": f"/opt/homebrew/share{suffix}", |
| 102 | + "site_cache_dir": f"/opt/homebrew/var/cache{suffix}", |
| 103 | + "site_runtime_dir": f"{home}/Library/Caches/TemporaryItems{suffix}", |
| 104 | + } |
| 105 | + if multipath: |
| 106 | + expected_map["site_data_dir"] += f":/Library/Application Support{suffix}" |
| 107 | + expected_map["site_config_dir"] += f":/Library/Application Support{suffix}" |
| 108 | + expected_map["site_cache_dir"] += f":/Library/Caches{suffix}" |
| 109 | + expected = expected_map[site_func] |
| 110 | + |
| 111 | + assert result == expected |
0 commit comments