summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexei Znamensky <103110+russoz@users.noreply.github.com>2021-03-02 04:40:38 +1300
committerGitHub <noreply@github.com>2021-03-01 10:40:38 -0500
commit920b68f5f25adda201549e46afdb8d6282eb1871 (patch)
treeda5c799ac9d6d5bdb4d53c7e29afe7775ea04b19
parente804fccf1c3fc94f35fac42ec8980eea0b431aa6 (diff)
downloadansible-920b68f5f25adda201549e46afdb8d6282eb1871.tar.gz
Fixed/improved regular expresssion for collection names (#73577)
* added changelog fragment * added a couple of tests to coll name validation
-rw-r--r--changelogs/fragments/73577-regex-fix.yml2
-rw-r--r--lib/ansible/utils/collection_loader/_collection_finder.py2
-rw-r--r--test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/schema.py2
-rw-r--r--test/units/utils/collection_loader/test_collection_loader.py2
4 files changed, 6 insertions, 2 deletions
diff --git a/changelogs/fragments/73577-regex-fix.yml b/changelogs/fragments/73577-regex-fix.yml
new file mode 100644
index 0000000000..9f29da1a07
--- /dev/null
+++ b/changelogs/fragments/73577-regex-fix.yml
@@ -0,0 +1,2 @@
+bugfixes:
+ - Improved/fixed regular expressions in ``validate-modules/validate_modules/schema.py`` and ``utils/collection_loader/_collection_finder.py`` (https://github.com/ansible/ansible/pull/73577).
diff --git a/lib/ansible/utils/collection_loader/_collection_finder.py b/lib/ansible/utils/collection_loader/_collection_finder.py
index be9c07e264..393ddbee7b 100644
--- a/lib/ansible/utils/collection_loader/_collection_finder.py
+++ b/lib/ansible/utils/collection_loader/_collection_finder.py
@@ -701,7 +701,7 @@ class AnsibleCollectionRef:
# FIXME: tighten this up to match Python identifier reqs, etc
VALID_SUBDIRS_RE = re.compile(to_text(r'^\w+(\.\w+)*$'))
- VALID_FQCR_RE = re.compile(to_text(r'^\w+\.\w+\.\w+(\.\w+)*$')) # can have 0-N included subdirs as well
+ VALID_FQCR_RE = re.compile(to_text(r'^\w+(\.\w+){2,}$')) # can have 0-N included subdirs as well
def __init__(self, collection_name, subdirs, resource, ref_type):
"""
diff --git a/test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/schema.py b/test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/schema.py
index 9e4cd42747..ecf63557f7 100644
--- a/test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/schema.py
+++ b/test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/schema.py
@@ -45,7 +45,7 @@ def isodate(v, error_code=None):
return v
-COLLECTION_NAME_RE = re.compile('^([^.]+.[^.]+)$')
+COLLECTION_NAME_RE = re.compile(r'^([^.]+(\.[^.]+)+)$')
def collection_name(v, error_code=None):
diff --git a/test/units/utils/collection_loader/test_collection_loader.py b/test/units/utils/collection_loader/test_collection_loader.py
index c8187676e6..425f770c77 100644
--- a/test/units/utils/collection_loader/test_collection_loader.py
+++ b/test/units/utils/collection_loader/test_collection_loader.py
@@ -722,6 +722,7 @@ def test_fqcr_parsing_valid(ref, ref_type, expected_collection,
('fqcn', 'expected'),
(
('ns1.coll2', True),
+ ('ns1#coll2', False),
('def.coll3', False),
('ns4.return', False),
('assert.this', False),
@@ -742,6 +743,7 @@ def test_fqcn_validation(fqcn, expected):
[
('no_dots_at_all_action', 'action', ValueError, 'is not a valid collection reference'),
('no_nscoll.myaction', 'action', ValueError, 'is not a valid collection reference'),
+ ('no_nscoll%myaction', 'action', ValueError, 'is not a valid collection reference'),
('ns.coll.myaction', 'bogus', ValueError, 'invalid collection ref_type'),
])
def test_fqcr_parsing_invalid(ref, ref_type, expected_error_type, expected_error_expression):