From 4c379bd3b2147710c3e7a1d900cddf47a6b7cd42 Mon Sep 17 00:00:00 2001 From: MichaelWasher Date: Wed, 28 Oct 2020 10:46:49 +1300 Subject: [PATCH] Resolve homebrew and homebrew_cask package name validation issues (#1038) Add basic regression tests Add changelog Rename _create_regex_group to better suit function Fix '-' use in Homebrew validation --- ...-and-homebrew-cask-package-validation.yaml | 5 +++++ plugins/modules/packaging/os/homebrew.py | 14 ++++++------ plugins/modules/packaging/os/homebrew_cask.py | 14 ++++++------ .../modules/packaging/os/test_homebrew.py | 22 +++++++++++++++++++ .../packaging/os/test_homebrew_cask.py | 21 ++++++++++++++++++ 5 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 changelogs/fragments/1038-fix-homebrew-and-homebrew-cask-package-validation.yaml create mode 100644 tests/unit/plugins/modules/packaging/os/test_homebrew.py create mode 100644 tests/unit/plugins/modules/packaging/os/test_homebrew_cask.py diff --git a/changelogs/fragments/1038-fix-homebrew-and-homebrew-cask-package-validation.yaml b/changelogs/fragments/1038-fix-homebrew-and-homebrew-cask-package-validation.yaml new file mode 100644 index 0000000000..f3ca271d1e --- /dev/null +++ b/changelogs/fragments/1038-fix-homebrew-and-homebrew-cask-package-validation.yaml @@ -0,0 +1,5 @@ +bugfixes: +- homebrew - fix package name validation for packages containing hypen ``-`` + (https://github.com/ansible-collections/community.general/issues/1037). +- homebrew_cask - fix package name validation for casks containing hypen ``-`` + (https://github.com/ansible-collections/community.general/issues/1037). diff --git a/plugins/modules/packaging/os/homebrew.py b/plugins/modules/packaging/os/homebrew.py index e765288d7a..d13a27d60d 100644 --- a/plugins/modules/packaging/os/homebrew.py +++ b/plugins/modules/packaging/os/homebrew.py @@ -168,7 +168,7 @@ class HomebrewException(Exception): # utils ------------------------------------------------------------------- {{{ -def _create_regex_group(s): +def _create_regex_group_complement(s): lines = (line.strip() for line in s.split('\n') if line.strip()) chars = filter(None, (line.split('#')[0].strip() for line in lines)) group = r'[^' + r''.join(chars) + r']' @@ -186,7 +186,7 @@ class Homebrew(object): : # colons {sep} # the OS-specific path separator . # dots - - # dashes + \- # dashes '''.format(sep=os.path.sep) VALID_BREW_PATH_CHARS = r''' @@ -194,7 +194,7 @@ class Homebrew(object): \s # spaces {sep} # the OS-specific path separator . # dots - - # dashes + \- # dashes '''.format(sep=os.path.sep) VALID_PACKAGE_CHARS = r''' @@ -202,14 +202,14 @@ class Homebrew(object): . # dots / # slash (for taps) \+ # plusses - - # dashes + \- # dashes : # colons (for URLs) @ # at-sign ''' - INVALID_PATH_REGEX = _create_regex_group(VALID_PATH_CHARS) - INVALID_BREW_PATH_REGEX = _create_regex_group(VALID_BREW_PATH_CHARS) - INVALID_PACKAGE_REGEX = _create_regex_group(VALID_PACKAGE_CHARS) + INVALID_PATH_REGEX = _create_regex_group_complement(VALID_PATH_CHARS) + INVALID_BREW_PATH_REGEX = _create_regex_group_complement(VALID_BREW_PATH_CHARS) + INVALID_PACKAGE_REGEX = _create_regex_group_complement(VALID_PACKAGE_CHARS) # /class regexes ----------------------------------------------- }}} # class validations -------------------------------------------- {{{ diff --git a/plugins/modules/packaging/os/homebrew_cask.py b/plugins/modules/packaging/os/homebrew_cask.py index d23fdc56fa..c7c71db549 100644 --- a/plugins/modules/packaging/os/homebrew_cask.py +++ b/plugins/modules/packaging/os/homebrew_cask.py @@ -152,7 +152,7 @@ class HomebrewCaskException(Exception): # utils ------------------------------------------------------------------- {{{ -def _create_regex_group(s): +def _create_regex_group_complement(s): lines = (line.strip() for line in s.split('\n') if line.strip()) chars = filter(None, (line.split('#')[0].strip() for line in lines)) group = r'[^' + r''.join(chars) + r']' @@ -170,7 +170,7 @@ class HomebrewCask(object): : # colons {sep} # the OS-specific path separator . # dots - - # dashes + \- # dashes '''.format(sep=os.path.sep) VALID_BREW_PATH_CHARS = r''' @@ -178,20 +178,20 @@ class HomebrewCask(object): \s # spaces {sep} # the OS-specific path separator . # dots - - # dashes + \- # dashes '''.format(sep=os.path.sep) VALID_CASK_CHARS = r''' \w # alphanumeric characters (i.e., [a-zA-Z0-9_]) . # dots / # slash (for taps) - - # dashes + \- # dashes @ # at symbol ''' - INVALID_PATH_REGEX = _create_regex_group(VALID_PATH_CHARS) - INVALID_BREW_PATH_REGEX = _create_regex_group(VALID_BREW_PATH_CHARS) - INVALID_CASK_REGEX = _create_regex_group(VALID_CASK_CHARS) + INVALID_PATH_REGEX = _create_regex_group_complement(VALID_PATH_CHARS) + INVALID_BREW_PATH_REGEX = _create_regex_group_complement(VALID_BREW_PATH_CHARS) + INVALID_CASK_REGEX = _create_regex_group_complement(VALID_CASK_CHARS) # /class regexes ----------------------------------------------- }}} # class validations -------------------------------------------- {{{ diff --git a/tests/unit/plugins/modules/packaging/os/test_homebrew.py b/tests/unit/plugins/modules/packaging/os/test_homebrew.py new file mode 100644 index 0000000000..c2297fd47d --- /dev/null +++ b/tests/unit/plugins/modules/packaging/os/test_homebrew.py @@ -0,0 +1,22 @@ +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +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.packaging.os.homebrew import Homebrew + + +class TestHomebrewModule(unittest.TestCase): + + def setUp(self): + self.brew_app_names = [ + 'git-ssh', + 'awscli@1', + 'bash' + ] + + def test_valid_package_names(self): + for name in self.brew_app_names: + self.assertTrue(Homebrew.valid_package(name)) diff --git a/tests/unit/plugins/modules/packaging/os/test_homebrew_cask.py b/tests/unit/plugins/modules/packaging/os/test_homebrew_cask.py new file mode 100644 index 0000000000..57cf225a94 --- /dev/null +++ b/tests/unit/plugins/modules/packaging/os/test_homebrew_cask.py @@ -0,0 +1,21 @@ +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +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.packaging.os.homebrew_cask import HomebrewCask + + +class TestHomebrewCaskModule(unittest.TestCase): + + 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))