Skip to content

Commit

Permalink
Fixed matching patterns used for mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Mar 17, 2021
1 parent 94a233d commit b987eea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .ansible-lint
Expand Up @@ -7,6 +7,10 @@ exclude_paths:

# Mock modules or roles in order to pass ansible-playbook --syntax-check
mock_modules:
- a
# "a.b" is not allowed by ansible
- a.b.c # module c inside a.b collection
- a.b.c.d # module c.d inside a.b collection
- zuul_return
- fake_namespace.fake_collection.fake_module
# note the foo.bar is invalid as being neither a module or a collection
Expand Down
11 changes: 8 additions & 3 deletions src/ansiblelint/_prerun.py
Expand Up @@ -226,9 +226,14 @@ 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(".")
if re.match(r"\w+\.\w+\.[\.\w]+$", module_name):
namespace, collection, module_file = module_name.split(".", 2)
path = f"{ options.project_dir }/.cache/collections/ansible_collections/{ namespace }/{ collection }/plugins/modules"
if "." in module_file:
parts = module_file.split(".")
path += "/" + "/".join(parts[0:-1])
module_file = parts[-1]
print(f"ERROR: {path} -> {module_file}")
os.makedirs(path, exist_ok=True)
_write_module_stub(
filename=f"{path}/{module_file}.py",
Expand Down Expand Up @@ -274,7 +279,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

0 comments on commit b987eea

Please sign in to comment.