|
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 | + |
11 | 15 |
|
12 | 16 | @pytest.mark.parametrize( |
13 | 17 | "params", |
|
17 | 21 | pytest.param({"appname": "foo", "version": "v1.0"}, id="app_name_version"), |
18 | 22 | ], |
19 | 23 | ) |
20 | | -def test_macos(params: dict[str, Any], func: str) -> None: |
| 24 | +def test_macos(mocker: MockerFixture, params: dict[str, Any], func: str) -> None: |
| 25 | + # Make sure we are not in Homebrew |
| 26 | + py_version = sys.version_info |
| 27 | + mocker.patch( |
| 28 | + "sys.prefix", |
| 29 | + ( |
| 30 | + "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions" |
| 31 | + f"/{py_version.major}.{py_version.minor}" |
| 32 | + ), |
| 33 | + ) |
| 34 | + |
21 | 35 | result = getattr(MacOS(**params), func) |
22 | 36 |
|
23 | 37 | home = str(Path("~").expanduser()) |
@@ -45,3 +59,48 @@ def test_macos(params: dict[str, Any], func: str) -> None: |
45 | 59 | expected = expected_map[func] |
46 | 60 |
|
47 | 61 | assert result == expected |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + "params", |
| 66 | + [ |
| 67 | + pytest.param({}, id="no_args"), |
| 68 | + pytest.param({"appname": "foo"}, id="app_name"), |
| 69 | + pytest.param({"appname": "foo", "version": "v1.0"}, id="app_name_version"), |
| 70 | + ], |
| 71 | +) |
| 72 | +@pytest.mark.parametrize("multipath", [pytest.param(True, id="multipath"), pytest.param(False, id="singlepath")]) |
| 73 | +def test_macos_homebrew(mocker: MockerFixture, params: dict[str, Any], multipath: bool, func: str) -> None: |
| 74 | + mocker.patch("sys.prefix", "/opt/homebrew/opt/python") |
| 75 | + |
| 76 | + result = getattr(MacOS(multipath=multipath, **params), func) |
| 77 | + |
| 78 | + home = str(Path("~").expanduser()) |
| 79 | + suffix_elements = tuple(params[i] for i in ("appname", "version") if i in params) |
| 80 | + suffix = os.sep.join(("", *suffix_elements)) if suffix_elements else "" # noqa: PTH118 |
| 81 | + |
| 82 | + expected_map = { |
| 83 | + "user_data_dir": f"{home}/Library/Application Support{suffix}", |
| 84 | + "site_data_dir": f"/opt/homebrew/share{suffix}", |
| 85 | + "user_config_dir": f"{home}/Library/Application Support{suffix}", |
| 86 | + "site_config_dir": f"/opt/homebrew/share{suffix}", |
| 87 | + "user_cache_dir": f"{home}/Library/Caches{suffix}", |
| 88 | + "site_cache_dir": f"/opt/homebrew/var/cache{suffix}", |
| 89 | + "user_state_dir": f"{home}/Library/Application Support{suffix}", |
| 90 | + "user_log_dir": f"{home}/Library/Logs{suffix}", |
| 91 | + "user_documents_dir": f"{home}/Documents", |
| 92 | + "user_downloads_dir": f"{home}/Downloads", |
| 93 | + "user_pictures_dir": f"{home}/Pictures", |
| 94 | + "user_videos_dir": f"{home}/Movies", |
| 95 | + "user_music_dir": f"{home}/Music", |
| 96 | + "user_desktop_dir": f"{home}/Desktop", |
| 97 | + "user_runtime_dir": f"{home}/Library/Caches/TemporaryItems{suffix}", |
| 98 | + "site_runtime_dir": f"{home}/Library/Caches/TemporaryItems{suffix}", |
| 99 | + } |
| 100 | + if multipath: |
| 101 | + expected_map["site_data_dir"] += f":/Library/Application Support{suffix}" |
| 102 | + expected_map["site_config_dir"] += f":/Library/Application Support{suffix}" |
| 103 | + expected_map["site_cache_dir"] += f":/Library/Caches{suffix}" |
| 104 | + expected = expected_map[func] |
| 105 | + |
| 106 | + assert result == expected |
0 commit comments