[PR #9881/e5eac9fe backport][stable-9] homebrew_cask: Handle unusual brew version strings (#9888)

homebrew_cask: Handle unusual brew version strings (#9881)

* Use regex to parse unusual brew version strings

Fixes: #8432

Signed-off-by: Abhijeet Kasurde <Akasurde@redhat.com>
(cherry picked from commit e5eac9fed1)

Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
patchback[bot] 2025-03-15 09:22:42 +01:00 committed by GitHub
parent 0dd960d8ab
commit 3bceabce93
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 19 deletions

View file

@ -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).

View file

@ -425,10 +425,7 @@ class HomebrewCask(object):
cmd = base_opts + [self.current_cask] cmd = base_opts + [self.current_cask]
rc, out, err = self.module.run_command(cmd) rc, out, err = self.module.run_command(cmd)
if rc == 0: return rc == 0
return True
else:
return False
def _get_brew_version(self): def _get_brew_version(self):
if self.brew_version: if self.brew_version:
@ -436,11 +433,13 @@ class HomebrewCask(object):
cmd = [self.brew_path, '--version'] 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 pattern = r"Homebrew (.*)(\d+\.\d+\.\d+)(-dirty)?"
version = out.split('\n')[0].split(' ')[1] rematch = re.search(pattern, out)
self.brew_version = version 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 return self.brew_version
def _brew_cask_command_is_deprecated(self): def _brew_cask_command_is_deprecated(self):

View file

@ -6,18 +6,33 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
from ansible_collections.community.general.tests.unit.compat import unittest from ansible_collections.community.general.plugins.modules.homebrew_cask import (
from ansible_collections.community.general.plugins.modules.homebrew_cask import HomebrewCask 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): def test_homebrew_version(mocker):
for name in self.brew_cask_names: brew_versions = [
self.assertTrue(HomebrewCask.valid_cask(name)) "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"