From b8cf13137526e5fda85939a037dbecb55c7dc49c Mon Sep 17 00:00:00 2001 From: Alejandro Guirao Date: Thu, 26 Feb 2015 15:31:15 +0100 Subject: [PATCH] Bug fix: Search only for files as candidates --- lib/ansible/utils/plugins.py | 3 ++- test/units/TestUtils.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/ansible/utils/plugins.py b/lib/ansible/utils/plugins.py index 8d356fa0a71..3deef4d3c28 100644 --- a/lib/ansible/utils/plugins.py +++ b/lib/ansible/utils/plugins.py @@ -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) diff --git a/test/units/TestUtils.py b/test/units/TestUtils.py index 80c290a9be8..16dd54ab1fe 100644 --- a/test/units/TestUtils.py +++ b/test/units/TestUtils.py @@ -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)