diff --git a/changelogs/fragments/homebrew_cask.yml b/changelogs/fragments/homebrew_cask.yml new file mode 100644 index 0000000000..623a982455 --- /dev/null +++ b/changelogs/fragments/homebrew_cask.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - homebrew_cask - handle unusual brew version strings (https://github.com/ansible-collections/community.general/issues/8432, https://github.com/ansible-collections/community.general/pull/9881). diff --git a/plugins/modules/homebrew_cask.py b/plugins/modules/homebrew_cask.py index d69fd266a2..7455a61d69 100644 --- a/plugins/modules/homebrew_cask.py +++ b/plugins/modules/homebrew_cask.py @@ -425,10 +425,7 @@ class HomebrewCask(object): cmd = base_opts + [self.current_cask] rc, out, err = self.module.run_command(cmd) - if rc == 0: - return True - else: - return False + return rc == 0 def _get_brew_version(self): if self.brew_version: @@ -436,11 +433,13 @@ class HomebrewCask(object): cmd = [self.brew_path, '--version'] - rc, out, err = self.module.run_command(cmd, check_rc=True) + dummy, out, dummy = self.module.run_command(cmd, check_rc=True) - # get version string from first line of "brew --version" output - version = out.split('\n')[0].split(' ')[1] - self.brew_version = version + pattern = r"Homebrew (.*)(\d+\.\d+\.\d+)(-dirty)?" + rematch = re.search(pattern, out) + if not rematch: + self.module.fail_json(msg="Failed to match regex to get brew version", stdout=out) + self.brew_version = rematch.groups()[1] return self.brew_version def _brew_cask_command_is_deprecated(self): diff --git a/tests/unit/plugins/modules/test_homebrew_cask.py b/tests/unit/plugins/modules/test_homebrew_cask.py index 6fcc06d976..85f5920912 100644 --- a/tests/unit/plugins/modules/test_homebrew_cask.py +++ b/tests/unit/plugins/modules/test_homebrew_cask.py @@ -6,18 +6,33 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -from ansible_collections.community.general.tests.unit.compat import unittest -from ansible_collections.community.general.plugins.modules.homebrew_cask import HomebrewCask +from ansible_collections.community.general.plugins.modules.homebrew_cask import ( + HomebrewCask, +) +from ansible_collections.community.general.plugins.module_utils.homebrew import ( + HomebrewValidate, +) -class TestHomebrewCaskModule(unittest.TestCase): +def test_valid_cask_names(): + brew_cask_names = ["visual-studio-code", "firefox"] + for name in brew_cask_names: + assert HomebrewCask.valid_cask(name) - def setUp(self): - self.brew_cask_names = [ - 'visual-studio-code', - 'firefox' - ] - def test_valid_cask_names(self): - for name in self.brew_cask_names: - self.assertTrue(HomebrewCask.valid_cask(name)) +def test_homebrew_version(mocker): + brew_versions = [ + "Homebrew 4.1.0", + "Homebrew >=4.1.0 (shallow or no git repository)", + "Homebrew 4.1.0-dirty", + ] + module = mocker.Mock() + + mocker.patch.object(HomebrewCask, "valid_module", return_value=True) + mocker.patch.object(HomebrewValidate, "valid_path", return_value=True) + mocker.patch.object(HomebrewValidate, "valid_brew_path", return_value=True) + + homebrewcask = HomebrewCask(module=module) + for version in brew_versions: + module.run_command.return_value = (0, version, "") + assert homebrewcask._get_brew_version() == "4.1.0"