From ea0d19f41649076a4a3ec119e9bedae7702951cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20=C3=81lvaro?= Date: Thu, 14 Dec 2023 21:44:39 +0100 Subject: [PATCH] test(mac_brew_pkg): Update tests for Apple Silicon --- salt/modules/mac_brew_pkg.py | 44 +++++++++---------- .../pytests/unit/modules/test_mac_brew_pkg.py | 10 ++--- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/salt/modules/mac_brew_pkg.py b/salt/modules/mac_brew_pkg.py index 9166b0f5c88..ff20b3f483f 100644 --- a/salt/modules/mac_brew_pkg.py +++ b/salt/modules/mac_brew_pkg.py @@ -36,11 +36,11 @@ __virtualname__ = "pkg" def __virtual__(): """ - Confine this module to Mac OS with Homebrew. + Confine this module to macOS with Homebrew. """ if __grains__["os"] != "MacOS": return False, "brew module is macos specific" - if not _homebrew_bin(quiet=False): + if not _homebrew_bin(): return False, "The 'brew' binary was not found" return __virtualname__ @@ -107,22 +107,21 @@ def _homebrew_os_bin(): Fetch PATH binary brew full path eg: /usr/local/bin/brew (symbolic link) """ - # Add "/opt/homebrew" temporary to the PATH for Apple Silicon if - # the PATH does not include "/opt/homebrew" - original_path = None - current_path = os.environ.get("PATH", "") - homebrew_path = "/opt/homebrew/bin" - if homebrew_path not in current_path.split(os.path.pathsep): - original_path = current_path - extended_path = os.path.pathsep.join([original_path, homebrew_path]) - os.environ["PATH"] = extended_path.lstrip(os.path.pathsep) + 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" + current_path = original_path or "" + homebrew_path = "/opt/homebrew/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) - # Search for the brew executable in the current PATH - brew = salt.utils.path.which("brew") - - # Restore the original PATH if needed - if original_path is not None: - if original_path == "": + # Search for the brew executable in the current PATH + brew = salt.utils.path.which("brew") + finally: + # Restore original PATH + if original_path is None: del os.environ["PATH"] else: os.environ["PATH"] = original_path @@ -130,12 +129,9 @@ def _homebrew_os_bin(): return brew -def _homebrew_bin(quiet=False): +def _homebrew_bin(): """ Returns the full path to the homebrew binary in the homebrew installation folder - - quiet - When ``True``, does not log warnings when the homebrew prefix cannot be found. """ ret = homebrew_prefix() if ret is not None: @@ -150,7 +146,7 @@ def _call_brew(*cmd, failhard=True): """ Calls the brew command with the user account of brew """ - brew_exec = _homebrew_bin(quiet=True) + brew_exec = _homebrew_bin() user = __salt__["file.get_user"](brew_exec) runas = user if user != __opts__["user"] else None @@ -195,13 +191,13 @@ def homebrew_prefix(): salt '*' pkg.homebrew_prefix """ - # Try HOMEBREW_PREFIX env variable + # If HOMEBREW_PREFIX env variable is present, use it env_homebrew_prefix = "HOMEBREW_PREFIX" if env_homebrew_prefix in os.environ: log.debug(f"{env_homebrew_prefix} is set. Using it for homebrew prefix.") return os.environ[env_homebrew_prefix] - # Try brew --prefix + # Try brew --prefix otherwise try: log.debug("Trying to find homebrew prefix by running 'brew --prefix'") diff --git a/tests/pytests/unit/modules/test_mac_brew_pkg.py b/tests/pytests/unit/modules/test_mac_brew_pkg.py index 3a1e007aac7..e824ba175df 100644 --- a/tests/pytests/unit/modules/test_mac_brew_pkg.py +++ b/tests/pytests/unit/modules/test_mac_brew_pkg.py @@ -481,7 +481,7 @@ def test_homebrew_prefix_command(HOMEBREW_PREFIX, HOMEBREW_BIN): def test_homebrew_prefix_returns_none(): """ - Tets that homebrew_prefix returns None when + Tests that homebrew_prefix returns None when all attempts fail. """ @@ -489,7 +489,7 @@ def test_homebrew_prefix_returns_none(): if "HOMEBREW_PREFIX" in mock_env: del mock_env["HOMEBREW_PREFIX"] - with patch.dict(os.environ, mock_env): + with patch.dict(os.environ, mock_env, clear=True): with patch( "salt.modules.mac_brew_pkg._homebrew_os_bin", MagicMock(return_value=None) ): @@ -498,7 +498,7 @@ def test_homebrew_prefix_returns_none(): def test_homebrew_prefix_returns_none_even_with_execution_errors(): """ - Tets that homebrew_prefix returns None when + Tests that homebrew_prefix returns None when all attempts fail even with command execution errors. """ @@ -506,7 +506,7 @@ def test_homebrew_prefix_returns_none_even_with_execution_errors(): if "HOMEBREW_PREFIX" in mock_env: del mock_env["HOMEBREW_PREFIX"] - with patch.dict(os.environ, mock_env): + with patch.dict(os.environ, mock_env, clear=True): with patch( "salt.modules.cmdmod.run", MagicMock(side_effect=CommandExecutionError) ), patch( @@ -523,7 +523,7 @@ def test_homebrew_os_bin_fallback_apple_silicon(): """ Test the path to the homebrew executable for Apple Silicon. - This test checks that even if the PATH does not contains + This test checks that even if the PATH does not contain the default Homebrew's prefix for the Apple Silicon architecture, it is appended. """