Skip to content

Commit

Permalink
Fix multi-path returned from _path methods on MacOS (#299)
Browse files Browse the repository at this point in the history
Co-authored-by: Bernát Gábor <[email protected]>
  • Loading branch information
matthewhughes934 and gaborbernat authored Sep 8, 2024
1 parent a420284 commit c596271
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
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 = {
"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

0 comments on commit c596271

Please sign in to comment.