Skip to content

Commit ddfa2d1

Browse files
authored
Merge pull request #50 from cipherboy/fix-external-module-loading
Fix has_module for non-builtin modules
2 parents 4161cd6 + 0504fd1 commit ddfa2d1

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

pytest_ansible/module_dispatcher/v1.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ class ModuleDispatcherV1(BaseModuleDispatcher):
2222
required_kwargs = ('inventory', 'inventory_manager', 'host_pattern')
2323

2424
def has_module(self, name):
25+
# Make sure we parse module_path and pass it to the loader,
26+
# otherwise, only built-in modules will work.
27+
if 'module_path' in self.options:
28+
paths = self.options['module_path']
29+
if isinstance(paths, (list, tuple, set)):
30+
for path in paths:
31+
ansible.utils.module_finder.add_directory(path)
32+
else:
33+
ansible.utils.module_finder.add_directory(paths)
34+
2535
return ansible.utils.module_finder.has_plugin(name)
2636

2737
def _run(self, *module_args, **complex_args):

pytest_ansible/module_dispatcher/v2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ class ModuleDispatcherV2(BaseModuleDispatcher):
4949
required_kwargs = ('inventory', 'inventory_manager', 'variable_manager', 'host_pattern', 'loader')
5050

5151
def has_module(self, name):
52+
# Make sure we parse module_path and pass it to the loader,
53+
# otherwise, only built-in modules will work.
54+
if 'module_path' in self.options:
55+
paths = self.options['module_path']
56+
if isinstance(paths, (list, tuple, set)):
57+
for path in paths:
58+
ansible.plugins.module_loader.add_directory(path)
59+
else:
60+
ansible.plugins.module_loader.add_directory(paths)
61+
5262
return ansible.plugins.module_loader.has_plugin(name)
5363
# return module_loader.has_plugin(name)
5464

pytest_ansible/module_dispatcher/v24.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ class ModuleDispatcherV24(ModuleDispatcherV2):
5151
required_kwargs = ('inventory', 'inventory_manager', 'variable_manager', 'host_pattern', 'loader')
5252

5353
def has_module(self, name):
54+
# Make sure we parse module_path and pass it to the loader,
55+
# otherwise, only built-in modules will work.
56+
if 'module_path' in self.options:
57+
paths = self.options['module_path']
58+
if isinstance(paths, (list, tuple, set)):
59+
for path in paths:
60+
module_loader.add_directory(path)
61+
else:
62+
module_loader.add_directory(paths)
5463

5564
return module_loader.has_plugin(name)
5665

pytest_ansible/module_dispatcher/v28.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ class ModuleDispatcherV28(ModuleDispatcherV2):
5353
required_kwargs = ('inventory', 'inventory_manager', 'variable_manager', 'host_pattern', 'loader')
5454

5555
def has_module(self, name):
56+
# Make sure we parse module_path and pass it to the loader,
57+
# otherwise, only built-in modules will work.
58+
if 'module_path' in self.options:
59+
paths = self.options['module_path']
60+
if isinstance(paths, (list, tuple, set)):
61+
for path in paths:
62+
module_loader.add_directory(path)
63+
else:
64+
module_loader.add_directory(paths)
5665

5766
return module_loader.has_plugin(name)
5867

0 commit comments

Comments
 (0)