|
1 | 1 | import errno
|
| 2 | +from itertools import product |
2 | 3 |
|
3 | 4 | import pytest
|
4 | 5 | from mock import patch
|
|
9 | 10 | decide_user_install,
|
10 | 11 | warn_deprecated_install_options,
|
11 | 12 | )
|
| 13 | +from pip._internal.exceptions import CommandError, InstallationError |
12 | 14 | from pip._internal.req.req_install import InstallRequirement
|
13 | 15 |
|
| 16 | +ENABLE_USER_SITE = 'site.ENABLE_USER_SITE' |
| 17 | +WRITABLE = 'pip._internal.commands.install.test_writable_dir' |
| 18 | +ISDIR = 'pip._internal.commands.install.os.path.isdir' |
| 19 | +EXISTS = 'pip._internal.commands.install.os.path.exists' |
14 | 20 |
|
| 21 | + |
| 22 | +def false(*args, **kwargs): |
| 23 | + """Return False.""" |
| 24 | + return False |
| 25 | + |
| 26 | + |
| 27 | +def true(*args, **kwargs): |
| 28 | + """Return True.""" |
| 29 | + return True |
| 30 | + |
| 31 | + |
| 32 | +# virtualenv_no_global is tested by functional test |
| 33 | +@patch('pip._internal.commands.install.virtualenv_no_global', false) |
15 | 34 | class TestDecideUserInstall:
|
16 |
| - @patch('site.ENABLE_USER_SITE', True) |
17 |
| - @patch('pip._internal.commands.install.site_packages_writable') |
18 |
| - def test_prefix_and_target(self, sp_writable): |
19 |
| - sp_writable.return_value = False |
20 |
| - |
21 |
| - assert decide_user_install( |
22 |
| - use_user_site=None, prefix_path='foo' |
23 |
| - ) is False |
24 |
| - |
25 |
| - assert decide_user_install( |
26 |
| - use_user_site=None, target_dir='bar' |
27 |
| - ) is False |
28 |
| - |
29 |
| - @pytest.mark.parametrize( |
30 |
| - "enable_user_site,site_packages_writable,result", [ |
31 |
| - (True, True, False), |
32 |
| - (True, False, True), |
33 |
| - (False, True, False), |
34 |
| - (False, False, False), |
35 |
| - ]) |
36 |
| - def test_most_cases( |
37 |
| - self, enable_user_site, site_packages_writable, result, monkeypatch, |
38 |
| - ): |
39 |
| - monkeypatch.setattr('site.ENABLE_USER_SITE', enable_user_site) |
40 |
| - monkeypatch.setattr( |
41 |
| - 'pip._internal.commands.install.site_packages_writable', |
42 |
| - lambda **kw: site_packages_writable |
43 |
| - ) |
44 |
| - assert decide_user_install(use_user_site=None) is result |
| 35 | + @pytest.mark.parametrize('use_user_site,prefix_path,target_dir,root_path', |
| 36 | + filter(lambda args: sum(map(bool, args)) > 1, |
| 37 | + product((False, True), (None, 'foo'), |
| 38 | + (None, 'bar'), (None, 'baz')))) |
| 39 | + def test_conflicts(self, use_user_site, |
| 40 | + prefix_path, target_dir, root_path): |
| 41 | + with pytest.raises(CommandError): |
| 42 | + decide_user_install( |
| 43 | + use_user_site=use_user_site, prefix_path=prefix_path, |
| 44 | + target_dir=target_dir, root_path=root_path) |
| 45 | + |
| 46 | + def test_target_dir(self): |
| 47 | + with patch(WRITABLE, true): |
| 48 | + with patch(EXISTS, true), patch(ISDIR, false): |
| 49 | + with pytest.raises(InstallationError): |
| 50 | + decide_user_install(target_dir='bar') |
| 51 | + for exists, isdir in (false, false), (false, true), (true, true): |
| 52 | + with patch(EXISTS, exists), patch(ISDIR, isdir): |
| 53 | + assert decide_user_install(target_dir='bar') is False |
| 54 | + |
| 55 | + def test_target_writable(self): |
| 56 | + with patch(EXISTS, false): |
| 57 | + with patch(WRITABLE, false), pytest.raises(InstallationError): |
| 58 | + decide_user_install(target_dir='bar') |
| 59 | + with patch(WRITABLE, true): |
| 60 | + assert decide_user_install(target_dir='bar') is False |
| 61 | + |
| 62 | + def test_prefix_writable(self): |
| 63 | + with patch(WRITABLE, false), pytest.raises(InstallationError): |
| 64 | + decide_user_install(prefix_path='foo') |
| 65 | + with patch(WRITABLE, true): |
| 66 | + assert decide_user_install(prefix_path='foo') is False |
| 67 | + |
| 68 | + def test_global_site_writable(self): |
| 69 | + with patch(ENABLE_USER_SITE, True): |
| 70 | + with patch(WRITABLE, false): |
| 71 | + with pytest.raises(InstallationError): |
| 72 | + decide_user_install(use_user_site=False) |
| 73 | + with pytest.raises(InstallationError): |
| 74 | + decide_user_install(root_path='baz') |
| 75 | + assert decide_user_install() is True |
| 76 | + with patch(WRITABLE, true): |
| 77 | + assert decide_user_install(use_user_site=True) is True |
| 78 | + assert decide_user_install(root_path='baz') is False |
| 79 | + assert decide_user_install() is False |
| 80 | + |
| 81 | + def test_enable_user_site(self): |
| 82 | + with patch(WRITABLE, true): |
| 83 | + with patch(ENABLE_USER_SITE, None): |
| 84 | + assert decide_user_install() is False |
| 85 | + assert decide_user_install(use_user_site=False) is False |
| 86 | + with pytest.raises(InstallationError): |
| 87 | + decide_user_install(use_user_site=True) |
| 88 | + with patch(ENABLE_USER_SITE, False): |
| 89 | + assert decide_user_install() is False |
| 90 | + assert decide_user_install(use_user_site=False) is False |
| 91 | + assert decide_user_install(use_user_site=True) is True |
| 92 | + with patch(ENABLE_USER_SITE, True): |
| 93 | + assert decide_user_install(use_user_site=False) is False |
| 94 | + assert decide_user_install(use_user_site=True) is True |
| 95 | + with patch(WRITABLE, false), patch(ENABLE_USER_SITE, True): |
| 96 | + assert decide_user_install() is True |
45 | 97 |
|
46 | 98 |
|
47 | 99 | def test_deprecation_notice_for_pip_install_options(recwarn):
|
|
0 commit comments