Skip to content

Commit 57d4d33

Browse files
authored
Fixed matching patterns used for mocking (#1470)
1 parent 94a233d commit 57d4d33

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

.ansible-lint

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ exclude_paths:
88
# Mock modules or roles in order to pass ansible-playbook --syntax-check
99
mock_modules:
1010
- zuul_return
11-
- fake_namespace.fake_collection.fake_module
1211
# note the foo.bar is invalid as being neither a module or a collection
12+
- fake_namespace.fake_collection.fake_module
13+
- fake_namespace.fake_collection.fake_module.fake_submodule
1314
mock_roles:
1415
- mocked_role
1516
- author.role_name # old standalone galaxy role

src/ansiblelint/_prerun.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -226,25 +226,28 @@ def _prepare_ansible_paths() -> None:
226226

227227
def _make_module_stub(module_name: str) -> None:
228228
# a.b.c is treated a collection
229-
if re.match(r"\w+\.\w+\.\w+", module_name):
230-
namespace, collection, module_file = module_name.split(".")
231-
path = f"{ options.project_dir }/.cache/collections/ansible_collections/{ namespace }/{ collection }/plugins/modules"
229+
if re.match(r"^(\w+|\w+\.\w+\.[\.\w]+)$", module_name):
230+
parts = module_name.split(".")
231+
if len(parts) < 3:
232+
path = f"{options.project_dir}/.cache/modules"
233+
module_file = f"{options.project_dir}/.cache/modules/{module_name}.py"
234+
namespace = None
235+
collection = None
236+
else:
237+
namespace = parts[0]
238+
collection = parts[1]
239+
path = f"{ options.project_dir }/.cache/collections/ansible_collections/{ namespace }/{ collection }/plugins/modules/{ '/'.join(parts[2:-1]) }"
240+
module_file = f"{path}/{parts[-1]}.py"
232241
os.makedirs(path, exist_ok=True)
233242
_write_module_stub(
234-
filename=f"{path}/{module_file}.py",
243+
filename=module_file,
235244
name=module_file,
236245
namespace=namespace,
237246
collection=collection,
238247
)
239-
elif "." in module_name:
248+
else:
240249
_logger.error("Config error: %s is not a valid module name.", module_name)
241250
sys.exit(INVALID_CONFIG_RC)
242-
else:
243-
os.makedirs(f"{options.project_dir}/.cache/modules", exist_ok=True)
244-
_write_module_stub(
245-
filename=f"{options.project_dir}/.cache/modules/{module_name}.py",
246-
name=module_name,
247-
)
248251

249252

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

0 commit comments

Comments
 (0)