Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi-path returned from _path methods on MacOS #299

Merged
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
6 changes: 6 additions & 0 deletions src/platformdirs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ def _optionally_create_directory(self, path: str) -> None:
if self.ensure_exists:
Path(path).mkdir(parents=True, exist_ok=True)

def _first_item_as_path_if_multipath(self, directory: str) -> Path:
if self.multipath:
# If multipath is True, the first path is returned.
directory = directory.split(os.pathsep)[0]
return Path(directory)

@property
@abstractmethod
def user_data_dir(self) -> str:
Expand Down
14 changes: 14 additions & 0 deletions src/platformdirs/macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

import os.path
import sys
from typing import TYPE_CHECKING

from .api import PlatformDirsABC

if TYPE_CHECKING:
from pathlib import Path


class MacOS(PlatformDirsABC):
"""
Expand Down Expand Up @@ -42,6 +46,11 @@ def site_data_dir(self) -> str:
return os.pathsep.join(path_list)
return path_list[0]

@property
def site_data_path(self) -> Path:
""":return: data path shared by users. Only return the first item, even if ``multipath`` is set to ``True``"""
return self._first_item_as_path_if_multipath(self.site_data_dir)

@property
def user_config_dir(self) -> str:
""":return: config directory tied to the user, same as `user_data_dir`"""
Expand Down Expand Up @@ -74,6 +83,11 @@ def site_cache_dir(self) -> str:
return os.pathsep.join(path_list)
return path_list[0]

@property
def site_cache_path(self) -> Path:
""":return: cache path shared by users. Only return the first item, even if ``multipath`` is set to ``True``"""
return self._first_item_as_path_if_multipath(self.site_cache_dir)

@property
def user_state_dir(self) -> str:
""":return: state directory tied to the user, same as `user_data_dir`"""
Expand Down
6 changes: 0 additions & 6 deletions src/platformdirs/unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,6 @@ def site_cache_path(self) -> Path:
""":return: cache path shared by users. Only return the first item, even if ``multipath`` is set to ``True``"""
return self._first_item_as_path_if_multipath(self.site_cache_dir)

def _first_item_as_path_if_multipath(self, directory: str) -> Path:
if self.multipath:
# If multipath is True, the first path is returned.
directory = directory.split(os.pathsep)[0]
return Path(directory)

def iter_config_dirs(self) -> Iterator[str]:
""":yield: all user and site configuration directories."""
yield self.user_config_dir
Expand Down
8 changes: 7 additions & 1 deletion tests/test_macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def test_macos(mocker: MockerFixture, params: dict[str, Any], func: str) -> None
"site_config_dir",
"site_cache_dir",
"site_runtime_dir",
"site_cache_path",
"site_data_path",
],
)
@pytest.mark.parametrize("multipath", [pytest.param(True, id="multipath"), pytest.param(False, id="singlepath")])
Expand All @@ -94,6 +96,10 @@ def test_macos_homebrew(mocker: MockerFixture, params: dict[str, Any], multipath
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 = {
gaborbernat marked this conversation as resolved.
Show resolved Hide resolved
"site_data_dir": f"/opt/homebrew/share{suffix}",
"site_config_dir": f"/opt/homebrew/share{suffix}",
Expand All @@ -104,6 +110,6 @@ def test_macos_homebrew(mocker: MockerFixture, params: dict[str, Any], 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_map[site_func]
expected = expected_path_map[site_func] if site_func.endswith("_path") else expected_map[site_func]

assert result == expected
Loading