Skip to content

Commit c596271

Browse files
Fix multi-path returned from _path methods on MacOS (#299)
Co-authored-by: Bernát Gábor <gaborjbernat@gmail.com>
1 parent a420284 commit c596271

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

src/platformdirs/api.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ def _optionally_create_directory(self, path: str) -> None:
9191
if self.ensure_exists:
9292
Path(path).mkdir(parents=True, exist_ok=True)
9393

94+
def _first_item_as_path_if_multipath(self, directory: str) -> Path:
95+
if self.multipath:
96+
# If multipath is True, the first path is returned.
97+
directory = directory.split(os.pathsep)[0]
98+
return Path(directory)
99+
94100
@property
95101
@abstractmethod
96102
def user_data_dir(self) -> str:

src/platformdirs/macos.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
import os.path
66
import sys
7+
from typing import TYPE_CHECKING
78

89
from .api import PlatformDirsABC
910

11+
if TYPE_CHECKING:
12+
from pathlib import Path
13+
1014

1115
class MacOS(PlatformDirsABC):
1216
"""
@@ -42,6 +46,11 @@ def site_data_dir(self) -> str:
4246
return os.pathsep.join(path_list)
4347
return path_list[0]
4448

49+
@property
50+
def site_data_path(self) -> Path:
51+
""":return: data path shared by users. Only return the first item, even if ``multipath`` is set to ``True``"""
52+
return self._first_item_as_path_if_multipath(self.site_data_dir)
53+
4554
@property
4655
def user_config_dir(self) -> str:
4756
""":return: config directory tied to the user, same as `user_data_dir`"""
@@ -74,6 +83,11 @@ def site_cache_dir(self) -> str:
7483
return os.pathsep.join(path_list)
7584
return path_list[0]
7685

86+
@property
87+
def site_cache_path(self) -> Path:
88+
""":return: cache path shared by users. Only return the first item, even if ``multipath`` is set to ``True``"""
89+
return self._first_item_as_path_if_multipath(self.site_cache_dir)
90+
7791
@property
7892
def user_state_dir(self) -> str:
7993
""":return: state directory tied to the user, same as `user_data_dir`"""

src/platformdirs/unix.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,6 @@ def site_cache_path(self) -> Path:
218218
""":return: cache path shared by users. Only return the first item, even if ``multipath`` is set to ``True``"""
219219
return self._first_item_as_path_if_multipath(self.site_cache_dir)
220220

221-
def _first_item_as_path_if_multipath(self, directory: str) -> Path:
222-
if self.multipath:
223-
# If multipath is True, the first path is returned.
224-
directory = directory.split(os.pathsep)[0]
225-
return Path(directory)
226-
227221
def iter_config_dirs(self) -> Iterator[str]:
228222
""":yield: all user and site configuration directories."""
229223
yield self.user_config_dir

tests/test_macos.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ def test_macos(mocker: MockerFixture, params: dict[str, Any], func: str) -> None
8282
"site_config_dir",
8383
"site_cache_dir",
8484
"site_runtime_dir",
85+
"site_cache_path",
86+
"site_data_path",
8587
],
8688
)
8789
@pytest.mark.parametrize("multipath", [pytest.param(True, id="multipath"), pytest.param(False, id="singlepath")])
@@ -94,6 +96,10 @@ def test_macos_homebrew(mocker: MockerFixture, params: dict[str, Any], multipath
9496
suffix_elements = tuple(params[i] for i in ("appname", "version") if i in params)
9597
suffix = os.sep.join(("", *suffix_elements)) if suffix_elements else "" # noqa: PTH118
9698

99+
expected_path_map = {
100+
"site_cache_path": Path(f"/opt/homebrew/var/cache{suffix}"),
101+
"site_data_path": Path(f"/opt/homebrew/share{suffix}"),
102+
}
97103
expected_map = {
98104
"site_data_dir": f"/opt/homebrew/share{suffix}",
99105
"site_config_dir": f"/opt/homebrew/share{suffix}",
@@ -104,6 +110,6 @@ def test_macos_homebrew(mocker: MockerFixture, params: dict[str, Any], multipath
104110
expected_map["site_data_dir"] += f":/Library/Application Support{suffix}"
105111
expected_map["site_config_dir"] += f":/Library/Application Support{suffix}"
106112
expected_map["site_cache_dir"] += f":/Library/Caches{suffix}"
107-
expected = expected_map[site_func]
113+
expected = expected_path_map[site_func] if site_func.endswith("_path") else expected_map[site_func]
108114

109115
assert result == expected

0 commit comments

Comments
 (0)