summaryrefslogtreecommitdiff
path: root/tests/functional/test_install_user.py
blob: df39b7ae55cc26b6f2e34398d232345aba759373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"""
tests specific to "--user" option
"""
import sys
import os

from os.path import abspath, join, curdir, isdir, isfile

import pytest

from tests.lib.local_repos import local_checkout
from tests.lib import (tests_data, reset_env, run_pip, pyversion, assert_all_changes,
                            path_to_url, find_links, pip_install_local)


patch_dist_in_site_packages = """
       def dist_in_site_packages(dist):
           return False
       from pip import req
       req.dist_in_site_packages=dist_in_site_packages
"""


# --user option is broken in pypy
@pytest.mark.skipif("hasattr(sys, 'pypy_version_info')")
class Tests_UserSite:

    def test_reset_env_system_site_packages_usersite(self):
        """
        reset_env(system_site_packages=True) produces env where a --user install can be found using pkg_resources
        """
        env = reset_env(system_site_packages=True)
        run_pip('install', '--user', 'INITools==0.2')
        result = env.run('python', '-c', "import pkg_resources; print(pkg_resources.get_distribution('initools').project_name)")
        project_name = result.stdout.strip()
        assert 'INITools'== project_name, "'%s' should be 'INITools'" %project_name


    def test_install_subversion_usersite_editable_with_distribute(self):
        """
        Test installing current directory ('.') into usersite after installing distribute
        """
        env = reset_env(system_site_packages=True)
        result = run_pip('install', '--user', '-e',
                         '%s#egg=initools-dev' %
                         local_checkout('svn+http://svn.colorstudy.com/INITools/trunk'))
        result.assert_installed('INITools', use_user_site=True)


    def test_install_curdir_usersite(self):
        """
        Test installing current directory ('.') into usersite
        """
        env = reset_env(system_site_packages=True)
        run_from = abspath(join(tests_data, 'packages', 'FSPkg'))
        result = run_pip('install', '--user', curdir, cwd=run_from, expect_error=False)
        fspkg_folder = env.user_site/'fspkg'
        egg_info_folder = env.user_site/'FSPkg-0.1dev-py%s.egg-info' % pyversion
        assert fspkg_folder in result.files_created, str(result.stdout)

        assert egg_info_folder in result.files_created, str(result)


    def test_install_user_venv_nositepkgs_fails(self):
        """
        user install in virtualenv (with no system packages) fails with message
        """
        env = reset_env()
        run_from = abspath(join(tests_data, 'packages', 'FSPkg'))
        result = run_pip('install', '--user', curdir, cwd=run_from, expect_error=True)
        assert "Can not perform a '--user' install. User site-packages are not visible in this virtualenv." in result.stdout


    def test_install_user_conflict_in_usersite(self):
        """
        Test user install with conflict in usersite updates usersite.
        """

        env = reset_env(system_site_packages=True)
        result1 = run_pip('install', '--user', 'INITools==0.3')
        result2 = run_pip('install', '--user', 'INITools==0.1')

        #usersite has 0.1
        egg_info_folder = env.user_site / 'INITools-0.1-py%s.egg-info' % pyversion
        initools_v3_file = env.root_path / env.user_site / 'initools' / 'configparser.py' #file only in 0.3
        assert egg_info_folder in result2.files_created, str(result2)
        assert not isfile(initools_v3_file), initools_v3_file


    def test_install_user_conflict_in_globalsite(self):
        """
        Test user install with conflict in global site ignores site and installs to usersite
        """

        # the test framework only supports testing using virtualenvs
        # the sys.path ordering for virtualenvs with --system-site-packages is this: virtualenv-site, user-site, global-site
        # this test will use 2 modifications to simulate the user-site/global-site relationship
        # 1) a monkey patch which will make it appear INITools==0.2 is not in in the virtualenv site
        #    if we don't patch this, pip will return an installation error:  "Will not install to the usersite because it will lack sys.path precedence..."
        # 2) adding usersite to PYTHONPATH, so usersite as sys.path precedence over the virtualenv site

        env = reset_env(system_site_packages=True, sitecustomize=patch_dist_in_site_packages)
        env.environ["PYTHONPATH"] = env.root_path / env.user_site

        result1 = run_pip('install', 'INITools==0.2')
        result2 = run_pip('install', '--user', 'INITools==0.1')

        #usersite has 0.1
        egg_info_folder = env.user_site / 'INITools-0.1-py%s.egg-info' % pyversion
        initools_folder = env.user_site / 'initools'
        assert egg_info_folder in result2.files_created, str(result2)
        assert initools_folder in result2.files_created, str(result2)

        #site still has 0.2 (can't look in result1; have to check)
        egg_info_folder = env.root_path / env.site_packages / 'INITools-0.2-py%s.egg-info' % pyversion
        initools_folder = env.root_path / env.site_packages / 'initools'
        assert isdir(egg_info_folder)
        assert isdir(initools_folder)


    def test_upgrade_user_conflict_in_globalsite(self):
        """
        Test user install/upgrade with conflict in global site ignores site and installs to usersite
        """

        # the test framework only supports testing using virtualenvs
        # the sys.path ordering for virtualenvs with --system-site-packages is this: virtualenv-site, user-site, global-site
        # this test will use 2 modifications to simulate the user-site/global-site relationship
        # 1) a monkey patch which will make it appear INITools==0.2 is not in in the virtualenv site
        #    if we don't patch this, pip will return an installation error:  "Will not install to the usersite because it will lack sys.path precedence..."
        # 2) adding usersite to PYTHONPATH, so usersite as sys.path precedence over the virtualenv site

        env = reset_env(system_site_packages=True, sitecustomize=patch_dist_in_site_packages)
        env.environ["PYTHONPATH"] = env.root_path / env.user_site

        result1 = run_pip('install', 'INITools==0.2')
        result2 = run_pip('install', '--user', '--upgrade', 'INITools')

        #usersite has 0.3.1
        egg_info_folder = env.user_site / 'INITools-0.3.1-py%s.egg-info' % pyversion
        initools_folder = env.user_site / 'initools'
        assert egg_info_folder in result2.files_created, str(result2)
        assert initools_folder in result2.files_created, str(result2)

        #site still has 0.2 (can't look in result1; have to check)
        egg_info_folder = env.root_path / env.site_packages / 'INITools-0.2-py%s.egg-info' % pyversion
        initools_folder = env.root_path / env.site_packages / 'initools'
        assert isdir(egg_info_folder), result2.stdout
        assert isdir(initools_folder)


    def test_install_user_conflict_in_globalsite_and_usersite(self):
        """
        Test user install with conflict in globalsite and usersite ignores global site and updates usersite.
        """

        # the test framework only supports testing using virtualenvs.
        # the sys.path ordering for virtualenvs with --system-site-packages is this: virtualenv-site, user-site, global-site.
        # this test will use 2 modifications to simulate the user-site/global-site relationship
        # 1) a monkey patch which will make it appear INITools==0.2 is not in in the virtualenv site
        #    if we don't patch this, pip will return an installation error:  "Will not install to the usersite because it will lack sys.path precedence..."
        # 2) adding usersite to PYTHONPATH, so usersite as sys.path precedence over the virtualenv site

        env = reset_env(system_site_packages=True, sitecustomize=patch_dist_in_site_packages)
        env.environ["PYTHONPATH"] = env.root_path / env.user_site

        result1 = run_pip('install', 'INITools==0.2')
        result2 = run_pip('install', '--user', 'INITools==0.3')
        result3 = run_pip('install', '--user', 'INITools==0.1')

        #usersite has 0.1
        egg_info_folder = env.user_site / 'INITools-0.1-py%s.egg-info' % pyversion
        initools_v3_file = env.root_path / env.user_site / 'initools' / 'configparser.py'  #file only in 0.3
        assert egg_info_folder in result3.files_created, str(result3)
        assert not isfile(initools_v3_file), initools_v3_file

        #site still has 0.2 (can't just look in result1; have to check)
        egg_info_folder = env.root_path / env.site_packages / 'INITools-0.2-py%s.egg-info' % pyversion
        initools_folder = env.root_path / env.site_packages / 'initools'
        assert isdir(egg_info_folder)
        assert isdir(initools_folder)


    def test_install_user_in_global_virtualenv_with_conflict_fails(self):
        """
        Test user install in --system-site-packages virtualenv with conflict in site fails.
        """
        env = reset_env(system_site_packages=True)
        result1 = run_pip('install', 'INITools==0.2')
        result2 = run_pip('install', '--user', 'INITools==0.1', expect_error=True)
        resultp = env.run('python', '-c', "import pkg_resources; print(pkg_resources.get_distribution('initools').location)")
        dist_location = resultp.stdout.strip()
        assert "Will not install to the user site because it will lack sys.path precedence to %s in %s" \
            % ('INITools', dist_location) in result2.stdout, result2.stdout


    def test_uninstall_from_usersite(self):
        """
        Test uninstall from usersite
        """
        env = reset_env(system_site_packages=True)
        result1 = run_pip('install', '--user', 'INITools==0.3')
        result2 = run_pip('uninstall', '-y', 'INITools')
        assert_all_changes(result1, result2, [env.venv/'build', 'cache'])


    def test_uninstall_editable_from_usersite(self):
        """
        Test uninstall editable local user install
        """
        env = reset_env(system_site_packages=True)

        #install
        to_install = abspath(join(tests_data, 'packages', 'FSPkg'))
        result1 = run_pip('install', '--user', '-e', to_install, expect_error=False)
        egg_link = env.user_site/'FSPkg.egg-link'
        assert egg_link in result1.files_created, str(result1.stdout)

        #uninstall
        result2 = run_pip('uninstall', '-y', 'FSPkg')
        assert not isfile(env.root_path / egg_link)

        assert_all_changes(result1, result2,
                           [env.venv/'build', 'cache', env.user_site/'easy-install.pth'])


    def test_install_user_wheel(self):
        """
        Test user install from wheel
        """
        env = reset_env(system_site_packages=True)
        pip_install_local('wheel')
        result = run_pip('install', 'simple.dist==0.1', '--user', '--use-wheel',
                     '--no-index', '--find-links='+find_links)
        egg_info_folder = env.user_site / 'simple.dist-0.1.dist-info'
        assert egg_info_folder in result.files_created, str(result)