Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use brew path from which in mac_brew_pkg module #57946

Merged
merged 8 commits into from
Aug 9, 2023
1 change: 1 addition & 0 deletions changelog/57946.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use brew path from which in mac_brew_pkg module and rely on _homebrew_bin() everytime
36 changes: 23 additions & 13 deletions salt/modules/mac_brew_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __virtual__():
"""
if __grains__["os"] != "MacOS":
return False, "brew module is macos specific"
if not salt.utils.path.which("brew"):
if not _homebrew_os_bin():
return False, "The 'brew' binary was not found"
return __virtualname__

Expand Down Expand Up @@ -93,13 +93,23 @@ def _tap(tap, runas=None):
return True


def _homebrew_os_bin():
"""
Fetch PATH binary brew full path eg: /usr/local/bin/brew (symbolic link)
"""
return salt.utils.path.which("brew")


def _homebrew_bin():
"""
Returns the full path to the homebrew binary in the PATH
Returns the full path to the homebrew binary in the homebrew installation folder
"""
ret = __salt__["cmd.run"]("brew --prefix", output_loglevel="trace")
ret += "/bin/brew"
return ret
brew = _homebrew_os_bin()
if brew:
# Fetch and ret brew installation folder full path eg: /opt/homebrew/bin/brew
brew = __salt__["cmd.run"](f"{brew} --prefix", output_loglevel="trace")
brew += "/bin/brew"
return brew


def _call_brew(*cmd, failhard=True):
Expand All @@ -110,8 +120,8 @@ def _call_brew(*cmd, failhard=True):
runas = user if user != __opts__["user"] else None
_cmd = []
if runas:
_cmd = ["sudo -i -n -H -u {} -- ".format(runas)]
_cmd = _cmd + [salt.utils.path.which("brew")] + list(cmd)
_cmd = [f"sudo -i -n -H -u {runas} -- "]
_cmd = _cmd + [_homebrew_bin()] + list(cmd)
_cmd = " ".join(_cmd)

runas = None
Expand Down Expand Up @@ -496,7 +506,7 @@ def list_upgrades(refresh=True, include_casks=False, **kwargs): # pylint: disab
try:
data = salt.utils.json.loads(res["stdout"])
except ValueError as err:
msg = 'unable to interpret output from "brew outdated": {}'.format(err)
msg = f'unable to interpret output from "brew outdated": {err}'
log.error(msg)
raise CommandExecutionError(msg)

Expand Down Expand Up @@ -637,11 +647,11 @@ def hold(name=None, pkgs=None, sources=None, **kwargs): # pylint: disable=W0613
ret[target] = {"name": target, "changes": {}, "result": False, "comment": ""}

if target not in installed:
ret[target]["comment"] = "Package {} does not have a state.".format(target)
ret[target]["comment"] = f"Package {target} does not have a state."
elif target not in pinned:
if "test" in __opts__ and __opts__["test"]:
ret[target].update(result=None)
ret[target]["comment"] = "Package {} is set to be held.".format(target)
ret[target]["comment"] = f"Package {target} is set to be held."
else:
result = _pin(target)
if result:
Expand All @@ -652,7 +662,7 @@ def hold(name=None, pkgs=None, sources=None, **kwargs): # pylint: disable=W0613
)
else:
ret[target].update(result=False)
ret[target]["comment"] = "Unable to hold package {}.".format(target)
ret[target]["comment"] = f"Unable to hold package {target}."
else:
ret[target].update(result=True)
ret[target]["comment"] = "Package {} is already set to be held.".format(
Expand Down Expand Up @@ -713,7 +723,7 @@ def unhold(name=None, pkgs=None, sources=None, **kwargs): # pylint: disable=W06
ret[target] = {"name": target, "changes": {}, "result": False, "comment": ""}

if target not in installed:
ret[target]["comment"] = "Package {} does not have a state.".format(target)
ret[target]["comment"] = f"Package {target} does not have a state."
elif target in pinned:
if "test" in __opts__ and __opts__["test"]:
ret[target].update(result=None)
Expand All @@ -727,7 +737,7 @@ def unhold(name=None, pkgs=None, sources=None, **kwargs): # pylint: disable=W06
ret[target].update(changes=changes, result=True)
ret[target][
"comment"
] = "Package {} is no longer being held.".format(target)
] = f"Package {target} is no longer being held."
else:
ret[target].update(result=False)
ret[target]["comment"] = "Unable to unhold package {}.".format(
Expand Down
5 changes: 3 additions & 2 deletions tests/pytests/unit/modules/test_mac_brew_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,9 @@ def test_homebrew_bin(HOMEBREW_BIN):
Tests the path to the homebrew binary
"""
mock_path = MagicMock(return_value="/usr/local")
with patch.dict(mac_brew.__salt__, {"cmd.run": mock_path}):
assert mac_brew._homebrew_bin() == HOMEBREW_BIN
with patch("salt.utils.path.which", MagicMock(return_value=HOMEBREW_BIN)):
with patch.dict(mac_brew.__salt__, {"cmd.run": mock_path}):
assert mac_brew._homebrew_bin() == HOMEBREW_BIN


# 'list_pkgs' function tests: 2
Expand Down