Skip to content

Commit

Permalink
Fix multi-path returned from _path methods on MacOS
Browse files Browse the repository at this point in the history
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
  • Loading branch information
matthewhughes934 committed Sep 8, 2024
1 parent 49a89ef commit 9c77dcd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 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
4 changes: 4 additions & 0 deletions 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 @@ -99,6 +101,8 @@ def test_macos_homebrew(mocker: MockerFixture, params: dict[str, Any], multipath
"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}",
"site_cache_path": Path(f"/opt/homebrew/var/cache{suffix}"),
"site_data_path": Path(f"/opt/homebrew/share{suffix}"),
}
if multipath:
expected_map["site_data_dir"] += f":/Library/Application Support{suffix}"
Expand Down

0 comments on commit 9c77dcd

Please sign in to comment.