summaryrefslogtreecommitdiff
path: root/tests/functional/test_install_upgrade.py
blob: 8762bcac6a248cb6b48e1cac57cc6b6fe4359811 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import os
import sys
import textwrap

import pytest

from tests.lib import (
    assert_all_changes, pyversion, _create_test_package,
    _change_test_package_version,
)
from tests.lib.local_repos import local_checkout


def test_no_upgrade_unless_requested(script):
    """
    No upgrade if not specifically requested.

    """
    script.pip('install', 'INITools==0.1', expect_error=True)
    result = script.pip('install', 'INITools', expect_error=True)
    assert not result.files_created, (
        'pip install INITools upgraded when it should not have'
    )


@pytest.mark.network
def test_upgrade_to_specific_version(script):
    """
    It does upgrade to specific version requested.

    """
    script.pip('install', 'INITools==0.1', expect_error=True)
    result = script.pip('install', 'INITools==0.2', expect_error=True)
    assert result.files_created, (
        'pip install with specific version did not upgrade'
    )
    assert (
        script.site_packages / 'INITools-0.1-py%s.egg-info' %
        pyversion in result.files_deleted
    )
    assert (
        script.site_packages / 'INITools-0.2-py%s.egg-info' %
        pyversion in result.files_created
    )


@pytest.mark.network
def test_upgrade_if_requested(script):
    """
    And it does upgrade if requested.

    """
    script.pip('install', 'INITools==0.1', expect_error=True)
    result = script.pip('install', '--upgrade', 'INITools', expect_error=True)
    assert result.files_created, 'pip install --upgrade did not upgrade'
    assert (
        script.site_packages / 'INITools-0.1-py%s.egg-info' %
        pyversion not in result.files_created
    )


def test_upgrade_with_newest_already_installed(script, data):
    """
    If the newest version of a package is already installed, the package should
    not be reinstalled and the user should be informed.
    """
    script.pip('install', '-f', data.find_links, '--no-index', 'simple')
    result = script.pip(
        'install', '--upgrade', '-f', data.find_links, '--no-index', 'simple'
    )
    assert not result.files_created, 'simple upgraded when it should not have'
    assert 'already up-to-date' in result.stdout, result.stdout


@pytest.mark.network
def test_upgrade_force_reinstall_newest(script):
    """
    Force reinstallation of a package even if it is already at its newest
    version if --force-reinstall is supplied.
    """
    result = script.pip('install', 'INITools')
    assert script.site_packages / 'initools' in result.files_created, (
        sorted(result.files_created.keys())
    )
    result2 = script.pip(
        'install', '--upgrade', '--force-reinstall', 'INITools'
    )
    assert result2.files_updated, 'upgrade to INITools 0.3 failed'
    result3 = script.pip('uninstall', 'initools', '-y', expect_error=True)
    assert_all_changes(result, result3, [script.venv / 'build', 'cache'])


@pytest.mark.network
def test_uninstall_before_upgrade(script):
    """
    Automatic uninstall-before-upgrade.

    """
    result = script.pip('install', 'INITools==0.2', expect_error=True)
    assert script.site_packages / 'initools' in result.files_created, (
        sorted(result.files_created.keys())
    )
    result2 = script.pip('install', 'INITools==0.3', expect_error=True)
    assert result2.files_created, 'upgrade to INITools 0.3 failed'
    result3 = script.pip('uninstall', 'initools', '-y', expect_error=True)
    assert_all_changes(result, result3, [script.venv / 'build', 'cache'])


@pytest.mark.network
def test_uninstall_before_upgrade_from_url(script):
    """
    Automatic uninstall-before-upgrade from URL.

    """
    result = script.pip('install', 'INITools==0.2', expect_error=True)
    assert script.site_packages / 'initools' in result.files_created, (
        sorted(result.files_created.keys())
    )
    result2 = script.pip(
        'install',
        'http://pypi.python.org/packages/source/I/INITools/INITools-'
        '0.3.tar.gz',
        expect_error=True,
    )
    assert result2.files_created, 'upgrade to INITools 0.3 failed'
    result3 = script.pip('uninstall', 'initools', '-y', expect_error=True)
    assert_all_changes(result, result3, [script.venv / 'build', 'cache'])


@pytest.mark.network
def test_upgrade_to_same_version_from_url(script):
    """
    When installing from a URL the same version that is already installed, no
    need to uninstall and reinstall if --upgrade is not specified.

    """
    result = script.pip('install', 'INITools==0.3', expect_error=True)
    assert script.site_packages / 'initools' in result.files_created, (
        sorted(result.files_created.keys())
    )
    result2 = script.pip(
        'install',
        'http://pypi.python.org/packages/source/I/INITools/INITools-'
        '0.3.tar.gz',
        expect_error=True,
    )
    assert not result2.files_updated, 'INITools 0.3 reinstalled same version'
    result3 = script.pip('uninstall', 'initools', '-y', expect_error=True)
    assert_all_changes(result, result3, [script.venv / 'build', 'cache'])


@pytest.mark.network
def test_upgrade_from_reqs_file(script):
    """
    Upgrade from a requirements file.

    """
    script.scratch_path.join("test-req.txt").write(textwrap.dedent("""\
        PyLogo<0.4
        # and something else to test out:
        INITools==0.3
        """))
    install_result = script.pip(
        'install', '-r', script.scratch_path / 'test-req.txt'
    )
    script.scratch_path.join("test-req.txt").write(textwrap.dedent("""\
        PyLogo
        # and something else to test out:
        INITools
        """))
    script.pip(
        'install', '--upgrade', '-r', script.scratch_path / 'test-req.txt'
    )
    uninstall_result = script.pip(
        'uninstall', '-r', script.scratch_path / 'test-req.txt', '-y'
    )
    assert_all_changes(
        install_result,
        uninstall_result,
        [script.venv / 'build', 'cache', script.scratch / 'test-req.txt'],
    )


def test_uninstall_rollback(script, data):
    """
    Test uninstall-rollback (using test package with a setup.py
    crafted to fail on install).

    """
    result = script.pip(
        'install', '-f', data.find_links, '--no-index', 'broken==0.1'
    )
    assert script.site_packages / 'broken.py' in result.files_created, list(
        result.files_created.keys()
    )
    result2 = script.pip(
        'install', '-f', data.find_links, '--no-index', 'broken===0.2broken',
        expect_error=True,
    )
    assert result2.returncode == 1, str(result2)
    assert script.run(
        'python', '-c', "import broken; print(broken.VERSION)"
    ).stdout == '0.1\n'
    assert_all_changes(
        result.files_after,
        result2,
        [script.venv / 'build'],
    )


# Issue #530 - temporarily disable flaky test
@pytest.mark.skipif
def test_editable_git_upgrade(script):
    """
    Test installing an editable git package from a repository, upgrading the
    repository, installing again, and check it gets the newer version
    """
    version_pkg_path = _create_test_package(script)
    script.pip(
        'install', '-e',
        '%s#egg=version_pkg' % ('git+file://' + version_pkg_path),
    )
    version = script.run('version_pkg')
    assert '0.1' in version.stdout
    _change_test_package_version(script, version_pkg_path)
    script.pip(
        'install', '-e',
        '%s#egg=version_pkg' % ('git+file://' + version_pkg_path),
    )
    version2 = script.run('version_pkg')
    assert 'some different version' in version2.stdout, (
        "Output: %s" % (version2.stdout)
    )


@pytest.mark.network
def test_should_not_install_always_from_cache(script):
    """
    If there is an old cached package, pip should download the newer version
    Related to issue #175
    """
    script.pip('install', 'INITools==0.2', expect_error=True)
    script.pip('uninstall', '-y', 'INITools')
    result = script.pip('install', 'INITools==0.1', expect_error=True)
    assert (
        script.site_packages / 'INITools-0.2-py%s.egg-info' %
        pyversion not in result.files_created
    )
    assert (
        script.site_packages / 'INITools-0.1-py%s.egg-info' %
        pyversion in result.files_created
    )


@pytest.mark.network
def test_install_with_ignoreinstalled_requested(script):
    """
    Test old conflicting package is completely ignored
    """
    script.pip('install', 'INITools==0.1', expect_error=True)
    result = script.pip('install', '-I', 'INITools==0.3', expect_error=True)
    assert result.files_created, 'pip install -I did not install'
    # both the old and new metadata should be present.
    assert os.path.exists(
        script.site_packages_path / 'INITools-0.1-py%s.egg-info' % pyversion
    )
    assert os.path.exists(
        script.site_packages_path / 'INITools-0.3-py%s.egg-info' % pyversion
    )


@pytest.mark.network
def test_upgrade_vcs_req_with_no_dists_found(script, tmpdir):
    """It can upgrade a VCS requirement that has no distributions otherwise."""
    req = "%s#egg=pip-test-package" % local_checkout(
        "git+http://github.com/pypa/pip-test-package.git",
        tmpdir.join("cache"),
    )
    script.pip("install", req)
    result = script.pip("install", "-U", req)
    assert not result.returncode


@pytest.mark.network
def test_upgrade_vcs_req_with_dist_found(script):
    """It can upgrade a VCS requirement that has distributions on the index."""
    # TODO(pnasrat) Using local_checkout fails on windows - oddness with the
    # test path urls/git.
    req = (
        "%s#egg=pretend" %
        (
            "git+git://github.com/alex/pretend@e7f26ad7dbcb4a02a4995aade4"
            "743aad47656b27"
        )
    )
    script.pip("install", req, expect_stderr=True)
    result = script.pip("install", "-U", req, expect_stderr=True)
    assert "pypi.python.org" not in result.stdout, result.stdout


class TestUpgradeDistributeToSetuptools(object):
    """
    From pip1.4 to pip6, pip supported a set of "hacks" (see Issue #1122) to
    allow distribute to conflict with setuptools, so that the following would
    work to upgrade distribute:

     ``pip install -U  setuptools``

    In pip7, the hacks were removed.  This test remains to at least confirm pip
    can upgrade distribute to setuptools using:

      ``pip install -U distribute``

    The reason this works is that a final version of distribute (v0.7.3) was
    released that is simple wrapper with:

      install_requires=['setuptools>=0.7']

    The test use a fixed set of packages from our test packages dir. Note that
    virtualenv-1.9.1 contains distribute-0.6.34 and virtualenv-1.10 contains
    setuptools-0.9.7
    """

    def prep_ve(self, script, version, pip_src, distribute=False):
        self.script = script
        self.script.pip_install_local('virtualenv==%s' % version)
        args = ['virtualenv', self.script.scratch_path / 'VE']
        if distribute:
            args.insert(1, '--distribute')
        if version == "1.9.1" and not distribute:
            # setuptools 0.6 didn't support PYTHONDONTWRITEBYTECODE
            del self.script.environ["PYTHONDONTWRITEBYTECODE"]
        self.script.run(*args)
        if sys.platform == 'win32':
            bindir = "Scripts"
        else:
            bindir = "bin"
        self.ve_bin = self.script.scratch_path / 'VE' / bindir
        self.script.run(self.ve_bin / 'pip', 'uninstall', '-y', 'pip')
        self.script.run(
            self.ve_bin / 'python', 'setup.py', 'install',
            cwd=pip_src,
            expect_stderr=True,
        )

    @pytest.mark.skipif(
        sys.version_info >= (3, 5),
        reason="distribute doesn't work on Python 3.5",
    )
    def test_from_distribute_6_to_setuptools_7(
            self, script, data, virtualenv):
        self.prep_ve(
            script, '1.9.1', virtualenv.pip_source_dir, distribute=True
        )
        result = self.script.run(
            self.ve_bin / 'pip', 'install', '--no-index',
            '--find-links=%s' % data.find_links, '-U', 'distribute',
            expect_stderr=True if sys.version_info[:2] == (2, 6) else False,
        )
        assert (
            "Found existing installation: distribute 0.6.34" in result.stdout
        )
        result = self.script.run(
            self.ve_bin / 'pip', 'list', '--format=legacy',
            expect_stderr=True if sys.version_info[:2] == (2, 6) else False,
        )
        assert "setuptools (0.9.8)" in result.stdout
        assert "distribute (0.7.3)" in result.stdout