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
pull/31491/head
Pilou 7 years ago committed by Matt Clay
parent 5e9384300d
commit 1c9bffe248

@ -286,7 +286,7 @@ def _get_pip(module, env=None, executable=None):
else: else:
# For-else: Means that we did not break out of the loop # For-else: Means that we did not break out of the loop
# (therefore, that pip was not found) # (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)) ' needs to be installed.' % ', '.join(candidate_pip_basenames))
else: else:
# If we're using a virtualenv we must use the pip from the # If we're using a virtualenv we must use the pip from the

@ -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()

@ -7,7 +7,7 @@ from ansible.module_utils.six.moves import xmlrpc_client
from ansible.module_utils._text import to_bytes from ansible.module_utils._text import to_bytes
from ansible.modules.packaging.os import rhn_channel from ansible.modules.packaging.os import rhn_channel
from .rhn_utils import (set_module_args, AnsibleExitJson, AnsibleFailJson, from ..utils import (set_module_args, AnsibleExitJson, AnsibleFailJson,
exit_json, fail_json, get_method_name, mock_request) exit_json, fail_json, get_method_name, mock_request)

@ -7,7 +7,7 @@ from ansible.module_utils._text import to_native
from ansible.module_utils.six.moves import xmlrpc_client from ansible.module_utils.six.moves import xmlrpc_client
from ansible.modules.packaging.os import rhn_register from ansible.modules.packaging.os import rhn_register
from .rhn_utils import (set_module_args, AnsibleExitJson, AnsibleFailJson, from ..utils import (set_module_args, AnsibleExitJson, AnsibleFailJson,
exit_json, fail_json, get_method_name, mock_request) exit_json, fail_json, get_method_name, mock_request)

Loading…
Cancel
Save