Skip to content

Commit

Permalink
feat(mac_brew_pkg): Allow homebrew in all systems but Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
cdalvaro committed Jun 2, 2024
1 parent e4c1da4 commit 33959c6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
26 changes: 20 additions & 6 deletions salt/modules/mac_brew_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
Typically, this is set to ``/usr/local`` for Intel Macs and ``/opt/homebrew``
for Apple Silicon Macs.
For Linux systems, the default prefix is: ``/home/linuxbrew/.linuxbrew``.
.. important::
If you feel that Salt should be using this module to manage packages on a
minion, and it is using a different module (or gives an error similar to
Expand Down Expand Up @@ -36,10 +38,10 @@

def __virtual__():
"""
Confine this module to macOS with Homebrew.
Confine this module to systems with Homebrew.
"""
if __grains__["os"] != "MacOS":
return False, "brew module is macos specific"
if __grains__.get("os") == "Windows":
return False, "Homebrew does not support Windows"
if not _homebrew_bin():
return False, "The 'brew' binary was not found"
return __virtualname__
Expand Down Expand Up @@ -109,10 +111,10 @@ def _homebrew_os_bin():

original_path = os.environ.get("PATH")
try:
# Add "/opt/homebrew" temporary to the PATH for Apple Silicon if
# the PATH does not include "/opt/homebrew"
# Temporary add the default homebrew's prefix to the PATH
# if the PATH does not include it.
current_path = original_path or ""
homebrew_path = "/opt/homebrew/bin"
homebrew_path = _homebrew_default_prefix() + "/bin"
if homebrew_path not in current_path.split(os.path.pathsep):
extended_path = os.path.pathsep.join([current_path, homebrew_path])
os.environ["PATH"] = extended_path.lstrip(os.path.pathsep)
Expand Down Expand Up @@ -180,6 +182,18 @@ def _list_pkgs_from_context(versions_as_list):
return ret


def _homebrew_default_prefix():
"""
Returns the default homebrew prefix based on the OS and CPU architecture.
"""
if __grains__.get("os") == "MacOS":
if __grains__.get("cpuarch") == "arm64":
return "/opt/homebrew"
return "/usr/local"

return "/home/linuxbrew/.linuxbrew"


def homebrew_prefix():
"""
Returns the full path to the homebrew prefix.
Expand Down
14 changes: 14 additions & 0 deletions tests/pytests/unit/modules/test_mac_brew_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1081,3 +1081,17 @@ def test_list_upgrades(HOMEBREW_BIN):
assert (
mac_brew.list_upgrades(refresh=False, include_casks=True) == _expected
)


def test_homebrew_default_prefix():
"""
Tests homebrew's default prefix
"""
with patch.dict(mac_brew.__grains__, {"os": "MacOS", "cpuarch": "arm64"}):
assert mac_brew._homebrew_default_prefix() == "/opt/homebrew"

with patch.dict(mac_brew.__grains__, {"os": "MacOS", "cpuarch": "x86_64"}):
assert mac_brew._homebrew_default_prefix() == "/usr/local"

with patch.dict(mac_brew.__grains__, {"os": "Linux", "cpuarch": "x86_64"}):
assert mac_brew._homebrew_default_prefix() == "/home/linuxbrew/.linuxbrew"

0 comments on commit 33959c6

Please sign in to comment.