Skip to content

Commit

Permalink
Resolve homebrew and homebrew_cask package name validation issues (#1038
Browse files Browse the repository at this point in the history
)

Add basic regression tests
Add changelog
Rename _create_regex_group to better suit function
Fix '-' use in Homebrew validation
  • Loading branch information
MichaelWasher authored Oct 27, 2020
1 parent 4842f67 commit 4c379bd
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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).
14 changes: 7 additions & 7 deletions plugins/modules/packaging/os/homebrew.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']'
Expand All @@ -186,30 +186,30 @@ class Homebrew(object):
: # colons
{sep} # the OS-specific path separator
. # dots
- # dashes
\- # dashes
'''.format(sep=os.path.sep)

VALID_BREW_PATH_CHARS = r'''
\w # alphanumeric characters (i.e., [a-zA-Z0-9_])
\s # spaces
{sep} # the OS-specific path separator
. # dots
- # dashes
\- # dashes
'''.format(sep=os.path.sep)

VALID_PACKAGE_CHARS = r'''
\w # alphanumeric characters (i.e., [a-zA-Z0-9_])
. # 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 -------------------------------------------- {{{
Expand Down
14 changes: 7 additions & 7 deletions plugins/modules/packaging/os/homebrew_cask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']'
Expand All @@ -170,28 +170,28 @@ class HomebrewCask(object):
: # colons
{sep} # the OS-specific path separator
. # dots
- # dashes
\- # dashes
'''.format(sep=os.path.sep)

VALID_BREW_PATH_CHARS = r'''
\w # alphanumeric characters (i.e., [a-zA-Z0-9_])
\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 -------------------------------------------- {{{
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/plugins/modules/packaging/os/test_homebrew.py
Original file line number Diff line number Diff line change
@@ -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))
21 changes: 21 additions & 0 deletions tests/unit/plugins/modules/packaging/os/test_homebrew_cask.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 4c379bd

Please sign in to comment.