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
18 changes: 10 additions & 8 deletions src/platformdirs/macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ def site_data_dir(self) -> str:
"""
:return: data directory shared by users, e.g. ``/Library/Application Support/$appname/$version``.
If we're using a Python binary managed by `Homebrew <https://brew.sh>`_, the directory
will be under the Homebrew prefix, e.g. ``/opt/homebrew/share/$appname/$version``.
will be under the Homebrew prefix, e.g. ``$homebrew_prefix/share/$appname/$version``.
If `multipath <platformdirs.api.PlatformDirsABC.multipath>` is enabled, and we're in Homebrew,
the response is a multi-path string separated by ":", e.g.
``/opt/homebrew/share/$appname/$version:/Library/Application Support/$appname/$version``
``$homebrew_prefix/share/$appname/$version:/Library/Application Support/$appname/$version``
"""
is_homebrew = sys.prefix.startswith("/opt/homebrew")
path_list = [self._append_app_name_and_version("/opt/homebrew/share")] if is_homebrew else []
is_homebrew = "/opt/python" in sys.prefix
homebrew_prefix = sys.prefix.split("/opt/python")[0] if is_homebrew else ""
path_list = [self._append_app_name_and_version(f"{homebrew_prefix}/share")] if is_homebrew else []
path_list.append(self._append_app_name_and_version("/Library/Application Support"))
if self.multipath:
return os.pathsep.join(path_list)
Expand Down Expand Up @@ -71,13 +72,14 @@ def site_cache_dir(self) -> str:
"""
:return: cache directory shared by users, e.g. ``/Library/Caches/$appname/$version``.
If we're using a Python binary managed by `Homebrew <https://brew.sh>`_, the directory
will be under the Homebrew prefix, e.g. ``/opt/homebrew/var/cache/$appname/$version``.
will be under the Homebrew prefix, e.g. ``$homebrew_prefix/var/cache/$appname/$version``.
If `multipath <platformdirs.api.PlatformDirsABC.multipath>` is enabled, and we're in Homebrew,
the response is a multi-path string separated by ":", e.g.
``/opt/homebrew/var/cache/$appname/$version:/Library/Caches/$appname/$version``
``$homebrew_prefix/var/cache/$appname/$version:/Library/Caches/$appname/$version``
"""
is_homebrew = sys.prefix.startswith("/opt/homebrew")
path_list = [self._append_app_name_and_version("/opt/homebrew/var/cache")] if is_homebrew else []
is_homebrew = "/opt/python" in sys.prefix
homebrew_prefix = sys.prefix.split("/opt/python")[0] if is_homebrew else ""
path_list = [self._append_app_name_and_version(f"{homebrew_prefix}/var/cache")] if is_homebrew else []
path_list.append(self._append_app_name_and_version("/Library/Caches"))
if self.multipath:
return os.pathsep.join(path_list)
Expand Down
65 changes: 40 additions & 25 deletions tests/test_macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,43 @@ def test_macos(mocker: MockerFixture, params: dict[str, Any], func: str) -> None
)
@pytest.mark.parametrize("multipath", [pytest.param(True, id="multipath"), pytest.param(False, id="singlepath")])
def test_macos_homebrew(mocker: MockerFixture, params: dict[str, Any], multipath: bool, site_func: str) -> None:
mocker.patch("sys.prefix", "/opt/homebrew/opt/python")

result = getattr(MacOS(multipath=multipath, **params), site_func)

home = str(Path("~").expanduser())
suffix_elements = tuple(params[i] for i in ("appname", "version") if i in params)
suffix = os.sep.join(("", *suffix_elements)) if suffix_elements else "" # noqa: PTH118

expected_path_map = {
"site_cache_path": Path(f"/opt/homebrew/var/cache{suffix}"),
"site_data_path": Path(f"/opt/homebrew/share{suffix}"),
}
expected_map = {
"site_data_dir": f"/opt/homebrew/share{suffix}",
"site_config_dir": f"/opt/homebrew/share{suffix}",
"site_cache_dir": f"/opt/homebrew/var/cache{suffix}",
"site_runtime_dir": f"{home}/Library/Caches/TemporaryItems{suffix}",
}
if multipath:
expected_map["site_data_dir"] += f":/Library/Application Support{suffix}"
expected_map["site_config_dir"] += f":/Library/Application Support{suffix}"
expected_map["site_cache_dir"] += f":/Library/Caches{suffix}"
expected = expected_path_map[site_func] if site_func.endswith("_path") else expected_map[site_func]

assert result == expected
test_data = [
{
"sys_prefix": "/opt/homebrew/opt/[email protected]/Frameworks/Python.framework/Versions/3.13",
"homebrew_prefix": "/opt/homebrew",
},
{
"sys_prefix": "/usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.13",
"homebrew_prefix": "/usr/local",
},
{
"sys_prefix": "/myown/arbitrary/prefix/opt/[email protected]/Frameworks/Python.framework/Versions/3.13",
"homebrew_prefix": "/myown/arbitrary/prefix",
},
]
for prefix in test_data:
mocker.patch("sys.prefix", prefix["sys_prefix"])

result = getattr(MacOS(multipath=multipath, **params), site_func)

home = str(Path("~").expanduser())
suffix_elements = tuple(params[i] for i in ("appname", "version") if i in params)
suffix = os.sep.join(("", *suffix_elements)) if suffix_elements else "" # noqa: PTH118

expected_path_map = {
"site_cache_path": Path(f"{prefix['homebrew_prefix']}/var/cache{suffix}"),
"site_data_path": Path(f"{prefix['homebrew_prefix']}/share{suffix}"),
}
expected_map = {
"site_data_dir": f"{prefix['homebrew_prefix']}/share{suffix}",
"site_config_dir": f"{prefix['homebrew_prefix']}/share{suffix}",
"site_cache_dir": f"{prefix['homebrew_prefix']}/var/cache{suffix}",
"site_runtime_dir": f"{home}/Library/Caches/TemporaryItems{suffix}",
}
if multipath:
expected_map["site_data_dir"] += f":/Library/Application Support{suffix}"
expected_map["site_config_dir"] += f":/Library/Application Support{suffix}"
expected_map["site_cache_dir"] += f":/Library/Caches{suffix}"
expected = expected_path_map[site_func] if site_func.endswith("_path") else expected_map[site_func]

assert result == expected
Loading