From 1c9bffe248b1e7f520663f33f9a7d1f08a3b9493 Mon Sep 17 00:00:00 2001 From: Pilou Date: Mon, 9 Oct 2017 19:01:21 +0200 Subject: [PATCH] pip module: fix TypeError (#31395) * pip: add test: an error occurs when pip not found * pip: fix TypeError exception when pip executable isn't found --- lib/ansible/modules/packaging/language/pip.py | 2 +- .../modules/packaging/language/__init__.py | 0 .../modules/packaging/language/test_pip.py | 26 +++++++++++++++++++ .../modules/packaging/os/test_rhn_channel.py | 4 +-- .../modules/packaging/os/test_rhn_register.py | 4 +-- .../packaging/{os/rhn_utils.py => utils.py} | 0 6 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 test/units/modules/packaging/language/__init__.py create mode 100644 test/units/modules/packaging/language/test_pip.py rename test/units/modules/packaging/{os/rhn_utils.py => utils.py} (100%) diff --git a/lib/ansible/modules/packaging/language/pip.py b/lib/ansible/modules/packaging/language/pip.py index ec96f25b36..ae66cb23f0 100644 --- a/lib/ansible/modules/packaging/language/pip.py +++ b/lib/ansible/modules/packaging/language/pip.py @@ -286,7 +286,7 @@ def _get_pip(module, env=None, executable=None): else: # For-else: Means that we did not break out of the loop # (therefore, that pip was not found) - module.fail_json(msg='Unable to find any of %s to use. pip' + + module.fail_json(msg='Unable to find any of %s to use. pip' ' needs to be installed.' % ', '.join(candidate_pip_basenames)) else: # If we're using a virtualenv we must use the pip from the diff --git a/test/units/modules/packaging/language/__init__.py b/test/units/modules/packaging/language/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/units/modules/packaging/language/test_pip.py b/test/units/modules/packaging/language/test_pip.py new file mode 100644 index 0000000000..3a4146ab62 --- /dev/null +++ b/test/units/modules/packaging/language/test_pip.py @@ -0,0 +1,26 @@ +import json + +from ansible.compat.tests import unittest +from ansible.compat.tests.mock import patch +from ansible.module_utils import basic + +from ansible.modules.packaging.language import pip + +from ..utils import (set_module_args, AnsibleFailJson, exit_json, fail_json) + + +class TestPip(unittest.TestCase): + + def setUp(self): + self.mock_ansible_module = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json) + self.mock_ansible_module.start() + self.addCleanup(self.mock_ansible_module.stop) + + @patch.object(basic.AnsibleModule, 'get_bin_path') + def test_failure_when_pip_absent(self, mock_get_bin_path): + + mock_get_bin_path.return_value = None + + with self.assertRaises(AnsibleFailJson): + set_module_args({'name': 'six'}) + pip.main() diff --git a/test/units/modules/packaging/os/test_rhn_channel.py b/test/units/modules/packaging/os/test_rhn_channel.py index ad459d29bd..c89eb39ca4 100644 --- a/test/units/modules/packaging/os/test_rhn_channel.py +++ b/test/units/modules/packaging/os/test_rhn_channel.py @@ -7,8 +7,8 @@ from ansible.module_utils.six.moves import xmlrpc_client from ansible.module_utils._text import to_bytes from ansible.modules.packaging.os import rhn_channel -from .rhn_utils import (set_module_args, AnsibleExitJson, AnsibleFailJson, - exit_json, fail_json, get_method_name, mock_request) +from ..utils import (set_module_args, AnsibleExitJson, AnsibleFailJson, + exit_json, fail_json, get_method_name, mock_request) class TestRhnChannel(unittest.TestCase): diff --git a/test/units/modules/packaging/os/test_rhn_register.py b/test/units/modules/packaging/os/test_rhn_register.py index d3f92f0fe4..647dd68f57 100644 --- a/test/units/modules/packaging/os/test_rhn_register.py +++ b/test/units/modules/packaging/os/test_rhn_register.py @@ -7,8 +7,8 @@ from ansible.module_utils._text import to_native from ansible.module_utils.six.moves import xmlrpc_client from ansible.modules.packaging.os import rhn_register -from .rhn_utils import (set_module_args, AnsibleExitJson, AnsibleFailJson, - exit_json, fail_json, get_method_name, mock_request) +from ..utils import (set_module_args, AnsibleExitJson, AnsibleFailJson, + exit_json, fail_json, get_method_name, mock_request) SYSTEMID = """ diff --git a/test/units/modules/packaging/os/rhn_utils.py b/test/units/modules/packaging/utils.py similarity index 100% rename from test/units/modules/packaging/os/rhn_utils.py rename to test/units/modules/packaging/utils.py