Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed matching patterns used for mocking #1470

Merged
merged 1 commit into from Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .ansible-lint
Expand Up @@ -8,8 +8,9 @@ exclude_paths:
# Mock modules or roles in order to pass ansible-playbook --syntax-check
mock_modules:
- zuul_return
- fake_namespace.fake_collection.fake_module
# note the foo.bar is invalid as being neither a module or a collection
- fake_namespace.fake_collection.fake_module
- fake_namespace.fake_collection.fake_module.fake_submodule
ssbarnea marked this conversation as resolved.
Show resolved Hide resolved
mock_roles:
- mocked_role
- author.role_name # old standalone galaxy role
Expand Down
27 changes: 15 additions & 12 deletions src/ansiblelint/_prerun.py
Expand Up @@ -226,25 +226,28 @@ def _prepare_ansible_paths() -> None:

def _make_module_stub(module_name: str) -> None:
# a.b.c is treated a collection
if re.match(r"\w+\.\w+\.\w+", module_name):
namespace, collection, module_file = module_name.split(".")
path = f"{ options.project_dir }/.cache/collections/ansible_collections/{ namespace }/{ collection }/plugins/modules"
if re.match(r"^(\w+|\w+\.\w+\.[\.\w]+)$", module_name):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if re.match(r"^(\w+|\w+\.\w+\.[\.\w]+)$", module_name):
if re.match(r"^(\w+|\w+\.\w+(\.\w+)+)$", module_name):

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my tests done on https://regex101.com/ mine example worked as intended but the one with nested matches failed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly did fail? I tried several variants, and they all worked.

parts = module_name.split(".")
if len(parts) < 3:
path = f"{options.project_dir}/.cache/modules"
module_file = f"{options.project_dir}/.cache/modules/{module_name}.py"
namespace = None
collection = None
else:
namespace = parts[0]
collection = parts[1]
path = f"{ options.project_dir }/.cache/collections/ansible_collections/{ namespace }/{ collection }/plugins/modules/{ '/'.join(parts[2:-1]) }"
module_file = f"{path}/{parts[-1]}.py"
os.makedirs(path, exist_ok=True)
_write_module_stub(
filename=f"{path}/{module_file}.py",
filename=module_file,
name=module_file,
namespace=namespace,
collection=collection,
)
elif "." in module_name:
else:
_logger.error("Config error: %s is not a valid module name.", module_name)
sys.exit(INVALID_CONFIG_RC)
else:
os.makedirs(f"{options.project_dir}/.cache/modules", exist_ok=True)
_write_module_stub(
filename=f"{options.project_dir}/.cache/modules/{module_name}.py",
name=module_name,
)


def _write_module_stub(
Expand Down Expand Up @@ -274,7 +277,7 @@ def _update_env(varname: str, value: List[str], default: str = "") -> None:
def _perform_mockings() -> None:
"""Mock modules and roles."""
for role_name in options.mock_roles:
if re.match(r"\w+\.\w+\.\w+", role_name):
if re.match(r"\w+\.\w+\.\w+$", role_name):
namespace, collection, role_dir = role_name.split(".")
path = f".cache/collections/ansible_collections/{ namespace }/{ collection }/roles/{ role_dir }/"
else:
Expand Down