Merge pull request #10346 from lekum/find_plugin_bugfix

Bug fix: Search only for files as candidates
This commit is contained in:
Toshio Kuratomi 2015-02-26 15:38:45 -08:00
commit 68c99a12b3
2 changed files with 31 additions and 1 deletions

View file

@ -176,7 +176,8 @@ class PluginLoader(object):
found = None
for path in [p for p in self._get_paths() if p not in self._searched_paths]:
if os.path.isdir(path):
for potential_file in os.listdir(path):
for potential_file in (f for f in os.listdir(path)
if os.path.isfile(os.path.join(path, f))):
for suffix in suffixes:
if potential_file.endswith(suffix):
full_path = os.path.join(path, potential_file)

View file

@ -11,8 +11,11 @@ import passlib.hash
import string
import StringIO
import copy
import tempfile
import shutil
from nose.plugins.skip import SkipTest
from mock import patch
import ansible.utils
import ansible.errors
@ -914,3 +917,29 @@ class TestUtils(unittest.TestCase):
for (role, result) in tests:
self.assertEqual(ansible.utils.role_yaml_parse(role), result)
@patch('ansible.utils.plugins.module_finder._get_paths')
def test_find_plugin(self, mock_get_paths):
tmp_path = tempfile.mkdtemp()
mock_get_paths.return_value = [tmp_path,]
right_module_1 = 'module.py'
right_module_2 = 'module_without_extension'
wrong_module_1 = 'folder'
wrong_module_2 = 'inexistent'
path_right_module_1 = os.path.join(tmp_path, right_module_1)
path_right_module_2 = os.path.join(tmp_path, right_module_2)
path_wrong_module_1 = os.path.join(tmp_path, wrong_module_1)
open(path_right_module_1, 'w').close()
open(path_right_module_2, 'w').close()
os.mkdir(path_wrong_module_1)
self.assertEqual(ansible.utils.plugins.module_finder.find_plugin(right_module_1),
path_right_module_1)
self.assertEqual(ansible.utils.plugins.module_finder.find_plugin(right_module_2),
path_right_module_2)
self.assertEqual(ansible.utils.plugins.module_finder.find_plugin(wrong_module_1),
None)
self.assertEqual(ansible.utils.plugins.module_finder.find_plugin(wrong_module_2),
None)
shutil.rmtree(tmp_path)