Skip to content

Commit 9c77dcd

Browse files
Fix multi-path returned from _path methods on MacOS
Fix `site_{data,cache}_path` returning `pathlib.Path` based on the entire `:` separated multipath under Homebrew with `multipath` enabled. Instead, follow the approach used by `Unix` and only return the first element if we're `multipath` These were the only two attributes that changed when under `homebrew` Issue: 292
1 parent 49a89ef commit 9c77dcd

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
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: 4 additions & 0 deletions
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")])
@@ -99,6 +101,8 @@ def test_macos_homebrew(mocker: MockerFixture, params: dict[str, Any], multipath
99101
"site_config_dir": f"/opt/homebrew/share{suffix}",
100102
"site_cache_dir": f"/opt/homebrew/var/cache{suffix}",
101103
"site_runtime_dir": f"{home}/Library/Caches/TemporaryItems{suffix}",
104+
"site_cache_path": Path(f"/opt/homebrew/var/cache{suffix}"),
105+
"site_data_path": Path(f"/opt/homebrew/share{suffix}"),
102106
}
103107
if multipath:
104108
expected_map["site_data_dir"] += f":/Library/Application Support{suffix}"

0 commit comments

Comments
 (0)