2019-10-22 18:00:08 +02:00
|
|
|
import errno
|
|
|
|
|
2019-10-21 09:52:44 +02:00
|
|
|
import pytest
|
2019-12-29 18:10:42 +01:00
|
|
|
from mock import patch
|
2019-11-17 02:42:27 +01:00
|
|
|
from pip._vendor.packaging.requirements import Requirement
|
2019-02-28 05:30:03 +01:00
|
|
|
|
2019-10-22 18:00:08 +02:00
|
|
|
from pip._internal.commands.install import (
|
|
|
|
create_env_error_message,
|
|
|
|
decide_user_install,
|
2020-07-08 00:31:54 +02:00
|
|
|
reject_location_related_install_options,
|
2019-10-22 18:00:08 +02:00
|
|
|
)
|
2020-07-08 00:29:07 +02:00
|
|
|
from pip._internal.exceptions import CommandError
|
2019-11-17 02:42:27 +01:00
|
|
|
from pip._internal.req.req_install import InstallRequirement
|
2019-02-28 05:30:03 +01:00
|
|
|
|
|
|
|
|
2019-10-20 22:48:18 +02:00
|
|
|
class TestDecideUserInstall:
|
|
|
|
@patch('site.ENABLE_USER_SITE', True)
|
|
|
|
@patch('pip._internal.commands.install.site_packages_writable')
|
|
|
|
def test_prefix_and_target(self, sp_writable):
|
|
|
|
sp_writable.return_value = False
|
|
|
|
|
|
|
|
assert decide_user_install(
|
|
|
|
use_user_site=None, prefix_path='foo'
|
|
|
|
) is False
|
|
|
|
|
|
|
|
assert decide_user_install(
|
|
|
|
use_user_site=None, target_dir='bar'
|
|
|
|
) is False
|
|
|
|
|
2019-10-21 09:52:44 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"enable_user_site,site_packages_writable,result", [
|
|
|
|
(True, True, False),
|
|
|
|
(True, False, True),
|
|
|
|
(False, True, False),
|
|
|
|
(False, False, False),
|
|
|
|
])
|
|
|
|
def test_most_cases(
|
|
|
|
self, enable_user_site, site_packages_writable, result, monkeypatch,
|
|
|
|
):
|
|
|
|
monkeypatch.setattr('site.ENABLE_USER_SITE', enable_user_site)
|
|
|
|
monkeypatch.setattr(
|
|
|
|
'pip._internal.commands.install.site_packages_writable',
|
|
|
|
lambda **kw: site_packages_writable
|
|
|
|
)
|
|
|
|
assert decide_user_install(use_user_site=None) is result
|
2019-10-22 18:00:08 +02:00
|
|
|
|
|
|
|
|
2020-07-08 00:31:54 +02:00
|
|
|
def test_rejection_for_pip_install_options():
|
2019-11-17 02:42:27 +01:00
|
|
|
install_options = ["--prefix=/hello"]
|
2020-07-08 00:29:07 +02:00
|
|
|
with pytest.raises(CommandError) as e:
|
2020-07-08 00:31:54 +02:00
|
|
|
reject_location_related_install_options([], install_options)
|
2019-11-17 02:42:27 +01:00
|
|
|
|
2020-07-08 00:29:07 +02:00
|
|
|
assert "['--prefix'] from command line" in str(e.value)
|
2019-11-17 02:42:27 +01:00
|
|
|
|
|
|
|
|
2020-07-08 00:31:54 +02:00
|
|
|
def test_rejection_for_location_requirement_options():
|
2019-11-17 02:42:27 +01:00
|
|
|
install_options = []
|
|
|
|
|
2020-02-06 17:05:11 +01:00
|
|
|
bad_named_req_options = ["--home=/wow"]
|
2019-11-17 02:42:27 +01:00
|
|
|
bad_named_req = InstallRequirement(
|
2020-02-06 17:05:11 +01:00
|
|
|
Requirement("hello"), "requirements.txt",
|
|
|
|
install_options=bad_named_req_options
|
2019-11-17 02:42:27 +01:00
|
|
|
)
|
|
|
|
|
2020-02-06 17:05:11 +01:00
|
|
|
bad_unnamed_req_options = ["--install-lib=/lib"]
|
2019-11-17 02:42:27 +01:00
|
|
|
bad_unnamed_req = InstallRequirement(
|
2020-02-06 17:05:11 +01:00
|
|
|
None, "requirements2.txt", install_options=bad_unnamed_req_options
|
2019-11-17 02:42:27 +01:00
|
|
|
)
|
|
|
|
|
2020-07-08 00:29:07 +02:00
|
|
|
with pytest.raises(CommandError) as e:
|
2020-07-08 00:31:54 +02:00
|
|
|
reject_location_related_install_options(
|
2020-07-08 00:29:07 +02:00
|
|
|
[bad_named_req, bad_unnamed_req], install_options
|
|
|
|
)
|
2019-11-17 02:42:27 +01:00
|
|
|
|
|
|
|
assert (
|
|
|
|
"['--install-lib'] from <InstallRequirement> (from requirements2.txt)"
|
2020-07-08 00:29:07 +02:00
|
|
|
in str(e.value)
|
2019-11-17 02:42:27 +01:00
|
|
|
)
|
2020-07-08 00:29:07 +02:00
|
|
|
assert "['--home'] from hello (from requirements.txt)" in str(e.value)
|
2019-11-17 02:42:27 +01:00
|
|
|
|
|
|
|
|
2019-10-22 18:00:08 +02:00
|
|
|
@pytest.mark.parametrize('error, show_traceback, using_user_site, expected', [
|
|
|
|
# show_traceback = True, using_user_site = True
|
2019-10-29 16:33:15 +01:00
|
|
|
(EnvironmentError("Illegal byte sequence"), True, True, 'Could not install'
|
|
|
|
' packages due to an EnvironmentError.\n'),
|
|
|
|
(EnvironmentError(errno.EACCES, "No file permission"), True, True, 'Could'
|
|
|
|
' not install packages due to an EnvironmentError.\nCheck the'
|
|
|
|
' permissions.\n'),
|
2019-10-22 18:00:08 +02:00
|
|
|
# show_traceback = True, using_user_site = False
|
2019-10-29 16:33:15 +01:00
|
|
|
(EnvironmentError("Illegal byte sequence"), True, False, 'Could not'
|
|
|
|
' install packages due to an EnvironmentError.\n'),
|
|
|
|
(EnvironmentError(errno.EACCES, "No file permission"), True, False, 'Could'
|
|
|
|
' not install packages due to an EnvironmentError.\nConsider using the'
|
|
|
|
' `--user` option or check the permissions.\n'),
|
2019-10-22 18:00:08 +02:00
|
|
|
# show_traceback = False, using_user_site = True
|
2019-10-29 16:33:15 +01:00
|
|
|
(EnvironmentError("Illegal byte sequence"), False, True, 'Could not'
|
|
|
|
' install packages due to an EnvironmentError: Illegal byte'
|
|
|
|
' sequence\n'),
|
|
|
|
(EnvironmentError(errno.EACCES, "No file permission"), False, True, 'Could'
|
|
|
|
' not install packages due to an EnvironmentError: [Errno 13] No file'
|
|
|
|
' permission\nCheck the permissions.\n'),
|
2019-10-22 18:00:08 +02:00
|
|
|
# show_traceback = False, using_user_site = False
|
2019-10-29 16:33:15 +01:00
|
|
|
(EnvironmentError("Illegal byte sequence"), False, False, 'Could not'
|
|
|
|
' install packages due to an EnvironmentError: Illegal byte sequence'
|
|
|
|
'\n'),
|
|
|
|
(EnvironmentError(errno.EACCES, "No file permission"), False, False,
|
|
|
|
'Could not install packages due to an EnvironmentError: [Errno 13] No'
|
|
|
|
' file permission\nConsider using the `--user` option or check the'
|
|
|
|
' permissions.\n'),
|
2019-10-22 18:00:08 +02:00
|
|
|
])
|
|
|
|
def test_create_env_error_message(
|
|
|
|
error, show_traceback, using_user_site, expected
|
|
|
|
):
|
|
|
|
msg = create_env_error_message(error, show_traceback, using_user_site)
|
|
|
|
assert msg == expected
|